Documentation of CSL
literal.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 LITERAL_H_INCLUDED
24 #define LITERAL_H_INCLUDED
25 
26 #include "buildingBlock.h"
27 #include "variableParent.h"
28 
29 namespace csl {
30 
36 
37  protected:
38 
40  std::vector<Equation*> props;
41 
42  public:
43 
45 
46  csl::PrimaryType getPrimaryType() const override {
48  }
49 
50  size_t memoryOverhead() const override;
51 
52  std::vector<Parent> getSubSymbols() const override;
53 
54  const std::vector<Equation*>& getProperties() const override;
55 
56  void addProperty(Equation* property) override;
57 
58  void removeProperty(Equation* property) override;
59 };
60 
61 inline AbstractLiteral::AbstractLiteral()
62  :Complexified(),
63  props(std::vector<Equation*>(0)){}
64 
65 
69 class Constant: public AbstractLiteral{
70 
71  protected:
72 
74 
75  public:
76 
81 
82 
83 #ifdef DEBUG
84  ~Constant(){ __record_data_alloc(static_cast<int>(getType())); }
85 #endif
86 
87  std::string const &getName() const override;
88 
89  std::string const &getLatexName() const override;
90 
91  bool getCommutable() const override;
92 
98  csl::PrimaryType getPrimaryType() const override;
99 
103  csl::Type getType() const override;
104 
105  Parent getParent() const override;
106 
107  Parent_info getParent_info() const override;
108 
109  csl::ComplexProperty getComplexProperty() const override;
110 
111  void setComplexProperty(csl::ComplexProperty prop) override;
112 
113  void setName(const std::string& name) override;
114 
115  void setCommutable(bool commutable) override;
116 
117  bool getValued() const override;
118 
119  long double getValue() const override;
120 
121  bool dependsOn(Expr_info expr) const override;
122 
128  void setValue(long double t_value) override;
129 
130  void setValue(Expr const &t_value) override;
131 
137  void print(int mode=0,
138  std::ostream& out=std::cout,
139  bool lib = false) const override;
140 
141  void printCode(
142  int mode=0,
143  std::ostream& out=std::cout
144  ) const override;
145 
146  std::string printLaTeX(int mode=0) const override;
147 
148  std::vector<Parent> getSubSymbols() const override;
149 
150  long double evaluateScalar() const override;
151 
152  std::optional<Expr> evaluate(
153  csl::eval::mode user_mode = csl::eval::base
154  ) const override;
155 
161  std::optional<Expr> derive(Expr_info expr) const override;
162 
163  int getParity(Expr_info t_variable) const override;
164 
165  unique_Expr copy_unique() const override;
166 
170  void operator=(double t_value) override;
171 
177  bool operator==(Expr_info expr) const override;
178 
179  bool operator<(const Abstract* expr) const override;
180 };
181 
185 class Variable: public AbstractLiteral{
186 
187  protected:
188 
190 
191  public:
192 
196  Variable(const std::shared_ptr<VariableParent>& t_parent);
197 
198 
199 #ifdef DEBUG
200  ~Variable(){ __record_data_alloc(static_cast<int>(getType())); }
201 #endif
202 
208  csl::PrimaryType getPrimaryType() const override;
209 
213  csl::Type getType() const override;
214 
215  std::string const &getName() const override;
216 
217  std::string const &getLatexName() const override;
218 
219  bool getCommutable() const override;
220 
221  csl::ComplexProperty getComplexProperty() const override;
222 
223  void setComplexProperty(csl::ComplexProperty prop) override;
224 
225  void setName(const std::string& name) override;
226 
227  void setCommutable(bool commutable) override;
228 
229  Parent getParent() const override;
230 
231  Parent_info getParent_info() const override;
232 
233  bool getElementary() const override;
234 
235  bool getAllDependencies() const override;
236 
237  bool getValued() const override;
238 
239  long double getValue() const override;
240 
241  bool dependsOn(Expr_info expr) const override;
242 
243  bool commutesWith(Expr_info expr, int sign=-1) const override;
244 
250  void setValue(long double t_value) override;
251 
252  void setValue(Expr const &t_value) override;
253 
254  void setElementary(bool t_elementary) override;
255 
256  void setAllDependencies(bool t_allDependencies) override;
257 
258  void addDependency(Expr const& expr) override;
259 
260  void removeDependency(Expr const& expr) override;
261 
267  void print(int mode=0,
268  std::ostream& out=std::cout,
269  bool lib = false) const override;
270 
271  void printCode(
272  int mode=0,
273  std::ostream& out=std::cout
274  ) const override;
275 
276  std::string printLaTeX(int mode=0) const override;
277 
278  std::vector<Parent> getSubSymbols() const override;
279 
280  long double evaluateScalar() const override;
281 
282  std::optional<Expr> evaluate(
283  csl::eval::mode user_mode = csl::eval::base
284  ) const override;
285 
291  std::optional<Expr> derive(Expr_info expr) const override;
292 
293  int getParity(Expr_info t_variable) const override;
294 
295  unique_Expr copy_unique() const override;
296 
297 
301  void operator=(double t_value) override;
302 
308  bool operator==(Expr_info expr) const override;
309 
310  bool operator<(const Abstract* expr) const override;
311 };
312 
319 
320  protected:
321 
322  int value;
324  public:
325 
329  IntFactorial();
330 
335  explicit IntFactorial(int t_value);
336 
337 
338 #ifdef DEBUG
339  ~IntFactorial(){ __record_data_alloc(static_cast<int>(getType())); }
340 #endif
341 
348  long double getValue() const override { return value;}
349 
355 
359  csl::Type getType() const override { return csl::Type::IntFactorial;};
360 
364  void setValue(long double t_value) override;
365 
366  void print(int mode=0,
367  std::ostream& out=std::cout,
368  bool lib = false) const override;
369 
370  void printCode(
371  int mode=0,
372  std::ostream& out=std::cout
373  ) const override;
374 
375  std::string printLaTeX(int mode=0) const override;
376 
380  long double evaluateScalar() const override;
381 
382  std::optional<Expr> evaluate(
383  csl::eval::mode user_mode = csl::eval::base
384  ) const override;
385 
386  unique_Expr copy_unique() const override;
387 
388  Expr refresh() const override;
389 
394  std::optional<Expr> derive(Expr_info expr) const override;
395 
399  void operator=(int t_value);
405  bool operator==(Expr_info expr) const override;
406 
407  bool operator<(const Abstract* expr) const override;
408 };
409 
416 
417  public:
418 
423  Imaginary();
424 
425 
426 #ifdef DEBUG
427  ~Imaginary(){ __record_data_alloc(static_cast<int>(getType())); }
428 #endif
429 
434 
438  csl::Type getType() const override { return csl::Type::Imaginary;}
439 
440  std::optional<Expr> getRealPart() const override;
441 
442  Expr getImaginaryPart() const override;
443 
444  std::optional<Expr> getComplexModulus() const override;
445 
446  std::optional<Expr> getComplexArgument() const override;
447 
448  csl::ComplexProperty getComplexProperty() const override {
450  }
451 
452  void print(int mode=0,
453  std::ostream& out=std::cout,
454  bool lib = false) const override;
455 
456  void printCode(
457  int mode=0,
458  std::ostream& out=std::cout
459  ) const override;
460 
461  std::string printLaTeX(int mode=0) const override;
462 
468  long double evaluateScalar() const override;
469 
477  std::optional<Expr> evaluate(
478  csl::eval::mode user_mode = csl::eval::base
479  ) const override;
480 
481  unique_Expr copy_unique() const override;
482 
486  std::optional<Expr> derive(Expr_info expr) const override;
487 
488  bool operator==(Expr_info expr) const override;
489 
490  bool operator<(const Abstract* expr) const override;
491 };
492 
493 /*************************************************/
494 // Inline functions (non virtual and short) //
495 /*************************************************/
497  :AbstractLiteral(), parent(t_parent)
498 {
499 
500 }
501 
503  :AbstractLiteral(), parent(t_parent)
504 {
505 
506 }
507 
509  :AbstractLiteral(),
510  value(0)
511 {
512 
513 }
514 
515 inline IntFactorial::IntFactorial(int t_value)
516  :AbstractLiteral(),
517  value(t_value)
518 {
519 
520 }
521 
523  :AbstractLiteral()
524 {
525 
526 }
527 
536 Expr intfactorial_s(int value);
537 
538 Expr constant_s(std::string const& name,
540 
541 Expr constant_s(std::string const& name,
542  long double value,
544 
545 Expr constant_s(std::string const& name,
546  Expr const& value,
548 
549 Expr variable_s(
550  std::string const& name,
552 
553 Expr variable_s(
554  std::string const& name,
555  long double value,
557 
558 Expr variable_s(
559  std::string const& name,
560  Expr const& value,
562 
563 /*************************************************/
564 // Global (useful) variables //
565 // (Numerical or Literal) //
566 /*************************************************/
567 
568 inline csl::allocator<Constant> alloc_constant;
569 inline csl::allocator<Variable> alloc_variable;
570 
571 } // End of namespace csl;
572 
577 inline const csl::Expr CSL_I = csl::make_shared<csl::Imaginary>();
578 
583 inline const csl::Expr CSL_PI = csl::constant_s(
584  "\\pi",
585  M_PI,
587 
592 inline const csl::Expr CSL_E = csl::constant_s(
593  "e",
594  M_E,
596 
597 
607 inline const csl::Expr CSL_INF = csl::constant_s(
608  "INF",
610 
617 inline const csl::Expr CSL_UNDEF = csl::constant_s(
618  "CSL_UNDEF",
620 
621 
622 #endif
csl::PrimaryType getPrimaryType() const override
Gives the primary type of Abstract.
Definition: literal.h:46
virtual bool getValued() const
Tells if the expression is valued, i.e. is a function of numbers and valued literals (a Variable or C...
Definition: abstract.cpp:180
void removeProperty(Equation *property) override
Removes a property to the object.
Definition: literal.cpp:60
virtual bool operator<(Expr_info expr) const =0
Compares the simplicity of the expression to another.
const csl::Expr CSL_UNDEF
Constant that is useful in return of non-defined calculations as for example Commutator(): [A...
Definition: literal.h:617
virtual Parent getParent() const
For indicial expressions this function returns a pointer to the parent object of type TensorParent (n...
Definition: abstract.cpp:259
Definition: buildingBlock.h:69
Namespace for csl library.
Definition: abreviation.h:34
Definition: parent.h:439
int value
Definition: literal.h:322
virtual std::string const & getName() const
Returns the Abstract&#39;s name.
Definition: abstract.cpp:37
Expr getImaginaryPart() const override
Evaluates the imaginary part of the Abstract and returns it.
Definition: buildingBlock.cpp:115
virtual long double getValue() const
Returns the value of the expression, if it has one explicitely. In particular, it will work only on N...
Definition: abstract.cpp:572
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47
Variable(const std::shared_ptr< VariableParent > &t_parent)
Default constructor.
Definition: literal.h:496
virtual long double evaluateScalar() const
Evaluates the value of the Abstract.
Definition: abstract.cpp:871
csl::PrimaryType getPrimaryType() const override
Definition: literal.h:433
virtual void print(int mode=0, std::ostream &out=std::cout, bool lib=false) const =0
Displays the abstract in standard output.
virtual int getParity(Expr_info t_variable) const
Returns the parity property of the expression with respect to t_variable.
Definition: abstract.cpp:960
std::optional< Expr > getComplexModulus() const override
Evaluates the modulus in the complex plane of the Abstract and returns it.
Definition: buildingBlock.cpp:53
csl::Type getType() const override
Gives the type of the Imaginary.
Definition: literal.h:438
const csl::Expr CSL_INF
Represents infinity in the program.
Definition: literal.h:607
Handle an object with a name and a value.
Definition: literal.h:185
Handles factorial of a Number.
Definition: literal.h:318
virtual std::optional< Expr > getComplexArgument() const
Evaluates the argument in the complex plane of the Abstract and returns it.
Definition: abstract.cpp:677
const std::vector< Equation * > & getProperties() const override
Definition: literal.cpp:47
virtual void operator=(double t_value)
Equivalent to the setValue() function.
Definition: abstract.cpp:972
IntFactorial()
Default Constructor.
Definition: literal.h:508
csl::Type getType() const override
Gives the type of a IntFraction.
Definition: literal.h:359
Definition: diagonalization.h:34
ComplexProperty
Contains all possible complex properties of objects. Real, purely imaginary, or complex.
Definition: enum.h:127
virtual void setValue(long double t_value)
Sets the value if there is one (for Numerical and Literal valued).
Definition: abstract.cpp:794
PrimaryType
Stores enumerations of types for Abstract objects.
Definition: enum.h:31
virtual void setCommutable(bool t_commutable)
Allows the abstract to commute or not.
Definition: abstract.cpp:62
virtual bool commutesWith(Expr_info expr, int sign=-1) const override
Tells if the object commutes with expr.
Definition: buildingBlock.cpp:75
Handle an object with a name and a value.
Definition: literal.h:69
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
std::vector< Equation * > props
Definition: literal.h:40
Imaginary()
Default constructor.
Definition: literal.h:522
Base class for all parents (indicial, fields etc). All parents derive from this class.
Definition: parent.h:81
virtual void setName(const std::string &t_name)
Change the name of the abstract.
Definition: abstract.cpp:57
virtual bool getCommutable() const
Allows to know if the object commutes with all the others.
Definition: abstract.cpp:47
bool operator==(Expr_info other) const override
Compares the Abstract with another.
Definition: buildingBlock.cpp:159
virtual bool dependsOn(Expr_info expr) const
Check recursively if the expression depends on expr.
Definition: abstract.cpp:906
void addProperty(Equation *property) override
Adds a property to the object.
Definition: literal.cpp:52
Abstract class from which derive literal building blocks: Constant, Variable, Imaginary and IntFactor...
Definition: literal.h:35
Expr intfactorial_s(int value)
Definition: literal.cpp:738
virtual std::string printLaTeX(int mode=0) const
Creates a LaTeX output for the Abstract.
Definition: abstract.cpp:168
csl::PrimaryType getPrimaryType() const override
Gives the primary type of a IntFraction.
Definition: literal.h:354
Definition: equation.h:43
virtual csl::Type getType() const =0
Gives the type of Abstract.
Numerical representation of i.
Definition: literal.h:415
Constant(const std::shared_ptr< ConstantParent > &t_parent)
Default constructor.
Definition: literal.h:502
std::optional< Expr > getRealPart() const override
Evaluates the real part of the Abstract and returns it.
Definition: buildingBlock.cpp:106
virtual std::optional< Expr > evaluate(csl::eval::mode user_mode=csl::eval::base) const =0
Evaluates the Abstract.
virtual std::optional< Expr > derive(Expr_info expr) const
Calculates the derivative of the Abstract wrt another.
Definition: abstract.cpp:774
Expression type/.
Definition: abstract.h:1573
long double getValue() const override
Returns the value in the factorial.
Definition: literal.h:348