-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
161 lines (120 loc) · 5.01 KB
/
demo.cpp
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
//This program will give a brief demo of the matrix library
#include "matrix.h"
int main(){
//Declaring new 3x3 matrix object populated initially with all zero(note: matrix dimensions cannot be less than 1)
matrix* mat = new matrix(3, 3);
std::cout << "Initialized Matrix: " << std::endl;
mat -> dispMat();
//Fill the first row of matrix with 1 and fill the first column of matrix with 1
mat -> fillrow(1, 1);
mat -> fillcol(1, 1);
//Obtaining the value at a sepcified row and column
std::cout << "Value at row 1 column 2 is: " << mat -> getVal(1, 2) << std::endl;
std::cout << "Matrix with first row and first column populated with 1s " << std::endl;
mat -> dispMat();
//Swapping 1st row with second row
mat -> rowswap(1, 2);
std::cout << "Matrix with first row and second row swapped" << std::endl;
mat -> dispMat();
//Fill matrix with all 1s
mat -> fillmat(1);
std::cout << "Matrix filled with all 1s" << std::endl;
mat -> dispMat();
//Reseting matrix to all 0s
mat -> clearmat();
std::cout << "Matrix cleared and all values reset to 0" << std::endl;
mat -> dispMat();
//populating matrix mat with values (note: Matrix index follow conventional mathematical indexing which starts with 1 rather than 0)
mat -> matedit(1, 1, 1); //The value at row 1 column 1 (the first value starting from the top left) is 1
mat -> matedit(4, 1, 3); //The value at row 1 column 3 (the first value starting from the top left) is 4
mat -> matedit(2, 1, 2); //The value at row 1 column 2 (the first value starting from the top left) is 2
mat -> matedit(5, 2, 1); //The value at row 2 column 1 (the first value starting from the top left) is 5
mat -> matedit(-3, 2, 2); //The value at row 2 column 2 (the first value starting from the top left) is -3
mat -> matedit(1, 3, 3); //The value at row 3 column 3 (the first value starting from the top left) is 1
mat -> matedit(8, 3, 2); //The value at row 3 column 2 (the first value starting from the top left) is 8
mat -> matedit(6, 3, 1); //The value at row 3 column 1 (the first value starting from the top left) is 6
mat -> matedit(1, 2, 3); //The value at row 2 column 3 (the first value starting from the top left) is 1
mat -> matedit(1, 3, 3); //The value at row 1 column 2 (the first value starting from the top left) is 2
//Display populated matrix to terminal
std::cout << "Populated Matrix: " << std::endl;
mat -> dispMat();
std::cout << "# of Rows in Matrix: " << mat -> getM() << std::endl;
std::cout << "# of Columns in Matrix: " << mat -> getN() << std::endl;
//Convert matrix to Row Echelon Form
matrix* mat2 = ref(mat);
std::cout << "Row Echelon Form converted to: " << std::endl;
mat2 -> dispMat();
//Convert matrix to Reduced Row Echelon Form
matrix* mat3 = rref(mat);
std::cout << "Reduced Row Echelon Form converted to: " << std::endl;
mat3 -> dispMat();
//Display Rank and Nullity of Matrix
std::cout << "Rank of matrix is: " << mat -> rank() << ", and the nullity is: " << mat -> nullity() << std::endl;
//Finding Determinant of matrix
std::cout << "Det is: " << mat -> findDet() << std::endl;
//Multiplying 2 matrices
matrix* a = new matrix(3, 3);
matrix* b = new matrix(3, 3);
matrix* x = new matrix(3, 4);
a -> matedit(1, 1, 1);
a -> matedit(4, 1, 3);
a -> matedit(2, 1, 2);
a -> matedit(5, 2, 1);
b -> matedit(6, 1, 1);
b -> matedit(2, 2, 3);
b -> matedit(1, 1, 2);
b -> matedit(8, 2, 1);
b -> matedit(8, 3, 3);
b -> matedit(8, 3, 1);
x -> matedit(1, 1, 1);
x -> matedit(2, 2, 3);
x -> matedit(4, 1, 2);
x -> matedit(8, 2, 1);
x -> matedit(7, 3, 3);
x -> matedit(19, 3, 1);
x -> matedit(2, 3, 4);
x -> matedit(5, 1, 4);
x -> matedit(8, 2, 4);
std::cout << "Demonstrating adding two matrices" << std::endl;
std::cout << "Matrix a: " << std::endl;
a -> dispMat();
std::cout << "Matrix b: " << std::endl;
b -> dispMat();
std::cout << "Matrix x: " << std::endl;
x -> dispMat();
std::cout << "a+b: " << std::endl;
matrix* c = matadd(a, b);
c -> dispMat();
delete c;
std::cout << "a*b: " << std::endl;
c = matmult(a, b);
c -> dispMat();
delete c;
std::cout << "a*x: " << std::endl;
c = matmult(a, x);
c -> dispMat();
delete c;
matrix* d = new matrix(1, 4);
matrix* e = new matrix(4, 1);
d -> matedit(3, 1, 1);
d -> matedit(4, 1, 2);
d -> matedit(5, 1, 3);
d -> matedit(6, 1, 4);
e -> matedit(1, 1, 1);
e -> matedit(2, 2, 1);
e -> matedit(3, 3, 1);
e -> matedit(4, 4, 1);
std::cout << "Matrix d: " << std::endl;
d -> dispMat();
std::cout << "Matrix e: " << std::endl;
e -> dispMat();
std::cout << "d * e: " << std::endl;
c = matmult(d, e);
c -> dispMat();
delete c;
//clearing matrix from memory
delete mat;
delete mat2;
delete mat3;
return EXIT_SUCCESS;
}