#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class Matrix {
private:
int rows;
int cols;
vector<vector<double>> data;
public:
Matrix(int rowCount, int colCount) : rows(rowCount), cols(colCount) {
data.resize(rows, vector<double>(cols, 0.0));
}
void input() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> data[i][j];
}
}
}
void display() const {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw runtime_error("Matrix dimensions do not match");
}
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 runtime_error("Matrix dimensions do not match");
}
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 runtime_error("Matrix dimensions do not match");
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
Matrix operator*(double scalar) const {
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] * scalar;
}
}
return result;
}
Matrix operator/(double scalar) const {
if (scalar == 0.0) {
throw runtime_error("Division by zero");
}
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] / scalar;
}
}
return result;
}
// 带变元的矩阵计算
Matrix substitute(double variable) const {
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] * variable;
}
}
return result;
}
// 计算矩阵的特征值和特征向量(仅支持2x2矩阵)
void eigenvaluesAndEigenvectors() const {
if (rows != 2 || cols != 2) {
throw runtime_error("Eigenvalue and eigenvector calculations are only supported for 2x2 matrices");
}
double a = data[0][0];
double b = data[0][1];
double c = data[1][0];
double d = data[1][1];
double discriminant = sqrt(pow((a + d), 2) - 4 * (a * d - b * c));
double lambda1 = (a + d + discriminant) / 2;
double lambda2 = (a + d - discriminant) / 2;
cout << "Eigenvalues: " << lambda1 << ", " << lambda2 << endl;
if (b != 0 || c != 0) {
double x1 = 1;
double y1 = (lambda1 - a) / b;
double x2 = 1;
double y2 = (lambda2 - a) / b;
cout << "Eigenvectors: (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << ")" << endl;
} else {
cout << "Eigenvectors cannot be calculated" << endl;
}
}
};
int main() {
int n;
cout << "Enter the size of the matrix: ";
cin >> n;
Matrix m1(n, n), m2(n, n);
cout << "Enter elements of first matrix:" << endl;
m1.input();
cout << "Enter elements of second matrix:" << endl;
m2.input();
cout << "First matrix:" << endl;
m1.display();
cout << "Second matrix:" << endl;
m2.display();
try {
// 四则运算
Matrix resultAddition = m1 + m2;
cout << "Addition:" << endl;
resultAddition.display();
Matrix resultSubtraction = m1 - m2;
cout << "Subtraction:" << endl;
resultSubtraction.display();
Matrix resultMultiplication = m1 * m2;
cout << "Multiplication:" << endl;
resultMultiplication.display();
Matrix resultScalarMultiplication = m1 * 2.0;
cout << "Scalar Multiplication:" << endl;
resultScalarMultiplication.display();
Matrix resultScalarDivision = m1 / 2.0;
cout << "Scalar Division:" << endl;
resultScalarDivision.display();
// 带变元的计算
double variable;
cout << "Enter the value of the variable: ";
cin >> variable;
Matrix resultSubstitution = m1.substitute(variable);
cout << "Substitution:" << endl;
resultSubstitution.display();
// 特征值和特征向量计算
cout << "Eigenvalues and Eigenvectors for the first matrix:" << endl;
m1.eigenvaluesAndEigenvectors();
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}