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 
27 namespace algebra
28 {
30 template <typename T>
31 T sq(const T x) { return x * x; }
32 
34 template <typename T>
35 void scaled( const T alpha, std::vector<T> & Y)
36  { std::for_each(Y.begin(),Y.end(),[alpha](T &_x){ _x *= alpha; }); }
37 
39 template <typename T>
40 void p_direct(const std::vector<T> & X,const std::vector<T> & Y, std::vector<T> & Z)
41  { std::transform(X.begin(),X.end(),Y.begin(),Z.begin(), std::multiplies<T>() ); }
42 
44 template <typename T>
45 void add(const std::vector<T> & X, std::vector<T> & Y)
46  {
47  for (size_t i = 0; i < Y.size(); ++i)
48  Y[i] += X[i];
49  }
50 
52 template <typename T>
53 void sub(const std::vector<T> & X, std::vector<T> & Y)
54  {
55  for (size_t i = 0; i < Y.size(); ++i)
56  Y[i] -= X[i];
57  }
58 
60 template <typename T>
61 void scaled_add(const std::vector<T> & X,const T alpha, std::vector<T> & Y)
62  {
63  for (size_t i = 0; i < Y.size(); ++i)
64  Y[i] += alpha * X[i];
65  }
66 
68 template <typename T>
69 void mult(SparseMatrix & A, std::vector<T> const& X, std::vector<T> &Y)
70  {
71  A.mult(X,Y); // Y = A*X
72  }
73 
75 template <typename T>
76 void applyMask(const std::vector<int>& mask, std::vector<T> & X)
77  { std::for_each(mask.begin(),mask.end(),[&X](const int _i){ X[_i] = (T)(0); }); }
78 
80 inline std::ostream & operator<<(std::ostream & flux, SparseMatrix const& m)
81  {m.print(flux); return flux;}
82 
84 template <typename T>
85 bool check(std::vector<T> &v)
86  { return std::none_of(v.begin(),v.end(), [](T x){ return std::isnan(x);} ); }
87 } // end namespace algebra
88 
89 #endif
90 
Square sparse matrix.
Definition: sparseMat.h:46
void print(std::ostream &flux=std::cout) const
Definition: sparseMat.h:127
iteration class from GMM, with some adaptations and simplifications.
constexpr double v[NPI]
Definition: facette.h:49
constexpr double A
Definition: tetra.h:52
void scaled_add(const std::vector< T > &X, const T alpha, std::vector< T > &Y)
Definition: algebra.h:61
void p_direct(const std::vector< T > &X, const std::vector< T > &Y, std::vector< T > &Z)
Definition: algebra.h:40
void applyMask(const std::vector< int > &mask, std::vector< T > &X)
Definition: algebra.h:76
void add(const std::vector< T > &X, std::vector< T > &Y)
Definition: algebra.h:45
void sub(const std::vector< T > &X, std::vector< T > &Y)
Definition: algebra.h:53
std::ostream & operator<<(std::ostream &flux, SparseMatrix const &m)
Definition: algebra.h:80
bool check(std::vector< T > &v)
Definition: algebra.h:85
void mult(SparseMatrix &A, std::vector< T > const &X, std::vector< T > &Y)
Definition: algebra.h:69
void scaled(const T alpha, std::vector< T > &Y)
Definition: algebra.h:35
T sq(const T x)
Definition: algebra.h:31
Sparse matrices.