Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Matrix by Linear Algebra algorithm in C language

Shulou Source: shulou.com Published: 2022-06-03 03:57:18 09月25日 Update

This article mainly introduces the C language linear algebra algorithm how to realize the matrix, the article introduces in great detail, has a certain reference value, interested friends must read it!

C language realization matrix

As a structure, a matrix should at least contain the number of rows, columns, and data.

# include # include # include typedef struct {int row, col, size; double * data;} Matrix; Special Matrix

Then some special matrices are realized through this structure, such as matrices including the same elements, diagonal matrices and so on.

# 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)) / / Special Matrix Matrix* Sames (double n, int row, int col) {Matrix* mat = SetBase (mat); for (int I = 0; I

< mat->

Size; iTunes +) mat- > data [I] = n; return mat;} # define Ones (row,col) Sames (1 int Rowl col) # define Zeros (row,col) Sames (0 double Rowl col) Matrix* Diag (double n, int row, int col) {Matrix* mat = Sames (0m Rowl col); for (int I = 0; I

< min(row,col) ; i++) mat->

Data [i*col+i] = n; return mat;} # define Eye (row,col) Diag Matrix* CountMatrix (int row, int col) {Matrix* mat = SetBase (mat); for (int I = 0; I

< mat->

Size; iTunes +) mat- > data [I] = i; return mat;} / / generate random matrices Matrix* RandMat (int row,int col, double L, double R) {Matrix* mat = SetBase (mat); int size=R-L; for (int I = 0; I) in the range of [LMagne R]

< mat->

Size; iTunes +) mat- > data [I] = rand ()% size+L; return mat;} Special Matrix Verification

Because you want to identify the input function, first create a new function structure

Typedef struct {char* name; int len; int numPara; / / number of parameters double params [MAXLEN]; / / Parameter list} Func

Then generate the Func through the string

/ used to identify the function 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] ='\ 0mm; int start = + + i; char temp [MAXLEN]; j = 0; while (str [I]! =')') {if (str[ I] = =',') {temp [i-start] ='\ 0mm; start = iTun1; func- > params [j] = atof (temp) Else temp [i-start] = str[ I]; iTunes;} temp [i-start] ='\ 0mm; func- > params [j] = atof (temp); func- > numPara = jacks 1;}

Next, you need to implement the function of printing matrix.

Void printMat (Matrix* mat) {printf ("mat:"); printf ("% dx%d=%d\ n", mat- > col,mat- > row,mat- > size); for (int I = 0; I

< mat->

Size; iTunes +) {printf ("% f,", mat- > data [I]); if ((iTun1)% mat- > col==0) printf ("\ n");}}

And finally, the main function.

Int isFunc (Func* func, char* str) {for (int I = 0; I)

< func->

Len; iTunes +) {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);}}

Verify it.

PS E:\ Code\ PL\ calc >.\ a.exeplease input:Ones (4) mat:4x4=161.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) mat:5x3=150.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) mat:3x3=941.000000,67.000000,34.000000,0.000000,69.000000,24.000000,78.000000,58.000000,62.000000 Please input:Eye (3) mat:3x3=91.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,1.000000,please input:CountMatrix (2) mat:4x2=80.000000,1.000000,2.000000,3.000000,4.000000,5.000000,6.000000,7.000000, these are all the contents of the article "how to realize Matrix by Linear Algebra algorithm in C language" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Matrix function special language structure algebra algorithm linear linear algebra next content parameters article generation verification same number value element interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information vpn MySQL Microsoft