-
Notifications
You must be signed in to change notification settings - Fork 7
/
matrix.hpp
184 lines (153 loc) · 3.89 KB
/
matrix.hpp
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
#ifndef matrix_hpp
#define matrix_hpp
#include <algorithm>
#include <complex>
#include "common.hpp"
// fwd declaration: in order to use enable_if
template<typename, typename = void> struct matrix;
// use matrices only for types that are arithmetic (i.e number types)
template<typename T>
struct matrix<T,
typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::complex<T>>::value>::type>
{
typedef unsigned long size_type;
matrix(size_type N, size_type M)
: vec(N * M),
rows(N),
columns(M)
{
}
matrix(size_type N, size_type M, T filler)
: vec(N * M),
rows(N),
columns(M)
{
std::fill(vec.begin(), vec.end(), filler);
}
matrix(const matrix& other)
: vec(other.vec),
rows(other.rows),
columns(other.columns)
{
}
matrix(size_type N, size_type M, std::initializer_list<T> lst)
: vec(N * M),
rows(N),
columns(M)
{
// only copy if they're the same size as the vector
if (lst.size() == N * M) {
std::copy(lst.begin(), lst.end(), vec.begin());
}
}
matrix(size_type N, size_type M, container<T> vector)
: vec(N * M),
rows(N),
columns(M)
{
if (vector.size() == N * M) {
std::copy(vector.begin(), vector.end(), vec.begin());
}
}
matrix& operator=(const matrix& other)
{
vec = other.vec;
rows = other.rows;
columns = other.columns;
return *this;
}
T& operator()(size_type i, size_type j)
{
return vec[i * columns + j];
}
const T& operator()(size_type i, size_type j) const
{
return vec[i * columns + j];
}
size_type rowCount() const
{
return rows;
}
size_type colCount() const
{
return columns;
}
// data interface: return internal data
container<T> data() const
{
return vec;
}
/*
* Extract the columns from this matrix into a
* vector of vectors so that they can be used in other
* routines (such as orthogonalization).
*/
container<container<T>> columnCollection() const
{
container<container<T>> result;
for (size_type i = 0; i < columns; ++i)
{
result.push_back(getColumn(i));
}
return result;
}
/*
* Extract the rows from this matrix into a vector of
* vectors so that they can be used in other routines
* (such as orthogonalization).
*/
container<container<T>> rowCollection() const
{
container<container<T>> result;
for (size_type i = 0; i < rows; ++i)
{
result.push_back(getRow(i));
}
return result;
}
private:
container<T> vec;
size_type rows;
size_type columns;
container<T> getRow(size_type rowIndex) const
{
container<T> result;
for (size_type i = 0; i < columns; ++i)
{
result.push_back(this->operator()(rowIndex, i));
}
return result;
}
container<T> getColumn(size_type columnIndex) const
{
container<T> result;
for (size_type i = 0; i < rows; ++i)
{
result.push_back(this->operator()(i, columnIndex));
}
return result;
}
};
template<typename T>
std::ostream& operator<<(std::ostream& stream, const matrix<T>& mat)
{
using size_type = typename matrix<T>::size_type;
stream << "[";
for (size_type i = 0; i < mat.rowCount(); ++i)
{
for (size_type j = 0; j < mat.colCount(); ++j)
{
if (j != mat.colCount() - 1) {
stream << mat(i, j) << ", ";
} else {
stream << mat(i, j);
}
}
if (i != mat.rowCount() - 1) {
stream << ";" << std::endl;
}
}
stream << "]";
return stream;
}
#endif /* matrix_hpp */