-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathComplexMatrix.txt
More file actions
101 lines (98 loc) · 1.96 KB
/
ComplexMatrix.txt
File metadata and controls
101 lines (98 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
A complex-valued matrix is represented by a pair of matrices , where and contain real values. Now, do the following:
• Define a proper data structure to represent the complex-valued matrix.
• Write a program that computes the product of two complex-valued matrices and
• Determine the number of additions and multiplications if the matrices are all nXn.
*/
#include<iostream>
using namespace std;
class complex{
public:
int r,i;
complex operator *(complex &a){
complex d;
d.r=(r*a.r)-(i*a.i);
d.i=(i*a.r)+(r*a.i);
return d;
}
complex operator+(complex &a){
complex c;
c.r=r+a.r;
c.i=i+a.i;
return c;
}
};
int main(){
int n,i,j,k,add=0,mul=0;
char s;
cout<<"Enter n:";
cin>>n;
complex a[n][n],b[n][n],c[n][n],t;
cout<<"Enter first matrix\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>a[i][j].r;
cin>>a[i][j].i;
}
}
cout<<"Enter second matrix\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>b[i][j].r;
cin>>b[i][j].i;
}
}
cout<<"Matrix 1:\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<a[i][j].r<<" +i"<<a[i][j].i<<"\t";
cout<<endl;
}
cout<<"Matrix 2:\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<b[i][j].r<<" +i"<<b[i][j].i<<"\t";
cout<<endl;
}
cout<<"Product matrix\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++){
c[i][j].r=c[i][j].i=0;
for(k=0;k<n;k++){
t=(a[i][k]*b[k][j]);
mul=mul+4;
c[i][j]=c[i][j]+t;
add=add+4;
}
cout<<c[i][j].r<<"+i"<<c[i][j].i<<"\t";
}
cout<<endl;
}
cout<<"No of multiplications:"<<mul<<endl;
cout<<"No of additions:"<<add<<endl;
return 0;
}
Sample output:
Enter n:3
Enter first matrix
1 1 1 -1 1 2
3 4 3 -4 1 3
2 1 2 -1 -1 1
Enter second matrix
2 3 1 4 5 -2
1 3 2 1 4 1
-1 3 3 2 4 2
Matrix 1:
1 +i1 1 +i-1 1 +i2
3 +i4 3 +i-4 1 +i3
2 +i1 2 +i-1 -1 +i1
Matrix 2:
2 +i3 1 +i4 5 +i-2
1 +i3 2 +i1 4 +i1
-1 +i3 3 +i2 4 +i2
Product matrix
-4+i8 -1+i12 12+i10
-1+i22 -6+i22 37+i15
4+i9 -2+i10 15+i1
No of multiplications:108
No of additions:108