n 阶方阵的逆矩阵代码
时间: 2023-05-21 10:03:13 浏览: 61
以下是求解 n 阶方阵的逆矩阵的代码:
```python
import numpy as np
# 定义一个 n 阶方阵
n = 3
A = np.random.rand(n, n)
# 求解逆矩阵
A_inv = np.linalg.inv(A)
print(A_inv)
```
注意:这里使用了 NumPy 库中的 `linalg.inv()` 函数来求解逆矩阵。
相关问题
c语言编写求n阶方阵的逆矩阵
求一个n阶方阵A的逆矩阵,可以使用高斯-约旦消元法。
具体步骤如下:
1.将n阶方阵A增广为一个2n阶矩阵B,其中左边为矩阵A,右边为一个n阶单位矩阵。
2.对B进行初等行变换,将左边的A变为一个n阶单位矩阵,此时右边的矩阵即为A的逆矩阵。
以下是C语言的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int main()
{
int n, i, j, k;
double a[MAX_SIZE][MAX_SIZE * 2], t;
printf("请输入矩阵阶数n:");
scanf("%d", &n);
printf("请依次输入矩阵元素:\n");
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
scanf("%lf", &a[i][j]);
//构造增广矩阵
for(i = 1; i <= n; i++)
for(j = n + 1; j <= 2 * n; j++)
if(j == i + n)
a[i][j] = 1;
else
a[i][j] = 0;
//高斯-约旦消元
for(i = 1; i <= n; i++)
{
if(a[i][i] == 0)
{
printf("该矩阵无法求逆!");
return 0;
}
t = a[i][i];
for(j = i; j <= 2 * n; j++)
a[i][j] /= t;
for(j = 1; j <= n; j++)
{
if(i != j)
{
t = a[j][i];
for(k = i; k <= 2 * n; k++)
a[j][k] -= t * a[i][k];
}
}
}
//输出逆矩阵
printf("该矩阵的逆矩阵为:\n");
for(i = 1; i <= n; i++)
{
for(j = n + 1; j <= 2 * n; j++)
printf("%lf ", a[i][j]);
printf("\n");
}
return 0;
}
```
注意:该代码中没有对输入进行检验,实际使用中应该加上输入检验和异常处理。
c语言编写求n阶方阵伴随矩阵
以下是一个通用的求 $n$ 阶方阵伴随矩阵的 C 语言代码:
```c
#include <stdio.h>
// 获取 n-1 阶子矩阵
void get_submatrix(int matrix[][100], int sub[][100], int row, int col, int n) {
int i, j, p = 0, q = 0;
for (i = 0; i < n; i++) {
if (i == row) continue;
for (j = 0; j < n; j++) {
if (j == col) continue;
sub[p][q++] = matrix[i][j];
}
q = 0;
p++;
}
}
// 计算行列式的值
int determinant(int matrix[][100], int n) {
int i, j, det = 0;
int sub[100][100];
if (n == 1) {
return matrix[0][0];
} else if (n == 2) {
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0];
} else {
for (i = 0; i < n; i++) {
get_submatrix(matrix, sub, 0, i, n);
det += ((i%2 == 0) ? 1 : -1) * matrix[0][i] * determinant(sub, n-1);
}
}
return det;
}
// 求伴随矩阵
void adjoint(int matrix[][100], int adj[][100], int n) {
int i, j, sign;
int sub[100][100];
if (n == 1) {
adj[0][0] = 1;
} else {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
get_submatrix(matrix, sub, i, j, n);
sign = ((i+j)%2 == 0) ? 1 : -1;
adj[j][i] = sign * determinant(sub, n-1);
}
}
}
}
int main() {
int n, i, j;
int matrix[100][100], adj[100][100];
printf("Enter the size of matrix: ");
scanf("%d", &n);
printf("Enter the elements of matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
adjoint(matrix, adj, n);
printf("Adjoint matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", adj[i][j]);
}
printf("\n");
}
return 0;
}
```
这个代码可以接受用户输入一个 $n$ 阶方阵,并将其伴随矩阵存储在 `adj` 中。你可以根据需要修改输入矩阵和输出矩阵,但请注意,该代码最多支持 $100$ 阶方阵。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)