Documentation of CSL
scalarFunc.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 
21 #ifndef SCALARFUNC_H_INCLUDED
22 #define SCALARFUNC_H_INCLUDED
23 #include "literal.h"
24 #include "numerical.h"
26 
27 namespace csl {
28 
29 class Tensor;
30 
38 class AbstractFunc: public Abstract{
39 
40  protected:
41 
44  public:
45 
49  AbstractFunc();
50 
51  explicit AbstractFunc(const Expr& t_argument);
52 
56  csl::PrimaryType getPrimaryType() const override{
58  }
59 
66  bool getCommutable() const override;
67 
68  std::vector<csl::Parent> getSubSymbols() const override;
69 
70  bool isIndexed() const override;
71 
72  size_t size() const override;
73 
74  bool compareWithDummy(Expr_info expr,
75  std::map<Index, Index>& constraints,
76  bool keepAllCosntraints = false) const override;
77 
78  IndexStructure getIndexStructure() const override;
79 
80  Tensor getPoint() const override;
81 
82  int getNArgs(int axis=0) const override;
83 
91  Expr const &getArgument(int iArg=0) const override;
92 
93  Expr& getArgument(int iArg=0) override;
94 
95  bool isReal() const override;
96 
97  bool isPurelyImaginary() const override;
98 
99  std::optional<Expr> getRealPart() const override;
100 
101  Expr getImaginaryPart() const override;
102 
103  std::optional<Expr> getComplexModulus() const override;
104 
105  std::optional<Expr> getComplexArgument() const override;
106 
107  std::optional<Expr> getComplexConjugate() const override;
108 
109  std::optional<Expr> findSubExpression(
110  Expr_info subExpression,
111  const Expr& newExpression) const override;
112 
120  void setArgument(const Expr& t_argument, int iArg=0) override;
121 
122  virtual std::optional<Expr> replaceIndex(
123  const Index& indexToReplace,
124  const Index& newIndex,
125  bool refresh = true) const override;
126 
127  virtual std::optional<Expr> replaceIndices(
128  std::vector<csl::Index> const &oldIndices,
129  std::vector<csl::Index> const &newIndices,
130  bool refresh = true,
131  bool flipped = false) const override;
132 
133  virtual std::optional<Expr> factor(bool full=false) const override;
134 
135  virtual std::optional<Expr> factor(
136  Expr_info expr,
137  bool full=false) const override;
138 
139  virtual std::optional<Expr> collect(
140  std::vector<Expr> const &factors,
141  bool full = false
142  ) const override;
143 
144  std::optional<Expr> expand(bool full=false,
145  bool inPlace=false) const override;
146 
155  virtual std::optional<Expr> expand_if(
156  std::function<bool(Expr const&)> const& f,
157  bool full=false,
158  bool inPlace=false) const override;
159 
160  bool dependsOn(Expr_info expr) const override;
161 
162  bool dependsOn(const AbstractParent* parent) const override;
163 
164  bool dependsExplicitlyOn(Expr_info expr) const override;
165 
166  bool dependsExplicitlyOn(Parent_info expr) const override;
167 
168  bool commutesWith(Expr_info expr, int sign=-1) const override;
169 
170  int isPolynomial(Expr_info expr) const override;
171 
177  std::optional<Expr> evaluate(
178  csl::eval::mode user_mode = csl::eval::base) const override;
179 
180  Expr const &operator[](int i) const override;
181 
182  Expr& operator[](int i) override;
183 
184  bool operator==(Expr_info expr) const override;
185 
186  bool operator<(const Abstract* expr) const override;
187 
188  protected:
189 
190  Expr evalNumerical(Expr const& argument) const;
191 };
192 
193 
199 class AbstractDuoFunc: public Abstract{
200 
201  protected:
202 
203  std::array<Expr,2> argument;
205  public:
206 
209  AbstractDuoFunc();
210 
211  AbstractDuoFunc(const Expr& left,
212  const Expr& right);
213 
214  csl::Type getType() const override {
215  return csl::Type::StandardDuo;
216  }
222  }
223 
230  bool getCommutable() const override;
231 
232  std::vector<csl::Parent> getSubSymbols() const override;
233 
234  bool isIndexed() const override;
235 
236  IndexStructure getIndexStructure() const override;
237 
238  Tensor getPoint() const override;
239 
243  int getNArgs(int axis=0) const override;
244 
245  size_t size() const override {
246  return 2;
247  }
248 
254  Expr const &getArgument(int iArg=0) const override;
255 
256  Expr& getArgument(int iArg=0) override;
257 
258  std::optional<Expr> findSubExpression(
259  Expr_info subExpression,
260  const Expr& newExpression) const override;
261 
266  void setArgument(const Expr& t_argument, int iArg=0) override;
267 
268  bool isReal() const override;
269 
270  bool isPurelyImaginary() const override;
271 
272  std::optional<Expr> getRealPart() const override;
273 
274  Expr getImaginaryPart() const override;
275 
276  std::optional<Expr> getComplexModulus() const override;
277 
278  std::optional<Expr> getComplexArgument() const override;
279 
280  std::optional<Expr> getComplexConjugate() const override;
281 
282  virtual std::optional<Expr> replaceIndex(
283  const Index& indexToReplace,
284  const Index& newIndex,
285  bool refresh = true) const override;
286 
287  virtual std::optional<Expr> replaceIndices(
288  std::vector<csl::Index> const &oldIndices,
289  std::vector<csl::Index> const &newIndices,
290  bool refresh = true,
291  bool flipped = false) const override;
292 
293  virtual std::optional<Expr> factor(bool full=false) const override;
294 
295  virtual std::optional<Expr> factor(
296  Expr_info expr,
297  bool full=false) const override;
298 
299  virtual std::optional<Expr> collect(
300  std::vector<Expr> const &factors,
301  bool full = false
302  ) const override;
303 
304  virtual std::optional<Expr> expand(bool full=false,
305  bool inPlace=false) const override;
306 
315  virtual std::optional<Expr> expand_if(
316  std::function<bool(Expr const&)> const& f,
317  bool full=false,
318  bool inPlace=false) const override;
319 
320  bool dependsOn(Expr_info expr) const override;
321 
322  bool dependsOn(Parent_info parent) const override;
323 
324  bool dependsExplicitlyOn(Expr_info expr) const override;
325 
326  bool dependsExplicitlyOn(Parent_info expr) const override;
327 
328  bool commutesWith(Expr_info expr, int sign=-1) const override;
329 
330  int isPolynomial(Expr_info expr) const override;
331 
332  Expr const &operator[](int i) const override;
333 
334  Expr& operator[](int i) override;
335 
336  bool operator<(Expr_info other) const override;
337 };
338 
339 
346 
347  protected:
348 
349  csl::vector_expr argument;
351  public:
352 
356 
357  csl::Type getType() const override {
359  }
360 
364  virtual csl::PrimaryType getPrimaryType() const override {
366  }
367 
374  bool getCommutable() const override;
375 
376  std::vector<csl::Parent> getSubSymbols() const override;
377 
378  virtual bool isIndexed() const override;
379 
383  int getNArgs(int axis=0) const override;
384 
388  const csl::vector_expr& getVectorArgument() const override;
389 
395  Expr const &getArgument(int iArg=0) const override;
396 
397  Expr& getArgument(int iArg=0) override;
398 
399  std::optional<Expr> findSubExpression(
400  Expr_info subExpression,
401  const Expr& newExpression) const override;
402 
403  size_t size() const override;
404 
405  bool empty() const override;
406 
410  csl::vector_expr::iterator begin() override;
411 
415  csl::vector_expr::iterator end() override;
416 
420  csl::vector_expr::const_iterator begin() const override;
421 
425  csl::vector_expr::const_iterator end() const override;
426 
431  void setArgument(const Expr& t_argument, int iArg=0) override;
432 
433  void setVectorArgument(const csl::vector_expr& t_argument) override;
434 
435  bool isReal() const override;
436 
437  bool isPurelyImaginary() const override;
438 
439  std::optional<Expr> getRealPart() const override;
440 
441  Expr getImaginaryPart() const override;
442 
443  std::optional<Expr> getComplexModulus() const override;
444 
445  std::optional<Expr> getComplexArgument() const override;
446 
447  std::optional<Expr> getComplexConjugate() const override;
448 
449  virtual std::optional<Expr> replaceIndex(
450  const Index& indexToReplace,
451  const Index& newIndex,
452  bool refresh = true) const override;
453 
454  virtual std::optional<Expr> replaceIndices(
455  std::vector<csl::Index> const &oldIndices,
456  std::vector<csl::Index> const &newIndices,
457  bool refresh = true,
458  bool flipped = false) const override;
459 
460  Tensor getPoint() const override;
461 
462  virtual std::optional<Expr> factor(bool full=false) const override;
463 
464  virtual std::optional<Expr> factor(Expr_info expr,
465  bool full=false) const override;
466 
467  virtual std::optional<Expr> collect(
468  std::vector<Expr> const &factors,
469  bool full = false
470  ) const override;
471 
472  virtual std::optional<Expr> expand(bool full=false,
473  bool inPlace=false) const override;
474 
483  virtual std::optional<Expr> expand_if(
484  std::function<bool(Expr const&)> const& f,
485  bool full=false,
486  bool inPlace=false) const override;
487 
488  bool dependsOn(Expr_info expr) const override;
489 
490  bool dependsOn(const AbstractParent* parent) const override;
491 
492  bool dependsExplicitlyOn(Expr_info expr) const override;
493 
494  bool dependsExplicitlyOn(Parent_info expr) const override;
495 
496  bool commutesWith(Expr_info expr, int sign=-1) const override;
497 
498  int isPolynomial(Expr_info expr) const override;
499 
500  bool operator<(Expr_info other) const override;
501 
502  Expr const &operator[](int i) const override;
503 
504  Expr& operator[](int i) override;
505 };
506 
507 /*************************************************/
508 // Inline functions //
509 /*************************************************/
511 inline AbstractFunc::AbstractFunc(const Expr& t_argument)
512  :Abstract(), argument(t_argument)
513 {}
514 
516  argument[0] = CSL_0;
517  argument[1] = CSL_0;
518 }
519 
521  const Expr& left,
522  const Expr& right
523  ): Abstract(){
524  argument[0] = left;
525  argument[1] = right;
526 }
527 
529  :Abstract(), argument(csl::vector_expr(alloc_expr))
530 {
531 
532 }
533 
534 } // End of namespace csl
535 
536 #endif
virtual std::optional< Expr > factor(bool full=false) const override
Factors the Abstract.
Definition: scalarFunc.cpp:194
Expr const & operator[](int i) const override
Access operator for multi-argument expressions, equivalent to the function getArgument().
Definition: scalarFunc.cpp:349
AbstractFunc()
Default constructor.
Definition: scalarFunc.h:510
csl::PrimaryType getPrimaryType() const override
Gives the primary type of an AbstractDuoFunc.
Definition: scalarFunc.h:220
Namespace for csl library.
Definition: abreviation.h:34
Expr const & getArgument(int iArg=0) const override
Returns the argument of the function.
Definition: scalarFunc.cpp:81
bool dependsOn(Expr_info expr) const override
Check recursively if the expression depends on expr.
Definition: scalarFunc.cpp:301
void setArgument(const Expr &t_argument, int iArg=0) override
Replaced the argument of the AbstractFunc.
Definition: scalarFunc.cpp:151
Handle functions of multiple arguments. In the case of the call of a simplification function...
Definition: scalarFunc.h:199
std::optional< Expr > getComplexConjugate() const override
Calculates and returns the complex conjugate of the expression.
Definition: scalarFunc.cpp:122
Index object that is used for indicial objects.
Definition: index.h:75
std::optional< Expr > findSubExpression(Expr_info subExpression, const Expr &newExpression) const override
Searches a sub-expression and replaces it.
Definition: scalarFunc.cpp:134
bool operator==(Expr_info expr) const override
Compares the Abstract with another.
Definition: scalarFunc.cpp:367
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47
Base class for scalar functions of one argument.
Definition: scalarFunc.h:38
csl::vector_expr argument
std::vector containing the arguments.
Definition: scalarFunc.h:349
std::array< Expr, 2 > argument
std::array containing the arguments.
Definition: scalarFunc.h:203
csl::PrimaryType getPrimaryType() const override
Definition: scalarFunc.h:56
Abstract()
Default Constructor.
Definition: abstract.h:92
int getNArgs(int axis=0) const override
Returns the number of arguments of the expression. If the expression is a building block (AbstractBui...
Definition: scalarFunc.cpp:77
bool isIndexed() const override
Definition: scalarFunc.cpp:49
virtual csl::vector_expr::iterator end()
Definition: abstract.cpp:468
std::optional< Expr > getComplexModulus() const override
Evaluates the modulus in the complex plane of the Abstract and returns it.
Definition: scalarFunc.cpp:110
std::optional< Expr > expand(bool full=false, bool inPlace=false) const override
Develops the Abstract.
Definition: scalarFunc.cpp:236
Expr argument
Argument of the considered function.
Definition: scalarFunc.h:42
bool commutesWith(Expr_info expr, int sign=-1) const override
Tells if the object commutes with expr.
Definition: scalarFunc.cpp:318
bool getCommutable() const override
Tells if the AbstractFunc is commutable.
Definition: scalarFunc.cpp:40
PrimaryType
Stores enumerations of types for Abstract objects.
Definition: enum.h:31
virtual csl::PrimaryType getPrimaryType() const override
Gives the primary type of an AbstractMultiFunc.
Definition: scalarFunc.h:364
IndexStructure getIndexStructure() const override
Definition: scalarFunc.cpp:67
Expr getImaginaryPart() const override
Evaluates the imaginary part of the Abstract and returns it.
Definition: scalarFunc.cpp:105
virtual std::optional< Expr > expand_if(std::function< bool(Expr const &)> const &f, bool full=false, bool inPlace=false) const override
Develops the Abstract.
Definition: scalarFunc.cpp:250
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
virtual void setVectorArgument(const csl::vector_expr &t_argument)
Replaced the entire std::vector of argument.
Definition: abstract.cpp:859
virtual std::optional< Expr > replaceIndex(const Index &indexToReplace, const Index &newIndex, bool refresh=true) const override
For indicial expressions, this function searches indexToContract and replaces it with newIndex...
Definition: scalarFunc.cpp:155
csl::Type getType() const override
Gives the type of Abstract.
Definition: scalarFunc.h:214
AbstractMultiFunc()
Default Constructor, initializes the std::vector argument empty.
Definition: scalarFunc.h:528
virtual std::optional< Expr > collect(std::vector< Expr > const &factors, bool full=false) const override
Collects terms in sum according to some factors given by the user.
Definition: scalarFunc.cpp:220
Base class for all parents (indicial, fields etc). All parents derive from this class.
Definition: parent.h:81
bool compareWithDummy(Expr_info expr, std::map< Index, Index > &constraints, bool keepAllCosntraints=false) const override
Comparison disregarding name of dummy indices, i.e. the two expressions * are equals even if dummy in...
Definition: scalarFunc.cpp:53
bool dependsExplicitlyOn(Expr_info expr) const override
Check recursively if expr is present in the expression.
Definition: scalarFunc.cpp:309
Handle functions of multiple arguments. In the case of the call of a simplification function...
Definition: scalarFunc.h:345
csl::Type getType() const override
Gives the type of Abstract.
Definition: scalarFunc.h:357
std::optional< Expr > getComplexArgument() const override
Evaluates the argument in the complex plane of the Abstract and returns it.
Definition: scalarFunc.cpp:116
virtual csl::vector_expr::iterator begin()
Definition: abstract.cpp:461
bool operator<(const Expr &a, const Expr &b)
see Abstract::operator<()
Definition: abstract.cpp:1423
Definition: indicial.h:675
std::optional< Expr > getRealPart() const override
Evaluates the real part of the Abstract and returns it.
Definition: scalarFunc.cpp:100
Manages a std::vector of Index, to be used by an TensorElement.
Definition: index.h:472
int isPolynomial(Expr_info expr) const override
Determines if the expression is a mononomial term in expr, i.e. a term of the form C*expr^n with C in...
Definition: scalarFunc.cpp:323
AbstractDuoFunc()
Default Constructor, initializes the std::array argument empty.
Definition: scalarFunc.h:515
Expression type/.
Definition: abstract.h:1573
std::optional< Expr > evaluate(csl::eval::mode user_mode=csl::eval::base) const override
Evaluates the function after evaluating the argument.
Definition: scalarFunc.cpp:327
virtual const csl::vector_expr & getVectorArgument() const
Allows to get the entire std::vector of arguments of the expression.
Definition: abstract.cpp:441