Repository navigation
Expand file tree
/
Copy pathclassMatrix.cpp
More file actions
462 lines (382 loc) · 8.98 KB
/
Copy pathclassMatrix.cpp
File metadata and controls
462 lines (382 loc) · 8.98 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include<iostream>
#include<string>
using namespace std;
class Matrix{
private:
int row;
int col;
int ** data;
public:
Matrix();
Matrix(const Matrix &);
Matrix(int, int);
void setRow(int);
void setCol(int);
int getRow() const;
int getCol() const;
int& at(const int r, const int c);
void printMatrix() const;
int isIdentity() const;
bool isRectangular()const ;
bool isDiagonal()const ;
bool isNullMatrix() const ;
bool isLowerTriangular() const;
bool isUpperTriangular() const;
bool isTriangular()const ;
Matrix getMatrixCopy()const;
bool isEqual(const Matrix m2) const;
void reSize(const int newrow, const int newcol);
bool isSymmetric() const;
bool isSkewSymmetric()const ;
~Matrix();
Matrix Transpose() const;
Matrix add(const Matrix ) const;
Matrix minus(const Matrix ) const;
Matrix multiply(const Matrix ) const;
};
Matrix :: Matrix (){
row=0;
col=0;
data = new int *[row];
for(int i=0; i<row; i++){
data[i]=new int [col];
}
}
Matrix :: Matrix (const Matrix & ref){
row =ref.row;
col=ref.col;
data =new int *[row];
for(int i=0; i<row; i++){
data[i]=new int [col];
for(int j=0; j<col; j++){
data [i][j]=ref.data[i][j];
}
}
}
Matrix :: Matrix(int i, int j){
row=i;
col=j;
data = new int *[row];
for(int i=0; i<row; i++){
data[i]=new int [col];
}
}
void Matrix:: setRow(int i){
row=i;
}
void Matrix:: setCol(int j){
col=j;
}
int Matrix:: getRow()const{
return row;
}
int Matrix:: getCol()const{
return col;
}
int & Matrix:: at(const int r, const int c){
return data[r][c];
}
void Matrix:: printMatrix()const{
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout<<data[i][j]<<" ";
}
cout<<endl;
}
}
int Matrix:: isIdentity()const{
if(row!=col){
return 0;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(i==j && data[i][j]!=1){
return 0;
}
if(i!=j && data[i][j] != 0){
return 0;
}
}
}
return 1;
}
bool Matrix:: isRectangular()const{
if(row==col){
return false;
}
return true;
}
bool Matrix:: isDiagonal()const{
if(row!=col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(i!=j && data[i][j] !=0){
return false;
}
}
}
return true;
}
bool Matrix:: isNullMatrix() const{
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(data[i][j] !=0){
return false;
}
}
}
return true;
}
bool Matrix:: isLowerTriangular()const {
if(row!=col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(i<j && data[i][j] != 0){
return false;
}
}
}
return true;
}
bool Matrix:: isUpperTriangular()const {
if(row!=col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(i>j && data[i][j] != 0){
return false;
}
}
}
return true;
}
bool Matrix:: isTriangular()const{
return isLowerTriangular() || isUpperTriangular();
}
Matrix Matrix::getMatrixCopy()const{
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];
}
}
return c;
}
bool Matrix:: isEqual(const Matrix m2)const{
if(row != m2.row || col!= m2.col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(data[i][j] != m2.data[i][j]){
return false;
}
}
}
return true;
}
void Matrix:: reSize(const int newrow, const int newcol){
int ** newdata= new int *[newrow];
for(int i=0 ; i<newrow; i++){
newdata[i] = new int[newcol];
for(int j=0;j < newcol; j++){
if(i<row && j <col){
newdata[i][j] =data[i][j];
}
else{
newdata[i][j]=0;
}
}
delete[] data;
row = newrow;
col= newcol;
data =newdata;
}
return ;
}
bool Matrix:: isSymmetric()const{
if(row!=col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(data[i][j]!=data[j][i]){
return false;
}
}
}
return true;
}
bool Matrix:: isSkewSymmetric()const{
if(row!=col){
return false;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
if(data[i][j]!=-data[j][i]){
return false;
}
}
}
return true;
}
Matrix:: ~Matrix(){
for(int i=0; i<row; i++){
delete [] data[i];
}
delete [] data;
}
Matrix Matrix:: Transpose()const{
Matrix t (col, row);
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
t.data[j][i]=data[i][j];
}
}
return t;
}
Matrix Matrix:: add(const Matrix m2)const{
Matrix m(row,col);
if(row!=m2.row || col!= m2.col){
cout<<"not comparable for addition"<<endl;
m.row=m.col=0;
m.data=nullptr;
return m;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
m.data[i][j]=data[i][j] + m2.data[i][j];
}
cout<<endl;
}
return m;
}
Matrix Matrix:: minus(const Matrix m2)const{
Matrix m(row,col);
if(row!=m2.row || col!= m2.col){
cout<<"not comparable for subtraction"<<endl;
m.row=m.col=0;
m.data=nullptr;
return m;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<col; j++){
m.data[i][j]=data[i][j] - m2.data[i][j];
}
cout<<endl;
}
return m;
}
Matrix Matrix:: multiply(const Matrix m2)const{
Matrix m(row,m2.col);
if(col!=m2.row ){
cout<<"not comparable for multiplication"<<endl;
m.row=m.col=0;
m.data=nullptr;
return m;
}
for(int i=0 ;i< row; i++){
for(int j=0; j<m2.col; j++){
m.data[i][j] =0;
for(int k=0; k<col; k++){
m.data[i][j] += data[i][k] * m2.data[k][j];
}
}
}
return m;
}
int main(){
Matrix m1(3,3);
cout<<"enter elements of first matrix:" <<endl;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cin>> m1.at(i,j);
}
}
m1.printMatrix();
Matrix m2(3,3);
cout<<"enter elements of second matrix:" <<endl;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cin>>m2.at(i,j);
}
}
m2.printMatrix();
int i= m1.isIdentity();
cout<<"m1 is identity?"<<i<<endl;
if(!(m1.isDiagonal())){
cout<<" m1 is not diagonal"<<endl;
}
else{
cout<<"m1 is diagonal"<<endl;
}
if(!(m1.isEqual(m2))){
cout<<" m1 is not equal to m2"<<endl;
}
else{
cout<<"m1 is equal to m2"<<endl;
}
if(!(m1.isNullMatrix())){
cout<<" m1 is not null"<<endl;
}
else{
cout<<"m1 is null"<<endl;
}
if(!(m1.isRectangular())){
cout<<"m1 is not rectangular"<<endl;
}
else{
cout<<"m1 is rectangular"<<endl;
}
Matrix t=m1.Transpose();
cout<<" trasnpose of matrix m1"<<endl;
t.printMatrix();
Matrix a =m1.add(m2);
cout<<" addition of m1 and m2"<<endl;
a.printMatrix();
if(!(m1.isSymmetric())){
cout<<" m1 is not symmetric"<<endl;
}
else{
cout<<"m1 is symemetric"<<endl;
}
if(!(m2.isSkewSymmetric())){
cout<<" m1 is not skewsymmetric"<<endl;
}
else{
cout<< "m1 is skew symmetric"<<endl;
}
if(!(m1.isUpperTriangular())){
cout<<" m1 is not upper triangular"<<endl;
}
else{
cout<<"m1 is upper triangular"<<endl;
}
if(!(m2.isLowerTriangular())){
cout<<" m2 is not lower triangular"<<endl;
}
else{
cout<<"m2 is lower triangular"<<endl;
}
if(!(m2.isTriangular())){
cout<<" m2 is not triangular"<<endl;
}
else{
cout<<" m2 is triangular"<<endl;
}
Matrix m= m1.multiply(m2);
cout<<" multipliication m1 x m2"<<endl;
m.printMatrix();
Matrix e=m1.minus(m2);
cout<<" subtraction m1-m2"<<endl;
e.printMatrix();
Matrix copy=m1.getMatrixCopy();
cout <<" copy matrix "<<endl;
copy.printMatrix();
return 0;
}