Documentation of CSL
numerical.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 NUMERICAL_H_INCLUDED
24 #define NUMERICAL_H_INCLUDED
25 
26 #include "buildingBlock.h"
27 
28 namespace csl {
29 
35  public:
36 
38 
39  csl::PrimaryType getPrimaryType() const override {
41  }
42 
43  void printCode(
44  int mode = 0,
45  std::ostream &out = std::cout
46  ) const override;
47 
48  std::vector<Parent> getSubSymbols() const override;
49 
50  Expr getNumericalFactor() const override;
51  std::optional<Expr> getTerm() const override;
52 
53  bool dependsOn(Expr_info) const override {
54  return false;
55  }
56  bool dependsOn(Parent_info) const override {
57  return false;
58  }
59  bool dependsExplicitlyOn(Expr_info) const override {
60  return false;
61  }
62  bool dependsExplicitlyOn(Parent_info) const override {
63  return false;
64  }
65 };
66 
67 inline AbstractNumerical::AbstractNumerical(): AbstractBuildingBlock(){}
68 
69 
70 /*************************************************/
71 // Numerical classes //
72 /*************************************************/
73 
78 class Integer: public AbstractNumerical{
79 
80  protected:
81 
82  long long int value;
84  public:
85 
89  Integer();
90 
94  explicit Integer(long long int t_value);
95 
96 #ifdef DEBUG
97  ~Integer(){ __record_data_alloc(static_cast<int>(getType())); }
98 #endif
99 
103  csl::Type getType() const override;
104 
105  bool isInteger() const override { return true;}
106 
110  void setValue(long double t_value) override;
111 
112  void print(int mode=0,
113  std::ostream& out=std::cout,
114  bool lib = false) const override;
115 
116  std::string printLaTeX(int mode=0) const override;
117 
118  long double evaluateScalar() const override;
119 
120  std::optional<Expr> evaluate(
121  csl::eval::mode user_mode = csl::eval::base
122  ) const override;
123 
124  unique_Expr copy_unique() const override;
125 
132  Expr multiplication_own(const Expr& expr, bool side=1) const override;
133 
140  Expr addition_own(const Expr& expr) const override;
141 
142  Expr division_own(const Expr& expr) const override;
143 
144  Expr exponentiation_own(const Expr& expr) const override;
145 
151  std::optional<Expr> derive(Expr_info expr) const override;
152 
156  void operator=(long long int t_value);
157 
158  bool operator==(Expr_info expr) const override;
159 
160  bool operator<(const Abstract* expr) const override;
161 };
162 
163 
168 class Float: public AbstractNumerical{
169 
170  protected:
171 
172  long double value;
174  public:
175 
180  Float();
181 
185  explicit Float(long double t_value);
186 
187 #ifdef DEBUG
188  ~Float(){ __record_data_alloc(static_cast<int>(getType())); }
189 #endif
190 
191 
195  csl::Type getType() const override;
196 
197  bool isInteger() const override;
198 
202  void setValue(long double t_value) override;
203 
204  void print(int mode=0,
205  std::ostream& out=std::cout,
206  bool lib = false) const override;
207 
208  std::string printLaTeX(int mode=0) const override;
209 
210  long double evaluateScalar() const override;
211 
212  std::optional<Expr> evaluate(
213  csl::eval::mode user_mode = csl::eval::base
214  ) const override;
215 
216  unique_Expr copy_unique() const override;
217 
218  Expr refresh() const override;
219 
226  Expr multiplication_own(const Expr& expr, bool side=1) const override;
227 
234  Expr addition_own(const Expr& expr) const override;
235 
236  Expr division_own(const Expr& expr) const override;
237 
238  Expr exponentiation_own(const Expr& expr) const override;
239 
245  std::optional<Expr> derive(Expr_info expr) const override;
246 
250  void operator=(int t_value);
251 
255  void operator=(double t_value) override;
256 
257  bool operator==(Expr_info expr) const override;
258 
259  bool operator<(const Abstract* expr) const override;
260 };
261 
268 
269  protected:
270 
271  long long int num;
272  long long int denom;
274  public:
275 
279  IntFraction();
280 
284  explicit IntFraction(long long int t_num);
285 
290  IntFraction(long long int t_num, long long int t_denom);
291 
292  IntFraction(IntFraction const&) = default;
293 
294 #ifdef DEBUG
295  ~IntFraction(){ __record_data_alloc(static_cast<int>(getType())); }
296 #endif
297 
301  csl::Type getType() const override;
302 
306  long long int getNum() const override;
307 
311  long long int getDenom() const override;
312 
313  void print(int mode=0,
314  std::ostream& out=std::cout,
315  bool lib = false) const override;
316 
317  void printCode(
318  int mode = 0,
319  std::ostream &out = std::cout
320  ) const override;
321 
322  std::string printLaTeX(int mode=0) const override;
323 
327  long double evaluateScalar() const override;
328 
329  std::optional<Expr> evaluate(
330  csl::eval::mode user_mode = csl::eval::base
331  ) const override;
332 
333  unique_Expr copy_unique() const override;
334 
335  Expr refresh() const override;
336 
344  Expr multiplication_own(const Expr& expr, bool side=1) const override;
345 
353  Expr addition_own(const Expr& expr) const override;
354 
355  Expr division_own(const Expr& expr) const override;
356 
357  Expr exponentiation_own(const Expr& expr) const override;
358 
363  std::optional<Expr> derive(Expr_info expr) const override;
364 
368  void operator=(long long int t_value);
369 
373  void operator=(double t_value) override;
374 
375  bool operator==(Expr_info expr) const override;
376 
377  bool operator<(const Abstract* expr) const override;
378 };
379 
380 /*************************************************/
381 // Inline functions (non virtual and short) //
382 /*************************************************/
383 inline Integer::Integer(): AbstractNumerical(), value(0){}
384 inline Integer::Integer(long long int t_value)
385  :AbstractNumerical(), value(t_value)
386 {}
387 
388 inline Float::Float(): AbstractNumerical(), value(0){}
389 inline Float::Float(long double t_value): AbstractNumerical(), value(t_value){}
390 
391 //The IntFraction constructor with t_num and t_denom is in the .cpp file (more complex).
392 inline IntFraction::IntFraction(): AbstractNumerical(), num(0), denom(1){}
393 inline IntFraction::IntFraction(long long int t_num)
394  :AbstractNumerical(), num(t_num), denom(1){}
395 
396 class Complex : public AbstractNumerical {
397 
398  protected:
399 
404  public:
405 
409  Complex();
410 
414  explicit Complex(Expr t_real);
415 
420  Complex(Expr t_real,
421  Expr t_imag);
422 
423 #ifdef DEBUG
424  ~Complex(){ __record_data_alloc(static_cast<int>(getType())); }
425 #endif
426 
430  csl::Type getType() const override;
431 
432  void print(int mode=0,
433  std::ostream& out=std::cout,
434  bool lib = false) const override;
435 
436  void printCode(
437  int mode=0,
438  std::ostream& out=std::cout
439  ) const override;
440 
441  std::string printLaTeX(int mode=0) const override;
442 
443  long double evaluateScalar() const override;
444 
445  std::optional<Expr> evaluate(
446  csl::eval::mode user_mode = csl::eval::base
447  ) const override;
448 
449  bool isReal() const override;
450 
451  bool isPurelyImaginary() const override;
452 
453  std::optional<Expr> getRealPart() const override;
454 
455  Expr getImaginaryPart() const override;
456 
457  std::optional<Expr> getComplexArgument() const override;
458 
459  std::optional<Expr> getComplexModulus() const override;
460 
461  std::optional<Expr> getComplexConjugate() const override;
462 
463  unique_Expr copy_unique() const override;
464 
465  Expr refresh() const override;
466 
467  Expr multiplication_own(const Expr& expr, bool side=1) const override;
468 
469  Expr addition_own(const Expr& expr) const override;
470 
471  Expr division_own(const Expr& expr) const override;
472 
473  Expr exponentiation_own(const Expr& expr) const override;
474 
475  std::optional<Expr> derive(Expr_info expr) const override;
476 
477  bool operator==(Expr_info expr) const override;
478 
479  bool operator<(const Abstract* expr) const override;
480 
481  private:
482 
483  void checkNumerical(Expr const& expr) const;
484 };
485 
486 
487 Expr float_s(long double value);
488 
489 Expr int_s(long long int value);
490 
491 Expr autonumber_s(long double value);
492 
493 Expr intfraction_s(long long int num, long long int denom);
494 
495 Expr complex_s(Expr const& real, Expr const& imag);
496 
497 inline csl::allocator<Integer> alloc_integer;
498 inline csl::allocator<Float> alloc_float;
499 inline csl::allocator<IntFraction> alloc_intfraction;
500 inline csl::allocator<Complex> alloc_complex;
501 
502 } // End of namespace csl
503 
504 inline const csl::Expr CSL_0 = csl::make_shared<csl::Integer>(0);
505 
506 inline const csl::Expr CSL_1 = csl::make_shared<csl::Integer>(1);
507 
508 inline const csl::Expr CSL_2 = csl::make_shared<csl::Integer>(2);
509 
510 inline const csl::Expr CSL_M_1 = csl::make_shared<csl::Integer>(-1);
511 
512 inline const csl::Expr CSL_M_2 = csl::make_shared<csl::Integer>(-2);
513 
514 inline const csl::Expr CSL_HALF = csl::make_shared<csl::IntFraction>(1, 2);
515 
516 inline const csl::Expr CSL_THIRD = csl::make_shared<csl::IntFraction>(1, 3);
517 
518 inline const csl::Expr CSL_M_HALF = csl::make_shared<csl::IntFraction>(-1, 2);
519 
520 inline const csl::Expr CSL_M_THIRD = csl::make_shared<csl::IntFraction>(-1, 3);
521 
522 #endif
virtual Expr addition_own(const Expr &expr) const
Contains implementation of special addition for Numerical- and Vectorial-types.
Definition: abstract.cpp:878
Expr multiplication_own(const Expr &expr, bool side=1) const override
Multiplicates the IntFraction with a Number or a IntFraction.
Definition: numerical.cpp:631
Expr addition_own(const Expr &expr) const override
Adds the IntFraction with a Number or a IntFraction.
Definition: numerical.cpp:654
virtual bool operator<(Expr_info expr) const =0
Compares the simplicity of the expression to another.
Namespace for csl library.
Definition: abreviation.h:34
Float()
Default constructor. expanded = CSL_0;.
Definition: numerical.h:388
std::string printLaTeX(int mode=0) const override
Creates a LaTeX output for the Abstract.
Definition: numerical.cpp:595
Abstract class from which derive all building blocks of exprs, i.e. objects not function of further e...
Definition: buildingBlock.h:34
Expr imag
Definition: numerical.h:402
bool operator==(Expr_info expr) const override
Compares the Abstract with another.
Definition: numerical.cpp:735
Expr exponentiation_own(const Expr &expr) const override
Contains implementation of special exponentiation for Numerical- and Vectorial-types.
Definition: numerical.cpp:701
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47
virtual long double evaluateScalar() const
Evaluates the value of the Abstract.
Definition: abstract.cpp:871
IntFraction()
Default Constructor.
Definition: numerical.h:392
virtual void print(int mode=0, std::ostream &out=std::cout, bool lib=false) const =0
Displays the abstract in standard output.
Expr division_own(const Expr &expr) const override
Contains implementation of special division for Numerical- and Polynomial-types. For polynomial...
Definition: numerical.cpp:676
void print(int mode=0, std::ostream &out=std::cout, bool lib=false) const override
Displays the abstract in standard output.
Definition: numerical.cpp:562
std::optional< Expr > getComplexModulus() const override
Evaluates the modulus in the complex plane of the Abstract and returns it.
Definition: buildingBlock.cpp:53
virtual long long int getNum() const
Definition: abstract.cpp:187
csl::PrimaryType getPrimaryType() const override
Gives the primary type of Abstract.
Definition: numerical.h:39
Definition: numerical.h:396
virtual Expr exponentiation_own(const Expr &expr) const
Contains implementation of special exponentiation for Numerical- and Vectorial-types.
Definition: abstract.cpp:899
long long int value
Value of the number.
Definition: numerical.h:82
bool dependsOn(Expr_info) const override
Check recursively if the expression depends on expr.
Definition: numerical.h:53
virtual std::optional< Expr > getComplexArgument() const
Evaluates the argument in the complex plane of the Abstract and returns it.
Definition: abstract.cpp:677
virtual void operator=(double t_value)
Equivalent to the setValue() function.
Definition: abstract.cpp:972
std::optional< Expr > getTerm() const override
This function returns the same expression as *this but amputated of its numerical factor...
Definition: numerical.cpp:60
virtual long long int getDenom() const
Definition: abstract.cpp:192
Expr getNumericalFactor() const override
Returns the numerical factor of the expression, i.e. returns C if the expression if of the form C*x (...
Definition: numerical.cpp:55
virtual bool operator==(int t_value) const
Definition: abstract.cpp:988
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
std::optional< Expr > getRealPart() const override
Evaluates the real part of the Abstract and returns it.
Definition: buildingBlock.cpp:49
bool dependsExplicitlyOn(Expr_info) const override
Check recursively if expr is present in the expression.
Definition: numerical.h:59
bool isInteger() const override
Tells if the expression is an integer. Either an Integer object directly, or a Float that has an inte...
Definition: numerical.h:105
Handle numbers in expr.
Definition: numerical.h:168
std::optional< Expr > evaluate(csl::eval::mode user_mode=csl::eval::base) const override
Evaluates the Abstract.
Definition: numerical.cpp:612
long long int num
Definition: numerical.h:271
long long int denom
Definition: numerical.h:272
Root class of the inheritance tree of abstracts.
Definition: abstract.h:76
Expr real
Definition: numerical.h:400
long double evaluateScalar() const override
Evaluates the IntFraction.
Definition: numerical.cpp:608
Base class for all parents (indicial, fields etc). All parents derive from this class.
Definition: parent.h:81
Integer()
Default constructor.
Definition: numerical.h:383
virtual bool isInteger() const
Tells if the expression is an integer. Either an Integer object directly, or a Float that has an inte...
Definition: abstract.cpp:568
csl::Type getType() const override
Gives the type of a IntFraction.
Definition: numerical.cpp:552
virtual Expr multiplication_own(const Expr &expr, bool side=1) const
Contains implementation of special multiplication for Numerical- and Vectorial-types.
Definition: abstract.cpp:885
Abstract class from which derive numerical types, i.e. Float, Integer, IntFraction.
Definition: numerical.h:34
virtual Expr getImaginaryPart() const
Evaluates the imaginary part of the Abstract and returns it.
Definition: abstract.cpp:667
virtual std::string printLaTeX(int mode=0) const
Creates a LaTeX output for the Abstract.
Definition: abstract.cpp:168
long double value
Value of the number.
Definition: numerical.h:172
Handle numbers in expr.
Definition: numerical.h:78
virtual csl::Type getType() const =0
Gives the type of Abstract.
Handles fractions of Number.
Definition: numerical.h:267
virtual Expr division_own(const Expr &expr) const
Contains implementation of special division for Numerical- and Polynomial-types. For polynomial...
Definition: abstract.cpp:892
bool operator<(const Expr &a, const Expr &b)
see Abstract::operator<()
Definition: abstract.cpp:1423
std::optional< Expr > derive(Expr_info expr) const override
Derives the IntFraction wrt the argument.
Definition: numerical.cpp:719
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
virtual std::optional< Expr > getComplexConjugate() const
Calculates and returns the complex conjugate of the expression.
Definition: abstract.cpp:682
Expression type/.
Definition: abstract.h:1573