forked from cricel/ScientificVisualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
59 lines (43 loc) · 1 KB
/
Matrix.cpp
File metadata and controls
59 lines (43 loc) · 1 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
#include "Matrix.h"
Matrix::Matrix(int row, int col) {
data = new double[row * col];
this->row = row;
this->col = col;
}
Matrix::~Matrix() {
delete[] data;
}
int Matrix::getIndex(int i, int j) {
return i * this->col + j;
}
Matrix Matrix::multiply(Matrix in) {
Matrix result(this->row, in.col);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < in.col; j++) {
double sum = 0;
for (int k = 0; k < this->col; k++) {
sum = sum + this->data[this->getIndex(i, k)] * in.data[in.getIndex(k, j)];
}
result.data[result.getIndex(i, j)] = sum;
}
}
return result;
}
Matrix::Matrix(const Matrix& M) {
this->row = M.row;
this->col = M.col;
this->data = new double[row * col];
for (int i = 0; i < M.row * M.col; i++) {
this->data[i] = M.data[i];
}
}
Matrix Matrix::operator=(const Matrix& M) {
delete[] this->data;
this->row = M.row;
this->col = M.col;
this->data = new double[row * col];
for (int i = 0; i < M.row * M.col; i++) {
this->data[i] = M.data[i];
}
return *this;
}