当前位置:网站首页>3. C language uses algebraic cofactor to calculate determinant
3. C language uses algebraic cofactor to calculate determinant
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
1. The principle of algebraic cofactor calculating determinant
Second order determinant calculation
Third order determinant calculation
Calculation of determinant Det
3. The shortcomings of this method
1. The principle of algebraic cofactor calculating determinant
Be careful : Determinant must be a square matrix !
Second order determinant calculation
Killing method :
The calculation method of second-order determinant is very simple , Multiply the main diagonal elements — Multiply the sub diagonal elements , So it is also called killing method .
Third order determinant calculation
Scribing method :
The scribing method is shown in the figure below
Calculated by definition
When calculating determinant , It can be expanded by row or column for calculation
Take the third-order determinant as an example :
Let's make the determinant A Expand by the first line :
among ,Aij by aij The algebraic covalent of (i Is line number ,j For column number ), for example :A11 by a11 The algebraic covalent of .
Calculation method of algebraic cofactor :
among ,(-1)^(i+j) Expressed as -1 Of i+j Power , Used to determine the positive and negative of algebraic cofactor ,Mij It is remainder formula .
The remainder formula :
Determinant to its i That's ok j Column , The remaining elements form a new determinant , The matrix is aij The Yu Zi form of .
for example ,a11 The remainder of is :
Go to the first row and the first column , The remaining elements are combined into a new determinant according to the existing arrangement .
When calculating the value of determinant , We can choose 0 Expand the extra row and column , Greatly reduce the amount of calculation .
2. Code implementation
The core idea :
When we find the value of higher-order determinant , We can use the method of finding algebraic cofactor to gradually transform the problem of finding high-order determinant into the problem of finding low-order determinant . utilize recursive The idea of can solve such problems , Make big things small , It's trivial .
Input matrix main
stay C In language , A matrix can be stored using a two-dimensional array .
Of course, using arrays to store matrices also has its drawbacks : The size of the array cannot be changed . Before learning dynamic programming , You can open up enough memory space according to the size used , To deal with this problem .
When inputting determinant , We also need to determine its order n To calculate .
#include <stdio.h>
#include <math.h>
#include<assert.h>
#define COL 10// Maximum memory for two-dimensional arrays
int main()
{
int arr[COL][COL] = { 0 };
int i = 0, j = 0;
int n = 0;// Order
printf(" Determinant order :>");
scanf("%d", &n);
printf(" Please enter the determinant :>\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("%d", Det(arr, n));
}
Calculation of determinant Det
because 1 The determinant of order does not need to be calculated , Is itself , and 3 rank 、2 The determinant of order can be calculated according to the scribing method and the killing method , Here you can set the condition of recursion to n>3, Only determinants of order 4 and above are recursive , The determinant below the fourth order can be calculated directly .
- Before determinant calculation , To ensure the security of the array , When receiving the array, add const, Ensure that the array cannot be modified inside the function .
- The order of determinant is only greater than or equal to 1 Only when makes sense , Use at the beginning of the function assert Assertion n Is it greater than 0: If the condition is true, the function runs , The condition is false assert A warning will be given ,assert Need to include header file .
int Cof(const int arr2[COL][COL], int i, int n);// Statement : Calculate determinant
// Calculate determinant
int Det(const int arr[COL][COL], int n)
{
assert(n > 0);
int sum = 0;
int i = 0;
if (n == 1)//1 The order determinant leads directly to the result
{
sum = arr[0][0];
}
else if (n == 2)
{
sum = arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0];// Kill method to solve
}
else if (n == 3)
{
sum = arr[0][0] * arr[1][1] * arr[2][2]
+ arr[0][1] * arr[1][2] * arr[2][0]
+ arr[1][0] * arr[2][1] * arr[0][2]
- arr[0][2] * arr[1][1] * arr[2][0]
- arr[0][1] * arr[1][0] * arr[2][2]
- arr[1][2] * arr[2][1] * arr[0][0];// Solve by scribing
}
else
{
for (i = 0; i < n; i++)// Expand by the first line
{
if (arr[0][i] != 0)// The expanded item is not 0 Only calculated
{
sum += ((int)pow(-1, i + 2)) * arr[0][i] * (Cof(arr, i, n));//2 Continue recursion above order
}
else
sum += 0;// The expansion item is 0
}
}
return sum;
}
The remainder formula Cof
receive Det The two-dimensional array passed by the function , Take its place , The remaining elements form a new two-dimensional array and then pass it to Det function . The two functions call each other , Until the order is less than 4.
int Det(const int arr[COL][COL], int n);// Statement : Computing algebraic cofactor
// Find the remainder formula
int Cof(const int arr[COL][COL], int i, int n)
{
assert(n > 0);
int k = 0;
int j = 0;
int arr2[COL][COL] = { 0 };
for (k = 0; k < n - 1; k++)// Remove 0 That's ok i Column , The rest forms a new matrix
{
for (j = 0; j < n - 1; j++)
{
if (j < i)
{
arr2[k][j] = arr[k + 1][j];
}
else
{
arr2[k][j] = arr[k + 1][j + 1];
}
}
}
return Det(arr2, n - 1);
}
3. The shortcomings of this method
Although we have set up many methods to avoid useless recursion
- The expansion item is 0 Then the calculation is not carried out
- 3 Direct calculation of determinant below order
But it is still insufficient to find the determinant of higher order : If you ask 10 Step determinant , Recursion is required 10!/ 6=604,800 Time , Deep recursion eventually causes the program to run very slowly .
边栏推荐
- Tyut Taiyuan University of technology 2022 introduction to software engineering summary
- 阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
- Redis介绍与使用
- Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
- Set container
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
- MySQL Database Constraints
- C语言实现扫雷游戏(完整版)
- 5. Download and use of MSDN
- 7.数组、指针和数组的关系
猜你喜欢
7.数组、指针和数组的关系
Arduino+ water level sensor +led display + buzzer alarm
How do architects draw system architecture blueprints?
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
TYUT太原理工大学2022数据库大题之E-R图转关系模式
TYUT太原理工大学2022数据库之关系代数小题
TYUT太原理工大学2022数据库大题之数据库操作
西安电子科技大学22学年上学期《基础实验》试题及答案
随机推荐
8.C语言——位操作符与位移操作符
Tyut Taiyuan University of technology 2022 introduction to software engineering
Abstract classes and interfaces
2. Preliminary exercises of C language (2)
4.30动态内存分配笔记
Several high-frequency JVM interview questions
arduino+水位传感器+led显示+蜂鸣器报警
View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
MySQL Database Constraints
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
【快趁你舍友打游戏,来看道题吧】
JS interview questions (I)
Application architecture of large live broadcast platform
初识指针笔记
View UI plus released version 1.2.0 and added image, skeleton and typography components
阿里云微服务(三)Sentinel开源流控熔断降级组件
Quickly generate illustrations
魏牌:产品叫好声一片,但为何销量还是受挫
IPv6 experiment
Tyut outline of 2022 database examination of Taiyuan University of Technology