您好,登錄后才能下訂單哦!
這篇文章主要介紹C語言線性代數算法如何實現矩陣,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
矩陣作為一個結構體而言,至少要包含行數、列數以及數據。
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int row, col, size; double *data; } Matrix;
接下來通過這個結構體實現一些特殊的矩陣,例如包括相同元素的矩陣、對角矩陣等。
#define SetBase(mat) \ (Matrix*)malloc(sizeof(Matrix));\ mat->row = row;\ mat->col = col;\ mat->size = row*col;\ mat->data = (double*)malloc(mat->size*sizeof(double)) //特殊矩陣 Matrix* Sames(double n, int row, int col){ Matrix* mat = SetBase(mat); for (int i = 0; i < mat->size; i++) mat->data[i]=n; return mat; } #define Ones(row,col) Sames(1,row,col) #define Zeros(row,col) Sames(0,row,col) Matrix* Diag(double n, int row, int col){ Matrix* mat = Sames(0,row,col); for (int i = 0; i < min(row,col) ; i++) mat->data[i*col+i] = n; return mat; } #define Eye(row,col) Diag(1,row,col) Matrix* CountMatrix(int row, int col){ Matrix* mat = SetBase(mat); for (int i = 0; i < mat->size; i++) mat->data[i]=i; return mat; } //生成[L,R]范圍內的隨機矩陣 Matrix* RandMat(int row,int col, double L, double R){ Matrix* mat = SetBase(mat); int size=R-L; for (int i = 0; i < mat->size; i++) mat->data[i] = rand()%size+L; return mat; }
由于要識別輸入的函數,所以先新建一個函數的結構體
typedef struct{ char* name; int len; int numPara; //參數個數 double params[MAXLEN]; //參數列表 }Func;
然后通過字符串來生成Func
//用于識別函數 void initFunc(Func* func,char* str){ int i = -1; int j = 0; while(str[++i]!='('){} func->len = i; func->name = (char*)malloc(sizeof(char)*func->len); for (j = 0; j < i; j++) func->name[j] = str[j]; func->name[i] = '\0'; int start = ++i; char temp[MAXLEN]; j = 0; while (str[i]!=')') { if(str[i]==','){ temp[i-start]='\0'; start = i+1; func->params[j]=atof(temp); j++; }else temp[i-start]=str[i]; i++; } temp[i-start]='\0'; func->params[j]=atof(temp); func->numPara = j+1; }
接下來需要實現打印矩陣的函數
void printMat(Matrix* mat){ printf("mat:"); printf("%dx%d=%d\n",mat->col,mat->row,mat->size); for (int i = 0; i < mat->size; i++) { printf("%f,",mat->data[i]); if((i+1)%mat->col==0) printf("\n"); } }
最后是main
函數
int isFunc(Func* func, char* str){ for (int i = 0; i < func->len; i++) { if(func->name[i]!=str[i]) return FALSE; if(str[i]=='\0') return FALSE; } return TRUE; } #define intPara (int)func->params #define floatPara func->params //#define isFunc(str) strcmp(func->name,str) int main(){ //char* str = (char*)malloc(sizeof(char) * MAXLEN); char str[MAXLEN]; Matrix* mat = NULL; Func* func = (Func*)malloc(sizeof(func)); while(1) { printf("please input:"); gets(str); initFunc(func,str); if(isFunc(func,"Sames")) mat = Sames(floatPara[0],intPara[1],intPara[2]); else if(isFunc(func,"Ones")) mat = Ones(intPara[0],intPara[1]); else if(isFunc(func,"Zeros")) mat = Zeros(intPara[0],intPara[1]); else if(isFunc(func,"Diag")) mat = Diag(floatPara[0],intPara[1],intPara[2]); else if(isFunc(func,"Eye")) mat = Eye(intPara[0],intPara[1]); else if(isFunc(func,"CountMatrix")) mat = CountMatrix(intPara[0],intPara[1]); else if(isFunc(func,"RandMat")) mat = RandMat(intPara[0],intPara[1], floatPara[2],floatPara[3]); else continue; printMat(mat); } }
驗證一下
PS E:\Code\PL\calc> .\a.exe please input:Ones(4,4) mat:4x4=16 1.000000,1.000000,1.000000,1.000000, 1.000000,1.000000,1.000000,1.000000, 1.000000,1.000000,1.000000,1.000000, 1.000000,1.000000,1.000000,1.000000, please input:Zeros(3,5) mat:5x3=15 0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000, please input:RandMat(3,3,0,100) mat:3x3=9 41.000000,67.000000,34.000000, 0.000000,69.000000,24.000000, 78.000000,58.000000,62.000000, please input:Eye(3,3) mat:3x3=9 1.000000,0.000000,0.000000, 0.000000,1.000000,0.000000, 0.000000,0.000000,1.000000, please input:CountMatrix(2,4) mat:4x2=8 0.000000,1.000000,2.000000,3.000000, 4.000000,5.000000,6.000000,7.000000,
以上是“C語言線性代數算法如何實現矩陣”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。