-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
409 lines (376 loc) · 8.37 KB
/
Matrix.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
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
#include<iostream>
#include<type_traits>
#include<valarray>
#define TRASH_IN -1
using namespace std;
template<typename T>
class Matrix {
private:
static_assert(is_arithmetic<T>::value, "Non arithmetic class");
valarray<T> matx;
int columns;
int rows;
int size;
protected:
void Set_Rows(int);
void Set_Columns(int);
void Set_Size();
void Resize_Matrix();
bool isbadinput(istream&);
public:
Matrix() = default;
Matrix(const Matrix&) = default;
Matrix& operator=(const Matrix&) = default;
Matrix(Matrix&&) = default;
Matrix& operator=(Matrix&&) = default;
Matrix(int, int);
Matrix operator+(const Matrix&);
Matrix operator+(const T&);
Matrix operator-(const Matrix&);
Matrix operator-(const T&);
Matrix operator*(const T&);
Matrix operator*(const Matrix&);
Matrix& operator+=(const Matrix&);
Matrix& operator+=(const T&);
Matrix& operator-=(const Matrix&);
Matrix& operator-=(const T&);
Matrix& operator*=(const T&);
template<typename T>
friend ostream& operator<<(ostream&, const Matrix<T>&);
template<typename T>
friend istream& operator>>(istream&, Matrix<T>&);
template<typename T>
friend Matrix<T> operator+(const T&, const Matrix<T>&);
template<typename T>
friend Matrix<T> operator-(const T&, const Matrix<T>&);
template<typename T>
friend Matrix<T> operator*(const T&, const Matrix<T>&);
~Matrix();
};
/*
*@[brief]: Function check if istream object state is set to failbit, shows error and fix input stream;
*@[in]: istream object needed for fail check;
*@[out]: bool statement that shows function result, true if failbit enabled and false otherwise;
*/
template<typename T>
bool Matrix<T>::isbadinput(istream& in) {
if (in.fail()) {
in.clear();
in.ignore();
cout << "Bad input, istream fixed, re-enter pls\n";
return true;
}
return false;
}
/*
*@[brief]: overloaded output operator for class Matrix
*@[in]: ostream object for output and const Matrix reference
*@[out]: ostream object for chain output
*/
template<typename T>
ostream & operator<<(ostream & os, const Matrix<T> & obj)
{
int i = 0;
for (auto x : obj.matx) {
cout << x << " ";
i++;
if (i == obj.columns) {
cout << "\n";
i = 0;
}
}
return os;
}
/*
*@[brief]: overloaded input operator for class Matrix
*@[in]: istream object for input and Matrix reference
*@[out]: istream object for chain input
*/
template<typename T>
istream & operator>>(istream & is, Matrix<T>& obj)
{
bool bad_size = true;
while (bad_size) {
cout << "Enter rows and columns: ";
is >> obj.rows >> obj.columns;
bad_size = isbadinput(is);
}
obj.Resize_Matrix();
cout << "Enter elements: \n";
for (int i = 0; i < obj.size; i++) {
is >> obj.matx[i];
if (isbadinput(is)) {
i--;
continue;
}
}
return is;
}
/*
*@[brief]: Constructor that takes rows and columns and waits for element input
*@[in]: Rows and columns;
*/
template<typename T>
Matrix<T>::Matrix(int row, int column){
bool tmp1 = (row < 0) ? false : true;
bool tmp2 = (column < 0) ? false : true;
if (!(tmp1 | tmp2)) {
cout << "Error negative numbers!!!\n";
abort();
}
Set_Rows(row);
Set_Columns(column);
Resize_Matrix();
cout << "Enter elements: \n";
for (int i = 0; i < size; i++) {
cin >> matx[i];
if (isbadinput(cin)) {
i--;
}
}
}
/*
*@[brief]: overloaded operator + for addition of two Matrix;
*@[in]: const reference to second object;
*@[out]: sum of two object
*/
template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix & obj)
{
Matrix tmp;
tmp.columns = this->columns;
tmp.rows = this->rows;
tmp.size = this->size;
tmp.matx *= 0;
tmp.matx += TRASH_IN;
tmp.matx = this->matx + obj.matx;
return tmp;
}
/*
*@[brief]: friend overloaded operator + for addition number to matrix;
*@[in]: const reference to number;
*@[out]: matrix with added numbers
*/
template<typename T>
Matrix<T> operator+(const T & obj1, const Matrix<T> & obj2)
{
Matrix<T> obj3{ obj2 };
obj3 += obj1;
return obj3;
}
/*
*@[brief]: friend overloaded operator - for addition number to matrix;
*@[in]: const reference to number;
*@[out]: matrix with added numbers
*/
template<typename T>
Matrix<T> operator-(const T & obj1, const Matrix<T> & obj2)
{
Matrix<T> obj3{ obj2 };
obj3 _= obj1;
return obj3;
}
/*
*@[brief]: friend overloaded operator * for addition number to matrix;
*@[in]: const reference to number;
*@[out]: matrix with added numbers
*/
template<typename T>
Matrix<T> operator*(const T & obj1, const Matrix<T> & obj2)
{
Matrix<T> obj3{ obj2 };
obj3 *= obj1;
return obj3;
}
/*
*@[brief]: overloaded operator + for addition matrix to number;
*@[in]: const reference to number;
*@[out]: matrix with added numbers
*/
template<typename T>
Matrix<T> Matrix<T>::operator+(const T & obj)
{
Matrix tmp = *this;
tmp.matx += obj;
return tmp;
}
/*
*@[brief]: overloaded operator - for sub of two Matrix;
*@[in]: const reference to second object;
*@[out]: diff of two object
*/
template<typename T>
Matrix<T> Matrix<T>::operator-(const Matrix & obj)
{
Matrix tmp = *this;
tmp.matx -= obj.matx;
return tmp;
}
/*
*@[brief]: overloaded operator - for sub number from matrix;
*@[in]: const reference to number;
*@[out]: matrix with subbed numbers
*/
template<typename T>
Matrix<T> Matrix<T>::operator-(const T & obj)
{
Matrix tmp = *this;
tmp.matx -= obj;
return tmp;
}
/*
*@[brief]: overloaded operator * for mul number to matrix;
*@[in]: const reference to number;
*@[out]: matrix with mulled numbers
*/
template<typename T>
Matrix<T> Matrix<T>::operator*(const T & obj)
{
Matrix tmp = *this;
tmp.matx *= obj;
return tmp;
}
template<typename T>
Matrix<T> Matrix<T>::operator*(const Matrix & mat2)
{
Matrix tmp = *this;
if (tmp.columns != mat2.rows) {
cout << "Bad matrix sizes!!!!\n";
exit(true);
}
Matrix res_mat;
res_mat.Set_Rows(tmp.rows);
res_mat.Set_Columns(mat2.columns);
res_mat.Resize_Matrix();
int sum = 0;
int next = 0;
int brk = 0;
int jrl = 0;
for (int k = 0; k < tmp.rows; k++) {
for (int p = 0; p < mat2.columns; p++) {
for (int i = jrl, j = brk++, iter = 0; iter < tmp.columns; i++, iter++, j = j + mat2.columns) {
sum = sum + tmp.matx[i] * mat2.matx[j];
}
res_mat.matx[next++] = sum;
sum = 0;
}
jrl += tmp.columns;
brk = 0;
sum = 0;
}
return res_mat;
}
/*
*@[brief]: overloaded operator += for addition of two Matrix;
*@[in]: const reference to second object;
*@[out]: added object
*/
template<typename T>
Matrix<T>& Matrix<T>::operator+=(const Matrix & obj)
{
this->matx += obj.matx;
return *this;
}
/*
*@[brief]: overloaded operator += for addition number to matrix;
*@[in]: const reference to number;
*@[out]: added object
*/
template<typename T>
Matrix<T>& Matrix<T>::operator+=(const T & obj)
{
this->matx += obj;
return *this;
}
/*
*@[brief]: overloaded operator -= for sub of two Matrix;
*@[in]: const reference to second object;
*@[out]: subbed object
*/
template<typename T>
Matrix<T>& Matrix<T>::operator-=(const Matrix & obj)
{
this->matx -= obj.matx;
return *this;
}
/*
*@[brief]: overloaded operator - for sub number from matrix;
*@[in]: const reference to number;
*@[out]: subbed object
*/
template<typename T>
Matrix<T>& Matrix<T>::operator-=(const T & obj)
{
this->matx -= obj;
return *this;
}
/*
*@[brief]: overloaded operator *= for mul number to matrix;
*@[in]: const reference to number;
*@[out]: mulled object
*/
template<typename T>
Matrix<T>& Matrix<T>::operator*=(const T & obj)
{
this->matx *= obj;
return *this;
}
/*
*@[brief]: Function set value of private data 'rows'
*@[in]: integer value of rows
*/
template<typename T>
void Matrix<T>::Set_Rows(int r_tmp)
{
if (r_tmp < 0) {
cout << "Error: Negative rows value!\n";
exit(1);
}
rows = r_tmp;
}
/*
*@[brief]: Function set value of private data 'columns'
*@[in]: integer value of columns
*/
template<typename T>
void Matrix<T>::Set_Columns(int c_tmp)
{
if (c_tmp < 0) {
cout << "Error: Negative columns value!\n";
exit(1);
}
columns = c_tmp;
}
/*
*@[brief]: function calculates size for given rows and columns
*/
template<typename T>
void Matrix<T>::Set_Size()
{
size = rows*columns;
}
/*
*@[brief]: Function resizes data array using size calculated from rows and columns ,multiplication
*/
template<typename T>
void Matrix<T>::Resize_Matrix()
{
Set_Size();
matx.resize(size);
matx *= 0;
matx += TRASH_IN;
}
template<typename T>
Matrix<T>::~Matrix()
{
Set_Columns(0);
Resize_Matrix();
}
void main() {
cout << "First matrix: \n";
Matrix<int> a(1,2);
cout << "Second matrix: \n";
Matrix<int> b(2,1);
Matrix<int>c = a*b;
cout << "\n" << c;
}