#include <iostream>
#include <vector>
// 复数类
class Complex {
public:
double real, imag;
Complex():real(0.0),imag(0.0){}
Complex(double r, double i) : real(r), imag(i) {}
Complex operator+(const Complex &c) const {
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex &c) const {
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex &c) const {
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
};
// 矩阵类
class Matrix {
public:
int rows, cols;
std::vector<std::vector<Complex>> data;
Matrix(int r, int c) : rows(r), cols(c), data(r, std::vector<Complex>(c)) {}
// 复数矩阵相乘
Matrix mul(const Matrix &m) const {
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] = result.data[i][j] + data[i][k] * m.data[k][j];
}
}
}
return result;
}
};
int main() {
// 示例:创建两个2x2的复数矩阵并计算乘积
Matrix A(2, 2);
Matrix B(2, 2);
A.data = { { Complex(1.0, 2.0), Complex(3.0, 4.0) },{ Complex(5.0, 6.0), Complex(7.0, 8.0) } };
B.data = { { Complex(9.0, 10.0), Complex(11.0, 12.0) },{ Complex(13.0, 14.0), Complex(15.0, 16.0) } };
Matrix C = A.mul(B);
// 输出结果
for (int i = 0; i < C.rows; ++i) {
for (int j = 0; j < C.cols; ++j) {
std::cout << C.data[i][j].real << " + " << C.data[i][j].imag << "i" << " ";
}
std::cout << std::endl;
}
system("pause");
return 0;
}
10-12
1474
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-06
7191
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-16