Sandeepin最近做的项目中需要在嵌入式芯片里跑一些算法,而这些单片机性能不上不下,它能跑些简单的程序,但又还没到上Linux系统的地步。所以只好用C语言写一些在高级语言里一个函数就解决的算法了,由于算法需要运用矩阵运算,自己就先用纯C语言写了个简单的矩阵运算库。

  代码只实现了矩阵最基本的运算,包括矩阵的加、减、乘、数乘、转置、行列式、逆矩阵、代数余子式、伴随矩阵等运算。此外增加了一些实用函数,如显示矩阵、从csv文件读取保存矩阵等函数。具体的例子在主函数中体现,其中还用自己这个矩阵运算库做了一个简单的应用,利用公式β=(X'X)^(-1)X'Y来进行多元线性回归系数计算,大家可以参考参考,欢迎批评。

  JfzMatLib.c文件代码:

#include "JfzMatLib.h"

int main(int argc, char** argv)
{
//矩阵的基本运算:加、减、乘、数乘、转置、行列式、逆矩阵、代数余子式、伴随矩阵
//初始实验矩阵
double A[] = { -3, 2, -5, -1, 0, -2, 3, -4, 1 };
double B[] = { 1, 4, 7, 3, 0, 5, -1, 9, 11 };
double C[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//计算结果矩阵
double *Add = (double*)malloc(sizeof(double) * 9);
double *Sub = (double*)malloc(sizeof(double) * 9);
double *Mul = (double*)malloc(sizeof(double) * 9);
double *kMat = (double*)malloc(sizeof(double) * 9);
double *CT = (double*)malloc(sizeof(double) * 9);
double *AInv = (double*)malloc(sizeof(double) * 9);
double *Adj = (double*)malloc(sizeof(double) * 9);
//显示矩阵A、B、C
printf("A:\n"); MatShow(A, 3, 3);
printf("B:\n"); MatShow(B, 3, 3);
printf("C:\n"); MatShow(C, 3, 3);
//矩阵相加
printf("A+B:\n");
Add = MatAdd(A, B, 3, 3); MatShow(Add, 3, 3);
//矩阵相减
printf("A-B:\n");
Sub = MatSub(A, B, 3, 3); MatShow(Sub, 3, 3);
//矩阵相乘
printf("A*B:\n");
Mul = MatMul(A, 3, 3, B, 3, 3); MatShow(Mul, 3, 3);
//矩阵数乘
printf("2*C:\n");
kMat = MatMulk(C, 3, 3, 2); MatShow(kMat, 3, 3);
//矩阵转置
printf("C的转置:\n");
CT = MatT(C, 3, 3); MatShow(CT, 3, 3);
//矩阵行列式值
printf("B的行列式值:\n");
printf("%16lf\n", MatDet(B, 3));
printf("C的行列式值:\n");
printf("%16lf\n", MatDet(C, 3));
//矩阵的逆
printf("A的逆:\n");
AInv = MatInv(A, 3, 3); MatShow(AInv, 3, 3);
printf("C的逆:\n");
MatInv(C, 3, 3);
//矩阵代数余子式
printf("C的(0,0)元素的代数余子式:\n");
printf("%16lf\n", MatACof(C, 3, 0, 0));
//矩阵伴随矩阵
printf("A的伴随矩阵:\n");
Adj = MatAdj(A, 3, 3); MatShow(Adj, 3, 3); //蒋方正矩阵库应用:多元线性回归
//多元线性回归公式:β=(X'X)^(-1)X'Y
double X[15][5] = {
1, 316, 1536, 874, 981,//第一列要补1
1, 385, 1771, 777, 1386,
1, 299, 1565, 678, 1672,
1, 326, 1970, 785, 1864,
1, 441, 1890, 785, 2143,
1, 460, 2050, 709, 2176,
1, 470, 1873, 673, 1769,
1, 504, 1955, 793, 2207,
1, 348, 2016, 968, 2251,
1, 400, 2199, 944, 2390,
1, 496, 1328, 749, 2287,
1, 497, 1920, 952, 2388,
1, 533, 1400, 1452, 2093,
1, 506, 1612, 1587, 2083,
1, 458, 1613, 1485, 2390
};
double Y[15][1] = {
3894,
4628,
4569,
5340,
5449,
5599,
5010,
5694,
5792,
6126,
5025,
5924,
5657,
6019,
6141
};
//多元线性回归公式:β=(X'X)^(-1)X'Y
double *XT = (double*)malloc(sizeof(double) * 5 * 15);
double *XTX = (double*)malloc(sizeof(double) * 5 * 5);
double *InvXTX = (double*)malloc(sizeof(double) * 5 * 5);
double *InvXTXXT = (double*)malloc(sizeof(double) * 5 * 15);
double *InvXTXXTY = (double*)malloc(sizeof(double) * 5 * 1);
XT = MatT((double*)X, 15, 5);
XTX = MatMul(XT, 5, 15, (double*)X, 15, 5);
InvXTX = MatInv(XTX, 5, 5);
InvXTXXT = MatMul(InvXTX, 5, 5, XT, 5, 15);
InvXTXXTY = MatMul(InvXTXXT, 5, 15, (double*)Y, 15, 1);
printf("多元线性回归β系数:\n");
MatShow(InvXTXXTY, 5, 1); //保存矩阵到csv
MatWrite("XTX.csv", XTX, 5, 5);
MatWrite("InvXTXXTY.csv", InvXTXXTY, 5, 1);
MatWrite("Fuck.csv", A, 3, 3);
MatWrite("Fuck2.csv", A, 9, 1); //从csv读取矩阵
double *Fuck = (double*)malloc(sizeof(double) * 3 * 3);
Fuck = MatRead("Fuck.csv");
MatShow(Fuck, 3, 3); double *Fuck2 = (double*)malloc(sizeof(double) * 9 * 1);
Fuck2 = MatRead("Fuck2.csv");
MatShow(Fuck2, 9, 1); double *InvXTXXTYread = (double*)malloc(sizeof(double) * 5 * 1);
InvXTXXTYread = MatRead("InvXTXXTY.csv");
MatShow(InvXTXXTYread, 5, 1); double *XTXread = (double*)malloc(sizeof(double) * 5 * 5);
XTXread = MatRead("XTX.csv");
MatShow(XTXread, 5, 5);
system("pause");
}

  JfzMatLib.h头文件代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void MatShow(double* Mat, int row, int col);
double* MatAdd(double* A, double* B, int row, int col);
double* MatSub(double* A, double* B, int row, int col);
double* MatMul(double* A, int Arow, int Acol, double* B, int Brow, int Bcol);
double* MatMulk(double *A, int row, int col, double k);
double* MatT(double* A, int row, int col);
double MatDet(double *A, int row);
double* MatInv(double *A, int row, int col);
double MatACof(double *A, int row, int m, int n);
double* MatAdj(double *A, int row, int col);
double *MatRead(char *csvFileName, int row, int col);
void MatWrite(char *A, int row, int col); // (det用)功能:求逆序对的个数
int inver_order(int list[], int n)
{
int ret = 0;
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (list[j] > list[i])
ret++;
return ret;
} // (det用)功能:符号函数,正数返回1,负数返回-1
int sgn(int order)
{
return order % 2 ? -1 : 1;
} // (det用)功能:交换两整数a、b的值
void swap(int *a, int *b)
{
int m;
m = *a;
*a = *b;
*b = m;
} // 功能:求矩阵行列式的核心函数
double det(double *p, int n, int k, int list[], double sum)
{
if (k >= n)
{
int order = inver_order(list, n);
double item = (double)sgn(order);
for (int i = 0; i < n; i++)
{
//item *= p[i][list[i]];
item *= *(p + i * n + list[i]);
}
return sum + item;
}
else
{
for (int i = k; i < n; i++)
{
swap(&list[k], &list[i]);
sum = det(p, n, k + 1, list, sum);
swap(&list[k], &list[i]);
}
}
return sum;
} // 功能:矩阵显示
// 形参:(输入)矩阵首地址指针Mat,矩阵行数row和列数col。
// 返回:无
void MatShow(double* Mat, int row, int col)
{
for (int i = 0; i < row*col; i++)
{
printf("%16lf ", Mat[i]);
if (0 == (i + 1) % col) printf("\n");
}
} // 功能:矩阵相加
// 形参:(输入)矩阵A首地址指针A,矩阵B首地址指针B,矩阵A(也是矩阵B)行数row和列数col
// 返回:A+B
double* MatAdd(double* A, double* B, int row, int col)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
Out[col*i + j] = A[col*i + j] + B[col*i + j];
return Out;
} // 功能:矩阵相减
// 形参:(输入)矩阵A,矩阵B,矩阵A(也是矩阵B)行数row和列数col
// 返回:A-B
double* MatSub(double* A, double* B, int row, int col)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
Out[col*i + j] = A[col*i + j] - B[col*i + j];
return Out;
} // 功能:矩阵相乘
// 形参:(输入)矩阵A,矩阵A行数row和列数col,矩阵B,矩阵B行数row和列数col
// 返回:A*B
double* MatMul(double* A, int Arow, int Acol, double* B, int Brow, int Bcol)
{
double *Out = (double*)malloc(sizeof(double) * Arow * Bcol);
if (Acol != Brow)
{
printf(" Shit!矩阵不可乘!\n");
return NULL;
}
if (Acol == Brow)
{
int i, j, k;
for (i = 0; i < Arow; i++)
for (j = 0; j < Bcol; j++)
{
Out[Bcol*i + j] = 0;
for (k = 0; k < Acol; k++)
Out[Bcol*i + j] = Out[Bcol*i + j] + A[Acol*i + k] * B[Bcol*k + j];
}
return Out;
}
} // 功能:矩阵数乘(实数k乘以矩阵A)
// 形参:(输入)矩阵A首地址指针,矩阵行数row和列数col,实数k
// 返回:kA
double* MatMulk(double *A, int row, int col, double k)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
for (int i = 0; i < row * col; i++)
{
*Out = *A * k;
Out++;
A++;
}
Out = Out - row * col;
return Out;
} // 功能:矩阵转置
// 形参:(输入)矩阵A首地址指针A,行数row和列数col
// 返回:A的转置
double* MatT(double* A, int row, int col)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
Out[row*j + i] = A[col*i + j];
return Out;
} // 功能:求行列式值
// 形参:(输入)矩阵A首地址指针A,行数row
// 返回:A的行列式值
double MatDet(double *A, int row)
{
int *list = (int*)malloc(sizeof(int) * row);
for (int i = 0; i < row; i++)
list[i] = i;
double Out = det(A, row, 0, list, 0.0);
free(list);
return Out;
} // 功能:矩阵的逆
// 形参:(输入)矩阵A首地址指针A,行数row和列数col
// 返回:A的逆
double *MatInv(double *A, int row, int col)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
double det = MatDet(A, row); //求行列式
if (det == 0)
{
printf(" Fuck!矩阵不可逆!\n");
return NULL;
}
if (det != 0)
{
Out = MatAdj(A, row, col); //求伴随矩阵
int len = row * row;
for (int i = 0; i < len; i++)
*(Out + i) /= det;
return Out;
}
} // 功能:求代数余子式
// 形参:(输入)矩阵A首地址指针A,矩阵行数row, 元素a的下标m,n(从0开始),
// 返回:NxN 矩阵中元素A(mn)的代数余子式
double MatACof(double *A, int row, int m, int n)
{
int len = (row - 1) * (row - 1);
double *cofactor = (double*)malloc(sizeof(double) * len); int count = 0;
int raw_len = row * row;
for (int i = 0; i < raw_len; i++)
if (i / row != m && i % row != n)
*(cofactor + count++) = *(A + i);
double ret = MatDet(cofactor, row - 1);
if ((m + n) % 2)
ret = -ret;
free(cofactor);
return ret;
} // 功能:求伴随矩阵
// 形参:(输入)矩阵A首地址指针A,行数row和列数col
// 返回:A的伴随矩阵
double *MatAdj(double *A, int row, int col)
{
double *Out = (double*)malloc(sizeof(double) * row * col);
int len = row * row;
int count = 0;
for (int i = 0; i < len; i++)
{
*(Out + count++) = MatACof(A, row, i % row, i / row);
}
return Out;
} // 读取文件行数
int FileReadRow(const char *filename)
{
FILE *f = fopen(filename, "r");
int i = 0;
char str[4096];
while (NULL != fgets(str, 4096, f))
++i;
printf("数组行数:%d\n", i);
return i;
} // 读取文件每行数据数(逗号数+1)
int FileReadCol(const char *filename)
{
FILE *f = fopen(filename, "r");
int i = 0;
char str[4096];
fgets(str, 4096, f);
for (int j = 0; j < strlen(str); j++)
{
if (',' == str[j]) i++;
}
i++;// 数据数=逗号数+1
printf("数组列数:%d\n", i);
return i;
} // 逗号间隔数据提取
void GetCommaData(char str_In[4096], double double_Out[1024])
{
int str_In_len = strlen(str_In);
//printf("str_In_len:%d\n", str_In_len);
char str_Data_temp[128];
int j = 0;
int double_Out_num = 0;
for (int i = 0; i < str_In_len; i++)
{
//不是逗号,则是数据,存入临时数组中
if (',' != str_In[i]) str_Data_temp[j++] = str_In[i];
//是逗号或\n(最后一个数据),则数据转换为double,保存到输出数组
if (',' == str_In[i] || '\n' == str_In[i]) { str_Data_temp[j] = '\0'; j = 0; /*printf("str_Data_temp:%s\n", str_Data_temp); */double_Out[double_Out_num++] = atof(str_Data_temp); memset(str_Data_temp, 0, sizeof(str_Data_temp)); }
}
} // 功能:从csv文件读矩阵,保存到指针中
// 形参:(输入)csv文件名,预计行数row和列数col
// 返回:矩阵指针A
double *MatRead(char *csvFileName)
{
int row = FileReadRow(csvFileName);
int col = FileReadCol(csvFileName);
double *Out = (double*)malloc(sizeof(double) * row * col);
FILE *f = fopen(csvFileName, "r");
char buffer[4096];
while (fgets(buffer, sizeof(buffer), f))
{
//printf("buffer[%s]\n",buffer);
double double_Out[128] = { 0 };
GetCommaData(buffer, double_Out);
for (int i = 0; i < col; i++)
{
//printf("double_Out:%lf\n", double_Out[i]);
*Out = double_Out[i];
Out++;
} }
Out = Out - row * col;//指针移回数据开头
fclose(f);
return Out;
} // 功能:将矩阵A存入csv文件中
// 形参:(输入)保存的csv文件名,矩阵A首地址指针A,行数row和列数col
// 返回:无
void MatWrite(const char *csvFileName, double *A, int row, int col)
{
FILE *DateFile;
double *Ap = A;
DateFile = fopen(csvFileName, "w");//追加的方式保存生成的时间戳
for (int i = 0; i < row*col; i++)
{
if ((i+1) % col == 0) fprintf(DateFile, "%lf\n", *Ap);//保存到文件,到列数换行
else fprintf(DateFile, "%lf,", *Ap);//加逗号
Ap++;
}
fclose(DateFile);
}

  运行结果如图:

异想家纯C语言矩阵运算库的更多相关文章

  1. 异想家Golang学习笔记

    1. 简介 官网:https://golang.google.cn/ 2. 编译器.工具链 编译 go build .\demo.go 编译和执行指令合二为一 go run demo.go 3. 注释 ...

  2. 异想家Win10常用的软件推荐

    本文总结一下自己日常使用Win10中涉及到的好用小软件,那些装机必备的软件在这里就不一一列出了.我重点想推荐一些自己觉得好用,符合自己偏好,但又不是每个人都知道的小工具: Rolan:一款类似于Win ...

  3. 异想家Ubuntu安装的软件

    [替换国内源] https://developer.aliyun.com/mirror/ubuntu 我提供一个下载,方便第一次安装懒得敲命令: https://jfz.me/16.04/source ...

  4. 异想家Win10系统安装的软件与配置

    1.C盘推荐一个硬盘,256G,安装好驱动,显卡配置好高性能,激活Win10,屏蔽WIn10驱动更新(Show or hide updates.diagcab),改电脑名称为Sandeepin-PC. ...

  5. 异想家Win7系统安装的软件与配置

    C盘推荐一个硬盘,256G以上,安装好驱动,激活Win7,备份一次系统(纯净)! 1.Mac.Linux时间同步(双系统时配置): 开始->运行->CMD,打开命令行程序(以管理员方式打开 ...

  6. 异想家IDEA的偏好配置

    最好将配置文件位置改为软件安装目录下,因为只有自己用,易于便携. 修改bin目录下的idea.properties,注释#去掉修改idea.config.path.idea.system.path配置 ...

  7. 异想家Eclipse的偏好配置

    1.汉化 http://www.eclipse.org/babel/downloads.php 找到Babel Language Pack Zips,下面选自己版本点进去,找到如下类似的中文包: Ba ...

  8. 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧拿出来拍啊

    花10天时间用C语言做了个小站 http://tieba.yunxunmi.com/index.html 简称: 云贴吧 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧 ...

  9. 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解

    Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解   多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...

随机推荐

  1. How to output the target message in dotnet build command line

    How can I output my target message when I using dotnet build in command line. I use command line to ...

  2. 小心Powershell的位数

    我们都知道64位的 Windows 中有两个Powershell,32位的 Windows Powershell(x86)和64位的 Windows Powershell.(当然,32位的Window ...

  3. 还在拼字符串?试试HTML5的template标签

    HTML5中<template>标签的详细介绍(图文) 这篇文章主要介绍了HTML5中的template标签,是HTML5入门中的重要知识,需要的朋友可以参考 一.HTML5 templa ...

  4. 记录安装Python第三方包“tesserocr”的方法和遇到的坑

    1. 环境: 系统环境:Win7 32 位系统 Python版本: 3.6.5        虚拟环境为:Miniconda3 2. 共需要安装的模块: a. tesserocr b. tessera ...

  5. com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1680 > 1024). You can change this value on the server by setting the max_allowed_packet' variable.

    这个错误是由于mysql的一个系统参数max_allowed_packet设置的值过小引起的 解决这个错误的方法就是修改这个参数的值, linux系统中我们在etc目录下找到my.cnf这个文件,打开 ...

  6. 换根DP(二次扫描)

    参考来自这里: https://blog.csdn.net/qq_41286356/article/details/94554729 题目在这里 https://ac.nowcoder.com/acm ...

  7. 【退役记】CSP2019 退役记

    Day -1 机房自习,因为一些奇怪原因心不在焉 我可能太在意csp了 晚上有点扛不住去七楼阳台思考人生,得到了一些惊人的结论想下来由于某种原因继续跑到七楼思考人生 然后晚自习下课仰天大笑出门去,我辈 ...

  8. 你好,babel

    写在前面 其实学babel是本人2019年Q3的一个计划,因为当时自己做的一个项目需要自己去配babel,也遇到了一些困难,发现自己对babel的了解还是很少的,所以决定好好看下babel:可是后来解 ...

  9. Python 超级玛丽代码实现:人物行走和碰撞检测

    功能介绍 人物行走 人物的行走速度这边分成水平方向(X轴)和竖直方向(Y轴),水平方向的速度要考虑加速度和摩擦力,竖直方向的速度要考虑重力加速度. 水平方向:设定X轴向右走的速度为大于0,向左走的速度 ...

  10. Redis 持久化的两种方案

    reids是一个key-value存储系统,为了保证效率,缓存在内存中,但是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,以保证数据的持久化. 所以:redis是一个支持持 ...