-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
211 lines (165 loc) · 4.54 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
//
// Created by goturak on 21/02/19.
//
#include <iostream>
#include <unistd.h>
#include "Matrix.hpp"
#include "Operation.hpp"
#include "Addition.h"
#include "Multiplication.h"
#include "Substraction.h"
int **Matrix::getElements() const {
return elements;
}
int Matrix::getWidth() const {
return width;
}
int Matrix::getHeight() const {
return height;
}
int Matrix::getModulo() const {
return modulo;
}
void Matrix::setElement(int x, int y, int value){
if(x >= 0 && x < height && y >= 0 && y < width){
elements[x][y]= ((value % modulo) + modulo) % modulo;
}
}
int Matrix::getElement(int x, int y)const{
if(x >= 0 && x < height && y >=0 && y < width){
return elements[x][y];
}else if(y >=0&&x >= 0){
return 0;
}
return -1;
}
void Matrix::resize(int h, int w) {
int** newElements = new int*[h];
for(int i = 0; i < h; i++){
newElements[i] = new int[w];
for(int j = 0; j < w; j++){
if(i < height && j < width) {
newElements[i][j] = elements[i][j];
} else {
newElements[i][j] = 0;
}
}
}
this->elements = newElements;
this->height = h;
this->width = w;
}
Matrix::Matrix(int height, int width, int modulo) : height(height), width(width), modulo(modulo) {
elements = new int*[height];
srand(time(NULL));
for(int i = 0; i < height; i++){
elements[i] = new int[width];
for(int j = 0; j < width; j++){
setElement(i, j, std::rand());
}
}
}
Matrix::Matrix(const Matrix &m2) : width(m2.getWidth()), height(m2.getHeight()), modulo(m2.getModulo()) {
elements = new int*[height];
for(int i = 0; i < height; i++){
elements[i] = new int[width];
for(int j = 0; j < width; j++){
setElement(i, j, m2.getElement(i, j));
}
}
}
std::ostream &operator<<(std::ostream &os, const Matrix &m) {
for(int i=0;i<m.height;i++){
for (int j = 0; j <m.width ; j++) {
os << m.getElement(i,j);
if(j<m.width-1){
os <<" ";
}
}
if(i<m.height-1){
os<< std::endl;
}
}
return os;
}
void Matrix::compute(Matrix &result,Matrix &m2, Operation *op ) {
if(!result.sameMod(m2)){
throw std::invalid_argument("modulos are not equal");
}
int maxHeight= std::max(getHeight(),m2.getHeight());
int maxWidth= std::max(getWidth(),m2.getWidth());
result.resize(maxHeight,maxWidth);
for (int i = 0; i < maxHeight; i++) {
for(int j= 0; j<maxWidth;j++){
int elem1= getElement(i,j);
int elem2=m2.getElement(i,j);
result.setElement(i,j, op->apply(elem1,elem2));
}
}
}
bool Matrix::sameMod(Matrix &m2) {
return getModulo()==m2.getModulo();
}
void Matrix::addInPlace(Matrix &m2) {
Addition *op= new Addition();
compute(*this, m2, op);
delete( op);
}
Matrix *Matrix::addPtr(Matrix &m2) {
Addition *op= new Addition();
Matrix *result=new Matrix(getHeight(),getWidth(),getModulo());
compute(*result,m2,op);
delete( op);
return result;
}
Matrix Matrix::addValue(Matrix &m2) {
Addition *op= new Addition();
Matrix result(getHeight(),getWidth(),getModulo());
compute(result,m2,op);
delete( op);
return result;
}
void Matrix::multInPlace(Matrix &m2) {
Multiplication *op= new Multiplication();
compute(*this, m2, op);
}
Matrix *Matrix::multPtr(Matrix &m2) {
Multiplication *op= new Multiplication();
Matrix *result=new Matrix(getHeight(),getWidth(),getModulo());
compute(*result,m2,op);
delete( op);
return result;
}
Matrix Matrix::multValue(Matrix &m2) {
Multiplication *op= new Multiplication();
Matrix result(getHeight(),getWidth(),getModulo());
compute(result,m2,op);
delete( op);
return result;
}
void Matrix::subInPlace(Matrix &m2) {
Substraction *op= new Substraction();
compute(*this, m2, op);
delete( op);
}
Matrix *Matrix::subPtr(Matrix &m2) {
Substraction *op= new Substraction();
Matrix *result=new Matrix(getHeight(),getWidth(),getModulo());
compute(*result,m2,op);
delete( op);
return result;
}
Matrix Matrix::subValue(Matrix &m2) {
Substraction *op= new Substraction();
Matrix result(getHeight(),getWidth(),getModulo());
compute(result,m2,op);
delete( op);
return result;
}
Matrix::~Matrix() {
std::cout << "Destructor is executed\n";
for(int i =0;i<height;i++){
delete [] elements[i];
}
delete [] elements;
}