Documentation of CSL
tensorField.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef TENSORFIELD_H_INCLUDED
24 #define TENSORFIELD_H_INCLUDED
25 
26 #include "field.h"
27 #include "parent.h"
28 #include "indicial.h"
29 
30 namespace csl {
31 
32  class TensorField;
33  class TDerivative;
34 
36 
37  public:
38 
40 
41  explicit TensorFieldParent(const std::string& t_name);
42 
43  TensorFieldParent(const TensorFieldParent& field) = default;
44 
45  TensorFieldParent(const std::string& t_name,
46  const Space* t_spaceField);
47 
48  TensorFieldParent(const std::string& t_name,
49  const Space* t_spaceField,
50  const std::vector<const Space*>& spaces);
51 
52  template<class ...Args>
53  friend
54  TensorField tensorfield_s(Args&& ...args);
55 
56  template<class ...Args>
57  friend
58  TensorField tensorfield_s(
59  std::string const& name,
60  csl::Space const* spaceField,
61  std::vector<const Space*> const& indices,
62  Args&& ...args);
63 
64  ~TensorFieldParent(){};
65 
66  std::vector<Parent> breakSpace(
67  const Space* broken,
68  const std::vector<const Space*>& newSpace,
69  const std::vector<size_t>& pieces
70  ) const override;
71 
72  cslParent::PrimaryType getPrimaryType() const override;
73 
74  cslParent::Type getType() const override;
75 
76  void printDefinition(
77  std::ostream &out = std::cout,
78  int indentSize = 4,
79  bool header = false
80  ) const override;
81 
82  const Space* getFieldSpace() const override;
83 
84  void setFieldSpace(const Space* t_space) override;
85 
86  virtual Expr operator()(const Tensor& point) override;
87 
88  virtual Expr operator()(Index index,
89  const Tensor& point) override;
90 
91  virtual Expr operator()(const std::vector<int>& indices,
92  const Tensor& point) override;
93 
94  virtual Expr operator()(std::vector<Index> indices,
95  const Tensor& point) override;
96 
97  protected:
98 
99  void checkFieldRequest(TensorParent* point);
100 
101  void checkIndicialAndFieldRequest(TensorParent* point);
102 
103  void checkIndicialAndFieldRequest(const Index& index,
104  TensorParent* point);
105 
106  void checkIndicialAndFieldRequest(const std::vector<Index>& indices,
107  TensorParent* point);
108 
109  protected:
110 
111  const Space* spaceField;
112 };
113 
114 
115 template<class ...Args>
116  std::shared_ptr<TensorFieldParent> tensorfield_s(Args&& ...args)
117  {
118  return csl::make_shared<TensorFieldParent>(
119  std::forward<Args>(args)...
120  );
121  }
122 
123 template<class ...Args>
125  std::string const& name,
126  csl::Space const* spaceField,
127  std::vector<const Space*> const& indices,
128  Args&& ...args)
129  {
130  return csl::make_shared<TensorFieldParent>(
131  name,
132  spaceField,
133  indices,
134  std::forward<Args>(args)...
135  );
136  }
137 
138 class TensorField: public std::shared_ptr<TensorFieldParent> {
139 
140  public:
141 
142  INHERIT_SHARED_PTR_CONSTRUCTOR(TensorField, TensorFieldParent)
143 
144  TensorField(
145  const std::string& t_name,
146  const Space* t_spaceField)
147  :TensorField(tensorfield_s(t_name, t_spaceField))
148  {}
149 
150  TensorField(
151  const std::string& t_name,
152  const Space* t_spaceField,
153  const std::vector<const Space*>& spaces)
154  :TensorField(tensorfield_s(t_name, t_spaceField, spaces))
155  {}
156 
157  template<class ...Args>
158  inline
159  Expr operator()(Args&& ...args)
160  {
161  return (**this)(std::forward<Args>(args)...);
162  }
163 
164  template<class ...Args>
165  inline
166  Expr operator()(const std::vector<int>& indices, Args&& ...args)
167  {
168  return (**this)(indices, std::forward<Args>(args)...);
169  }
170 
171  template<class ...Args>
172  inline
173  Expr operator()(const std::vector<Index>& indices, Args&& ...args)
174  {
175  return (**this)(indices, std::forward<Args>(args)...);
176  }
177 };
178 
179 DEFINE_SHARED_PTR_OPERATOR(TensorField)
180 
182 
183  protected:
184 
185  mutable Tensor point;
186 
187  public:
188 
189  TensorFieldElement(const Tensor& t_vector,
190  const Parent& t_parent);
191 
192  TensorFieldElement(const Tensor& t_vector,
193  const Parent& t_parent,
194  const std::vector<Index>& indices);
195 
196  TensorFieldElement(const Tensor& t_vector,
197  const Parent& t_parent,
198  const IndexStructure& indices);
199  public:
200 
201  template<typename ...Args>
202  friend Expr tensorfieldelement_s(Args&& ...args);
203 
204  ~TensorFieldElement(){};
205 
206  csl::PrimaryType getPrimaryType() const override;
207 
208  csl::Type getType() const override;
209 
210  bool isIndexed() const override {
211  return (not index.empty());
212  }
213 
214  csl::Tensor getPoint() const override;
215 
216  void setPoint(csl::Tensor const& t_point) override;
217 
218  unique_Expr copy_unique() const override;
219 
220  Expr refresh() const override;
221 
222  void print(int mode=0,
223  std::ostream& out=std::cout,
224  bool lib = false) const override;
225 
226  void printCode(
227  int mode=0,
228  std::ostream& out=std::cout
229  ) const override;
230 
231  std::string printLaTeX(int mode=0) const override;
232 
233  std::vector<csl::Parent> getSubSymbols() const override;
234 
235  std::optional<Expr> evaluate(
236  csl::eval::mode user_mode = csl::eval::base
237  ) const override;
238 
239  bool commutesWith(Expr_info expr, int sign=-1) const override;
240 
241  bool dependsOn(Expr_info expr) const override;
242 
243  bool dependsOn(Parent_info t_parent) const override;
244 
245  bool dependsExplicitlyOn(Expr_info expr) const override;
246 
247  bool compareWithDummy(Expr_info other,
248  std::map<Index, Index>& constraints,
249  bool keepAllCosntraints = false) const override;
250 
251  bool operator==(Expr_info expr) const override;
252 
253  bool operator<(const Abstract* expr) const override;
254 };
255 
256 template<typename ...Args>
257 Expr tensorfieldelement_s(Args&& ...args)
258 {
259  return csl::make_shared<TensorFieldElement>(std::forward<Args>(args)...)
260  ->getCanonicalPermutation();
261 }
262 
263 
264 class TDerivativeElement: public Operator<TensorFieldElement>{
265 
266  protected:
267 
268  Expr operand = CSL_1;
269 
270  public:
271 
272  TDerivativeElement(const Tensor& t_vector,
273  const Parent& t_parent,
274  const Index& index);
275 
276  TDerivativeElement(const Tensor& t_vector,
277  const Parent& t_parent,
278  const Index& index,
279  const Expr& operand,
280  bool empty);
281 
282  public:
283 
284  ~TDerivativeElement(){};
285 
286  csl::Type getType() const override;
287 
288  size_t size() const override;
289 
290  void print(int mode = 0,
291  std::ostream& out = std::cout,
292  bool lib = false) const override;
293 
294  void printCode(
295  int mode=0,
296  std::ostream& out=std::cout
297  ) const override;
298 
299  std::string printLaTeX(int mode = 0) const override;
300 
301  std::vector<csl::Parent> getSubSymbols() const override;
302 
303  std::optional<Expr> factor(bool full=false) const override;
304 
305  std::optional<Expr> factor(
306  Expr_info expr,
307  bool full=false) const override;
308 
309  std::optional<Expr> collect(
310  std::vector<Expr> const &factors,
311  bool full = false
312  ) const override;
313 
314  std::optional<Expr> expand(bool full = false,
315  bool inPlace = false) const override;
316 
317  std::optional<Expr> expand_if(
318  std::function<bool(Expr const&)> const& f,
319  bool full = false,
320  bool inPlace = false) const override;
321 
322  unique_Expr copy_unique() const override;
323 
324  Expr deepCopy() const override;
325 
326  Expr refresh() const override;
327 
328  Expr deepRefresh() const override;
329 
330  bool operatorAppliesOn(Expr_info expr) const override;
331 
332  bool dependsOn(Expr_info expr) const override;
333 
334  bool dependsOn(Parent_info t_parent) const override;
335 
336  bool dependsExplicitlyOn(Expr_info expr) const override;
337 
338  bool dependsExplicitlyOn(Parent_info expr) const override;
339 
340  Expr const &getArgument(int i) const override;
341 
342  Expr& getArgument(int i) override;
343 
344  void setArgument(Expr const& arg, int i) override;
345 
346  Expr getOperand() const override;
347 
348  void setOperand(const Expr& t_operand) override;
349 
350  void setPoint(const Tensor& newPoint) override;
351 
352  IndexStructure getIndexStructure() const override;
353 
354  void setIndexStructure(const IndexStructure& t_struct) override;
355 
362  void selfCheckIndexStructure();
363 
364  void replaceIndexInPlace(
365  Index const &oldIndex,
366  Index const &newIndex) override;
367 
368  std::optional<Expr> replaceIndex(
369  const Index& indexToReplace,
370  const Index& newIndex,
371  bool refresh = true) const override;
372 
373  std::optional<Expr> replaceIndices(
374  std::vector<Index> const &indexToReplace,
375  std::vector<Index> const &newIndex,
376  bool refresh = true,
377  bool flipped = false) const override;
378 
379  csl::vector_expr breakSpace(
380  const Space* brokenSpace,
381  const std::vector<const Space*>& newSpaces,
382  const std::vector<std::string>& indexNames
383  ) const override;
384 
385  std::optional<Expr> getComplexConjugate() const override;
386 
387  bool compareWithDummy(Expr_info other,
388  std::map<Index, Index>& constraints,
389  bool keepAllCosntraints = false) const override;
390 
391  bool operator==(Expr_info other) const override;
392 
393  bool operator<(const Abstract* expr) const override;
394 
395  Expr const &operator[](int i) const override;
396 
397  Expr& operator[](int i) override;
398 
399  friend
400  Expr tderivativeelement_s(const Tensor& t_vector,
401  const Parent& t_parent,
402  const Index& t_index)
403  {
404  return csl::make_shared<TDerivativeElement>(
405  t_vector, t_parent, t_index);
406  }
407 
408  friend
409  Expr tderivativeelement_s(const Tensor& t_vector,
410  const Parent& t_parent,
411  const Index& t_index,
412  const Expr& t_operand,
413  bool t_empty)
414  {
415  return csl::make_shared<TDerivativeElement>(
416  t_vector, t_parent, t_index)->applyOperator(t_operand, t_empty);
417  }
418 };
419 
420 Expr tderivativeelement_s(const Tensor& t_vector,
421  const Parent& t_parent,
422  const Index& t_index);
423 
424 Expr tderivativeelement_s(const Tensor& t_vector,
425  const Parent& t_parent,
426  const Index& t_index,
427  const Expr& t_operand,
428  bool t_empty);
429 
431 
432  public:
433 
435 
436  TDerivativeParent(const std::string& t_name,
437  const Space* t_space);
438 
439  TDerivativeParent(const TDerivativeParent& other) = default;
440 
441  void printDefinition(
442  std::ostream &out,
443  int indentSize = 4,
444  bool header = false
445  ) const override;
446 
447 template<class ...Args>
448  friend
449  TDerivative tderivative_s(Args&& ...args);
450 
451  ~TDerivativeParent(){};
452 
453  Expr operator()(Index index,
454  const Tensor& point) override;
455 };
456 
457 template<class ...Args>
458  std::shared_ptr<TDerivativeParent> tderivative_s(Args&& ...args)
459  {
460  return csl::make_shared<TDerivativeParent>(
461  std::forward<Args>(args)...);
462  }
463 
464 class TDerivative: public std::shared_ptr<TDerivativeParent> {
465 
466  public:
467 
468  INHERIT_SHARED_PTR_CONSTRUCTOR(TDerivative, TDerivativeParent)
469 
470  TDerivative(const std::string& t_name,
471  const Space* t_space)
472  :TDerivative(tderivative_s(t_name, t_space))
473  {}
474 
475  template<class ...Args>
476  inline
477  Expr operator()(Args&& ...args)
478  {
479  return (**this)(std::forward<Args>(args)...);
480  }
481 };
482 
483 DEFINE_SHARED_PTR_OPERATOR(TDerivative)
484 
485 
486 } // End of namespace csl
487 
488 #endif
std::string name
Definition: parent.h:87
Namespace for csl library.
Definition: abreviation.h:34
Definition: parent.h:439
Parent of an indicial object, allows to generate them (TensorElement).
Definition: indicial.h:245
Index object that is used for indicial objects.
Definition: index.h:75
Definition: tensorField.h:264
Base classes for parents and elements.
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47
Definition: tensorField.h:181
PrimaryType
Definition: parent.h:39
virtual bool operator==(const TensorParent &other) const
Compares the parent with another.
Definition: indicial.cpp:1958
Definition: tensorField.h:35
cslParent::Type getType() const override
Definition: tensorField.cpp:55
Specialization of Abstract for Indicial tensor. Building block carrying indices, and respecting Einst...
Definition: indicial.h:763
Definition: tensorField.h:430
PrimaryType
Stores enumerations of types for Abstract objects.
Definition: enum.h:31
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
Definition: tensorField.h:464
Vector space that has a name, a dimension, a delta tensor and possibly a non-trivial metric...
Definition: space.h:64
Base class for all parents (indicial, fields etc). All parents derive from this class.
Definition: parent.h:81
cslParent::PrimaryType getPrimaryType() const override
Definition: tensorField.cpp:50
Definition: tensorField.h:138
Type
Definition: parent.h:47
bool dependsExplicitlyOn(Expr_info expr) const override
Tells if the parent depends explicitely on another expression or not.
Definition: indicial.cpp:1179
Objects handling indexed expressions in order to perform indicial tensor calculations.
bool isIndexed() const override
Definition: tensorField.h:210
bool dependsOn(Expr_info expr) const override
Tells if the parent depends on another expression or not.
Definition: indicial.cpp:1168
bool operator<(const Expr &a, const Expr &b)
see Abstract::operator<()
Definition: abstract.cpp:1423
Definition: indicial.h:675
Linear operator O(a*X+b*Y) = a*O(X) + b*O(Y)
Definition: operator.h:38
Manages a std::vector of Index, to be used by an TensorElement.
Definition: index.h:472
Expression type/.
Definition: abstract.h:1573