Repository navigation
Expand file tree
/
Copy pathMatrixCalc.cpp
More file actions
200 lines (188 loc) · 6.06 KB
/
Copy pathMatrixCalc.cpp
File metadata and controls
200 lines (188 loc) · 6.06 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
//functions prototypes
void fillMatrixA(long double matA[][10], int r1);
void fillMatrixB(long double matB[][10], int r2);
void addMats(long double matA[][10], int r1, long double matB[][10], int r2);
void subMats(long double matA[][10], int r1, long double matB[][10], int r2);
void multMats(long double matA[][10], int r1, long double matB[][10], int r2);
long long matDet(long double matA[][10],int r);
void matDiv(long double matA[][10], int r1, long double matB[][10], int r2);
//Global variables
int r1=0,r2=0,c1=0,c2=0;
//main function
int main(){
int y;
cout << "Please enter dimensions of Matrix A:\n";
cin >> r1 >> c1;
cout << "Please enter dimensions of Matrix B:\n";
cin >> r2 >> c2;
long double matA[10][10], matB[10][10];
cout << "Please enter values of Matrix A:\n";
fillMatrixA(matA, r1);
cout << "Please enter values of Matrix B:\n";
fillMatrixB(matB, r2);
do{
cout << "Please choose operation type(1: A+B, 2: A-B, 3: AxB, 4: A*inverse(B), 5: |A|, 6: |B|, 7: quit):"<< "\n";
cin >> y;
switch(y){
case(1):
addMats(matA, r1, matB, r2);
break;
case(2):
subMats(matA, r1, matB, r2);
break;
case(3):
multMats(matA, r1, matB, r2);
break;
case(4):
matDiv(matA, r1, matB, r2);
break;
case(5):
if(r1!=c1){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else{
cout << matDet(matA,r1)<< "\n";
}
break;
case(6):
if(r2!=c2){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else{
cout << matDet(matB,r2)<< "\n";
}
break;
case(7):
cout << "Thank you!"<< "\n";
break;
}}while(y!=7);
}
//End of main
//functions::
//function for filling the matrices
void fillMatrixA(long double matA[][10], int r1){
for (int i=0; r1>i; i++){
for(int j = 0; c1>j; j++){
cin >> matA[i][j];
}
}
}
void fillMatrixB(long double matB[][10], int r2){
for (int i=0; r2>i; i++){
for(int j = 0; c2>j; j++){
cin >> matB[i][j];
}
}
}
//end of filling functions
//addition function
void addMats(long double matA[][10], int r1, long double matB[][10], int r2){
if(r1!=r2 || c1!=c2){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else{
for(int i = 0 ; r1>i; i++){
for (int j =0; c1>j; j++){
cout << matA[i][j] + matB[i][j] << " ";
}cout << "\n";
}
}
}
//end of addition functions
//subtraction function
void subMats(long double matA[][10], int r1, long double matB[][10], int r2){
if(r1!=r2 || c1!=c2){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else{
for(int i = 0 ; r1>i; i++){
for (int j =0; c1>j; j++){
cout << matA[i][j] - matB[i][j] << " ";
}cout << "\n";
}
}
}
//end of subtraction functions
//Multiplication function
//for multiplication, number of columns of matrix A must be equal number of rows of matrix B
void multMats(long double matA[][10], int r1, long double matB[][10], int r2){
long double sum=0;
if(c1!=r2){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else{
for(int j=0; r1>j; j++){
for(int i = 0; c2>i; i++){
for(int k = 0; c1>k; k++){
sum = (matA[j][k] * matB[k][i]) + sum;
}sum = roundl(sum);
if(sum == -0) {sum = 0;}
cout << sum << " ";
sum = 0;
}cout << "\n";}
}
}
//end of Multiplication function
//Determinant function
//the matrix must be square
long long matDet(long double mat[][10],int r){
long double tempMat[10][10];
long long result=0;
if(r == 1){
result = mat[0][0];
return result;
}else if(r == 2){
result = mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0];
return result;
}else{
for(int i = 0; i<r; i++){
int sign,temR=0,temC=0;
if(i%2==0)sign=1;
else sign = -1;
for(int j=1; r>j; j++){
for(int k=0; r>k; k++){
if(k==i)continue;
tempMat[temR][temC] = mat[j][k];
temC++;
if(temC == r-1){
temR++;
temC=0;
}
}
}result = result + mat[0][i]*sign*matDet(tempMat, r-1);
}return result;
}
}
//end of determinant function
//matrices division function
void matDiv(long double matA[][10], int r1, long double matB[][10], int r2){
long double tempMat[10][10];
long double tempMat2[10][10];
long double detResult = matDet(matB,r2);
if(detResult == 0){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else if(c2!=r2){
cout << "The operation you chose is invalid for the given matrices." << "\n";
}else if(r2==1){
tempMat2[0][0] = pow(matB[0][0],-1);
multMats(matA, r1, tempMat2, r2);
}else if(r2 >= 2){
for(int y = 0; y<r2; y++){
for(int i = 0; i<r2; i++){
int temR=0,temC=0;
for(int j=0; r2>j; j++){
for(int k=0; r2>k; k++){
if(j==y)continue;
if(k==i)continue;
tempMat[temR][temC] = matB[j][k];
temC++;
if(temC == r2-1){
temR++;
temC=0;}
}
}tempMat2[i][y] = pow(detResult,-1) * matDet(tempMat,r2-1) * pow(-1,y+i);
}
}multMats(matA, r1, tempMat2, r2);
}
}
//end matrices division function
//end of Functions s