这篇文章主要介绍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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。