KADATH
tensor_impl.hpp
1 //
2 // Created by sauliac on 18/04/2020.
3 //
4 
5 #include "headcpp.hpp"
6 #include "tensor.hpp"
7 #include "scalar.hpp"
8 #include "vector.hpp"
9 #include "metric_tensor.hpp"
10 
11 #ifndef __TENSOR_IMPL_HPP_
12 #define __TENSOR_IMPL_HPP_
13 namespace Kadath {
14 // int std_position_array (const Array<int>& , int );
15 // int std_position_index (const Index& , int );
16 // Array<int> std_indices (int , int , int );
17 
19  // INLINED TENSOR METHODS //
21 
22 
23  inline void Tensor::annule_hard() {
24  for (int i=0 ; i<n_comp ; i++)
25  cmp[i]->annule_hard() ;
26  }
27 
28  //-------------
29  // Accessors
30  //-------------
31 
32  // Affectation d'un tenseur d'ordre 1 :
33  inline Scalar& Tensor::set(int i) {
34 
35  assert (valence == 1) ;
36 
37  Array<int> ind (valence) ;
38  ind.set(0) = i ;
39 
40  int place = position(ind) ;
41 
42  return *cmp[place] ;
43  }
44 
45  // Affectation d'un tenseur d'ordre 2 :
46  inline Scalar& Tensor::set(int ind1, int ind2) {
47 
48  assert (valence == 2) ;
49 
50  Array<int> ind (valence) ;
51  ind.set(0) = ind1 ;
52  ind.set(1) = ind2 ;
53 
54  int place = position(ind) ;
55 
56  return *cmp[place] ;
57  }
58 
59  // Affectation d'un tenseur d'ordre 3 :
60  inline Scalar& Tensor::set(int ind1, int ind2, int ind3) {
61 
62  assert (valence == 3) ;
63 
64  Array<int> idx(valence) ;
65  idx.set(0) = ind1 ;
66  idx.set(1) = ind2 ;
67  idx.set(2) = ind3 ;
68  int place = position(idx) ;
69 
70  return *cmp[place] ;
71  }
72 
73 
74  // Affectation d'un tenseur d'ordre 4 :
75  inline Scalar& Tensor::set(int ind1, int ind2, int ind3, int ind4) {
76 
77  assert (valence == 4) ;
78 
79  Array<int> idx(valence) ;
80  idx.set(0) = ind1 ;
81  idx.set(1) = ind2 ;
82  idx.set(2) = ind3 ;
83  idx.set(3) = ind4 ;
84  int place = position(idx) ;
85 
86  return *cmp[place] ;
87  }
88 
89 
90  // Affectation cas general
91  inline Scalar& Tensor::set(const Array<int>& idx) {
92 
93  assert (idx.get_ndim() == 1) ;
94  assert (idx.get_size(0) == valence) ;
95 
96  int place = position(idx) ;
97  return *cmp[place] ;
98  }
99 
100  // Affectation cas general from an Index
101  inline Scalar& Tensor::set(const Index& idx) {
102 
103  Array<int> ind (valence) ;
104  for (int i=0 ; i<valence ; i++)
105  ind.set(i) = idx(i)+1 ;
106 
107  return set(ind) ;
108  }
109 
110  inline const Scalar& Tensor::operator()() const {
111 
112  assert(valence == 0) ;
113 
114  return *cmp[0] ;
115 
116  }
117 
118  inline const Scalar& Tensor::operator()(int indice) const {
119 
120  assert(valence == 1) ;
121 
122  Array<int> idx(1) ;
123  idx.set(0) = indice ;
124  return *cmp[position(idx)] ;
125 
126  }
127 
128  inline const Scalar& Tensor::operator()(int indice1, int indice2) const {
129 
130  assert(valence == 2) ;
131 
132  Array<int> idx(2) ;
133  idx.set(0) = indice1 ;
134  idx.set(1) = indice2 ;
135  return *cmp[position(idx)] ;
136 
137  }
138 
139  inline const Scalar& Tensor::operator()(int indice1, int indice2, int indice3) const {
140 
141  assert(valence == 3) ;
142 
143  Array<int> idx(3) ;
144  idx.set(0) = indice1 ;
145  idx.set(1) = indice2 ;
146  idx.set(2) = indice3 ;
147  return *cmp[position(idx)] ;
148  }
149 
150 
151  inline const Scalar& Tensor::operator()(int indice1, int indice2, int indice3,
152  int indice4) const {
153 
154  assert(valence == 4) ;
155 
156  Array<int> idx(4) ;
157  idx.set(0) = indice1 ;
158  idx.set(1) = indice2 ;
159  idx.set(2) = indice3 ;
160  idx.set(3) = indice4 ;
161  return *cmp[position(idx)] ;
162  }
163 
164  inline const Scalar& Tensor::at(int indice1, int indice2) const {
165  return operator()(indice1,indice2);
166 
167  }
168 
169  inline const Scalar& Tensor::operator()(const Array<int>& ind) const {
170 
171  assert (ind.get_ndim() == 1) ;
172  assert (ind.get_size(0) == valence) ;
173  return *cmp[position(ind)] ;
174 
175  }
176 
177  inline const Scalar& Tensor::operator()(const Index& idx) const {
178  Array<int> ind (valence) ;
179  for (int i=0 ; i<valence ; i++)
180  ind.set(i) = idx(i)+1 ;
181 
182  return operator()(ind) ;
183 
184  }
185 
186  inline void Tensor::set_name_ind (int pos, char name) {
187  assert((pos>=0) && (pos<valence)) ;
188  if (!name_affected)
189  name_affected = true ;
190  name_indice[pos] = name ;
191  }
192 
193  inline void Tensor::coef() const
194  {
195  Index pos(*this);
196  do
197  {
198  (*this)(pos).coef();
199  }while(pos.inc());
200  }
201 
202  inline void Tensor::coef_i() const
203  {
204  Index pos(*this);
205  do
206  {
207  (*this)(pos).coef_i();
208  }while(pos.inc());
209  }
210 
211  inline void Tensor::filter_phi(int dom, int ncf)
212  {
213  Index pos(*this);
214  do
215  {
216  set(pos).filter_phi(dom, ncf);
217  }while(pos.inc());
218  }
219 
220 
221  inline void Tensor::filter (double threshold) {
222 
223  for (int d=0 ; d<espace.get_nbr_domains() ; d++) {
224  espace.get_domain(d)->filter(*this, d, threshold) ;
225  }
226  }
227 
228 
229  inline void Vector::annule_hard() {
230  for (int i=0 ; i<3 ; i++)
231  cmp[i]->annule_hard() ;
232  }
233 
234 
236  // INLINED VECTOR METHODS //
238 
239  inline Scalar& Vector::set(int index) {
240  assert ( (index>=1) && (index<=3) ) ;
241  return *cmp[index - 1] ;
242  }
243 
244  inline const Scalar& Vector::operator()(int index) const {
245  assert ((index>=1) && (index<=3)) ;
246  return *cmp[index - 1] ;
247  }
248 
249  inline const Scalar& Vector::at(int index) const {
250  return operator()(index);
251  }
252 }
253 #endif //__TENSOR_IMPL_HPP_
reference set(const Index &pos)
Read/write of an element.
Definition: array.hpp:186
int get_ndim() const
Returns the number of dimensions.
Definition: array.hpp:323
int get_size(int i) const
Returns the size of a given dimension.
Definition: array.hpp:331
virtual void filter(Tensor &tt, int dom, double treshold) const
Puts to zero all the coefficients below a given treshold.
Definition: domain.cpp:1759
Class that gives the position inside a multi-dimensional Array.
Definition: index.hpp:38
bool inc(int increm, int var=0)
Increments the position of the Index.
Definition: index.hpp:99
The class Scalar does not really implements scalars in the mathematical sense but rather tensorial co...
Definition: scalar.hpp:67
void filter_phi(int dom, int ncf)
Sets to zero all the coefficients above a given order, for the coefficients, in a gicen Domain.
Definition: scalar.cpp:130
const Domain * get_domain(int i) const
returns a pointer on the domain.
Definition: space.hpp:1385
int get_nbr_domains() const
Returns the number of Domains.
Definition: space.hpp:1375
const Scalar & operator()() const
Read only for a Scalar.
bool name_affected
Indicator that states if the indices have been given names.
Definition: tensor.hpp:172
Memory_mapped_array< char > name_indice
If the indices haves names they are stored here.
Definition: tensor.hpp:176
void set_name_ind(int dd, char name)
Sets the name of one index ; the names must have been affected first.
int valence
Valence of the tensor (0 = scalar, 1 = vector, etc...)
Definition: tensor.hpp:157
void coef() const
Computes the coefficients.
void filter_phi(int dom, int ncf)
Sets to zero all the coefficients above a given order, for the coefficients, in a gicen Domain.
void coef_i() const
Computes the values in the configuration space.
Memory_mapped_array< Scalar * > cmp
Array of size n_comp of pointers onto the components.
Definition: tensor.hpp:179
void filter(double tre)
Sets all the coefficients below a given treshold, to zero (maintaining regularity).
virtual void annule_hard()
Sets the Tensor to zero (hard version ; no logical state used).
Definition: tensor_impl.hpp:23
Scalar & set()
Read/write for a Scalar.
Definition: tensor.hpp:364
int n_comp
Number of stored components, depending on the symmetry.
Definition: tensor.hpp:178
const Space & espace
The Space.
Definition: tensor.hpp:154
const Scalar & at(int i1, int i2) const
Sylvain' stuff.
virtual int position(const Array< int > &idx) const
Gives the location of a given component in the array used for storage (Array version).
Definition: tensor.hpp:470
const Scalar & at(int) const
Readonly access to a component.
Scalar & set(const Array< int > &ind)
Returns the value of a component (read/write version).
Definition: tensor_impl.hpp:91
void annule_hard() override
Sets the Tensor to zero (hard version ; no logical state used).