scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_UTIL_MATRIX_HPP_INCLUDED
2#define SCIFIR_UNITS_UTIL_MATRIX_HPP_INCLUDED
3
4#include "../units/scalar_unit.hpp"
5
6#include <iostream>
7#include <sstream>
8#include <vector>
9
10using namespace std;
11
12namespace scifir
13{
14 template<typename T, int M, int N>
15 class matrix
16 {
17 public:
19 {}
20
22 {
23 if(x.size() != M)
24 {
25 return;
26 }
27 for(const auto& column : x)
28 {
29 if(column.size() != N)
30 {
31 return;
32 }
33 }
34 int i = 0;
35 for(const auto& column : x)
36 {
37 for(int j = 0; j < N; j++)
38 {
39 data[i][j] = column[j];
40 }
41 i++;
42 }
43 }
44
45 virtual matrix<T, M, N>* clone() const
46 {
47 return new matrix<T, M, N>(static_cast<const matrix<T, M, N>&>(*this));
48 }
49
50 inline int row_size() const
51 {
52 return M;
53 }
54
55 inline int column_size() const
56 {
57 return N;
58 }
59
60 matrix<T,1,N> row(int i) const
61 {
63 {
64 return matrix<T,1,N>();
65 }
67 for (int j = 0; j < N; j++)
68 {
69 x(1,j + 1) = data[i - 1][j];
70 }
71 return std::move(x);
72 }
73
75 {
77 {
78 return matrix<T,M,1>();
79 }
81 for (int i = 0; i < M; i++)
82 {
83 x(i + 1,1) = data[i][j - 1];
84 }
85 return std::move(x);
86 }
87
88 const T& operator()(int i, int j) const
89 {
90 if (outside_limits(i,j))
91 {
92 return T();
93 }
94 return data[i - 1][j - 1];
95 }
96
97 T& operator()(int i, int j)
98 {
99 if (outside_limits(i,j))
100 {
101 return T();
102 }
103 return data[i - 1][j - 1];
104 }
105
107 {
108 matrix<T, M, N> y = *clone();
109 y += x;
110 return std::move(y);
111 }
112
114 {
115 matrix<T, M, N> y = *clone();
116 y -= x;
117 return std::move(y);
118 }
119
120 template<int N2>
122 {
124 for(int i = 0; i < M; i++)
125 {
126 for(int j = 0; j < N2; j++)
127 {
128 T a = 0;
129 for(int k = 0; k < N; k++)
130 {
131 a += data[i][k] * x(k + 1,j + 1);
132 }
133 new_matrix(i + 1,j + 1) = a;
134 }
135 }
136 return move(new_matrix);
137 }
138
141 {
142 matrix<T, M, N> y = *clone();
143 y ^= x;
144 return move(y);
145 }
146
148 {
149 for(int i = 0; i < M; i++)
150 {
151 for(int j = 0; j < N; j++)
152 {
153 data[i][j] += x(i + 1,j + 1);
154 }
155 }
156 }
157
159 {
160 for(int i = 0; i < M; i++)
161 {
162 for(int j = 0; j < N; j++)
163 {
164 data[i][j] -= x(i + 1,j + 1);
165 }
166 }
167 }
168
171 {
173 for(int i = 2; i <= x; i++)
174 {
175 new_data = (new_data * (*this));
176 }
177 for (int i = 0; i < M; i++)
178 {
179 for (int j = 0; j < N; j++)
180 {
181 data[i][j] = new_data(i + 1,j + 1);
182 }
183 }
184 }
185
187 {
189 for(int i = 0; i < M; i++)
190 {
191 for(int j = 0; j < N; j++)
192 {
193 new_data(j + 1,i + 1) = data[i][j];
194 }
195 }
196 return move(new_data);
197 }
198
199 bool is_square() const
200 {
201 if(M == N)
202 {
203 return true;
204 }
205 else
206 {
207 return false;
208 }
209 }
210
211 bool is_diagonal() const
212 {
213 for(int i = 0; i < M; i++)
214 {
215 for(int j = 0; j < N; j++)
216 {
217 if(i != j and data[i][j] != 0)
218 {
219 return false;
220 }
221 }
222 }
223 return true;
224 }
225
226 private:
228
229 bool outside_row_limits(int i) const
230 {
231 if (i < 1 or i > M)
232 {
233 return true;
234 }
235 return false;
236 }
237
238 bool outside_column_limits(int i) const
239 {
240 if (i < 1 or i > N)
241 {
242 return true;
243 }
244 return false;
245 }
246
247 bool outside_limits(int i,int j) const
248 {
250 {
251 return true;
252 }
253 return false;
254 }
255 };
256}
257
258template<typename T, int M, int N>
260{
261 for(int i = 0; i < x.row_size(); i++)
262 {
263 for(int j = 0; j < x.column_size(); j++)
264 {
265 if (x(i,j) != y(i,j))
266 {
267 return false;
268 }
269 }
270 }
271 return true;
272}
273
274template<typename T, int M, int N>
276{
277 return !(x == y);
278}
279
280template<typename T, int M, int N>
281ostream& operator <<(ostream& os, const scifir::matrix<T, M, N>& x)
282{
283 ostringstream output;
284 output << "[";
285 for(int i = 1; i <= x.row_size(); i++)
286 {
287 for(int j = 1; j <= x.column_size(); j++)
288 {
289 output << x(i, j);
290 if(j < x.column_size())
291 {
292 output << ", ";
293 }
294 }
295 if(i < x.row_size())
296 {
297 output << endl;
298 }
299 }
300 output << "]";
301 return os << output.str();
302}
303
304#endif // SCIFIR_UNITS_UTIL_MATRIX_HPP_INCLUDED
bool is_diagonal() const
Definition matrix.hpp:211
matrix< T, M, N > operator^(U x)
Definition matrix.hpp:140
virtual matrix< T, M, N > * clone() const
Definition matrix.hpp:45
int column_size() const
Definition matrix.hpp:55
bool is_square() const
Definition matrix.hpp:199
matrix< T, M, N > operator+(const matrix< T, M, N > &x)
Definition matrix.hpp:106
bool outside_column_limits(int i) const
Definition matrix.hpp:238
matrix(initializer_list< vector< T > > x)
Definition matrix.hpp:21
bool outside_limits(int i, int j) const
Definition matrix.hpp:247
void operator-=(const matrix< T, M, N > &x)
Definition matrix.hpp:158
matrix< T, M, 1 > column(int j) const
Definition matrix.hpp:74
int row_size() const
Definition matrix.hpp:50
matrix< T, M, N > operator-(const matrix< T, M, N > &x)
Definition matrix.hpp:113
bool outside_row_limits(int i) const
Definition matrix.hpp:229
matrix< T, N, N2 > operator*(const matrix< T, N, N2 > &x)
Definition matrix.hpp:121
T & operator()(int i, int j)
Definition matrix.hpp:97
const T & operator()(int i, int j) const
Definition matrix.hpp:88
matrix< T, N, M > t() const
Definition matrix.hpp:186
void operator^=(U x)
Definition matrix.hpp:170
matrix< T, 1, N > row(int i) const
Definition matrix.hpp:60
vector< vector< T > > data
Definition matrix.hpp:227
void operator+=(const matrix< T, M, N > &x)
Definition matrix.hpp:147
bool operator!=(const scifir::matrix< T, M, N > &x, const scifir::matrix< T, M, N > &y)
Definition matrix.hpp:275
bool operator==(const scifir::matrix< T, M, N > &x, const scifir::matrix< T, M, N > &y)
Definition matrix.hpp:259
ostream & operator<<(ostream &os, const scifir::matrix< T, M, N > &x)
Definition matrix.hpp:281
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6