-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrix.cpp
More file actions
180 lines (145 loc) · 4.56 KB
/
Matrix.cpp
File metadata and controls
180 lines (145 loc) · 4.56 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
/*
* File : Matrix.cpp
* Authors : Robin Demarta, Loïc Dessaules
* Date : 21.02.2020
*/
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include "Matrix.hpp"
#include "Operator.hpp"
#include "Addition.hpp"
#include "Subtraction.hpp"
#include "Multiply.hpp"
using namespace std;
const Addition Matrix::addOp;
const Subtraction Matrix::subOp;
const Multiply Matrix::mulOp;
Matrix::Matrix(int n, int m, int modulus) : n(n), m(m), modulus(modulus) {
testModulus(modulus);
testMatrixDimensions(n, m);
// Initialize content array
content = new int* [n];
for(int i = 0; i < n; ++i)
content[i] = new int[m];
randomPopulate();
}
Matrix::Matrix(const Matrix& matrix) {
// variables copy
n = matrix.n;
m = matrix.m;
modulus = matrix.modulus;
// Initialize new content array and copy same values as the cpy matrix content
content = new int* [n];
for(int i = 0; i < n; ++i) {
content[i] = new int[m];
for(int j = 0; j < m; j++) {
content[i][j] = matrix.content[i][j];
}
}
}
void Matrix::randomPopulate() {
srand((unsigned int)(time(nullptr))); // Random seed
for(int row = 0; row < n; ++row)
for(int col = 0; col < m; ++col)
content[row][col] = (int) (rand() / (RAND_MAX + 1.0) * modulus); // Random number [0;modulus[
}
void Matrix::testModulus(int mod) const {
if(mod <= 0) {
throw invalid_argument(
"We cannot have a modulus of 0 because the matrix values must be between 0 and modulus - 1");
}
}
void Matrix::testMatrixDimensions(int nVal, int mVal) const {
if(nVal <= 0 || mVal <= 0)
throw invalid_argument("Matrix dimension cannot be void or negative");
}
int Matrix::trueModulus(int lhs, int rhs) const {
if(rhs < 0)
throw runtime_error("Cannot operate with negative rhs modulus.");
return (lhs % rhs + rhs) % rhs;
}
Matrix& Matrix::calc(const Matrix& other, const Operator& op) {
if(this->modulus != other.modulus)
throw invalid_argument("Both matrix must have same modulus");
int maxN = max(this->n, other.n);
int maxM = max(this->m, other.m);
Matrix result(maxN, maxM, this->modulus);
// Do the arithmetic operation component by component
for(int row = 0; row < maxN; ++row) {
for(int col = 0; col < maxM; ++col) {
// Complete with 0 value if one of the matrices doesn't have the same dimensions
int op1 = row >= this->n || col >= this->m ? 0 : this->content[row][col];
int op2 = row >= other.n || col >= other.m ? 0 : other.content[row][col];
int operationResult = op.calculate(op1, op2);
result.content[row][col] = trueModulus(operationResult, result.modulus);
}
}
swap(*this, result); // Actually modify current matrix
return *this;
}
Matrix Matrix::addCopy(const Matrix& other) const {
Matrix result(*this);
result.calc(other, addOp);
return result;
}
Matrix Matrix::subCopy(const Matrix& other) const {
Matrix result(*this);
result.calc(other, subOp);
return result;
}
Matrix Matrix::multiplyCopy(const Matrix& other) const {
Matrix result(*this);
result.calc(other, mulOp);
return result;
}
Matrix* Matrix::addDynamic(const Matrix& other) const {
Matrix* result = new Matrix(*this);
result->calc(other, addOp);
return result;
}
Matrix* Matrix::subDynamic(const Matrix& other) const {
Matrix* result = new Matrix(*this);
result->calc(other, subOp);
return result;
}
Matrix* Matrix::multiplyDynamic(const Matrix& other) const {
Matrix* result = new Matrix(*this);
result->calc(other, mulOp);
return result;
}
Matrix& Matrix::addModify(const Matrix& other) {
return calc(other, addOp);
}
Matrix& Matrix::subModify(const Matrix& other) {
return calc(other, subOp);
}
Matrix& Matrix::multiplyModify(const Matrix& other) {
return calc(other, mulOp);
}
Matrix& Matrix::operator=(const Matrix& matrix) {
// Both object must be different to do the copy
if(this != &matrix) {
Matrix newMatrix(matrix);
swap(n, newMatrix.n);
swap(m, newMatrix.m);
swap(modulus, newMatrix.modulus);
swap(content, newMatrix.content);
}
return *this;
}
std::ostream& operator<<(std::ostream& os, const Matrix& m) {
for(int i = 0; i < m.n; ++i) {
for(int j = 0; j < m.m; ++j) {
os << m.content[i][j] << " ";
}
os << endl;
}
return os;
}
Matrix::~Matrix() {
for(int i = 0; i < n; ++i) {
delete[] content[i];
}
delete[] content;
}