constants.cpp
1 #include <symengine/complex.h>
2 #include <symengine/add.h>
3 #include <symengine/infinity.h>
4 #include <symengine/pow.h>
5 #include <symengine/nan.h>
6 
7 namespace SymEngine
8 {
9 
11  : name_{name} {SYMENGINE_ASSIGN_TYPEID()}
12 
13  hash_t Constant::__hash__() const
14 {
15  hash_t seed = SYMENGINE_CONSTANT;
16  hash_combine<std::string>(seed, name_);
17  return seed;
18 }
19 
20 bool Constant::__eq__(const Basic &o) const
21 {
22  if (is_a<Constant>(o))
23  return name_ == down_cast<const Constant &>(o).name_;
24  return false;
25 }
26 
27 int Constant::compare(const Basic &o) const
28 {
29  SYMENGINE_ASSERT(is_a<Constant>(o))
30  const Constant &s = down_cast<const Constant &>(o);
31  if (name_ == s.name_)
32  return 0;
33  return name_ < s.name_ ? -1 : 1;
34 }
35 
36 #define DEFINE_CONSTANT(t, n, d) \
37  RCP<const t> n = []() { \
38  static const RCP<const t> c = d; \
39  return c; \
40  }()
41 
42 DEFINE_CONSTANT(Integer, zero, integer(0));
43 DEFINE_CONSTANT(Integer, one, integer(1));
44 DEFINE_CONSTANT(Integer, minus_one, integer(-1));
45 DEFINE_CONSTANT(Integer, two, integer(2));
46 
47 DEFINE_CONSTANT(Number, I, Complex::from_two_nums(*zero, *one));
48 
49 DEFINE_CONSTANT(Constant, pi, constant("pi"));
50 DEFINE_CONSTANT(Constant, E, constant("E"));
51 DEFINE_CONSTANT(Constant, EulerGamma, constant("EulerGamma"));
52 DEFINE_CONSTANT(Constant, Catalan, constant("Catalan"));
53 DEFINE_CONSTANT(Constant, GoldenRatio, constant("GoldenRatio"));
54 
55 DEFINE_CONSTANT(Infty, Inf, Infty::from_int(1));
56 DEFINE_CONSTANT(Infty, NegInf, Infty::from_int(-1));
57 DEFINE_CONSTANT(Infty, ComplexInf, Infty::from_int(0));
58 
59 DEFINE_CONSTANT(NaN, Nan, make_rcp<NaN>());
60 
61 // Global variables declared in functions.cpp
62 // Look over https://github.com/sympy/symengine/issues/272
63 // for further details
64 DEFINE_CONSTANT(Basic, i2, integer(2));
65 
66 namespace
67 {
68 RCP<const Basic> sqrt_(const RCP<const Basic> &arg)
69 {
70  return pow(arg, div(one, i2));
71 }
72 } // namespace
73 
74 DEFINE_CONSTANT(Basic, i3, integer(3));
75 DEFINE_CONSTANT(Basic, i5, integer(5));
76 DEFINE_CONSTANT(Basic, im2, integer(-2));
77 DEFINE_CONSTANT(Basic, im3, integer(-3));
78 DEFINE_CONSTANT(Basic, im5, integer(-5));
79 
80 DEFINE_CONSTANT(Basic, sq3, sqrt_(i3));
81 DEFINE_CONSTANT(Basic, sq2, sqrt_(i2));
82 DEFINE_CONSTANT(Basic, sq5, sqrt_(i5));
83 
84 DEFINE_CONSTANT(Basic, C0, div(sub(sq3, one), mul(i2, sq2)));
85 DEFINE_CONSTANT(Basic, C1, div(one, i2));
86 DEFINE_CONSTANT(Basic, C2, div(sq2, i2));
87 DEFINE_CONSTANT(Basic, C3, div(sq3, i2));
88 DEFINE_CONSTANT(Basic, C4, div(add(sq3, one), mul(i2, sq2)));
89 DEFINE_CONSTANT(Basic, C5, div(sqrt_(sub(i5, sqrt_(i5))), integer(8)));
90 DEFINE_CONSTANT(Basic, C6, div(sub(sqrt_(i5), one), integer(4)));
91 
92 DEFINE_CONSTANT(Basic, mC0, mul(minus_one, C0));
93 DEFINE_CONSTANT(Basic, mC1, mul(minus_one, C1));
94 DEFINE_CONSTANT(Basic, mC2, mul(minus_one, C2));
95 DEFINE_CONSTANT(Basic, mC3, mul(minus_one, C3));
96 DEFINE_CONSTANT(Basic, mC4, mul(minus_one, C4));
97 DEFINE_CONSTANT(Basic, mC5, mul(minus_one, C5));
98 DEFINE_CONSTANT(Basic, mC6, mul(minus_one, C6));
99 
100 #undef DEFINE_CONSTANT
101 
102 } // namespace SymEngine
Classes and functions relating to the binary operation of addition.
The lowest unit of symbolic representation.
Definition: basic.h:97
static RCP< const Number > from_two_nums(const Number &re, const Number &im)
Definition: complex.cpp:109
hash_t __hash__() const override
Definition: constants.cpp:13
std::string name_
name of Constant
Definition: constants.h:22
Constant(const std::string &name)
Constant Constructor.
Definition: constants.cpp:10
int compare(const Basic &o) const override
Definition: constants.cpp:27
bool __eq__(const Basic &o) const override
Definition: constants.cpp:20
static RCP< const Infty > from_int(const int val)
Constructs Infty using sign of val
Definition: infinity.cpp:33
Integer Class.
Definition: integer.h:19
Main namespace for SymEngine package.
Definition: add.cpp:19
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
Definition: mul.cpp:431
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
Definition: integer.h:197
RCP< const Basic > sub(const RCP< const Basic > &a, const RCP< const Basic > &b)
Substracts b from a.
Definition: add.cpp:495
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition: mul.cpp:352
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition: add.cpp:425
RCP< const Constant > constant(const std::string &name)
inline version to return Constant
Definition: constants.h:53