number.h
Go to the documentation of this file.
1 
7 #ifndef SYMENGINE_NUMBER_H
8 #define SYMENGINE_NUMBER_H
9 
10 #include <symengine/basic.h>
11 #include <symengine/assumptions.h>
12 
13 namespace SymEngine
14 {
15 
16 class Evaluate;
17 
18 class Number : public Basic
19 {
20 public:
22  virtual bool is_zero() const = 0;
24  virtual bool is_one() const = 0;
26  virtual bool is_minus_one() const = 0;
28  virtual bool is_negative() const = 0;
30  virtual bool is_positive() const = 0;
32  virtual bool is_complex() const = 0;
34  virtual RCP<const Basic> conjugate() const;
36  // false if the number is an approximation
37  virtual bool is_exact() const
38  {
39  return true;
40  };
42  inline bool is_exact_zero() const
43  {
44  return is_exact() and is_zero();
45  };
47  virtual Evaluate &get_eval() const
48  {
49  throw NotImplementedError("Not Implemented.");
50  };
51 
53  virtual RCP<const Number> add(const Number &other) const = 0;
55  virtual RCP<const Number> sub(const Number &other) const;
56  virtual RCP<const Number> rsub(const Number &other) const;
58  virtual RCP<const Number> mul(const Number &other) const = 0;
60  virtual RCP<const Number> div(const Number &other) const;
61  virtual RCP<const Number> rdiv(const Number &other) const;
63  virtual RCP<const Number> pow(const Number &other) const = 0;
64  virtual RCP<const Number> rpow(const Number &other) const = 0;
65 
66  vec_basic get_args() const override
67  {
68  return {};
69  }
70 
71  virtual bool is_perfect_power(bool is_expected = false) const
72  {
73  throw NotImplementedError("Not Implemented.");
74  };
75  virtual bool nth_root(const Ptr<RCP<const Number>> &, unsigned long n) const
76  {
77  throw NotImplementedError("Not Implemented.");
78  };
79 };
81 inline RCP<const Number> addnum(const RCP<const Number> &self,
82  const RCP<const Number> &other)
83 {
84  return self->add(*other);
85 }
87 inline RCP<const Number> subnum(const RCP<const Number> &self,
88  const RCP<const Number> &other)
89 {
90  return self->sub(*other);
91 }
93 inline RCP<const Number> mulnum(const RCP<const Number> &self,
94  const RCP<const Number> &other)
95 {
96  return self->mul(*other);
97 }
99 inline RCP<const Number> divnum(const RCP<const Number> &self,
100  const RCP<const Number> &other)
101 {
102  return self->div(*other);
103 }
105 inline RCP<const Number> pownum(const RCP<const Number> &self,
106  const RCP<const Number> &other)
107 {
108  return self->pow(*other);
109 }
110 
111 inline void iaddnum(const Ptr<RCP<const Number>> &self,
112  const RCP<const Number> &other)
113 {
114  *self = addnum(*self, other);
115 }
116 
117 inline void imulnum(const Ptr<RCP<const Number>> &self,
118  const RCP<const Number> &other)
119 {
120  *self = mulnum(*self, other);
121 }
122 
123 inline void idivnum(const Ptr<RCP<const Number>> &self,
124  const RCP<const Number> &other)
125 {
126  *self = divnum(*self, other);
127 }
128 
130 inline bool is_a_Number(const Basic &b)
131 {
132  // `NumberWraper` is the last subclass of Number in TypeID
133  // An enum should be before `SYMENIGNE_NUMBER_WRAPPER` iff it is a
134  // subclass of Number
135  return b.get_type_code() <= SYMENGINE_NUMBER_WRAPPER;
136 }
137 
139 inline bool is_number_and_zero(const Basic &b)
140 {
141  return is_a_Number(b) and down_cast<const Number &>(b).is_zero();
142 }
143 
152 tribool is_zero(const Basic &b, const Assumptions *assumptions = nullptr);
161 tribool is_nonzero(const Basic &b, const Assumptions *assumptions = nullptr);
162 tribool is_positive(const Basic &b, const Assumptions *assumptions = nullptr);
163 tribool is_nonpositive(const Basic &b,
164  const Assumptions *assumptions = nullptr);
165 tribool is_negative(const Basic &b, const Assumptions *assumptions = nullptr);
166 tribool is_nonnegative(const Basic &b,
167  const Assumptions *assumptions = nullptr);
168 tribool is_integer(const Basic &b, const Assumptions *assumptions = nullptr);
169 tribool is_real(const Basic &b, const Assumptions *assumptions = nullptr);
170 tribool is_complex(const Basic &b, const Assumptions *assumptions = nullptr);
171 tribool is_rational(const Basic &b);
172 tribool is_irrational(const Basic &b);
173 tribool is_finite(const Basic &b, const Assumptions *assumptions = nullptr);
174 tribool is_infinite(const Basic &b, const Assumptions *assumptions = nullptr);
175 tribool is_even(const Basic &b, const Assumptions *assumptions = nullptr);
176 tribool is_odd(const Basic &b, const Assumptions *assumptions = nullptr);
177 tribool is_algebraic(const Basic &b, const Assumptions *assumptions = nullptr);
178 tribool is_transcendental(const Basic &b,
179  const Assumptions *assumptions = nullptr);
180 
181 class NumberWrapper : public Number
182 {
183 public:
184  NumberWrapper(){SYMENGINE_ASSIGN_TYPEID()}
185 
186  IMPLEMENT_TYPEID(SYMENGINE_NUMBER_WRAPPER)
187 
188  virtual std::string __str__() const
189  {
190  throw NotImplementedError("Not Implemented.");
191  };
192  virtual RCP<const Number> eval(long bits) const
193  {
194  throw NotImplementedError("Not Implemented.");
195  };
196 };
197 
199 class Evaluate
200 {
201 public:
202  virtual RCP<const Basic> sin(const Basic &) const = 0;
203  virtual RCP<const Basic> cos(const Basic &) const = 0;
204  virtual RCP<const Basic> tan(const Basic &) const = 0;
205  virtual RCP<const Basic> cot(const Basic &) const = 0;
206  virtual RCP<const Basic> sec(const Basic &) const = 0;
207  virtual RCP<const Basic> csc(const Basic &) const = 0;
208  virtual RCP<const Basic> asin(const Basic &) const = 0;
209  virtual RCP<const Basic> acos(const Basic &) const = 0;
210  virtual RCP<const Basic> atan(const Basic &) const = 0;
211  virtual RCP<const Basic> acot(const Basic &) const = 0;
212  virtual RCP<const Basic> asec(const Basic &) const = 0;
213  virtual RCP<const Basic> acsc(const Basic &) const = 0;
214  virtual RCP<const Basic> sinh(const Basic &) const = 0;
215  virtual RCP<const Basic> csch(const Basic &) const = 0;
216  virtual RCP<const Basic> cosh(const Basic &) const = 0;
217  virtual RCP<const Basic> sech(const Basic &) const = 0;
218  virtual RCP<const Basic> tanh(const Basic &) const = 0;
219  virtual RCP<const Basic> coth(const Basic &) const = 0;
220  virtual RCP<const Basic> asinh(const Basic &) const = 0;
221  virtual RCP<const Basic> acsch(const Basic &) const = 0;
222  virtual RCP<const Basic> acosh(const Basic &) const = 0;
223  virtual RCP<const Basic> atanh(const Basic &) const = 0;
224  virtual RCP<const Basic> acoth(const Basic &) const = 0;
225  virtual RCP<const Basic> asech(const Basic &) const = 0;
226  virtual RCP<const Basic> log(const Basic &) const = 0;
227  virtual RCP<const Basic> gamma(const Basic &) const = 0;
228  virtual RCP<const Basic> abs(const Basic &) const = 0;
229  virtual RCP<const Basic> exp(const Basic &) const = 0;
230  virtual RCP<const Basic> floor(const Basic &) const = 0;
231  virtual RCP<const Basic> ceiling(const Basic &) const = 0;
232  virtual RCP<const Basic> truncate(const Basic &) const = 0;
233  virtual RCP<const Basic> erf(const Basic &) const = 0;
234  virtual RCP<const Basic> erfc(const Basic &) const = 0;
235 };
236 
237 } // namespace SymEngine
238 
239 #endif
The base class for SymEngine.
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition: basic.h:340
The lowest unit of symbolic representation.
Definition: basic.h:97
A class that will evaluate functions numerically.
Definition: number.h:200
virtual Evaluate & get_eval() const
Get Evaluate singleton to evaluate numerically.
Definition: number.h:47
virtual RCP< const Number > mul(const Number &other) const =0
Multiplication.
virtual bool is_one() const =0
virtual bool is_complex() const =0
virtual bool is_exact() const
return true if the number is an exact representation
Definition: number.h:37
virtual RCP< const Number > add(const Number &other) const =0
Addition.
virtual RCP< const Number > pow(const Number &other) const =0
Power.
virtual bool is_negative() const =0
bool is_exact_zero() const
Definition: number.h:42
virtual RCP< const Basic > conjugate() const
Definition: number.cpp:8
virtual bool is_positive() const =0
virtual RCP< const Number > div(const Number &other) const
Division.
Definition: number.cpp:26
virtual RCP< const Number > sub(const Number &other) const
Subtraction.
Definition: number.cpp:16
virtual bool is_zero() const =0
virtual bool is_minus_one() const =0
vec_basic get_args() const override
Returns the list of arguments.
Definition: number.h:66
Main namespace for SymEngine package.
Definition: add.cpp:19
bool is_a_Number(const Basic &b)
Definition: number.h:130
bool is_number_and_zero(const Basic &b)
Definition: number.h:139
RCP< const Number > mulnum(const RCP< const Number > &self, const RCP< const Number > &other)
Multiply self and other
Definition: number.h:93
RCP< const Number > pownum(const RCP< const Number > &self, const RCP< const Number > &other)
Raise self to power other
Definition: number.h:105
RCP< const Number > divnum(const RCP< const Number > &self, const RCP< const Number > &other)
Divide self and other
Definition: number.h:99
RCP< const Number > subnum(const RCP< const Number > &self, const RCP< const Number > &other)
Subtract self and other
Definition: number.h:87
tribool is_nonzero(const Basic &b, const Assumptions *assumptions=nullptr)
Check if a number is non-zero.
RCP< const Number > addnum(const RCP< const Number > &self, const RCP< const Number > &other)
Add self and other
Definition: number.h:81