您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

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

2024/4/17 10:30:04发布4次查看
给定一个矩阵 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-singularinput-: mat[3][3]= { 0, 0, 0 }, { 10, 20, 30 }, { 1, 4, -3 }output-: matrix is singularsince the entire first row is 0 the determinant will be zero only
算法startin 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 forin 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 din 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 <stdio.h>#define n 4//to find the cofactorsint 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 functionsint 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
以上就是检查矩阵是否奇异的c程序的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录