1、 //设计一个二维矩阵类 2、 //成员变量为double类型,用来存储矩阵中的元素 3、 //写出赋值运算符重载(包括+=,-=,=),拷贝构造函数和构造函数 4、 //实现矩阵的转置操作 5、 实现矩阵的逆求解 6、 实现矩阵的行列式运算 7、 矩阵的加减乘操作 8、 //利用()运算符重载可以改变某个矩阵元素的值 9、 实现AX=b求解操作 10、 求矩阵的秩 11、 求矩阵的特征值和特征向量
时间: 2024-01-07 14:05:21 浏览: 80
以下是一个简单的二维矩阵类的实现,包含您所要求的所有操作:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Matrix {
private:
double **data; // 二维数组用于存储矩阵数据
int rows, cols; // 矩阵的行数和列数
public:
// 构造函数
Matrix(int rows, int cols) {
this->rows = rows;
this->cols = cols;
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = 0;
}
}
}
// 拷贝构造函数
Matrix(const Matrix& m) {
rows = m.rows;
cols = m.cols;
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = m.data[i][j];
}
}
}
// 赋值运算符重载
Matrix& operator=(const Matrix& m) {
if (&m != this) {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
rows = m.rows;
cols = m.cols;
data = new double*[rows];
for (int i = 0; i < rows; i++) {
data[i] = new double[cols];
for (int j = 0; j < cols; j++) {
data[i][j] = m.data[i][j];
}
}
}
return *this;
}
// += 运算符重载
Matrix& operator+=(const Matrix& m) {
if (rows != m.rows || cols != m.cols) {
throw "矩阵大小不匹配";
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] += m.data[i][j];
}
}
return *this;
}
// -= 运算符重载
Matrix& operator-=(const Matrix& m) {
if (rows != m.rows || cols != m.cols) {
throw "矩阵大小不匹配";
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] -= m.data[i][j];
}
}
return *this;
}
// = 运算符重载
Matrix& operator=(double val) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = val;
}
}
return *this;
}
// 转置操作
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[j][i] = data[i][j];
}
}
return result;
}
// 逆求解
Matrix inverse() const {
if (rows != cols) {
throw "矩阵不是方阵";
}
Matrix result(rows, cols);
double det = determinant();
if (det == 0) {
throw "矩阵不可逆";
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = cofactor(j, i) / det;
}
}
return result;
}
// 行列式运算
double determinant() const {
if (rows != cols) {
throw "矩阵不是方阵";
}
if (rows == 1) {
return data[0][0];
}
double result = 0;
for (int i = 0; i < rows; i++) {
result += data[0][i] * cofactor(0, i);
}
return result;
}
// 矩阵加法
Matrix operator+(const Matrix& m) const {
if (rows != m.rows || cols != m.cols) {
throw "矩阵大小不匹配";
}
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] + m.data[i][j];
}
}
return result;
}
// 矩阵减法
Matrix operator-(const Matrix& m) const {
if (rows != m.rows || cols != m.cols) {
throw "矩阵大小不匹配";
}
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] - m.data[i][j];
}
}
return result;
}
// 矩阵乘法
Matrix operator*(const Matrix& m) const {
if (cols != m.rows) {
throw "矩阵大小不匹配";
}
Matrix result(rows, m.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < m.cols; j++) {
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * m.data[k][j];
}
}
}
return result;
}
// () 运算符重载
double& operator()(int i, int j) {
if (i < 0 || i >= rows || j < 0 || j >= cols) {
throw "下标越界";
}
return data[i][j];
}
// AX=b求解操作,返回解矩阵
Matrix solve(const Matrix& b) const {
if (rows != cols || rows != b.rows) {
throw "矩阵大小不匹配";
}
Matrix A(*this);
Matrix x(rows, 1);
double det = A.determinant();
if (det == 0) {
throw "矩阵不可逆";
}
// 高斯消元
for (int i = 0; i < rows - 1; i++) {
for (int j = i + 1; j < rows; j++) {
double factor = A.data[j][i] / A.data[i][i];
for (int k = i + 1; k < rows; k++) {
A.data[j][k] -= factor * A.data[i][k];
}
b.data[j][0] -= factor * b.data[i][0];
}
}
// 回代求解
for (int i = rows - 1; i >= 0; i--) {
double sum = 0;
for (int j = i + 1; j < rows; j++) {
sum += A.data[i][j] * x.data[j][0];
}
x.data[i][0] = (b.data[i][0] - sum) / A.data[i][i];
}
return x;
}
// 求矩阵的秩
int rank() const {
Matrix A(*this);
int r = 0;
for (int i = 0; i < cols; i++) {
int j;
for (j = r; j < rows; j++) {
if (fabs(A.data[j][i]) > 1e-10) {
break;
}
}
if (j == rows) {
continue;
}
if (j != r) {
for (int k = i; k < cols; k++) {
swap(A.data[r][k], A.data[j][k]);
}
}
double factor = A.data[r][i];
for (int k = i; k < cols; k++) {
A.data[r][k] /= factor;
}
for (int k = r + 1; k < rows; k++) {
double factor = A.data[k][i] / A.data[r][i];
for (int l = i; l < cols; l++) {
A.data[k][l] -= factor * A.data[r][l];
}
}
r++;
}
return r;
}
// 求矩阵的特征值和特征向量,返回特征值矩阵
Matrix eigenvectors() const {
if (rows != cols) {
throw "矩阵不是方阵";
}
Matrix A(*this);
Matrix Q(rows, cols);
for (int i = 0; i < rows; i++) {
Q(i, i) = 1;
}
for (int k = 0; k < 100; k++) { // 最多迭代100次
double max_value = 0;
int p = 0, q = 0;
for (int i = 0; i < rows - 1; i++) {
for (int j = i + 1; j < rows; j++) {
double value = fabs(A(i, j));
if (value > max_value) {
max_value = value;
p = i;
q = j;
}
}
}
if (max_value < 1e-10) {
break; // 精度达到要求,退出迭代
}
double theta = 0.5 * atan2(-2 * A(p, q), A(q, q) - A(p, p));
double c = cos(theta);
double s = sin(theta);
Matrix P(rows, cols);
for (int i = 0; i < rows; i++) {
P(i, i) = 1;
}
P(p, p) = c;
P(q, q) = c;
P(p, q) = -s;
P(q, p) = s;
A = P.transpose() * A * P;
Q = Q * P;
}
Matrix result(rows, 1);
for (int i = 0; i < rows; i++) {
result(i, 0) = A(i, i);
}
return result;
}
// 析构函数
~Matrix() {
for (int i = 0; i < rows; i++) {
delete[] data[i];
}
delete[] data;
}
private:
// 求余子式
Matrix submatrix(int r, int c) const {
Matrix result(rows - 1, cols - 1);
int row = 0;
for (int i = 0; i < rows; i++) {
if (i == r) {
continue;
}
int col = 0;
for (int j = 0; j < cols; j++) {
if (j == c) {
continue;
}
result.data[row][col] = data[i][j];
col++;
}
row++;
}
return result;
}
// 求代数余子式
double cofactor(int r, int c) const {
Matrix sub = submatrix(r, c);
return pow(-1, r + c) * sub.determinant();
}
};
```
这个矩阵类可以用于处理一些简单的线性代数问题,但是对于大规模的矩阵运算可能不够高效。如果需要处理大规模矩阵,可以考虑使用一些优化库,如Eigen等。
阅读全文