-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
166 lines (140 loc) · 3.21 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
#include "matrix.h"
#include <iostream>
Matrix::Matrix()
{
size.first = 0;
size.second = 0;
}
Matrix::Matrix(int rows, int columns)
: size{ rows, columns }
{
for (int i = 0; i < rows; i++)
mat.push_back(std::vector<float>(columns, 0));
}
Matrix::Matrix(std::vector<float> v) : Matrix::Matrix()
{
if (!v.size())
return;
size.first = 1;
size.second = v.size();
mat.push_back(v);
}
Matrix::Matrix(std::vector<std::vector<float>> v) : Matrix::Matrix()
{
if (!v.size())
return;
int temp = v[0].size();
for (int i = 1; i < v.size(); i++)
if (v[i].size() != temp)
throw std::exception("Trying to create Matrix with dynamic row length");
size.first = v.size();
size.second = v[0].size();
mat = v;
}
Matrix Matrix::transpose()
{
Matrix newM(size.second, size.first);
for (int i = 0; i < size.second; i++)
{
for (int j = 0; j < size.first; j++)
{
newM[i][j] = mat[j][i];
}
}
*this = Matrix(newM);
return newM;
}
void Matrix::printMatrix()
{
//std::cout << "Printing matrix!" << std::endl;
for (int i = 0; i < size.first; i++)
{
for (int j = 0; j < size.second; j++)
{
std::cout << mat[i][j] << ' ';
}
std::cout << std::endl;
}
}
void Matrix::fill(float f)
{
for (int i = 0; i < size.first; i++)
for (int j = 0; j < size.second; j++)
mat[i][j] = f;
}
Matrix Matrix::getIdentityMatrix(int size)
{
Matrix res(size, size);
for (int i = 0; i < size; i++)
mat[i][i] = 1.0f;
return res;
}
std::vector<float>& Matrix::operator[](int i)
{
if (i < 0 || i >= size.first)
throw std::exception("Invalid matrix row index");
return mat[i];
}
Matrix Matrix::operator+(const Matrix& m)
{
if(size.first != m.size.first || size.second != m.size.second)
throw std::exception("Invalid matrix operation");
for (int i = 0; i < size.first; i++)
for (int j = 0; j < size.second; j++)
mat[i][j] += m.mat[i][j];
return *this;
}
Matrix Matrix::operator-(const Matrix& m)
{
if (size.first != m.size.first || size.second != m.size.second)
throw std::exception("Invalid matrix operation");
for (int i = 0; i < size.first; i++)
for (int j = 0; j < size.second; j++)
mat[i][j] -= m.mat[i][j];
return *this;
}
Matrix Matrix::operator*(float s)
{
for (int i = 0; i < size.first; i++)
for (int j = 0; j < size.second; j++)
mat[i][j] *= s;
return *this;
}
Matrix Matrix::operator/(float s)
{
if (!s)
throw std::exception("Division by zero");
s = 1 / s; // Multiplying is faster than dividing
for (int i = 0; i < size.first; i++)
for (int j = 0; j < size.second; j++)
mat[i][j] *= s;
return *this;
}
Matrix Matrix::operator*(const Matrix& m)
{
if (size.second != m.size.first)
throw std::exception("Invalid matrix operation");
Matrix newM(size.first, m.size.second);
for (int i = 0; i < size.first; i++)
{
for (int j = 0; j < m.size.second; j++)
{
float sum = 0.0f;
for (int k = 0; k < m.size.first; k++)
{
sum += mat[i][k] * m.mat[k][j];
}
newM[i][j] = sum;
}
}
*this = Matrix(newM);
return newM;
}
int Matrix::getRowCount()
{
return size.first;
}
int Matrix::getColumnCount()
{
return size.second;
}