当前位置:网站首页>Solving inverse matrix with C language
Solving inverse matrix with C language
2022-07-20 08:53:00 【Shi Shang がりへ】
C Language implementation Solving the inverse matrix
Recently, I just completed the review of the probability theory foundation of postgraduate entrance examination , So I began to recall the line generation shortly after the review , For the theoretical part of the matrix , I just feel that I haven't experienced code level interpretation except the routine manual calculation between paper and pen , In view of this , Then he began to look back carefully , Supplemented by the ideas of the first , So far, , In order to grasp the meaning from the lower logic , Choose to use C Language to realize functions , The following is an implementation module that can run through . What one does , Unavoidable omissions , For reference only .
Algorithm ideas
- The identity matrix of the same row and column is spliced on the right side of the original matrix to Expand , get The number of rows remains the same 、 Column number multiplication The extended matrix of ;
- The expanded matrix is Elementary line transformation , obtain The left half is the identity matrix The new matrix of ;
- Intercept the right half of the new matrix , That's what you want Inverse matrix ;
- It should be noted that , Before extending the original matrix , It is necessary to determine whether it meets the extension conditions , That is, whether the original matrix has an inverse matrix .
Implementation code
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
double** expand_matrix;
double** new_matrix;
void initExpandMatrix(double ** mat, double ** expand_mat, int);
int adjustMatrix(double ** expand_mat, int len);
void calculateExpandMatrix(double ** expand_mat, int len);
void getNewMatrix(double ** expand_mat, double ** new_mat, int len);
void printMatrix(double ** mat, int len);
double** getProductMatrix(double ** init_mat, double ** new_mat, int len);
double** getInvMatrix(double ** mat, int len);
int main() {
int len;
scanf("%d", &len);
double** init_mat = (double **)malloc(sizeof(double *) * len);
for (int i = 0; i < len; i++) {
init_mat[i] = (double *)malloc(sizeof(double) * len);
}
/* Test examples 1 2 -1 3 4 -2 5 -4 1 */
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
scanf("%lf", &init_mat[i][j]);
}
}
printf("====== init_matrix ======\n");
printMatrix(init_mat, len);
printf("==== inverse matrix ====\n\n");
double ** new_mat = getInvMatrix(init_mat, len);
printMatrix(new_mat, len);
printf("====== init * inv ======\n\n");
double** product_mat = getProductMatrix(init_mat, new_mat, len);
printMatrix(product_mat, len);
return 0;
}
// inverse matrix
double** getInvMatrix(double ** mat, int len) {
// Extended matrix definition //
expand_matrix = (double**)malloc(sizeof(double *) * len);
for (int i = 0; i < len; i++) {
expand_matrix[i] = (double *)malloc(sizeof(double ) * (len * 2));
}
// Inverse matrix definition //
new_matrix = (double**)malloc(sizeof(double *) * len);
for (int i = 0; i < len; i++) {
new_matrix[i] = (double *)malloc(sizeof(double ) * len);
}
// init
initExpandMatrix(mat, expand_matrix, len);
// adjust
int canAdjust = adjustMatrix(expand_matrix, len);
if (canAdjust == 0) {
return NULL;
}
// calc expand
calculateExpandMatrix(expand_matrix, len);
// Take the back N*N matrix , It's what you want //
getNewMatrix(expand_matrix, new_matrix, len);
return new_matrix;
}
// init expand_matrix
void initExpandMatrix(double ** mat, double ** expand_mat, int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len * 2; j++) {
if (j < len) {
expand_mat[i][j] = mat[i][j];
} else {
if (j == len + i) {
expand_mat[i][j] = 1;
} else {
expand_mat[i][j] = 0;
}
}
}
}
}
// adjust expand matrix
int adjustMatrix(double ** expand_mat, int len) {
for (int i = 0; i < len; i++) {
if (expand_mat[i][i] == 0) {
int j;
for (j = 0; j < len; j++) {
if (expand_mat[j][i] != 0) {
double* tmp = expand_mat[i];
expand_mat[i] = expand_mat[j];
expand_mat[j] = tmp;
break;
}
}
if (j >= len) {
printf("Inv Matrix does not exists\n");
return false;
}
}
}
return true;
}
// calc
void calculateExpandMatrix(double ** expand_mat, int len) {
for (int i = 0; i < len; i++) {
double fir_ele = expand_mat[i][i];
for (int j = 0; j < len * 2; j++) {
expand_mat[i][j] /= fir_ele; // Divide all elements of the line by the first element //
}
for (int m = 0; m < len; m++) {
if (m == i) {
continue;
}
// Multiple //
double times = expand_mat[m][i];
for (int n = 0; n < len * 2; n++) {
expand_mat[m][n] -= expand_mat[i][n] * times;
}
}
}
}
// get res
void getNewMatrix(double ** expand_mat, double ** new_mat, int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len * 2; j++) {
if (j >= len) {
new_mat[i][j - len] = expand_mat[i][j];
}
}
}
}
// print matrix
void printMatrix(double** mat, int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
printf("%.1lf ", mat[i][j]);
}
putchar('\n');
}
putchar('\n');
}
// matrix multiplying
double** getProductMatrix(double** init_mat, double** new_mat, int len) {
double** product_mat = (double**)malloc(sizeof(double*) * len);
for (int i = 0; i < len; i++) {
product_mat[i] = (double*)malloc(sizeof(double) * len);
}
// need initializing to zero
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
product_mat[i][j] = 0;
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
for (int k = 0; k < len; k++) {
product_mat[i][j] += init_mat[i][k] * new_mat[k][j];
}
}
}
return product_mat;
}
- Example test results
- Every day I don't dance , It's all a betrayal of life .
边栏推荐
- The second way to write 85 props
- Talking about wechat payment risk control
- Supplement: ES6 knowledge points
- 淘宝搜索案例
- el-transfer 左侧右侧默认数据展示
- thinkphp6临时关闭布局
- 基于多数据源零代码同时生成多个数据库CRUD增删改查RESTful API接口——MySql,PostgreSql,Oracle,Microsoft SQL Server
- Advantages of aggregate collection code
- 防抖和节流
- How to open the enterprise payment to change | red envelope function
猜你喜欢
随机推荐
密码学科普
Why are there tripartite payments?
What are the three-party payment companies?
100 JD navigation bar slot usage flexible layout (display: flex;)
Talking about wechat payment risk control
数据代理理解
聚合支付满足各行业接入多种支付通道
15. Built in instructions
10S polkadot substrate : 建立代币合约
53 sessionstorage session storage
Object of ES6 Difference between defineproperty and proxy
【数据库基础】MySql基础总结
C语言实现 求解逆矩阵
支付账户体系(分账接口)的9大价值
淘宝搜索案例
Moment.js
列表的渲染、过滤(筛选)、排序操作
91 pop up case - father passes son - son passes father
Simple construction of local image server
4. MVC model and MVVM model