-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
153 lines (143 loc) · 4.7 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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Matrix.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Lifecycle
//---------------------------------------------------------------------------
Matrix::Matrix (unsigned r, unsigned c)
: values(matrix_values(r, vector<double>(c, 0)))
{ }
//---------------------------------------------------------------------------
Matrix::Matrix(const Matrix& src)
: values(src.values)
{ }
//---------------------------------------------------------------------------
// Custom methods
//---------------------------------------------------------------------------
void Matrix::fillWithValue(const double value)
{
for (mvIt i = values.begin(); i != values.end(); ++i) {
for (dIt j = (*i).begin(); j != (*i).end(); ++j) {
*j = value;
}
}
}
//---------------------------------------------------------------------------
void Matrix::fillDiagWithValue (const double value)
{
for (unsigned i = 0; i < values.size(); ++i) {
values[i][i] = value;
}
}
//---------------------------------------------------------------------------
// Transformations
//---------------------------------------------------------------------------
void Matrix::Transpose ()
{
matrix_values newvalues = matrix_values(cols(), vector<double>(rows(), 0));
for (unsigned i = 0; i < cols(); ++i) {
for (unsigned j = 0; j < rows(); ++j) {
newvalues[i][j] = values[j][i];
}
}
values = newvalues;
}
//---------------------------------------------------------------------------
// Operators overloading
//---------------------------------------------------------------------------
Matrix& Matrix::operator=(const Matrix& right)
{
if (&right != this)
{
values = right.values;
}
return *this;
}
//---------------------------------------------------------------------------
Matrix operator+ (const Matrix& left, const Matrix& right)
{
Matrix result(left);
return result += right;
}
//---------------------------------------------------------------------------
Matrix& Matrix::operator+=(const Matrix& right)
{
if (rows() == right.rows() && cols() == right.cols())
{
for (unsigned i = 0; i < rows(); ++i)
for (unsigned j = 0; j < cols(); ++j)
values[i][j] += right.values[i][j];
}
return *this;
}
//---------------------------------------------------------------------------
Matrix operator* (const Matrix& left, const Matrix& right)
{
Matrix result(left);
return result *= right;
}
//---------------------------------------------------------------------------
Matrix& Matrix::operator*=(const Matrix& right)
{
if (cols() == right.rows())
{
Matrix temp(rows(), right.cols());
for (unsigned i = 0; i < rows(); ++i)
for (unsigned j = 0; j < right.cols(); ++j)
for (unsigned k = 0; k < right.rows(); ++k)
temp.values[i][j] += values[i][k] * right.values[k][j];
values = temp.values;
}
return *this;
}
//---------------------------------------------------------------------------
Matrix operator* (const Matrix& left, const double right)
{
Matrix result(left);
return (result *= right);
}
//---------------------------------------------------------------------------
Matrix operator* (const double left, const Matrix& right)
{
Matrix result(right);
return (result *= left);
}
//---------------------------------------------------------------------------
Matrix& Matrix::operator*= (const double right)
{
for (mvIt i = values.begin(); i != values.end(); ++i)
for (dIt j = (*i).begin(); j != (*i).end(); ++j)
*j *= right;
return *this;
}
//---------------------------------------------------------------------------
Matrix operator- (const Matrix& left, const Matrix& right)
{
Matrix result(left);
return result -= right;
}
//---------------------------------------------------------------------------
Matrix& Matrix::operator-=(const Matrix& right)
{
return *this += (-1)*right;
}
//---------------------------------------------------------------------------
Matrix operator^ (const Matrix& left, const double right)
{
Matrix result(left);
return (result ^= right);
}
//---------------------------------------------------------------------------
Matrix& Matrix::operator^= (const double right)
{
for (mvIt i = values.begin(); i != values.end(); ++i)
for (dIt j = (*i).begin(); j != (*i).end(); ++j)
*j = pow(*j, right);
return *this;
}
//---------------------------------------------------------------------------