int main(int argc, char* argv[]) { int k = 0; int n = 0; int flag = 0; int** array; printf("Please input the line and row number of the matrix:\n"); scanf("%d",&n); //Create matrix printf("Start create a %d * %d matrix.\n",n,n); array = (int**)malloc(sizeof(int*)*n); for(k = 0; k < n; ++k) { array[k] = (int*)malloc(sizeof(int)*n); } printf("If you want get an identity matrix, please input 0, while if you want an invertible matrix, just input 1.\n"); scanf("%d",&flag); if(flag == 0) { printf("Your input word is %d, so you want an identity matrix:\n",flag); //get indentity matrix getIdentityMatrix(n, array); //put indentity matrix into file putIdentityMatrixIntoFile(n, array); //print identity matrix printIdentityMatrix(n, array); } else if(flag == 1) { printf("Your input word is %d, so you want an invertible matrix:\n",flag); //get indentity matrix getIdentityMatrix(n, array); //get invertible matrix getInvertibleMatrix(n, array); //put invertible matrix into file putInvertibleMatrixIntoFile(n, array); //print invertible matrix printInvertibleMatrix(n, array); } else { printf("Error: You input a wrong number!\n"); return -1; } //free matrix printf("Free matrix.\n"); freeMatrix(n, array); return 1; }
注:代碼中最後出現的freeMatrix函數是我專門為了釋放數組內存寫的,代碼如下:
void freeMatrix(int n, int** array) { int k = 0; for(k = 0; k < n; ++k) { free(array[k]); } free(array); }
void getIdentityMatrix(int n, int** array) { int r = 0; int c = 0; for(r = 0; r < n; ++r) { for(c = 0; c < n; ++c) { if(r == c) array[r][c] = 1; else array[r][c] = 0; } } }
mainRowNum = (int)(rand()%(n-1));這句話是隨機的在矩陣0~n-1行中選擇一個主行作初等行變換,第一個循環是將主行中的元素進行一定的處理
array[mainRowNum][k])*((int)(rand()%5 - 10)後存入中介數組tempArray中(處理方式是我隨便寫的,也可以是別的運算方式)。 而這一句:
((UINT16_MAX - (array[mainRowNum][k])*((int)(rand()%5 - 10))) < 0) || ((UINT16_MAX*(-1)) - (array[mainRowNum][k])*((int)(rand()%5 - 10)) > tempArray[k])則是為了判斷采用上述處理方式對矩陣元素進行運算後,元素數值是否會溢出。(如果你想了解更多,請看這裡:點擊打開鏈接) 第二個循環的作用是遍歷矩陣中所有行,然後令處理過後主行和其他所有的行依次進行相加,這裡也進行了溢出判斷。 最後不要忘了釋放內存喲~
void getInvertibleMatrix(int n, int** array) { int i = 0; int j = 0; int k = 0; int mainRowNum = 0; int* tempArray = NULL; srand((int)time(NULL)); int transformTime = (int)(rand()%1000); printf("We will do %d times tansformation.\n",transformTime); tempArray = (int*)malloc(sizeof(int)*n); for(i = 0; i < transformTime; ++i) { mainRowNum = (int)(rand()%(n-1)); for(k = 0; k < n; ++k) if(((UINT16_MAX - (array[mainRowNum][k])*((int)(rand()%5 - 10))) < 0) || ((UINT16_MAX*(-1)) - (array[mainRowNum][k])*((int)(rand()%5 - 10)) > tempArray[k])) tempArray[k] = (array[mainRowNum][k]); else tempArray[k] = (array[mainRowNum][k])*((int)(rand()%5 - 10)); for(j = 0; j < n; ++j) if(mainRowNum != j) for(k = 0; k < n; ++k) { if(((UINT16_MAX - array[j][k]) < tempArray[k]) || ((UINT16_MAX*(-1)) - array[j][k] > tempArray[k])) array[j][k] = array[j][k]/4; else array[j][k] = array[j][k] + tempArray[k]; } } free(tempArray); }
int putInvertibleMatrixIntoFile(int n, int** array) { FILE* fp = NULL; int i = 0; int j = 0; if((fp = fopen("input","w"))==NULL) { printf("Error: writing file error!\n"); return -1; } for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) { if(j != (n-1)) fprintf(fp,"%d\t", array[i][j]); else fprintf(fp,"%d", array[i][j]); } fputs("\n",fp); } fclose(fp); return 1; }
gcc -o invertible invertiblematrix.c我們以5*5的矩陣為例試一下 選擇1,讓其生成非單位矩陣的可逆矩陣 可以看出,做了780次行變換,只是矩陣有一列有很多0,看起來不太完美。 用vim看一下我們得到的文件(名為input): 初步成功~