c++利用类矩阵逆矩阵
时间: 2023-08-03 13:12:29 浏览: 90
要利用类实现矩阵逆矩阵的计算,需要以下步骤:
1. 定义一个矩阵类,包含矩阵的行数、列数和元素值等信息,以及实现矩阵加减、乘法、转置等基本运算。
2. 实现高斯-约旦消元法(Gauss-Jordan elimination)求解矩阵的逆矩阵。具体步骤如下:
- 将原矩阵和单位矩阵按行拼接成增广矩阵;
- 对增广矩阵进行初等行变换,将其变为左半部分为单位矩阵的形式;
- 右半部分即为所求的逆矩阵。
3. 实现矩阵行列式的计算,以判断矩阵是否可逆。
下面是一个简单的示例代码,仅供参考:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
public:
Matrix(int r = 0, int c = 0) : rows(r), cols(c), data(r * c) {}
int rows, cols;
vector<double> data;
double& operator()(int i, int j) {
return data[i * cols + j];
}
friend Matrix operator+(const Matrix& a, const Matrix& b);
friend Matrix operator*(const Matrix& a, const Matrix& b);
friend Matrix transpose(const Matrix& a);
friend double det(const Matrix& a);
friend Matrix inverse(const Matrix& a);
};
Matrix operator+(const Matrix& a, const Matrix& b) {
Matrix c(a.rows, a.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
c(i, j) = a(i, j) + b(i, j);
}
}
return c;
}
Matrix operator*(const Matrix& a, const Matrix& b) {
Matrix c(a.rows, b.cols);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < b.cols; j++) {
for (int k = 0; k < a.cols; k++) {
c(i, j) += a(i, k) * b(k, j);
}
}
}
return c;
}
Matrix transpose(const Matrix& a) {
Matrix b(a.cols, a.rows);
for (int i = 0; i < a.rows; i++) {
for (int j = 0; j < a.cols; j++) {
b(j, i) = a(i, j);
}
}
return b;
}
double det(const Matrix& a) {
if (a.rows != a.cols) {
throw "not a square matrix";
}
int n = a.rows;
Matrix b = a;
double d = 1;
for (int i = 0; i < n; i++) {
int p = i;
for (int j = i + 1; j < n; j++) {
if (abs(b(j, i)) > abs(b(p, i))) {
p = j;
}
}
if (p != i) {
swap(b.data.begin() + i * n, b.data.begin() + p * n);
d = -d;
}
if (b(i, i) == 0) {
return 0;
}
for (int j = i + 1; j < n; j++) {
double f = b(j, i) / b(i, i);
for (int k = i; k < n; k++) {
b(j, k) -= f * b(i, k);
}
}
d *= b(i, i);
}
return d;
}
Matrix inverse(const Matrix& a) {
if (a.rows != a.cols) {
throw "not a square matrix";
}
int n = a.rows;
Matrix b(n, 2 * n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
b(i, j) = a(i, j);
}
b(i, n + i) = 1;
}
for (int i = 0; i < n; i++) {
int p = i;
for (int j = i + 1; j < n; j++) {
if (abs(b(j, i)) > abs(b(p, i))) {
p = j;
}
}
if (p != i) {
swap(b.data.begin() + i * 2 * n, b.data.begin() + p * 2 * n);
}
if (b(i, i) == 0) {
throw "matrix is not invertible";
}
double f = b(i, i);
for (int j = i; j < 2 * n; j++) {
b(i, j) /= f;
}
for (int j = 0; j < n; j++) {
if (j != i) {
f = b(j, i);
for (int k = i; k < 2 * n; k++) {
b(j, k) -= f * b(i, k);
}
}
}
}
Matrix c(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c(i, j) = b(i, n + j);
}
}
return c;
}
int main() {
Matrix a(3, 3);
a(0, 0) = 2; a(0, 1) = 1; a(0, 2) = 1;
a(1, 0) = 4; a(1, 1) = -6; a(1, 2) = 0;
a(2, 0) = -2; a(2, 1) = 7; a(2, 2) = 2;
cout << "A =\n" << a.data[0] << " " << a.data[1] << " " << a.data[2] << "\n"
<< a.data[3] << " " << a.data[4] << " " << a.data[5] << "\n"
<< a.data[6] << " " << a.data[7] << " " << a.data[8] << endl;
cout << "det(A) = " << det(a) << endl;
Matrix b = inverse(a);
cout << "A^-1 =\n" << b.data[0] << " " << b.data[1] << " " << b.data[2] << "\n"
<< b.data[3] << " " << b.data[4] << " " << b.data[5] << "\n"
<< b.data[6] << " " << b.data[7] << " " << b.data[8] << endl;
Matrix c = a * b;
cout << "A * A^-1 =\n" << c.data[0] << " " << c.data[1] << " " << c.data[2] << "\n"
<< c.data[3] << " " << c.data[4] << " " << c.data[5] << "\n"
<< c.data[6] << " " << c.data[7] << " " << c.data[8] << endl;
return 0;
}
```
这段代码定义了一个矩阵类 Matrix,包含了矩阵的基本运算和逆矩阵计算。在 main 函数中,我们定义了一个 3x3 的矩阵 A,计算其行列式和逆矩阵,并验证 A 乘以 A 的逆矩阵是否得到单位矩阵。注意,这里的逆矩阵计算使用的是高斯-约旦消元法,而不是传统的伴随矩阵法,因为前者更简单、更高效。
阅读全文