-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_math.cpp
More file actions
262 lines (222 loc) · 12.8 KB
/
matrix_math.cpp
File metadata and controls
262 lines (222 loc) · 12.8 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
/*===============================================================================
FILE: matrix_math.cpp
DESCRIPTION: This program is the main driver program for matrix.cpp and Matrix.h
COMPILER: Linux GNU g++ compiler c++ -std=c++11
using custom makefile and Clion IDE editor
NOTES: Includes the implementation file Matrix.cpp
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 functions for command line
Shawn Ray 2017-04-17 Version 1.4 templated open file.
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
Shawn Ray 2017-04-23 Version 2.1
Shawn Ray 2017-04-25 Version 2.2 added stuffs for class objects
Shawn Ray 2017-04-26 Version 2.3 added stuffs for class objects
Shawn Ray 2017-04-27 Version 2.4 added stuffs for class objects
Shawn Ray 2017-04-28 Version 2.5 added stuffs for class objects
Shawn Ray 2017-04-29 Version 2.6 bug fix
Shawn Ray 2017-04-30 Version 2.7 bug fix
Shawn Ray 2017-05-01 Version 2.8
Shawn Ray 2017-05-02 Version 2.9
Shawn Ray 2017-05-03 Version 3.0 bug fix
Shawn Ray 2017-05-04 Version 3.1 added ability for output file
================================================================================*/
#include "Matrix.cpp" // specification
/*=============================================================================
FUNCTION: main()
DESCRIPTION: Creates a matrix object, and calls the appropriate functions
RETURNS: Main being in type returns a 0 to signify successful completion
NOTES: Put other information here ...
===============================================================================*/
int main(int argc, char *argv[]){
Stuff command_var;
if(test_arg(argc, command_var)) return 0;
compare_arg(argc, argv, command_var);
return 0;
}
/*=============================================================================
FUNCTION: bool test_arg(int argc, const Stuff &command_var)
DESCRIPTION: This function tests to see if there are enough command line
RETURNS: Being boolean type, it returns true or false depending on
if there are enough command line arguments
NOTES: passed Class object for command line variables
===============================================================================*/
bool test_arg(int argc, Stuff &command_var){
if(argc < 2){
std::cout << "Not enough Arguments ?\n\n";
return true;
} else {
command_var.position = 1;
return false;
}
}
/*=============================================================================
FUNCTION: void compare_arg(char *argv[], Stuff &command_var)
DESCRIPTION: This function tests the argument vector to see if there are valid
commands
RETURNS: Nothing (Void Function)
NOTES: passed Class object for command line variables
===============================================================================*/
void compare_arg(int argc, char *argv[], Stuff &command_var){
char file[20];
bool file_true = false;
try {
if(argc > 4) { // note this method doesn't work with no arguments unless this if is here
if ((strcmp(argv[command_var.position + 3], "-out")) == 0) {
strcpy(file, argv[command_var.position + 4]);
file_true = true;
}
}
if ((strcmp(argv[command_var.position], "-h")) == 0) {
help();
}
if ((strcmp(argv[command_var.position], "-inp")) == 0) {
Matrix<int> mat1 = open_file<Matrix<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
}
if ((strcmp(argv[command_var.position], "-add")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
Matrix_ops<int> mat2 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat2;
std::cout << std::setw(4) << argv[command_var.position-1] << " + " << argv[command_var.position] << " = ";
std::cout << mat2 + mat1;
if(file_true) (mat2 + mat1).out_file(file);
}
if ((strcmp(argv[command_var.position], "-sub")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
Matrix_ops<int> mat2 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat2;
std::cout << std::setw(4) << argv[command_var.position-1] << " - " << argv[command_var.position] << " = ";
std::cout << mat2 - mat1;
if(file_true) (mat2 - mat1).out_file(file);
}
if ((strcmp(argv[command_var.position], "-mul")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
Matrix_ops<int> mat2 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat2;
std::cout << std::setw(4) << argv[command_var.position-1] << " * " << argv[command_var.position] << " = ";
std::cout << mat1 * mat2;
if(file_true) (mat1 * mat2).out_file(file);
}
if ((strcmp(argv[command_var.position], "-eq")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
Matrix_ops<int> mat2 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat2;
std::cout << std::setw(4) << argv[command_var.position-1] << " == " << argv[command_var.position] << " ? \n";
if (mat1 == mat2)
std::cout << "\tYes: the two matrices are equal\n";
else
std::cout << "\tNo: the two matrices are not equal\n";
}
if ((strcmp(argv[command_var.position], "-T")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
std::cout << std::setw(4) << "The transpose of Matrix " << argv[command_var.position];
std::cout << " = ";
std::cout << mat1.trans();
if(file_true) (mat1.trans()).out_file(file);
}
if ((strcmp(argv[command_var.position], "-det")) == 0) {
Matrix_ops<int> mat1 = open_file<Matrix_ops<int> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
std::cout << std::setw(4) << "The determinant of Matrix " << argv[command_var.position];
std::cout << " = ";
std::cout << mat1.det();
}
if ((strcmp(argv[command_var.position], "-1")) == 0) {
Matrix_ops<double> mat1 = open_file<Matrix_ops<double> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
std::cout << std::setw(4) << "The inverse of Matrix " << argv[command_var.position];
std::cout << " = ";
std::cout << mat1.inv();
if(file_true) (mat1.inv()).out_file(file);
}
if ((strcmp(argv[command_var.position], "-solve")) == 0) {
Matrix_ops<double> mat1 = open_file<Matrix_ops<double> >(argc, argv, command_var);
std::cout << std::setw(4) << "Matrix " << argv[command_var.position] << " = ";
std::cout << mat1;
std::cout << std::setw(4) << "The solutions are :";
std::cout << std::setw(4) << mat1.solve();
}
}
catch(const char *msg)
{
std::cout << msg;
}
}
/*=============================================================================
FUNCTION: Matrix open_file(int argc, char *argv[],Stuff &command_var)
DESCRIPTION: This function opens the file to be read from, then creates a class
object of the matrix
RETURNS: Being class type, this function returns the matrix created after
opening the file
NOTES: passed another class object for variables, passed position from Stuff
to generate a matrix object from multiple files
===============================================================================*/
template<typename T>
T open_file(int argc, char *argv[],Stuff &command_var){
++command_var.position; // incrememnt the position of the command line, to account for multiple args
if(argc > 1){ //
strcpy(command_var.file1, argv[command_var.position]);
strcat(command_var.file1, ".mtx");
command_var.infile.open(command_var.file1);
}
if(!command_var.infile){
throw "\n\tError opening the file, please try another command\n";
}
command_var.infile >> command_var.m >> command_var.let_x >> command_var.n;
T mat1(command_var.m,command_var.n);
command_var.infile >> mat1;
command_var.infile.close();
return mat1;
}
/*=============================================================================
FUNCTION: void help()
DESCRIPTION: This function displays the help menu instructing the user
what commands to enter.
RETURNS: Nothing (Void Function)
===============================================================================*/
void help(){
std::cout << "\t==============================================================\n";
std::cout << "\t======= Welcome to the Help menu for matrix_math.cpp =========\n";
std::cout << "\t= This Help menu will guide you through the valid commands ===\n";
std::cout << "\t========= that you can enter: They are as follows ============\n";
std::cout << "\t=============== -h for the help menu =========================\n";
std::cout << "\t==============================================================\n";
std::cout << "\t=== -inp to read input from a file and display the matrix ===\n";
std::cout << "\t=== -out tells the current operation to be printed to a file =\n";
std::cout << "\t=== -add input the two matrices and adds them together =======\n";
std::cout << "\t=== -sub inputs the two matrices and subtracts them ==========\n";
std::cout << "\t=== -mul multiplies two matrices together ====================\n";
std::cout << "\t=== -eq tests to see if two matrices are equal ===============\n";
std::cout << "\t=== -T computes the tranverse of a matrix ====================\n";
std::cout << "\t=== -1 computes the inverse of a matrix ======================\n";
std::cout << "\t=== -det calculates the determinant of a matrix ==============\n";
std::cout << "\t=== -solve solves a system of linear equations --------------=\n";
std::cout << "\t==============================================================\n";
}