-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
589 lines (463 loc) · 22.6 KB
/
Matrix.cpp
File metadata and controls
589 lines (463 loc) · 22.6 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*===============================================================================
FILE: Matrix.cpp
DESCRIPTION: This program is the implementation file for the Matrix and Matrix_ops
Classes declared in Matrix.h
COMPILER: Linux GNU g++ compiler c++ -std=c++11
using custom makefile and Clion IDE editor
NOTES: Includes the member functions for both the Matrix and Matrix_ops objects
includes Matrix.h for the Classes
includes Expo function but not a class member
MODIFICATION HISTORY:
Author Date Version
--------------- ---------- --------------
Shawn Ray 2017-04-13 Version 1.0 started project
Shawn Ray 2017-04-14 Version 1.1
Shawn Ray 2017-04-15 Version 1.2 overloaded operators
Shawn Ray 2017-04-16 Version 1.3 added class definition
for command line stuffs
Shawn Ray 2017-04-17 Version 1.4 Template class
Shawn Ray 2017-04-18 Version 1.5
Shawn Ray 2017-04-19 Version 1.6 fixed bugged
Shawn Ray 2017-04-20 Version 1.7 templates
Shawn Ray 2017-04-21 Version 1.8 inheritance
Shawn Ray 2017-04-22 Version 1.9
Shawn Ray 2017-04-23 Version 2.0 Multiplication and Transpose
Shawn Ray 2017-04-24 Version 2.1 worked on the determinate
Shawn Ray 2017-04-25 Version 2.2
Shawn Ray 2017-04-26 Version 2.3
Shawn Ray 2017-04-27 Version 2.4 cleaned up code
Shawn Ray 2017-04-28 Version 2.5 added Clear and resize functions
clear clears the memory and resize resizes
a class object
Shawn Ray 2017-04-29 Version 2.6 destructor bug,
Shawn Ray 2017-04-30 Version 2.7 Virtual destruction,
Shawn Ray 2017-05-01 Version 2.8 inverse of a matrix
Shawn Ray 2017-05-02 Version 2.9 inverse of a matrix
Shawn Ray 2017-05-03 Version 3.0 continued project
Shawn Ray 2017-05-04 Version 3.1 added member function for
for output file
Shawn Ray 2017-05-05 Version 3.2 Worked on completing the inverse
================================================================================*/
#include "Matrix.h"
/*=============================================================================
FUNCTION: Matrix<T>::Matrix(
DESCRIPTION: This function is the default constructor for the Matrix class
it sets all values to null whenever an object is created.
RETURNS: Being a constructor, this function returns a Matrix object
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix<T>::Matrix(){
rows = 0;
cols = 0;
array = NULL;
}
/*=============================================================================
FUNCTION: Matrix<T>::Matrix(int n_rows, int n_cols)
DESCRIPTION: This is the overloaded constructor for the Matrix class,
This takes a size of a matrix (rows, cols) and calls the
resize function
RETURNS: Being a constructor, this function returns a Matrix object
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix<T>::Matrix(int n_rows, int n_cols){
resize(n_rows, n_cols, false);
}
/*=============================================================================
FUNCTION: Matrix<T>::Matrix(const Matrix<T> &m1)
DESCRIPTION: This is the copy constructor, it calls the assignment operator
to copy a Matrix to another.
RETURNS: Being a constructor, this function returns a Matrix object
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix<T>::Matrix(const Matrix<T> &m1){
(*this) = m1;
}
/*=============================================================================
FUNCTION: std::istream& operator >> (std::istream &input, Matrix<T> &m1)
DESCRIPTION: This function overloads the extraction operators to take input
into a Matrix Object
RETURNS: The function returns an istream type named input
NOTES: templated class functions
===============================================================================*/
template<typename T>
std::istream& operator >> (std::istream &input, Matrix<T> &m1){
for(int i = 0; i < m1.rows; ++i){
for(int j = 0; j < m1.cols; ++j){
input >> m1.array[i][j];
}
}
return input;
}
/*=============================================================================
FUNCTION: std::ostream& operator << (std::ostream &output, const Matrix<T> &m1)
DESCRIPTION: This function overloads the insertion operators to display a Matrix
object not only to a screen but also a file
RETURNS: This function returns the ostream type named output
NOTES: templated class functions
===============================================================================*/
template<typename T>
std::ostream& operator << (std::ostream &output, const Matrix<T> &m1){
output << std::endl;
for(int i = 0; i < m1.rows; ++i){
for(int j = 0; j < m1.cols; j++){
output << std::setw(4) << m1.array[i][j];
}
output << std::endl;
}
output << std::endl;
return output;
}
/*=============================================================================
FUNCTION: Matrix Matrix::operator = (const Matrix &m1
DESCRIPTION: Assigned the elements of polynomial m1 to the
this polynomial
RETURNS: Returns the object being worked with (this)
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix<T>& Matrix<T>::operator = (const Matrix<T> &m1){
for(int i = 0; i < this->rows; ++i){
for(int j = 0; j < this->cols; j++){
this->array[i][j] = m1.array[i][j];
}
}
return *this;
}
/*=============================================================================
FUNCTION: Matrix<T>::resize(const int &s_row, const int &s_col, bool doclear)
DESCRIPTION: The purpose of this function is to resize a Matrix object as needed
RETURNS: Nothing (Void Function)
NOTES: templated class functions and is a *protected* member
===============================================================================*/
template<typename T>
void Matrix<T>::resize(const int &s_row, const int &s_col, bool doclear){
if(doclear)
this->clear();
this->rows = s_row;
this->cols = s_col;
this->array = new T*[s_row];
for(int i = 0; i < s_row; ++i){
this->array[i] = new T[s_col];
for(int j = 0; j < s_col; ++j){
this->array[i][j] = 0;
}
}
}
/*=============================================================================
FUNCTION: Matrix_ops<T>::Matrix_ops(int i, int j) : Matrix<T>::Matrix(i, j) {}
DESCRIPTION: Overloaded constructor for Matrix_ops
RETURNS: Being a constructor, this function returns a Matrix object
NOTES: templated class functions -- initialized from the overloaded
constructor from Matrix class
===============================================================================*/
template<typename T>
Matrix_ops<T>::Matrix_ops(int i, int j) : Matrix<T>::Matrix(i, j) {}
/*=============================================================================
FUNCTION: Matrix_ops<T>::Matrix_ops(const Matrix_ops<T>& m1) : Matrix<T>::Matrix(m1)
DESCRIPTION: This function is the copy constructor for Matrix_ops class
RETURNS: Being a constructor, this function returns a Matrix object
NOTES: templated class functions -- initialized from the copy
constructor from Matrix class
===============================================================================*/
template<typename T>
Matrix_ops<T>::Matrix_ops(const Matrix_ops<T>& m1) : Matrix<T>::Matrix(m1) {}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::operator + (const Matrix_ops<T> &m1) const
DESCRIPTION: This function overloads the + operator to add to Matrices
RETURNS: Returns the result of the addition as an Object
NOTES: templated class functions -- declared const as to not modify
the objects passed to it.
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::operator + (const Matrix_ops<T> &m1) const{
if(this->rows != m1.rows || this->cols != m1.cols)
throw "\tThe matrices are of different sizes!\n";
Matrix_ops<T> m2(this->rows,this->cols);
for(int i = 0; i < m1.rows; ++i){
for(int j = 0; j < m1.cols; j++){
m2.array[i][j] = this->array[i][j] + m1.array[i][j];
}
}
return m2;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::operator - (const Matrix_ops<T> &m1) const
DESCRIPTION: This function overloads the - operator to subtract two Matrices
RETURNS: Returns the result of the subtraction as an Object
NOTES: templated class functions -- declared const as to not modify
the objects passed to it.
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::operator - (const Matrix_ops<T> &m1) const{
if(this->rows != m1.rows || this->cols != m1.cols)
throw "\tThe matrices are of different sizes!\n";
Matrix_ops<T> m2(this->rows,this->cols);
for(int i = 0; i < m1.rows; ++i){
for(int j = 0; j < m1.cols; j++){
m2.array[i][j] = this->array[i][j] - m1.array[i][j];
}
}
return m2;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::operator * (const Matrix_ops<T> &m1) const
DESCRIPTION: This function overloads the * operator to multiply two Matrices
RETURNS: Returns the result of the multiplication as an Object
NOTES: templated class functions -- declared const as to not modify
the objects passed to it.
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::operator * (const Matrix_ops<T> &m1) const{
Matrix_ops<T> m2(this->rows,m1.cols);
if(this->cols != m1.rows)
throw "\n\tYou can not multiply Matrices without matching sizes\n";
for(int k = 0; k < this->rows; ++k){
for(int i = 0; i < m1.cols; ++i) {
for (int j = 0; j < m1.rows; ++j) {
m2.array[k][i] += (this->array[k][j] * m1.array[j][i]);
}
}
}
return m2;
}
/*=============================================================================
FUNCTION: Matrix_ops<T>::operator == (const Matrix_ops<T> &m1) const
DESCRIPTION: This function tests if two Matrix objects are equal by
overloading the == sign
RETURNS: This function returns true or false depending on if the Matrices
are equal or not.
NOTES: templated class functions -- declared const as to not modify
the objects passed to it.
===============================================================================*/
template<typename T>
bool Matrix_ops<T>::operator == (const Matrix_ops<T> &m1) const{
if(this->rows != m1.rows || this->cols != m1.cols){
return false;
}else {
for(int i = 0; i < m1.rows; ++i){
for(int j = 0; j < m1.cols; j++){
if(this->array[i][j] != m1.array[i][j])
return false;
}
}
}
return true;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::trans() const
DESCRIPTION: This function computers the transpose of a Matrix
RETURNS: Returns the resulting object of the tranpose
NOTES: templated class functions -- declared const as to not modify
the objects passed to it.
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::trans() const{
Matrix_ops<T> m1(this->cols, this->rows);
for(int i = 0; i < this->rows; ++i){
for(int j = 0; j < this->cols; ++j){
m1.array[j][i] = this->array[i][j];
}
}
return m1;
}
/*=============================================================================
FUNCTION: Matrix_ops<T>::det()
DESCRIPTION: This function computes the determinant of a Matrix
RETURNS: This function returns the result of the determinant as
the templated data after the result of determination
NOTES: templated class functions -- This function uses Recursion
===============================================================================*/
template<typename T>
T Matrix_ops<T>::det(){
if(this->rows != this->cols)
throw "\n\tYou can not take a determinant of a non-square matrix\n";
if (this->rows == 2) {
this->deter = ((this->array[0][0] * this->array[1][1]) - (this->array[1][0] * this->array[0][1]));
} else {
Matrix_ops<T> m1(this->rows - 1, this->cols - 1);
this->deter = 0;
int l = 0;
for (int i = 0; i < this->cols; ++i) {
for (int j = 1; j < this->rows; ++j) {
l = 0;
for (int k = 0; k < this->cols; ++k) {
if (k != i){
m1.array[j - 1][l] = this->array[j][k];
++l;
}
}
}
this->deter += this->array[0][i] * expo(-1,i) * m1.det() ;
}
}
return this->deter;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::inv()
DESCRIPTION: This function computes the inverse of a Matrix
RETURNS: The function returns the resulting Matrix inverse object
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::inv(){
if(this->det() == 0)
throw "\n\tThis matrix does not have an inverse\n";
Matrix_ops<T> m1(this->rows, (2 * this->cols));
for (int i = 0; i < this->cols; ++i) { // add an identity matrix
for (int j = 0; j < this->rows; ++j) {
m1.array[i][j] = this->array[i][j];
m1.array[i][this->cols + i] = 1;
}
}
m1.reduced_row();
//*this = m1;
return m1;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::reduced_row() const
DESCRIPTION: This function puts a Matrix in reduced row echelon form
RETURNS: This function returns the resulting Matrix object
NOTES: templated class functions -- declared const as not to modify
the objects passed to it
===============================================================================*/
template <typename T>
Matrix_ops<T> Matrix_ops<T>::reduced_row(){
if(this->rows >= this->cols)
throw "\n\tThis is not an augmented matrix!\n";
Matrix_ops<T> m1(this->rows, this->cols);
m1 = *this;
for(int i = 0; i < m1.rows; ++i) {
m1.div_row(i, m1.array[i][i]);
for (int j = 1; j < m1.rows; ++j) {
m1.sub_row(j);
}
}
return m1;
}
/*=============================================================================
FUNCTION: Matrix_ops<T>& Matrix_ops<T>::div_row(int i)
DESCRIPTION: This function takes a Matrix object and divides a row
of that Matrix by the element in the position passed
RETURNS: This function returns the object being worked with (this)
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix_ops<T>& Matrix_ops<T>::div_row(int row_pos, T temp){
for(int k = 0; k < this->cols; ++k){
this->array[row_pos][k] /= temp;
}
return *this;
}
/*=============================================================================
FUNCTION: Matrix_ops<T>& Matrix_ops<T>::sub_row(int row_pos)
DESCRIPTION: This function takes a row from a Matrix object and subtracts a
previous row from it.
of that Matrix by the element in the position passed
RETURNS: This function returns the object being worked with (this)
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix_ops<T>& Matrix_ops<T>::sub_row(int row_pos){
for(int i = 0; i < this->cols; ++i){
this->array[row_pos][i] = this->array[row_pos][i] - this->array[row_pos-1][i] * this->array[row_pos-1][row_pos-1];
}
return *this;
}
/*=============================================================================
FUNCTION: Matrix_ops<T> Matrix_ops<T>::solve() const
DESCRIPTION: This function solves a system of equations using cramers rule
RETURNS: Returns a *vector* Matrix of the results of the solving
NOTES: templated class functions -- declared const as not to modify
the objects passed to it
===============================================================================*/
template<typename T>
Matrix_ops<T> Matrix_ops<T>::solve() const{
if(this->cols - 1 != this->rows)
throw "\n\tThis system can not be solved by Cramer's rule!\n";
Matrix_ops<T> maj_det(this->rows, this->cols - 1);
Matrix_ops<T> var_det(this->rows, this->cols - 1);
Matrix_ops<T> result_mat(this->cols - 1, 1);
for (int i = 0; i < this->cols; ++i) {
for (int j = 0; j < this->rows; ++j) {
for(int k = 0; k < this->cols-1; ++k){
var_det.array[j][k] = this->array[j][k];
if(!i){
maj_det.array[j][k] = this->array[j][k];
}
if(k == i){
var_det.array[j][k] = this->array[j][this->cols - 1];
}
}
}
if(i == this->cols - 1) continue;
result_mat.array[i][0] = (var_det.det() / maj_det.det());
}
return result_mat;
}
/*=============================================================================
FUNCTION: Matrix_ops<T>& Matrix_ops<T>::out_file(char file [])
DESCRIPTION: This function is a member of Matrix and does the output file
of the object passed into it.
RETURNS: Returns the object being worked with (this) but implied
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix_ops<T>& Matrix_ops<T>::out_file(char file []){
std::ofstream outfile;
strcat(file, ".mtx");
outfile.open(file);
outfile << std::setw(4) << this->rows << " X " << this->cols;
outfile << *this;
outfile.close();
return *this;
}
/*=============================================================================
FUNCTION: Matrix<T>::~Matrix()
DESCRIPTION: This is the destructor used by both Matrix and Matrix_ops classes
This destructs objects after they are done being used and calls
clear to do it.
RETURNS: destructors do not return Objects
NOTES: templated class functions
===============================================================================*/
template<typename T>
Matrix<T>::~Matrix(){
this->clear();
}
/*=============================================================================
FUNCTION: void Matrix<T>::clear()
DESCRIPTION: This functions sole purpose is too clear out the memory of a
Matrix object
RETURNS: Nothing (Void Function)
NOTES: templated class functions -- *protected* member function
===============================================================================*/
template<typename T>
void Matrix<T>::clear(){
for(int i = 0; i < this->rows; ++i){
delete [] this->array[i];
this->array[i] = NULL;
}
delete [] this->array;
}
/*=============================================================================
FUNCTION: expo(const T &base, const T &power)
DESCRIPTION: This function computes the exponent of a base and the number
is is raised to (like the pow function)
RETURNS: Returns the templated data after the result of exponentiation
NOTES: templated class functions and uses recursion
===============================================================================*/
template<typename T>
T expo(const T &base, const T &power) {
switch (power) {
case 0:
return 1;
case 1:
return base;
case 2:
return base * base;
default:
return (base * (expo(base, power - 1)));
}
}