C语言线性代数算法实现矩阵示例代码

时间:2022-09-04 17:44:26

C语言实现矩阵

矩阵作为一个结构体而言,至少要包含行数、列数以及数据。

?
1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
    int row, col, size;
    double *data;
} Matrix;

特殊矩阵

接下来通过这个结构体实现一些特殊的矩阵,例如包括相同元素的矩阵、对角矩阵等。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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;
}

特殊矩阵验证

由于要识别输入的函数,所以先新建一个函数的结构体

?
1
2
3
4
5
6
typedef struct{
    char* name;
    int len;
    int numPara;                //参数个数
    double params[MAXLEN];      //参数列表
}Func;

然后通过字符串来生成Func

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//用于识别函数
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;
}

接下来需要实现打印矩阵的函数

?
1
2
3
4
5
6
7
8
9
10
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函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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);
    }
}

验证一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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语言算法的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/m0_37816922/article/details/120622463?spm=1001.2014.3001.5501