C語言線性代數算法實現矩陣示例代碼
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語言線性代數算法實現矩陣示例代碼的詳細內容,更多關於C語言算法的資料請關註WalkonNet其它相關文章!