c++写矩阵类实现加减乘除操作
时间: 2024-01-23 09:03:52 浏览: 121
C++ 矩阵的加减乘除.docx
5星 · 资源好评率100%
好的,以下是一个简单的矩阵类的实现,包括加减乘除操作:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
int row, col;
vector<vector<double>> data;
public:
Matrix(int r, int c) {
row = r;
col = c;
data.resize(row, vector<double>(col, 0));
}
Matrix(const vector<vector<double>>& d) {
row = d.size();
col = d[0].size();
data = d;
}
Matrix operator+(const Matrix& m) const {
if (row != m.row || col != m.col) {
throw invalid_argument("The size of the two matrices must be the same.");
}
Matrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] + m.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix& m) const {
if (row != m.row || col != m.col) {
throw invalid_argument("The size of the two matrices must be the same.");
}
Matrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] - m.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& m) const {
if (col != m.row) {
throw invalid_argument("The number of columns of the first matrix must be equal to the number of rows of the second matrix.");
}
Matrix result(row, m.col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < m.col; j++) {
for (int k = 0; k < col; k++) {
result.data[i][j] += data[i][k] * m.data[k][j];
}
}
}
return result;
}
Matrix operator/(const Matrix& m) const {
if (m.row != m.col) {
throw invalid_argument("The second matrix must be a square matrix.");
}
Matrix result(row, col);
Matrix invM = m.inverse();
result = (*this) * invM;
return result;
}
Matrix inverse() const {
if (row != col) {
throw invalid_argument("The matrix must be a square matrix.");
}
int n = row;
Matrix result(n, n);
vector<vector<double>> a = data;
vector<vector<double>> b(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
b[i][i] = 1;
}
for (int k = 0; k < n; k++) {
double maxElem = abs(a[k][k]);
int maxRow = k;
for (int i = k + 1; i < n; i++) {
if (abs(a[i][k]) > maxElem) {
maxElem = abs(a[i][k]);
maxRow = i;
}
}
if (maxRow != k) {
swap(a[k], a[maxRow]);
swap(b[k], b[maxRow]);
}
double elem = a[k][k];
for (int j = k; j < n; j++) {
a[k][j] /= elem;
}
for (int j = 0; j < n; j++) {
b[k][j] /= elem;
}
for (int i = k + 1; i < n; i++) {
double f = a[i][k];
for (int j = k; j < n; j++) {
a[i][j] -= f * a[k][j];
}
for (int j = 0; j < n; j++) {
b[i][j] -= f * b[k][j];
}
}
}
for (int k = n - 1; k > 0; k--) {
for (int i = k - 1; i >= 0; i--) {
double f = a[i][k];
for (int j = 0; j < n; j++) {
b[i][j] -= f * b[k][j];
}
}
}
result = b;
return result;
}
friend ostream& operator<<(ostream& out, const Matrix& m) {
for (int i = 0; i < m.row; i++) {
for (int j = 0; j < m.col; j++) {
out << m.data[i][j] << " ";
}
out << endl;
}
return out;
}
};
int main() {
vector<vector<double>> a = {{1, 2, 3}, {4, 5, 6}};
vector<vector<double>> b = {{7, 8}, {9, 10}, {11, 12}};
Matrix A(a);
Matrix B(b);
Matrix C = A + B;
cout << C << endl;
C = A - B;
cout << C << endl;
C = A * B;
cout << C << endl;
C = A / B;
cout << C << endl;
return 0;
}
```
在这个实现中,矩阵类包含了矩阵的行数、列数和数据,可以用向量向量的方式初始化矩阵,或者用整数指定矩阵的大小,然后初始化为零矩阵。加、减、乘、除操作都被重载为运算符,可以直接使用。其中,矩阵的除法使用了矩阵求逆的方式实现。矩阵的输出使用了友元函数的方式实现,可以直接使用 `cout` 进行输出。
阅读全文