用C++实现矩阵基本运算
时间: 2023-07-11 09:35:00 浏览: 244
在C++中,可以使用二维数组来表示矩阵,并实现矩阵的基本运算。下面是一个简单的示例代码,演示了如何实现矩阵的加、减、乘、转置和求逆等基本运算:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 矩阵加法
vector<vector<double>> matrix_addition(vector<vector<double>> A, vector<vector<double>> B)
{
int rows = A.size();
int cols = A[0].size();
vector<vector<double>> C(rows, vector<double>(cols));
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}
// 矩阵减法
vector<vector<double>> matrix_subtraction(vector<vector<double>> A, vector<vector<double>> B)
{
int rows = A.size();
int cols = A[0].size();
vector<vector<double>> C(rows, vector<double>(cols));
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
C[i][j] = A[i][j] - B[i][j];
}
}
return C;
}
// 矩阵乘法
vector<vector<double>> matrix_multiplication(vector<vector<double>> A, vector<vector<double>> B)
{
int rows_A = A.size();
int cols_A = A[0].size();
int rows_B = B.size();
int cols_B = B[0].size();
vector<vector<double>> C(rows_A, vector<double>(cols_B));
if(cols_A != rows_B)
{
cout<<"Error: Invalid matrix dimensions"<<endl;
return C;
}
for(int i=0; i<rows_A; i++)
{
for(int j=0; j<cols_B; j++)
{
double sum = 0.0;
for(int k=0; k<cols_A; k++)
{
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
return C;
}
// 矩阵转置
vector<vector<double>> matrix_transpose(vector<vector<double>> A)
{
int rows = A.size();
int cols = A[0].size();
vector<vector<double>> C(cols, vector<double>(rows));
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
C[j][i] = A[i][j];
}
}
return C;
}
// 矩阵求逆
vector<vector<double>> matrix_inverse(vector<vector<double>> A)
{
int n = A.size();
vector<vector<double>> B(n, vector<double>(n));
vector<vector<double>> C(n, vector<double>(n));
// 初始化单位矩阵
for(int i=0; i<n; i++)
{
B[i][i] = 1.0;
}
// 高斯-约旦消元法求逆矩阵
for(int i=0; i<n; i++)
{
double pivot = A[i][i];
if(pivot == 0.0)
{
cout<<"Error: The matrix is singular"<<endl;
return C;
}
for(int j=0; j<n; j++)
{
A[i][j] /= pivot;
B[i][j] /= pivot;
}
for(int j=0; j<n; j++)
{
if(j != i)
{
double ratio = A[j][i];
for(int k=0; k<n; k++)
{
A[j][k] -= ratio * A[i][k];
B[j][k] -= ratio * B[i][k];
}
}
}
}
return B;
}
int main()
{
// 定义两个矩阵
vector<vector<double>> A = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
vector<vector<double>> B = {{9, 8, 7},
{6, 5, 4},
{3, 2, 1}};
// 矩阵加法
vector<vector<double>> C = matrix_addition(A, B);
cout<<"A + B = "<<endl;
for(int i=0; i<C.size(); i++)
{
for(int j=0; j<C[0].size(); j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
// 矩阵减法
C = matrix_subtraction(A, B);
cout<<"A - B = "<<endl;
for(int i=0; i<C.size(); i++)
{
for(int j=0; j<C[0].size(); j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
// 矩阵乘法
C = matrix_multiplication(A, B);
cout<<"A * B = "<<endl;
for(int i=0; i<C.size(); i++)
{
for(int j=0; j<C[0].size(); j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
// 矩阵转置
C = matrix_transpose(A);
cout<<"A' = "<<endl;
for(int i=0; i<C.size(); i++)
{
for(int j=0; j<C[0].size(); j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
// 矩阵求逆
C = matrix_inverse(A);
cout<<"inv(A) = "<<endl;
for(int i=0; i<C.size(); i++)
{
for(int j=0; j<C[0].size(); j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
```
在上面的代码中,我们定义了一个名为`matrix_addition`的函数,用于实现矩阵加法;定义了一个名为`matrix_subtraction`的函数,用于实现矩阵减法;定义了一个名为`matrix_multiplication`的函数,用于实现矩阵乘法;定义了一个名为`matrix_transpose`的函数,用于实现矩阵转置;定义了一个名为`matrix_inverse`的函数,用于实现矩阵求逆。在`main`函数中,我们定义了两个矩阵A和B,并演示了如何调用上述函数实现矩阵的基本运算。
阅读全文