-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixLib.cpp
342 lines (330 loc) · 12 KB
/
matrixLib.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
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
#include "matrixLib.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iterator>
using namespace std;
//------------------------------Public Matrix(row,column) Constructor----------------
Matrix::Matrix (int row, int column) : matrix_( vector<vector<double >>(row,vector<double >(column)) )
{
if (row <= 0)
throw invalid_argument("\nERROR! Your matrix 'row' must be bigger than 0.\n");
if (column <= 0)
throw invalid_argument("\nERROR! Your matrix 'column' must be bigger than 0.\n");
this->column_=column;
this->row_=row;
}
//------------------------------Public Matrix(File) Constructor------------
Matrix::Matrix (string address, char delim)
{
ifstream matrixfile;
matrixfile.open (address);
if ( !matrixfile.is_open() )
throw runtime_error("\nYour address or txt file name is wrong.Could not open file.\n");
this->row_ = this->column_ = 0;
size_t thisDelim = 0;
string line = "";
while (getline(matrixfile, line) && line != "")
{
while (line.find(delim, thisDelim + 1) && row_ == 0)
{
thisDelim = line.find(delim, thisDelim + 1);
if (thisDelim == string::npos)
{
column_++;
break;
}
column_++;
}
row_++;
}
this->matrix_ = vector<vector<double > > (row_,vector<double >(column_));
this->openFile_(address, delim);
matrixfile.close();
}
//------------------------------Overloading >> ---------------------------------
istream& operator >> (istream &in, Matrix &matrix1)
{
for(int it = 0 ; it < matrix1.row_ ; it++)
{
cout<< it+1 <<" row:\n" ;
for ( int innerIt = 0 ; innerIt < matrix1.column_ ; innerIt++ )
in >> matrix1.matrix_.at(it).at(innerIt);
}
}
//------------------------------Overloading << --------------------------------
ostream &operator << (ostream &out , const Matrix &matrix1)
{
for (int it = 0; it < matrix1.row_; it++)
{
for (int innerIt = 0; innerIt < matrix1.column_; innerIt++)
{
if (innerIt == matrix1.column_ -1)
out << matrix1.matrix_.at(it).at(innerIt);
else
out << matrix1.matrix_.at(it).at(innerIt) << '\t';
}
out << '\n';
}
}
//------------------------------show the this->matrix WITH delim---------------------------
void Matrix::show(char delim)
{
for (int it = 0; it < this->row_; it++)
{
for(int innerIt = 0; innerIt < this->column_; innerIt++)
{
if (innerIt == this->column_ -1)
cout << this->matrix_.at(it).at(innerIt);
else
cout << this->matrix_.at(it).at(innerIt) << delim;
}
cout << '\n';
}
}
//------------------------------show the this->matrix WITHOUT delim---------------------------
void Matrix::show()
{
for (int it = 0; it < this->row_; it++)
{
for(int innerIt = 0; innerIt < this->column_; innerIt++)
{
if (innerIt == this->column_ -1)
cout << this->matrix_.at(it).at(innerIt);
else
cout << this->matrix_.at(it).at(innerIt) << '\t';
}
cout << '\n';
}
}
//---------------------------Overloading SUM------------------------
Matrix Matrix::operator+ (Matrix const &matrix)
{
if (this->column_ != matrix.column_ || this->row_ != matrix.row_)
throw logic_error("\nSummation error: These matrices are not in a same shape.\n");
Matrix totalMartix (this->row_, this->column_);
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
totalMartix.matrix_.at(it).at(innerIt) = this->matrix_.at(it).at(innerIt) + matrix.matrix_.at(it).at(innerIt);
return totalMartix;
}
//-------------------------Overloading Multiply With Another Matrix -----------------------
Matrix Matrix::operator * (Matrix const &matrix)
{
if (this->column_ != matrix.row_)
throw logic_error("\nMultiplication error: Column of first matrix is not equal to row of second matrix.\n");
Matrix productMatrix(this->row_, matrix.column_);
for (int it = 0; it < this->row_; it++)
for(int firstInnerIt = 0; firstInnerIt < matrix.column_; firstInnerIt++)
for (int secondInnerIt = 0; secondInnerIt < this->column_; secondInnerIt++)
{
productMatrix.matrix_.at(it).at(firstInnerIt) += this->matrix_.at(it).at(secondInnerIt) * matrix.matrix_.at(secondInnerIt).at(firstInnerIt);
}
return productMatrix;
}
//-------------------------Overloading Multiply With Number----------------------
Matrix Matrix::operator * (int const coefficient)
{
Matrix productMatrix(this->row_, this->column_);
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
productMatrix.matrix_.at(it).at(innerIt) = this->matrix_.at(it).at(innerIt) * coefficient;
return productMatrix;
}
//--------------------------- int * matrix---------------------------
Matrix operator * (int const coefficient, Matrix const matrix)
{
Matrix productMatrix(matrix.row_, matrix.column_);
for (int it = 0; it < matrix.row_; it++)
for (int innerIt = 0; innerIt < matrix.column_; innerIt++)
productMatrix.matrix_.at(it).at(innerIt) = matrix.matrix_.at(it).at(innerIt) * coefficient;
return productMatrix;
}
//----------------------------Overloading Minus----------------------
Matrix Matrix::operator - (Matrix const matrix)
{
if (this->column_ != matrix.column_ || this->row_ != matrix.row_)
throw logic_error("\nSubtraction error: These matrices are not in a same shape.\n");
Matrix minusMatrix (this->row_, this->column_);
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
minusMatrix.matrix_.at(it).at(innerIt) = this->matrix_.at(it).at(innerIt) - matrix.matrix_.at(it).at(innerIt);
return minusMatrix;
}
//----------------------------- overloading == -----------------------
bool Matrix ::operator == (Matrix const matrix)
{
if(this->row_ == matrix.row_ && this->column_ == matrix.column_)
{
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
if (this->matrix_.at (it).at (innerIt) != matrix.matrix_.at (it).at (innerIt))
return false;
return true;
}
else
return false;
}
//----------------------------Transposing----------------------------
Matrix Matrix::transpose()
{
Matrix transposeMatrix (this->column_, this->row_);
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
transposeMatrix.matrix_.at(innerIt).at(it) = this->matrix_.at(it).at(innerIt);
return transposeMatrix;
}
//-------------------------Cofactor_-----------------------------
void Matrix::getCofactor_(Matrix matrix1, Matrix &tempMatrix, int p, int q, int n)
{
int i = 0, j = 0;
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
{
if (row != p && column != q)
{
tempMatrix.matrix_.at(i).at(j++) = matrix1.matrix_.at(row).at(column);
if (j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
//----------------------private determinant_ -----------------------------
double Matrix::determinant_ (int n)
{
double determ = 0;
if (n == 1)
return this->matrix_.at(0).at(0);
Matrix tempMatrix (this->row_, this->row_);
int sign = 1;
for (int f = 0; f < n; f++)
{
getCofactor_(*this, tempMatrix, 0, f, n);
determ += sign * this->matrix_.at(0).at(f) * tempMatrix.determinant_ (n-1);
sign = -sign;
}
return determ;
}
//-------------------Public determinant------------------------------------
double Matrix::determinant()
{
if (this->row_ != this->column_)
throw logic_error("\nDeterminants are only used for square matrices.\n");
return this->determinant_(this->row_);
}
//-----------------Elements Textfile To Matrix----------------------
void Matrix::openFile_(string address, char delim)
{
ifstream elementFile (address);
if( !elementFile.is_open() )
{
elementFile.open(address);
if ( !elementFile.is_open() )
throw runtime_error("\nInternal error: The address is not passed right to private 'Matrix::openFile_(string address, char delim)' function.\n");
}
size_t previousDelim = -1;
size_t thisDelim = 0;
string line = "";
int it = 0;
while (getline (elementFile, line) && line != "")
{
int innerIt = 0;
while (line.find (delim, thisDelim + 1))
{
thisDelim = line.find(delim, thisDelim + 1);
if (thisDelim == string::npos)
{
thisDelim = line.find('\n');
if (line [previousDelim + 1] == '-')
this->matrix_.at(it).at(innerIt) = -stod (line.substr (previousDelim + 2, thisDelim));
else
this->matrix_.at(it).at(innerIt) = stod (line.substr (previousDelim + 1, thisDelim - 1));
break;
}
if (line[previousDelim + 1] == '-')
{
this->matrix_.at(it).at(innerIt) = -stod (line.substr (previousDelim + 2, thisDelim));
innerIt++;
}
else {
this->matrix_.at(it).at(innerIt) = stod (line.substr (previousDelim + 1, thisDelim));
innerIt++;
}
previousDelim = thisDelim;
}
it++;
previousDelim = -1;
thisDelim = 0;
}
}
//------------------------Saving Matrix To TextFile----------------------------------
void Matrix::save(string name , char delim)
{
ofstream outputFile (name);
for (int it = 0; it < this->row_; it++)
{
for (int innerIt = 0; innerIt < this->column_; innerIt++)
{
if (innerIt == this->column_ -1)
outputFile << this->matrix_.at(it).at(innerIt);
else
outputFile << this->matrix_.at(it).at(innerIt) << delim ;
}
outputFile << '\n';
}
}
//-------------------------Get Row-------------------------------------
int Matrix::getRow()
{
return this->row_;
}
//---------------------Get column-------------------------------------
int Matrix::getColumn()
{
return this->column_;
}//------------------- show matrixSize-------------------------------------
string Matrix::showSize()
{
string size = "";
size = to_string(this->row_) + " , " + to_string(this->column_);
return size;
}
//------------------private adjoint_ ------------------------------------------
void Matrix::adjoint_(Matrix &adjoint)
{
if (this->column_ == 1)
{
adjoint.matrix_.at(0).at(0) = 1;
return;
}
int sign = 1;
Matrix tempMatrix(this->row_, this->column_);
for (int it= 0; it <this->row_; it++)
for (int innerIt = 0; innerIt< this->column_; innerIt++)
{
getCofactor_(*this, tempMatrix, it, innerIt, this->row_);
sign = ((it+innerIt)%2==0)? 1: -1;
int n = this->row_;
adjoint.matrix_.at(innerIt).at(it)= (sign) * (tempMatrix.determinant_(n-1));
}
}
//--------------------Public inverse ----------------------------------------
Matrix Matrix::inverse()
{
double determ = this->determinant_(this->column_);
if (determ != 0)
throw logic_error ("\nSingular matrix, can't find its inverse.\n");
Matrix inverse(this->row_, this->column_);
Matrix adjoint(this->row_, this->column_);
this->adjoint_ (adjoint);
for (int it = 0; it < this->row_; it++)
for (int innerIt = 0; innerIt < this->column_; innerIt++)
inverse.matrix_.at(it).at(innerIt) = adjoint.matrix_.at(it).at(innerIt) / int(determ);
return inverse;
}