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 #include <symengine/logic.h>
7 #ifdef WITH_SYMENGINE_THREAD_SAFE
8 #include <atomic>
9 #endif
10 #include <cstddef>
11 
12 namespace SymEngine
13 {
14 
16  : name_{name} {SYMENGINE_ASSIGN_TYPEID()}
17 
18  hash_t Constant::__hash__() const
19 {
20  hash_t seed = SYMENGINE_CONSTANT;
21  hash_combine<std::string>(seed, name_);
22  return seed;
23 }
24 
25 bool Constant::__eq__(const Basic &o) const
26 {
27  if (is_a<Constant>(o))
28  return name_ == down_cast<const Constant &>(o).name_;
29  return false;
30 }
31 
32 int Constant::compare(const Basic &o) const
33 {
34  SYMENGINE_ASSERT(is_a<Constant>(o))
35  const Constant &s = down_cast<const Constant &>(o);
36  if (name_ == s.name_)
37  return 0;
38  return name_ < s.name_ ? -1 : 1;
39 }
40 
41 #define sqrt_(arg) pow(arg, div(one, i2))
42 
43 #ifdef WITH_SYMENGINE_THREAD_SAFE
44 static std::atomic<int> nifty_counter;
45 #else
46 static int nifty_counter;
47 #endif
48 
49 // Some global variables are declared in functions.cpp
50 // Look over https://github.com/sympy/symengine/issues/272
51 // for further details
52 #define DEFINE_CONSTANTS \
53  DEFINE_CONSTANT(Integer, zero, integer(0)); \
54  DEFINE_CONSTANT(Integer, one, integer(1)); \
55  DEFINE_CONSTANT(Integer, minus_one, integer(-1)); \
56  DEFINE_CONSTANT(Integer, two, integer(2)); \
57  DEFINE_CONSTANT(Number, I, Complex::from_two_nums(*zero, *one)); \
58  DEFINE_CONSTANT(Constant, pi, constant("pi")); \
59  DEFINE_CONSTANT(Constant, E, constant("E")); \
60  DEFINE_CONSTANT(Constant, EulerGamma, constant("EulerGamma")); \
61  DEFINE_CONSTANT(Constant, Catalan, constant("Catalan")); \
62  DEFINE_CONSTANT(Constant, GoldenRatio, constant("GoldenRatio")); \
63  DEFINE_CONSTANT(Infty, Inf, Infty::from_int(1)); \
64  DEFINE_CONSTANT(Infty, NegInf, Infty::from_int(-1)); \
65  DEFINE_CONSTANT(Infty, ComplexInf, Infty::from_int(0)); \
66  DEFINE_CONSTANT(NaN, Nan, make_rcp<NaN>()); \
67  DEFINE_CONSTANT(Basic, i2, integer(2)); \
68  DEFINE_CONSTANT(Basic, i3, integer(3)); \
69  DEFINE_CONSTANT(Basic, i5, integer(5)); \
70  DEFINE_CONSTANT(Basic, im2, integer(-2)); \
71  DEFINE_CONSTANT(Basic, im3, integer(-3)); \
72  DEFINE_CONSTANT(Basic, im5, integer(-5)); \
73  DEFINE_CONSTANT(Basic, sq3, sqrt_(i3)); \
74  DEFINE_CONSTANT(Basic, sq2, sqrt_(i2)); \
75  DEFINE_CONSTANT(Basic, sq5, sqrt_(i5)); \
76  DEFINE_CONSTANT(Basic, C0, div(sub(sq3, one), mul(i2, sq2))); \
77  DEFINE_CONSTANT(Basic, C1, div(one, i2)); \
78  DEFINE_CONSTANT(Basic, C2, div(sq2, i2)); \
79  DEFINE_CONSTANT(Basic, C3, div(sq3, i2)); \
80  DEFINE_CONSTANT(Basic, C4, div(add(sq3, one), mul(i2, sq2))); \
81  DEFINE_CONSTANT(Basic, C5, div(sqrt_(sub(i5, sqrt_(i5))), integer(8))); \
82  DEFINE_CONSTANT(Basic, C6, div(sub(sqrt_(i5), one), integer(4))); \
83  DEFINE_CONSTANT(Basic, mC0, mul(minus_one, C0)); \
84  DEFINE_CONSTANT(Basic, mC1, mul(minus_one, C1)); \
85  DEFINE_CONSTANT(Basic, mC2, mul(minus_one, C2)); \
86  DEFINE_CONSTANT(Basic, mC3, mul(minus_one, C3)); \
87  DEFINE_CONSTANT(Basic, mC4, mul(minus_one, C4)); \
88  DEFINE_CONSTANT(Basic, mC5, mul(minus_one, C5)); \
89  DEFINE_CONSTANT(Basic, mC6, mul(minus_one, C6)); \
90  DEFINE_CONSTANT(BooleanAtom, boolTrue, make_rcp<BooleanAtom>(true)); \
91  DEFINE_CONSTANT(BooleanAtom, boolFalse, make_rcp<BooleanAtom>(false));
92 
93 #if __cplusplus >= 201703L
94 template <typename T>
95 struct storage_for {
96  alignas(T) std::byte data[sizeof(T)];
97 };
98 #else
99 template <typename T>
100 struct storage_for {
101  typename std::aligned_storage<sizeof(T), alignof(T)>::type data;
102 };
103 #endif
104 
105 #define DEFINE_CONSTANT(t, n, d) \
106  static storage_for<RCP<const t>> n##_buf; \
107  RCP<const t> &n = reinterpret_cast<RCP<const t> &>(n##_buf);
108 
109 DEFINE_CONSTANTS
110 #undef DEFINE_CONSTANT
111 
112 ConstantInitializer::ConstantInitializer()
113 {
114  if (nifty_counter++ == 0) {
115 #define DEFINE_CONSTANT(t, n, d) new (&n) RCP<const t>(d);
116  DEFINE_CONSTANTS
117 #undef DEFINE_CONSTANT
118  }
119 }
120 
121 ConstantInitializer::~ConstantInitializer()
122 {
123  if (--nifty_counter == 0) {
124 #define DEFINE_CONSTANT(t, n, d) n.~RCP();
125  DEFINE_CONSTANTS
126 #undef DEFINE_CONSTANT
127  }
128 }
129 
130 } // namespace SymEngine
Classes and functions relating to the binary operation of addition.
The lowest unit of symbolic representation.
Definition: basic.h:97
hash_t __hash__() const override
Definition: constants.cpp:18
std::string name_
name of Constant
Definition: constants.h:22
Constant(const std::string &name)
Constant Constructor.
Definition: constants.cpp:15
int compare(const Basic &o) const override
Definition: constants.cpp:32
bool __eq__(const Basic &o) const override
Definition: constants.cpp:25
Main namespace for SymEngine package.
Definition: add.cpp:19