请用c++写一个矩阵类,能实现矩阵的加法,减法,乘法和逆运算,并各举一个运算示例
时间: 2024-06-06 22:08:19 浏览: 14
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
int rows;
int cols;
vector<vector<double>> data;
public:
Matrix(int r, int c) : rows(r), cols(c), data(r, vector<double>(c)) {}
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("matrices must have the same dimensions");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw invalid_argument("matrices must have the same dimensions");
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] - other.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& other) const {
if (cols != other.rows) {
throw invalid_argument("number of columns in the first matrix must be equal to the number of rows in the second matrix");
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
double sum = 0;
for (int k = 0; k < cols; k++) {
sum += data[i][k] * other.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
Matrix inverse() const {
if (rows != cols) {
throw invalid_argument("matrix must be square");
}
int n = rows;
Matrix a(n, 2*n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a.data[i][j] = data[i][j];
}
a.data[i][n+i] = 1;
}
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i+1; j < n; j++) {
if (abs(a.data[j][i]) > abs(a.data[pivot][i])) {
pivot = j;
}
}
swap(a.data[i], a.data[pivot]);
for (int j = i+1; j < 2*n; j++) {
a.data[i][j] /= a.data[i][i];
}
for (int j = 0; j < n; j++) {
if (j == i) continue;
double factor = a.data[j][i];
for (int k = i+1; k < 2*n; k++) {
a.data[j][k] -= factor * a.data[i][k];
}
}
}
Matrix result(n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result.data[i][j] = a.data[i][n+j];
}
}
return result;
}
friend ostream& operator<<(ostream& os, const Matrix& m) {
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
os << m.data[i][j] << ' ';
}
os << '\n';
}
return os;
}
};
int main() {
Matrix a(2, 3);
a.data = {{1, 2, 3}, {4, 5, 6}};
Matrix b(2, 3);
b.data = {{7, 8, 9}, {10, 11, 12}};
cout << a + b << '\n';
cout << a - b << '\n';
Matrix c(3, 2);
c.data = {{1, 2}, {3, 4}, {5, 6}};
Matrix d(2, 4);
d.data = {{7, 8, 9, 10}, {11, 12, 13, 14}};
cout << c * d << '\n';
Matrix e(3, 3);
e.data = {{1, 2, 3}, {0, 1, 4}, {5, 6, 0}};
cout << e.inverse() << '\n';
return 0;
}
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)