0

0

检查矩阵是否奇异的C程序

WBOY

WBOY

发布时间:2023-08-25 23:09:06

|

1738人浏览过

|

来源于tutorialspoint

转载

检查矩阵是否奇异的c程序

给定一个矩阵 mat[row][column],我们的任务是通过函数检查给定矩阵是否奇异并显示结果。

奇异矩阵是其行列式的矩阵为零,如果行列式不为零,则该矩阵是非奇异的。

因此,要确定该矩阵是奇异的还是非奇异的,我们需要首先计算行列式。矩阵的行列式可以计算为 -

$$M1[3][3]\:=\:\begin{bmatrix}a & b & c \d & e & f \g & h & i \end{bmatrix}$$

|m1| = a(e*i - f*h) - b(d*i - f*g) + c(d*h - e*g)

示例

Input-: mat[3][3]= { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 }
Output-: matrix is non-singular
Input-: mat[3][3]= { 0, 0, 0 },
   { 10, 20, 30 },
   { 1, 4, -3 }
Output-: matrix is singular
Since the entire first row is 0 the determinant will be zero only

算法

Start
In function cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n)
{
   Step 1-> Declare and initialize i = 0, j = 0, row, col
   Step 2-> Loop For row = 0 and row < n and row++
   Loop For col = 0 and col < n and col++
      If row != p && col != q then,
      Set  matrix2[i][j++] as matrix[row][col]
         If j == n – 1 then,
            Set j = 0
            Increment i by 1
         End for
      End for
In function int check_singular(int matrix[N][N], int n)
   Step 1-> Declare and initialize int D = 0;
   Step 2-> If n == 1 then,
      Return matrix[0][0]
   Step 3-> Declare matrix2[N][N], sign = 1
   Step 4-> Loop For f = 0 and f < n and f++
      Call function cofactor(matrix, matrix2, 0, f, n)
         Set D += sign * matrix[0][f] * check_singular(matrix2, n - 1)
         Set sign = -sign
      End loop
   Step 5-> Return D
In main()
   Step 1-> Declare and initialize a matrix[N][N]
   Step 2-> If call check_singular(matrix, N) returns non 0 value then,
      Print "Matrix is Singular "
   Step 3-> Else
      Print "Matrix is non-Singular "
Stop

示例

 实时演示

#include 
#define N 4
//to find the cofactors
int cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n) {
   int i = 0, j = 0;
   int row, col;
   // Looping for each element of the matrix
   for (row = 0; row < n; row++) {
      for (col = 0; col < n; col++) {
         // Copying into temporary matrix only
         // those element which are not in given
         // row and column
         if (row != p && col != q) {
            matrix2[i][j++] = matrix[row][col];
            // Row is filled, so increase row
            // index and reset col index
            if (j == n - 1) {
               j = 0;
               i++;
            }
         }
      }
   }
   return 0;
}
/* Recursive function to check if matrix[][] is singular or not. */
int check_singular(int matrix[N][N], int n) {
   int D = 0; // Initialize result
   // Base case : if matrix contains single element
   if (n == 1)
   return matrix[0][0];
   int matrix2[N][N]; // To store cofactors
   int sign = 1; // To store sign multiplier
   // Iterate for each element of first row
   for (int f = 0; f < n; f++) {
      // Getting Cofactor of matrix[0][f]
      cofactor(matrix, matrix2, 0, f, n);
      D += sign * matrix[0][f] * check_singular(matrix2, n - 1);
      // terms are to be added with alternate sign
      sign = -sign;
   }
   return D;
}
// Driver program to test above functions
int main() {
   int matrix[N][N] = { { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 } };
   if (check_singular(matrix, N))
      printf("Matrix is Singular

"); else printf("Matrix is non-Singular

"); return 0; }

输出

如果运行上面的代码,它将生成以下输出 -

Matrix is non-Singular

相关专题

更多
页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

383

2023.08.14

虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

26

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

19

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

31

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

5

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

3

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
NumPy 教程
NumPy 教程

共44课时 | 2.6万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

MySQL 初学入门(mosh老师)
MySQL 初学入门(mosh老师)

共3课时 | 0.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号