Skip to content

Commit

Permalink
Update v1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Amoiensis committed Feb 2, 2021
1 parent 152b79b commit c191e90
Show file tree
Hide file tree
Showing 53 changed files with 3,094 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Optimization-Algorithm(最优化算法):https://github.com/Amoiensis/Optimizat
| 两矩阵对应位置逻辑运算 | Logical operation of corresponding positions of two matrices | M_logic |
| 矩阵对应元素乘/除 | Multiply / divide corresponding elements of matrix | M_pmuldiv |
| 矩阵批量赋值(使用矩阵传递) | Matrix batch assignment (using matrix transfer) | M_setval |
| 矩阵对矩阵,对各行进行数乘 | Matrix Number Multiplication (using matrix transfer) | M_setval |
| 帮助 | Help File | help |

Demo (Matrix_hub)
---------------------------------------
Expand Down Expand Up @@ -164,7 +166,11 @@ int main(int argc, char *argv[]) {
// Others
M_free(_mat_T);
// Help 函数
help("help");
help("Update");
system("pause");
return 0;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions code_src/Matrix_Hub_v1.4/_Matrix_Hub_v1.4更新说明.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Matrix_Hub
---------------------------------------
矩阵运算库--C语言
The lib of Matrix operation for C language.
---------------------------------------
Author: Amoiensis
Email: [email protected]
Data:2020.02.12
***************************************************************
[更新说明v1.4]
1. 新增 help() 函数,可以输入各函数名称,查看具体使用方法;如,help("help")、help("Matrix_gen")、help("README")、help("Update"),等;
2. 新增函数“M_numul_m ()”,用于矩阵数乘 ,矩阵对于矩阵进行操作,各行对应数乘 ;
3. 将原 M_matFull() 函数中,最左侧,和最上侧,row_up和column_left取值从“0”设置为“1(_HEAD_)”;
4. 修正原代码中"Matirx"的误写,修正为"Matrix";
***************************************************************
更多资料和信息:
https://github.com/Amoiensis/Matrix_hub
***************************************************************
具体应用例子:
Optimization-Algorithm(最优化算法):https://github.com/Amoiensis/Optimization-Algorithm
***************************************************************
149 changes: 149 additions & 0 deletions code_src/Matrix_Hub_v1.4/demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
%% IMFORMATION
%% MATRIX_HUB
% Author: Xiping Yu
% Email:[email protected]
% Github: https://github.com/Amoiensis/Matrix_hub
% Data: 2020.02.12
% Case: Matrix Operation
% Dtailed: the code_file of Matrix_hub
*/

#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"


int main(int argc, char *argv[]) {
system("color 0F");

// Matrix
// Mat_1
MATRIX_TYPE _mat_1[3][5] = { 2,2,1,1,1,4,1,1,1,1,1,5,1,1,1 };
int row = sizeof(_mat_1) / sizeof(_mat_1[0]);
int column = sizeof(_mat_1[0]) / sizeof(_mat_1[0][0]);
Matrix* mat_1 = Matrix_gen(row,column,_mat_1);
M_print(mat_1);
// Mat_2
MATRIX_TYPE _mat_2[5][3] = { 1,2,3,4,5,6,7,8,7,10,11,12,11,14,15 };
// MATRIX_TYPE _mat_2[5][2] = { 1,2,3,4,5,6,7,8,9,10};
row = sizeof(_mat_2) / sizeof(_mat_2[0]);
column = sizeof(_mat_2[0]) / sizeof(_mat_2[0][0]);
Matrix* mat_2 = Matrix_gen(row,column,_mat_2);
M_print(mat_2);
// Mat_A
MATRIX_TYPE _mat_A[5][5] = { 1,0,7,0,9,0,7,0,9,0,0,0,-5,0,4,2,0,0,2,0,0,3,8,0,1};
row = sizeof(_mat_A) / sizeof(_mat_A[0]);
column = sizeof(_mat_A[0]) / sizeof(_mat_A[0][0]);
Matrix* mat_A = Matrix_gen(row,column,_mat_A);
M_print(mat_A);
// A_10
MATRIX_TYPE _mat_A10[10][10] = {0.221746734017240,0.928854139478045,0.488897743920167,0.0987122786555743,0.715037078400694,0.904722238067363,0.489901388512224,0.521649842464284,0.800330575352402,0.0604711791698936,0.117417650855806,0.730330862855453,0.624060088173690,0.261871183870716,0.903720560556316,0.609866648422558,0.167927145682257,0.0967300257808670,0.453797708726920,0.399257770613576,0.296675873218327,0.488608973803579,0.679135540865748,0.335356839962797,0.890922504330789,0.617666389588455,0.978680649641159,0.818148553859625,0.432391503783462,0.526875830508296,0.318778301925882,0.578525061023439,0.395515215668593,0.679727951377338,0.334163052737496,0.859442305646212,0.712694471678914,0.817547092079286,0.825313795402046,0.416799467930787,0.424166759713807,0.237283579771521,0.367436648544477,0.136553137355370,0.698745832334795,0.805489424529686,0.500471624154843,0.722439592366842,0.0834698148589140,0.656859890973707,0.507858284661118,0.458848828179931,0.987982003161633,0.721227498581740,0.197809826685929,0.576721515614685,0.471088374541939,0.149865442477967,0.133171007607162,0.627973359190104,0.0855157970900440,0.963088539286913,0.0377388662395521,0.106761861607241,0.0305409463046367,0.182922469414914,0.0596188675796392,0.659605252908307,0.173388613119006,0.291984079961715,0.262482234698333,0.546805718738968,0.885168008202475,0.653757348668560,0.744074260367462,0.239932010568717,0.681971904149063,0.518594942510538,0.390937802323736,0.431651170248720,0.801014622769739,0.521135830804002,0.913286827639239,0.494173936639270,0.500022435590201,0.886511933076101,0.0424311375007417,0.972974554763863,0.831379742839070,0.0154871256360190,0.0292202775621463,0.231594386708524,0.796183873585212,0.779051723231275,0.479922141146060,0.0286741524641061,0.0714454646006424,0.648991492712356,0.803364391602440,0.984063724379154};
row = sizeof(_mat_A10) / sizeof(_mat_A10[0]);
column = sizeof(_mat_A10[0]) / sizeof(_mat_A10[0][0]);
Matrix* mat_A10 = Matrix_gen(row,column,_mat_A10);
M_print(mat_A10);
// Mat_21
MATRIX_TYPE _mat_21[3][3] = { 0,2,3,2,1,2,4,1,3};
// MATRIX_TYPE _mat_21[3][3] = {0,0,1,0,0,1,0,0,1};
row = sizeof(_mat_21) / sizeof(_mat_21[0]);
column = sizeof(_mat_21[0]) / sizeof(_mat_21[0][0]);
Matrix* mat_21 = Matrix_gen(row,column,_mat_21);
M_print(mat_21);
// Mat_21b
MATRIX_TYPE _mat_21b[3][3] = { 1,0,0,0,1,0,0,0,1};
row = sizeof(_mat_21b) / sizeof(_mat_21b[0]);
column = sizeof(_mat_21b[0]) / sizeof(_mat_21b[0][0]);
Matrix* mat_21b = Matrix_gen(row,column,_mat_21b);
M_print(mat_21b);
// Mat_b
MATRIX_TYPE _mat_b[5][1] = { 1,2,7,2,4};
row = sizeof(_mat_b) / sizeof(_mat_b[0]);
column = sizeof(_mat_b[0]) / sizeof(_mat_b[0][0]);
Matrix* mat_b = Matrix_gen(row,column,_mat_b);
M_print(mat_b);
// B_10
MATRIX_TYPE _mat_b10[10][1] = {0.167168409914656,0.106216344928664,0.372409740055537,0.198118402542975,0.489687638016024,0.339493413390758,0.951630464777727,0.920332039836564,0.0526769976807926,0.737858095516997};
row = sizeof(_mat_b10) / sizeof(_mat_b10[0]);
column = sizeof(_mat_b10[0]) / sizeof(_mat_b10[0][0]);
Matrix* mat_b10 = Matrix_gen(row,column,_mat_b10);
M_print(mat_b10);

// Operation
// 乘法
Matrix* mat_3 = M_mul(mat_2,mat_1);
M_print(mat_3);
// 加减法
Matrix* mat_diff = M_add_sub(1,mat_21,1,mat_21b);
M_print(mat_diff);
// 初等变换
Etrans_struct _Etrans_;
_Etrans_.minuend_line = 2;
_Etrans_.subtractor_line = 1;
_Etrans_.scale = 2;
_Etrans_.next_E_trans = NULL;
_Etrans_.forward_E_trans = NULL;
M_E_trans(mat_2,&_Etrans_,_ROW_);
M_print(mat_2);
// 单位矩阵
M_print(M_I(5));
// 初等变换to矩阵
Matrix* mat_4 = Etrans_2_Matrix(&_Etrans_,5,_ROW_);
M_print(mat_4);
Matrix* mat_5 = Etrans_2_Inverse(&_Etrans_,5,_ROW_);
M_print(mat_5);
// 上三角变换
Uptri_struct* _Uptri_ = M_Uptri_(mat_21);
M_print(_Uptri_->trans_matrix );
M_print(_Uptri_->Uptri_matrix);
// 下三角变换
Lowtri_struct* _Lowtri_ = M_Lowtri_(mat_21);
M_print(_Lowtri_->Lowtri_matrix);
M_print(_Lowtri_->trans_matrix);
// 对角化
Dia_struct* _Dia_ = M_Diatri_(mat_21);
M_print(_Dia_->trans_leftmatrix);
M_print(_Dia_->Diatri_matrix);
M_print(_Dia_->trans_rightmatrix);
// 矩阵求逆
Matrix* _mat_inv = M_Inverse(mat_21);
M_print(_mat_inv );
// 行列交换
M_Swap(_mat_inv,1,2,_ROW_);
M_print(_mat_inv);
// 切割部分
Matrix* _mat_cut = M_Cut(_mat_inv,_END_,_END_,2,3);
M_print(_mat_cut);
// 转置
Matrix* _mat_T = M_T(_mat_inv);
M_print(_mat_T);
// 迹
MATRIX_TYPE _tr_mat = M_tr(_mat_inv);
printf("Tr(Matrix_%x) = %.4lf\n",_mat_inv,_tr_mat);
// 行列式
MATRIX_TYPE _det_mat = M_det(_mat_inv);
printf("Det(Matrix_%x) = %.4lf\n",mat_21,_det_mat);
// 填充
Matrix* mat_full = M_full(mat_2,1,1,1,1,0);
M_print(mat_full);
// 范数
printf("NORM1 = %lf\n",M_norm(mat_b,1));
printf("NORM2 = %lf\n",M_norm(mat_b,2));
// Application
// 解线性方程
// mat_A*x = mat_b
printf("#Solver:mat_A*x = mat_b\n");
Matrix* _mat_result = M_mul(M_Inverse(mat_A10),mat_b10);
M_print(_mat_result);

// Others
M_free(_mat_T);

// Help 函数
help("help");
help("Matrix_gen");
help("Update");

system("pause");
return 0;
}
29 changes: 29 additions & 0 deletions code_src/Matrix_Hub_v1.4/help/ERROR_TABLE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---------------------
$ ERROR_TABLE $
---------------------
[Type]
<TABLE>
/*ERROR TABLE*/
#define M_mul_001 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_mul_001)_mat_left->column != _mat_right->row\n"
#define M_Dia_Inv_002 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_Dia_Inv_002)_mat_left->column != _mat_right->row\n"
#define M_add_sub_003 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_add_sub_003)_mat_subed != _mat_minus\n"
#define M_swap_004 "@ERROR: Matrix_Swap_Line Over!\n\tDetails:(M_swap_004)_Swap_line over the limited\n"
#define M_Cut_005 "@ERROR: Matrix_Cut Over!\n\tDetails:(M_Cut_005)_Cut_tail over_the limited\n"
#define M_Cut_006 "@ERROR: Matrix_Cut Wrong!\n\tDetails:(M_Cut_006)_Head_>_Tail\n"
#define M_Cut_007 "@ERROR: Matrix_Cut Wrong!\n\tDetails:(M_Cut_007)_Range_can't_be_negative!'\n"
#define M_tr_008 "@ERROR: Matrix_trace Wrong!\n\tDetails:(M_tr_008)_ROW_!=_COLUMN.'\n"
#define M_det_009 "@ERROR: Matrix_Determinant_ Wrong!\n\tDetails:(M_det_009)_ROW_!=_COLUMN.'\n"
#define M_Mfull_010 "@ERROR: M_matFull Wrong!\n\tDetails:(M_Mfull_010)_ROW_OVER!.'\n"
#define M_Mfull_011 "@ERROR: M_matFull Wrong!\n\tDetails:(M_Mfull_011)_COLUMN_OVER!.'\n"
#define M_logic_012 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_logic_012)_mat_left->row != _mat_right->row\n"
#define M_logic_013 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_logic_013)_mat_left->column != _mat_right->column\n"
#define M_logic_014 "@ERROR: Operation Wrong(Dont Exist)!\n\tDetails:(M_logic_014)Operation_Wrong !\n"
#define M_pmuldiv_015 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_pmuldiv_015)_mat_left->row != _mat_right->row\n"
#define M_pmuldiv_016 "@ERROR: Matrix_Dimensions Wrong!\n\tDetails:(M_pmuldiv_016)_mat_left->column != _mat_right->column\n"
#define M_pmuldiv_017 "@ERROR: Operation Wrong(Dont Exist)!\n\tDetails:(M_pmuldiv_017)Operation_Wrong !\n"
#define M_setval_018 "@ERROR: Mat_order lager than Mat_ini !\n\tDetails:(M_setval_018)Mat_order_Size_Wrong !\n"
#define M_setval_019 "@ERROR: Mat_order lager than Mat_val !\n\tDetails:(M_setval_019)Mat_order_Size_Wrong !\n"
#define M_setval_020 "@ERROR: Mat_ini lager than Mat_order !\n\tDetails:(M_setval_020)Mat_ini_Size_Wrong !\n"

(END)
---------------------
35 changes: 35 additions & 0 deletions code_src/Matrix_Hub_v1.4/help/Etrans_2_Inverse.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---------------------
$ Etrans_2_Inverse $
---------------------
[Type]
<function>
Matrix* Etrans_2_Inverse(Etrans_struct* _Etrans_,int order,int line_setting);
/*Inverse_Element_trans_to_Matrix �����任ת�����ת�ã�Ϊ����׼���ij��ȱ任������ʹ��M_Inverse()��
�����任ת���������ڵ�λ����*/
|--Etrans_struct* _Etrans_; -> �г��ȱ任�ṹ�壨������ help(Etrans_struct)��;
|--int order; -> ���Ⱦ����ά����
|--int line_setting; -> ���ý��С��С����С� ���ȱ任��
----
|--Matrix* return value-> ��λ����I�����任��Ľ�������������棻
[Usage]
M_E_trans(mat, &_Etrans_,_ROW_);
�����任ת���������ڵ�λ���󣬶Ե�λ����ʹ�ó��ȱ任����_Etrans_������_ROW_���г��ȣ��任��

[e.g.]
<code>
// Element teransfor
Etrans_struct _Etrans_;
_Etrans_.minuend_line = 2;
_Etrans_.subtractor_line = 1;
_Etrans_.scale = 2;
_Etrans_.next_E_trans = NULL;
_Etrans_.forward_E_trans = NULL;
M_print(Etrans_2_Inverse(&_Etrans_,4,_ROW_));
<output>
>>Matrix_ba1830:
1.00 0.00 0.00 0.00
-2.00 1.00 0.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 0.00 1.00
(END)
---------------------
36 changes: 36 additions & 0 deletions code_src/Matrix_Hub_v1.4/help/Etrans_2_Matrix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---------------------
$ Etrans_2_Matrix $
---------------------
[Type]
<function>
Matrix* Etrans_2_Matrix(Etrans_struct* _Etrans_,int order,int line_setting);
/*Element_trans_to_Matrix �����任ת����*/
/*Ϊ����׼���ij��ȱ任������ʹ��M_Inverse()��
�����任ת���������ڵ�λ����*/
|--Etrans_struct* _Etrans_; -> �г��ȱ任�ṹ�壨������ help(Etrans_struct)��;
|--int order; -> ���Ⱦ����ά����
|--int line_setting; -> ���ý��С��С����С� ���ȱ任��
----
|--Matrix* return value-> ��λ����I�����任��Ľ�������������棻
[Usage]
M_E_trans(mat, &_Etrans_,_ROW_);
�����任ת���������ڵ�λ���󣬶Ե�λ����ʹ�ó��ȱ任����_Etrans_������_ROW_���г��ȣ��任��

[e.g.]
<code>
// Element teransfor
Etrans_struct _Etrans_;
_Etrans_.minuend_line = 2;
_Etrans_.subtractor_line = 1;
_Etrans_.scale = 2;
_Etrans_.next_E_trans = NULL;
_Etrans_.forward_E_trans = NULL;
M_printEtrans_2_Matrix (&_Etrans_,4,_ROW_));
<output>
>>Matrix_1e1830:
1.00 0.00 0.00 0.00
2.00 1.00 0.00 0.00
0.00 0.00 1.00 0.00
0.00 0.00 0.00 1.00
(END)
---------------------
39 changes: 39 additions & 0 deletions code_src/Matrix_Hub_v1.4/help/M_Cut.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---------------------
$ M_Cut $
---------------------
[Type]
<function>
Matrix* M_Cut(Matrix* _mat,int row_head,int row_tail,int column_head,int column_tail);
/*Cut_out_part_of_Matrix ��ȡ���־���*/
|--Matrix* _mat-> ԭ����
|--int row_head-> ��ͷ��������ѡ,_HEAD_/_END_����
|--int row_tail-> ��β��������ѡ,_HEAD_/_END_����
|--int column_head-> ��ͷ��������ѡ,_HEAD_/_END_����
|--int column_tail-> ��β��������ѡ,_HEAD_/_END_����
----
|--Matrix* return-> �����и����õĽ������
[Usage]
Matrix* _mat_cut = M_Cut(mat,_END_,_END_,2,3);
�и�ã�mat�����У����һ�У���2-3�У�

[e.g.]
<code>
// Mat_21
MATRIX_TYPE _mat_21[3][3] = { 0,2,3,2,1,2,4,1,3};
row = sizeof(_mat_21) / sizeof(_mat_21[0]);
column = sizeof(_mat_21[0]) / sizeof(_mat_21[0][0]);
Matrix* mat_21 = Matrix_gen(row,column,_mat_21);
M_print(mat_21);
// Cut
M_print(mat_21);
Matrix* _mat_cut = M_Cut(mat_21,_END_,_END_,2,3);
M_print(_mat_cut);
<output>
>>Matrix_1a1680:
0.00 2.00 3.00
2.00 1.00 2.00
4.00 1.00 3.00
>>Matrix_1a1830:
1.00 3.00
(END)
---------------------
24 changes: 24 additions & 0 deletions code_src/Matrix_Hub_v1.4/help/M_Dia_Inv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---------------------
$ M_Dia_Inv $
---------------------
[Type]
<function>
Matrix* M_Dia_Inv(Matrix* _mat_source)v;
/*M_Inv for Dia_matrix �ԽǾ�������*/
|--Matrix* _mat_source; -> �������ԽǾ���
----
|--Matrix* return-> �ԽǾ�������������
[Usage]
M_Dia_Inv(M_numul(M_I(3),2));
�Ը����ԽǾ�֤����������棬�����������

[e.g.]
<code>
M_print(M_Dia_Inv(M_numul(M_I(3),2)));
<output>
>>Matrix_741850:
0.50 0.00 0.00
0.00 0.50 0.00
0.00 0.00 0.50
(END)
---------------------
Loading

0 comments on commit c191e90

Please sign in to comment.