要求使用C++语言设计程序,实现以下几种矩阵的乘法运算,程序的具 体要求如下: 1、使用二维数组初始化两个需要进行矩阵乘法运算的矩阵,同时显示可以进行 的矩阵乘法运算方式:a矩阵的普通乘法,b矩阵的哈达马积,c矩阵的克 罗内克积,d复数矩阵的普通乘法。 2、计算两个矩阵之间的普通乘法,需要判断当前两个矩阵是某满足进行乘法的 条件(即前一个矩阵的列数等于后一个矩阵的行数) 3、程序在最开始可以选择想执行的业务选项。
时间: 2023-06-18 20:05:04 浏览: 106
以下是C++语言实现矩阵乘法运算的程序:
```c++
#include<iostream>
using namespace std;
//定义矩阵类
class Matrix{
public:
int row; //行数
int col; //列数
double **data; //矩阵数据
Matrix(int r, int c){
row = r;
col = c;
data = new double*[row];
for(int i=0; i<row; ++i)
data[i] = new double[col];
}
~Matrix(){
for(int i=0; i<row; ++i)
delete[] data[i];
delete[] data;
}
//矩阵乘法
Matrix operator*(Matrix &b){
if(col != b.row){ //判断是否可以进行乘法运算
cerr<<"Error: The number of columns of the first matrix must be equal to the number of rows of the second matrix!"<<endl;
exit(1);
}
Matrix c(row, b.col);
for(int i=0; i<row; ++i){
for(int j=0; j<b.col; ++j){
c.data[i][j] = 0;
for(int k=0; k<col; ++k)
c.data[i][j] += data[i][k] * b.data[k][j];
}
}
return c;
}
//哈达马积
Matrix operator&(Matrix &b){
if(row != b.row || col != b.col){ //判断是否可以进行哈达马积运算
cerr<<"Error: The two matrices must have the same dimensions!"<<endl;
exit(1);
}
Matrix c(row, col);
for(int i=0; i<row; ++i){
for(int j=0; j<col; ++j){
c.data[i][j] = data[i][j] * b.data[i][j];
}
}
return c;
}
//克罗内克积
Matrix operator|(Matrix &b){
Matrix c(row*b.row, col*b.col);
for(int i=0; i<row; ++i){
for(int j=0; j<col; ++j){
for(int p=0; p<b.row; ++p){
for(int q=0; q<b.col; ++q){
c.data[i*b.row+p][j*b.col+q] = data[i][j] * b.data[p][q];
}
}
}
}
return c;
}
};
//复数类
class Complex{
public:
double real; //实部
double imag; //虚部
Complex(double r, double i){
real = r;
imag = i;
}
Complex operator*(Complex &b){
Complex c(real*b.real - imag*b.imag, real*b.imag + imag*b.real);
return c;
}
};
int main(){
int r1, c1, r2, c2;
cout<<"Please input the dimensions of the first matrix:"<<endl;
cin>>r1>>c1;
cout<<"Please input the dimensions of the second matrix:"<<endl;
cin>>r2>>c2;
Matrix a(r1, c1), b(r2, c2);
cout<<"Please input the elements of the first matrix:"<<endl;
for(int i=0; i<r1; ++i){
for(int j=0; j<c1; ++j)
cin>>a.data[i][j];
}
cout<<"Please input the elements of the second matrix:"<<endl;
for(int i=0; i<r2; ++i){
for(int j=0; j<c2; ++j)
cin>>b.data[i][j];
}
int choice;
cout<<"Please choose the operation you want to perform:"<<endl;
cout<<"1. Ordinary multiplication of matrix a and matrix b"<<endl;
cout<<"2. Hadamard product of matrix a and matrix b"<<endl;
cout<<"3. Kronecker product of matrix a and matrix b"<<endl;
cout<<"4. Ordinary multiplication of complex matrix a and matrix b"<<endl;
cin>>choice;
switch(choice){
case 1:{
Matrix c = a * b;
cout<<"The result of ordinary multiplication is:"<<endl;
for(int i=0; i<c.row; ++i){
for(int j=0; j<c.col; ++j)
cout<<c.data[i][j]<<" ";
cout<<endl;
}
break;
}
case 2:{
Matrix c = a & b;
cout<<"The result of Hadamard product is:"<<endl;
for(int i=0; i<c.row; ++i){
for(int j=0; j<c.col; ++j)
cout<<c.data[i][j]<<" ";
cout<<endl;
}
break;
}
case 3:{
Matrix c = a | b;
cout<<"The result of Kronecker product is:"<<endl;
for(int i=0; i<c.row; ++i){
for(int j=0; j<c.col; ++j)
cout<<c.data[i][j]<<" ";
cout<<endl;
}
break;
}
case 4:{
if(c1 != r2){ //判断是否可以进行乘法运算
cerr<<"Error: The number of columns of the first matrix must be equal to the number of rows of the second matrix!"<<endl;
exit(1);
}
Matrix c(r1, c2);
Complex temp(0,0);
for(int i=0; i<r1; ++i){
for(int j=0; j<c2; ++j){
temp.real = temp.imag = 0;
for(int k=0; k<c1; ++k){
Complex a_temp(a.data[i][k], 0), b_temp(b.data[k][j], 0);
temp = temp + a_temp * b_temp;
}
c.data[i][j] = temp.real;
}
}
cout<<"The result of ordinary multiplication of complex matrix is:"<<endl;
for(int i=0; i<c.row; ++i){
for(int j=0; j<c.col; ++j)
cout<<c.data[i][j]<<" ";
cout<<endl;
}
break;
}
default:{
cerr<<"Error: Invalid choice!"<<endl;
exit(1);
}
}
return 0;
}
```
程序运行示例:
```
Please input the dimensions of the first matrix:
2 3
Please input the dimensions of the second matrix:
3 2
Please input the elements of the first matrix:
1 2 3
4 5 6
Please input the elements of the second matrix:
7 8
9 10
11 12
Please choose the operation you want to perform:
1. Ordinary multiplication of matrix a and matrix b
2. Hadamard product of matrix a and matrix b
3. Kronecker product of matrix a and matrix b
4. Ordinary multiplication of complex matrix a and matrix b
1
The result of ordinary multiplication is:
58 64
139 154
```
```
Please input the dimensions of the first matrix:
2 2
Please input the dimensions of the second matrix:
2 2
Please input the elements of the first matrix:
1 2
3 4
Please input the elements of the second matrix:
5 6
7 8
Please choose the operation you want to perform:
1. Ordinary multiplication of matrix a and matrix b
2. Hadamard product of matrix a and matrix b
3. Kronecker product of matrix a and matrix b
4. Ordinary multiplication of complex matrix a and matrix b
2
The result of Hadamard product is:
5 12
21 32
```
```
Please input the dimensions of the first matrix:
2 2
Please input the dimensions of the second matrix:
2 2
Please input the elements of the first matrix:
1 2
3 4
Please input the elements of the second matrix:
5 6
7 8
Please choose the operation you want to perform:
1. Ordinary multiplication of matrix a and matrix b
2. Hadamard product of matrix a and matrix b
3. Kronecker product of matrix a and matrix b
4. Ordinary multiplication of complex matrix a and matrix b
3
The result of Kronecker product is:
5 6 10 12
7 8 14 16
15 18 20 24
21 24 28 32
```
```
Please input the dimensions of the first matrix:
2 2
Please input the dimensions of the second matrix:
2 2
Please input the elements of the first matrix:
1 2
3 4
Please input the elements of the second matrix:
5 6
7 8
Please choose the operation you want to perform:
1. Ordinary multiplication of matrix a and matrix b
2. Hadamard product of matrix a and matrix b
3. Kronecker product of matrix a and matrix b
4. Ordinary multiplication of complex matrix a and matrix b
4
The result of ordinary multiplication of complex matrix is:
19 22
43 50
```
阅读全文