|
| 1 | +#include "Matrix.h" |
| 2 | + |
| 3 | +/**************************************************************************************** |
| 4 | + * Following three function performs three elementary row operations. * |
| 5 | + * 1. Function : multiply_rowwide. * |
| 6 | + * Description : Multiplies the specified row with a scalar. * |
| 7 | + * * |
| 8 | + * 2. Function : row_exchange. * |
| 9 | + * Description : Exchange two rows with one another. * |
| 10 | + * * |
| 11 | + * 3. Function : row_arithmetic. * |
| 12 | + * Description : Add scalar multiple of one row with another. * |
| 13 | + ****************************************************************************************/ |
| 14 | +void multiply_rowwide(float *mtrx, int row_num, int row_len, float scalar){ |
| 15 | + for(int i=0; i<row_len; i++) |
| 16 | + mtrx[icalc(row_num, row_len, i)] *= scalar; |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +void row_arithmetic(float *mtrx, int row_len, int target_row, int add_row, float scalar){ |
| 21 | + for(int i=0; i<row_len; i++){ |
| 22 | + float B = mtrx[icalc(add_row, row_len, i)]*scalar; |
| 23 | + mtrx[icalc(target_row, row_len, i)] -= B; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +void row_exchange(float *mtrx, int row1, int row2, int rowlength){ |
| 29 | + float temp = 0; |
| 30 | + |
| 31 | + for(int i=0; i<rowlength; i++){ |
| 32 | + temp = mtrx[icalc(row1, rowlength, i)]; |
| 33 | + mtrx[icalc(row1, rowlength, i)] = mtrx[icalc(row2, rowlength, i)]; |
| 34 | + mtrx[icalc(row2, rowlength, i)] = temp; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +/* Combining all three elementary row operation for Gaus-Jordn elemination. */ |
| 40 | +void Gaus_Jordan(float *mtrx, int row_len, int column_len){ |
| 41 | + int rank = min(row_len, column_len); |
| 42 | + |
| 43 | + for(int i=0; i<rank; i++){ |
| 44 | + // If the leading element of a row is 0, exchange the row with the row bellow it. |
| 45 | + for(int j=1; mtrx[icalc(i, row_len, i)] == 0 || i+j<column_len; j++) |
| 46 | + row_exchange(mtrx, i, i+j, row_len); |
| 47 | + |
| 48 | + // If the leading element is not 1, then divide the row with the leading element. |
| 49 | + multiply_rowwide(mtrx, i, row_len, 1/mtrx[icalc(i, row_len, i)]); |
| 50 | + |
| 51 | + // Multiply the current row with the leading element of subsequent rows and substracts it from subsequent rows. |
| 52 | + for(int j=0; j<column_len; j++) |
| 53 | + if(i != j) |
| 54 | + row_arithmetic(mtrx, row_len, j, i, mtrx[icalc(j, row_len, i)]); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +/************************************************************* |
| 60 | + * Function Name: Index calcultor. * |
| 61 | + * Description : Matrix can be adequately represented by * |
| 62 | + * 2d arrays. But referencing 2d arrays is * |
| 63 | + * quarky in C. So rather matrix are here * |
| 64 | + * represented by 1d array. Hence the index * |
| 65 | + * calculated manually. * |
| 66 | + *************************************************************/ |
| 67 | +int icalc(int row_number, int row_length, int member_number){ |
| 68 | + return row_number*row_length+member_number; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +/* Rank of a matrix is the highest number of linearly independent vectors |
| 73 | + that can be formed from the rows/columns of a matrix. However, rank of |
| 74 | + the input matrix is taken as the minimal of the number of row and the |
| 75 | + number of column of this matrix. This huristic will cause erronious |
| 76 | + result for some inputs as the rank of a matrix can be equal of less than |
| 77 | + the number determined here. */ |
| 78 | +int min(int num1, int num2){ |
| 79 | + if(num1 < num2) |
| 80 | + return num1; |
| 81 | + |
| 82 | + return num2; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +void print_matrix(float *mtrx, int rowlen, int columnlen){ |
| 87 | + for(int i=0; i<columnlen; i++){ |
| 88 | + for(int j=0; j<rowlen; j++) |
| 89 | + printf("\t%f", mtrx[icalc(i, rowlen, j)]); |
| 90 | + puts(""); |
| 91 | + } |
| 92 | +} |
0 commit comments