C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C语言线性代数算法实现矩阵

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

作者:微小冷

这篇文章主要为大家介绍了使用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语言算法的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文