#include<iostream>
#include<C:\Eigen\Eigen>
using namespace std;
using namespace Eigen;
int ReadyWords(){
cout<<"请输入您想对矩阵的操作 :"<<endl<<"A: 求矩阵的秩"<<endl<<"B: 求矩阵对应齐次方程组的基础解系"<<endl<<"C: 矩阵间的加减或相乘"<<endl;
cout<<"D: 矩阵的数乘"<<endl<<"E:求逆矩阵"<<endl<<"F:求矩阵的行列式"<<endl<<"G: 能否对角化"<<endl<<"H:将矩阵转置"<<endl;
return 0;
}
int FinishWords(){
cout<<"是否想要继续计算?"<<endl;
cout<<"如果想,请输入除1外的任一非0字符;若想停止计算,请输入0;若想在上一个结果的基础上计算,请输入1"<<endl;
}
MatrixXd PrintInMatrix(MatrixXd A){
cout<<"请输入您的矩阵大小(以m*n的形式)"<<endl;
int m,n;char multiple_sign;
cin>>m>>multiple_sign>>n;
MatrixXd X;
X.resize(m,n);
cout<<"请输入您的矩阵"<<endl;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>X(i,j);
}
}
return X;
}
MatrixXd PrintInSquareMatrix(MatrixXd A){
cout<<"请输入您的方阵大小(只需输入n)";
int n;cin>>n;
MatrixXd X;
X.resize(n,n);
cout<<"请输入您的方阵"<<endl;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>A(i,j);
}
}
return X;
}
int MatrixOperation1(MatrixXd A,MatrixXd B,char operation1){
if(A.cols()!=B.cols()||A.rows()!=B.rows()){
cout<<"矩阵无法相加减!";
return 0;
}
if(operation1==43)
cout<<A+B;
if(operation1==45)
cout<<A-B;
return 0;
}
int MatrixOperation2(MatrixXd A,MatrixXd B){
if(A.cols()!=B.rows()){
cout<<"矩阵无法相乘!";
return 0;
}
else{
cout<<A*B;
return 0;
}
}
int MatrixOperation3(MatrixXd A,int num){
cout<<A*num;
return 0;
}
int OperationFunction(char choice_operation){
if(choice_operation==65||choice_operation==97){ //A求矩阵的秩
MatrixXd A;
A=PrintInMatrix(A);
int rank=A.fullPivLu().rank();
cout<<rank<<endl;
return 0;
}
if(choice_operation==66||choice_operation==98){ //B求基础解系
}
if(choice_operation==67||choice_operation==99){ //C矩阵间的相加减和相乘
MatrixXd A,B;
A=PrintInMatrix(A);
char operation1;
cout<<"请输入+或-或*"<<endl;
cin>>operation1;
for(;operation1!=42&&operation1!=43&&operation1!=45;){
cout<<"请输入正确的字符";
cin>>operation1;
}
B=PrintInMatrix(B);
//cout<<A<<endl<<operation1<<endl<<B<<endl;
if(operation1==45||operation1==43)
MatrixOperation1(A,B,operation1);
else
MatrixOperation2(A,B);
return 0;
}
if(choice_operation==68||100){ //D矩阵的数乘
MatrixXd A;
A==PrintInMatrix(A);
cout<<endl<<1;
cout<<"请输入数乘数字:"<<endl;
double num;cin>>num;
MatrixOperation3(A,num);
}
if(choice_operation==69||choice_operation==101){ //E求逆矩阵
MatrixXd A;
A=PrintInSquareMatrix(A);
}
if(choice_operation==70||102){ //F求行列式
}
if(choice_operation==71||103){ //G能否对角化
}
if(choice_operation==72||104){ //H转置
MatrixXd A;
A=PrintInMatrix(A);
cout<<A.transpose();
}
}
int ChoiceOperation(){
char choice_operation;
cin.get(choice_operation);
OperationFunction(choice_operation);
FinishWords();
}
int NodeCalculation(){
char node;
cin>>node;
if(node==0)
return 0;
if(node==1){
}
else
ChoiceOperation();
}
int main(){
ReadyWords();//头文字的输出
ChoiceOperation();
NodeCalculation();
return 0;
}
实现部分功能的矩阵运算
该程序提供了一系列矩阵操作选项,包括求矩阵的秩、进行矩阵加减乘运算、求逆矩阵等。用户需输入矩阵数据,程序会利用Eigen库进行计算并显示结果。
摘要由CSDN通过智能技术生成