Feellgood
algebra.h
Go to the documentation of this file.
1 #ifndef ALGEBRA_H
2 #define ALGEBRA_H
3 
12 #include <iostream>
13 #include <cmath> // sqrt,fabs
14 #include <algorithm>
15 
16 
17 #include "sparseMat.h"
18 
19 #include "algebraCore.h"
20 #include "iter.h"
21 
26 namespace algebra
27 {
29 template <typename T>
30 T sq(const T x) { return x * x; }
31 
33 template <typename T>
34 void scaled( const T alpha, std::vector<T> & Y)
35  { std::for_each(Y.begin(),Y.end(),[alpha](T &_x){ _x *= alpha; }); }
36 
38 template <typename T>
39 void p_direct(const std::vector<T> & X,const std::vector<T> & Y, std::vector<T> & Z)
40  { std::transform(X.begin(),X.end(),Y.begin(),Z.begin(), std::multiplies<T>() ); }
41 
43 template <typename T>
44 void add(const std::vector<T> & X, std::vector<T> & Y)
45  {
46  for (size_t i = 0; i < Y.size(); ++i)
47  Y[i] += X[i];
48  }
49 
51 template <typename T>
52 void sub(const std::vector<T> & X, std::vector<T> & Y)
53  {
54  for (size_t i = 0; i < Y.size(); ++i)
55  Y[i] -= X[i];
56  }
57 
59 template <typename T>
60 void scaled_add(const std::vector<T> & X,const T alpha, std::vector<T> & Y)
61  {
62  for (size_t i = 0; i < Y.size(); ++i)
63  Y[i] += alpha * X[i];
64  }
65 
67 template <typename T>
68 void mult(r_sparseMat & A, std::vector<T> const& X, std::vector<T> &Y)
69  {
70  A.mult(X,Y); // Y = A*X
71  }
72 
74 template <typename T>
75 void applyMask(const std::vector<int>& mask, std::vector<T> & X)
76  { std::for_each(mask.begin(),mask.end(),[&X](const int _i){ X[_i] = (T)(0); }); }
77 
79 inline std::ostream & operator<<(std::ostream & flux, r_sparseMat const& m)
80  {m.print(flux); return flux;}
81 
83 template <typename T>
84 bool check(std::vector<T> &v)
85  { return std::none_of(v.begin(),v.end(), [](T x){ return std::isnan(x);} ); }
86 } // end namespace algebra
87 
88 #endif
89 
Read-mode square sparse matrix.
Definition: sparseMat.h:87
void print(std::ostream &flux=std::cout) const
Definition: sparseMat.h:167
iteration class from GMM, with some adaptations and simplifications.
constexpr double v[NPI]
Definition: facette.h:22
constexpr double A
Definition: tetra.h:29
void scaled_add(const std::vector< T > &X, const T alpha, std::vector< T > &Y)
Definition: algebra.h:60
void p_direct(const std::vector< T > &X, const std::vector< T > &Y, std::vector< T > &Z)
Definition: algebra.h:39
void applyMask(const std::vector< int > &mask, std::vector< T > &X)
Definition: algebra.h:75
void add(const std::vector< T > &X, std::vector< T > &Y)
Definition: algebra.h:44
void sub(const std::vector< T > &X, std::vector< T > &Y)
Definition: algebra.h:52
bool check(std::vector< T > &v)
Definition: algebra.h:84
std::ostream & operator<<(std::ostream &flux, r_sparseMat const &m)
Definition: algebra.h:79
void mult(r_sparseMat &A, std::vector< T > const &X, std::vector< T > &Y)
Definition: algebra.h:68
void scaled(const T alpha, std::vector< T > &Y)
Definition: algebra.h:34
T sq(const T x)
Definition: algebra.h:30
Sparse matrices.