chemmisol 0.1
Loading...
Searching...
No Matches
linear.h
Go to the documentation of this file.
1#ifndef CHEMMISOL_LINEAR_H
2#define CHEMMISOL_LINEAR_H
3
4#include <array>
5#include <vector>
6#include <initializer_list>
7#include <iostream>
8#include <cmath>
9#include <complex>
10#include "../logging.h"
11
17namespace chemmisol {
18 /*
19 * Fixed size array linear algebra
20 */
21
25 template<typename T, std::size_t N>
26 using X = std::array<T, N>;
27
33 template<typename T, std::size_t N, std::size_t P = N>
34 using M = std::array<std::array<T, P>, N>;
35
39 template<typename T>
40 using VecX = std::vector<T>;
44 template<typename T>
45 using VecM = std::vector<std::vector<T>>;
46
54 template<typename T, std::size_t N, std::size_t P>
55 X<T, N> operator*(const M<T, N, P>& m, const X<T, P>& x) {
56 X<T, N> x1;
57 for(std::size_t i = 0; i < N; i++) {
58 x1[i] = 0;
59 for(std::size_t j = 0; j < P; j++) {
60 x1[i] += m[i][j] * x[j];
61 }
62 }
63 return x1;
64 }
65
72 template<typename T, std::size_t N>
74 X<T, N> x1;
75 for(std::size_t i = 0; i < N; i++)
76 x1[i] = -x[i];
77 return x1;
78 }
79
87 template<typename T, std::size_t N>
88 X<T, N> operator+(const X<T, N>& x1, const X<T, N>& x2) {
89 X<T, N> x3;
90 for(std::size_t i = 0; i < N; i++)
91 x3[i] = x1[i]+x2[i];
92 return x3;
93 }
94
95 template<typename T, std::size_t N>
96 X<T, N> operator*(const T& a, const X<T, N>& x) {
97 X<T, N> result;
98 for(std::size_t i = 0; i < N; i++)
99 result[i] = a*x[i];
100 return result;
101 }
102
103 template<typename T, std::size_t N, std::size_t P>
104 M<T, N, P> operator*(const T& a, const M<T, N, P>& m) {
105 M<T, N, P> result;
106 for(std::size_t i = 0; i < N; i++) {
107 for(std::size_t j = 0; j < P; j++) {
108 result[i][j] = a * m[i][j];
109 }
110 }
111 return result;
112 }
116 template<typename T, std::size_t N>
117 double norm(const X<T, N>& x) {
118 T a = 0;
119 for(auto& v : x) {
120 a+=std::pow(v, 2);
121 }
122 return std::sqrt(a);
123 }
124
125 template<typename T, std::size_t N>
126 double norm(const X<std::complex<T>, N>& x) {
127 T a = 0;
128 for(auto& v : x) {
129 a+=std::pow(std::norm(v), 2);
130 }
131 return std::sqrt(a);
132 }
133
137 template<typename T, std::size_t N>
138 X<T, N> abs(const X<T, N>& x) {
139 X<T, N> abs_x;
140 for(std::size_t i = 0; i < N; i++)
141 abs_x[i] = std::abs(x[i]);
142 return abs_x;
143 }
144
159 template<typename _M>
160 M<
161 typename _M::value_type::value_type,
162 std::tuple_size<_M>::value,
163 std::tuple_size<typename _M::value_type>::value+1>
165 const _M& m,
166 const X<typename _M::value_type::value_type, std::tuple_size<_M>::value>& x
167 ) {
168 M<
169 typename _M::value_type::value_type,
170 std::tuple_size<_M>::value,
171 std::tuple_size<typename _M::value_type>::value+1> a;
172 for(std::size_t i = 0; i < std::tuple_size<_M>::value; i++) {
173 for(std::size_t j = 0; j < std::tuple_size<typename _M::value_type>::value; j++) {
174 a[i][j] = m[i][j];
175 }
176 a[i][std::tuple_size<_M>::value] = x[i];
177 }
178 return a;
179 };
180
186 template<typename T, std::size_t N>
187 std::ostream& operator<<(std::ostream& o, const X<T, N>& x) {
188 o << "[";
189 for(std::size_t i = 0; i < N-1; i++)
190 o << x[i] << ", ";
191 if(N > 0)
192 o << x[N-1];
193 o << "]";
194 return o;
195 }
196
197 /*
198 * Vector linear algebra
199 */
200
208 template<typename T>
209 VecX<T> operator*(const VecM<T>& m, const VecX<T>& x) {
210 VecX<T> x1(x.size());
211 for(std::size_t i = 0; i < x.size(); i++) {
212 x1[i] = 0;
213 for(std::size_t j = 0; j < m[i].size(); j++) {
214 x1[i] += m[i][j] * x[j];
215 }
216 }
217 return x1;
218 }
219
226 template<typename T>
228 VecX<T> x1(x.size());
229 for(std::size_t i = 0; i < x.size(); i++)
230 x1[i] = -x[i];
231 return x1;
232 }
233
243 template<typename T>
244 VecX<T> operator+(const VecX<T>& x1, const VecX<T>& x2) {
245 VecX<T> x3(x1.size());
246 for(std::size_t i = 0; i < x1.size(); i++)
247 x3[i] = x1[i]+x2[i];
248 return x3;
249 }
250
254 template<typename T>
255 double norm(const VecX<T>& x) {
256 double a = 0;
257 for(auto& v : x) {
258 a+=std::pow(v, 2);
259 }
260 return std::sqrt(a);
261 }
262
266 template<typename T>
267 VecX<T> abs(const VecX<T>& x) {
268 VecX<T> abs_x(x);
269 for(std::size_t i = 0; i < x.size(); i++)
270 abs_x[i] = std::abs(x[i]);
271 return abs_x;
272 }
273
289 template<typename T>
290 VecM<T>
291 augment(const VecM<T>& m, const VecX<T>& x) {
292 VecM<T> a(m.size());
293 for(std::size_t i = 0; i < m.size(); i++) {
294 a[i].resize(m[i].size()+1);
295 for(std::size_t j = 0; j < m[i].size(); j++) {
296 a[i][j] = m[i][j];
297 }
298 a[i][m[i].size()] = x[i];
299 }
300 return a;
301 };
302
308 template<typename T>
309 std::ostream& operator<<(std::ostream& o, const VecX<T>& x) {
310 o << "[";
311 for(std::size_t i = 0; i < x.size()-1; i++)
312 o << x[i] << ", ";
313 if(x.size() > 0)
314 o << x[x.size()-1];
315 o << "]";
316 return o;
317 }
318
319 /*
320 * Scalar values algebra.
321 */
322
326 template<typename T>
327 double norm(const T& x) {
328 return std::abs(x);
329 }
330
334 template<typename T>
335 T abs(const T& x) {
336 return std::abs(x);
337 }
338}
339#endif
Definition chemmisol.h:31
X< T, N > operator+(const X< T, N > &x1, const X< T, N > &x2)
Definition linear.h:88
std::vector< T > VecX
Definition linear.h:40
double norm(const X< T, N > &x)
Definition linear.h:117
std::array< T, N > X
Definition linear.h:26
std::ostream & operator<<(std::ostream &o, const Phase &phase)
M< typename _M::value_type::value_type, std::tuple_size< _M >::value, std::tuple_size< typename _M::value_type >::value+1 > augment(const _M &m, const X< typename _M::value_type::value_type, std::tuple_size< _M >::value > &x)
Definition linear.h:164
std::vector< std::vector< T > > VecM
Definition linear.h:45
std::array< std::array< T, P >, N > M
Definition linear.h:34
X< T, N > abs(const X< T, N > &x)
Definition linear.h:138
X< T, N > operator-(const X< T, N > &x)
Definition linear.h:73
X< T, N > operator*(const M< T, N, P > &m, const X< T, P > &x)
Definition linear.h:55