Loading...
Searching...
No Matches
real_double.h
1
6#ifndef SYMENGINE_REAL_DOUBLE_H
7#define SYMENGINE_REAL_DOUBLE_H
8
9#include <symengine/complex.h>
10#include <symengine/symengine_exception.h>
11
12namespace SymEngine
13{
14
15RCP<const Number> number(std::complex<double> x);
16RCP<const Number> number(double x);
17
19class RealDouble : public Number
20{
21public:
22 double i;
23
24public:
27 explicit RealDouble(double i);
29 hash_t __hash__() const override;
34 bool __eq__(const Basic &o) const override;
35 int compare(const Basic &o) const override;
37 inline bool is_positive() const override
38 {
39 return i > 0;
40 }
42 inline bool is_negative() const override
43 {
44 return i < 0;
45 }
47 inline double as_double() const
48 {
49 return i;
50 }
52 inline bool is_exact() const override
53 {
54 return false;
55 }
57 Evaluate &get_eval() const override;
58
60 bool is_zero() const override
61 {
62 return this->i == 0.0;
63 }
65 // A double is not exactly equal to `1`
66 bool is_one() const override
67 {
68 return false;
69 }
71 // A double is not exactly equal to `-1`
72 bool is_minus_one() const override
73 {
74 return false;
75 }
77 // False is returned because a RealDouble cannot have a imaginary part
78 bool is_complex() const override
79 {
80 return false;
81 }
82
86 RCP<const Number> addreal(const Integer &other) const
87 {
89 + mp_get_d(other.as_integer_class()));
90 }
91
95 RCP<const Number> addreal(const Rational &other) const
96 {
98 i + mp_get_d(other.as_rational_class()));
99 }
100
104 RCP<const Number> addreal(const Complex &other) const
105 {
106 return number(i
107 + std::complex<double>(mp_get_d(other.real_),
108 mp_get_d(other.imaginary_)));
109 }
110
114 RCP<const Number> addreal(const RealDouble &other) const
115 {
116 return make_rcp<const RealDouble>(i + other.i);
117 }
118
120 RCP<const Number> add(const Number &other) const override
121 {
122 if (is_a<Rational>(other)) {
124 } else if (is_a<Integer>(other)) {
126 } else if (is_a<Complex>(other)) {
128 } else if (is_a<RealDouble>(other)) {
130 } else {
131 return other.add(*this);
132 }
133 }
134
138 RCP<const Number> subreal(const Integer &other) const
139 {
141 - mp_get_d(other.as_integer_class()));
142 }
143
147 RCP<const Number> subreal(const Rational &other) const
148 {
150 i - mp_get_d(other.as_rational_class()));
151 }
152
156 RCP<const Number> subreal(const Complex &other) const
157 {
158 return number(i
159 - std::complex<double>(mp_get_d(other.real_),
160 mp_get_d(other.imaginary_)));
161 }
162
166 RCP<const Number> subreal(const RealDouble &other) const
167 {
168 return make_rcp<const RealDouble>(i - other.i);
169 }
170
172 RCP<const Number> sub(const Number &other) const override
173 {
174 if (is_a<Rational>(other)) {
176 } else if (is_a<Integer>(other)) {
178 } else if (is_a<Complex>(other)) {
180 } else if (is_a<RealDouble>(other)) {
182 } else {
183 return other.rsub(*this);
184 }
185 }
186
190 RCP<const Number> rsubreal(const Integer &other) const
191 {
192 return make_rcp<const RealDouble>(mp_get_d(other.as_integer_class())
193 - i);
194 }
195
199 RCP<const Number> rsubreal(const Rational &other) const
200 {
201 return make_rcp<const RealDouble>(mp_get_d(other.as_rational_class())
202 - i);
203 }
204
208 RCP<const Number> rsubreal(const Complex &other) const
209 {
210 return number(-i
211 + std::complex<double>(mp_get_d(other.real_),
212 mp_get_d(other.imaginary_)));
213 }
214
216 RCP<const Number> rsub(const Number &other) const override
217 {
218 if (is_a<Rational>(other)) {
220 } else if (is_a<Integer>(other)) {
222 } else if (is_a<Complex>(other)) {
224 } else {
225 throw NotImplementedError("Not Implemented");
226 }
227 }
228
232 RCP<const Number> mulreal(const Integer &other) const
233 {
234 if (other.is_zero()) {
235 return zero;
236 }
238 * mp_get_d(other.as_integer_class()));
239 }
240
244 RCP<const Number> mulreal(const Rational &other) const
245 {
247 i * mp_get_d(other.as_rational_class()));
248 }
249
253 RCP<const Number> mulreal(const Complex &other) const
254 {
255 return number(i
256 * std::complex<double>(mp_get_d(other.real_),
257 mp_get_d(other.imaginary_)));
258 }
259
263 RCP<const Number> mulreal(const RealDouble &other) const
264 {
265 return make_rcp<const RealDouble>(i * other.i);
266 }
267
269 RCP<const Number> mul(const Number &other) const override
270 {
271 if (is_a<Rational>(other)) {
273 } else if (is_a<Integer>(other)) {
275 } else if (is_a<Complex>(other)) {
277 } else if (is_a<RealDouble>(other)) {
279 } else {
280 return other.mul(*this);
281 }
282 }
283
287 RCP<const Number> divreal(const Integer &other) const
288 {
290 / mp_get_d(other.as_integer_class()));
291 }
292
296 RCP<const Number> divreal(const Rational &other) const
297 {
299 i / mp_get_d(other.as_rational_class()));
300 }
301
305 RCP<const Number> divreal(const Complex &other) const
306 {
307 return number(i
308 / std::complex<double>(mp_get_d(other.real_),
309 mp_get_d(other.imaginary_)));
310 }
311
315 RCP<const Number> divreal(const RealDouble &other) const
316 {
317 return make_rcp<const RealDouble>(i / other.i);
318 }
319
321 RCP<const Number> div(const Number &other) const override
322 {
323 if (is_a<Rational>(other)) {
325 } else if (is_a<Integer>(other)) {
327 } else if (is_a<Complex>(other)) {
329 } else if (is_a<RealDouble>(other)) {
331 } else {
332 return other.rdiv(*this);
333 }
334 }
335
339 RCP<const Number> rdivreal(const Integer &other) const
340 {
341 return make_rcp<const RealDouble>(mp_get_d(other.as_integer_class())
342 / i);
343 }
344
348 RCP<const Number> rdivreal(const Rational &other) const
349 {
350 return make_rcp<const RealDouble>(mp_get_d(other.as_rational_class())
351 / i);
352 }
353
357 RCP<const Number> rdivreal(const Complex &other) const
358 {
359 return number(std::complex<double>(mp_get_d(other.real_),
360 mp_get_d(other.imaginary_))
361 / i);
362 }
363
365 RCP<const Number> rdiv(const Number &other) const override
366 {
367 if (is_a<Rational>(other)) {
369 } else if (is_a<Integer>(other)) {
371 } else if (is_a<Complex>(other)) {
373 } else {
374 throw NotImplementedError("Not Implemented");
375 }
376 }
377
381 RCP<const Number> powreal(const Integer &other) const
382 {
384 std::pow(i, mp_get_d(other.as_integer_class())));
385 }
386
390 RCP<const Number> powreal(const Rational &other) const
391 {
392 if (i < 0) {
393 return number(std::pow(std::complex<double>(i),
394 mp_get_d(other.as_rational_class())));
395 }
397 std::pow(i, mp_get_d(other.as_rational_class())));
398 }
399
403 RCP<const Number> powreal(const Complex &other) const
404 {
405 return number(
406 std::pow(i, std::complex<double>(mp_get_d(other.real_),
407 mp_get_d(other.imaginary_))));
408 }
409
413 RCP<const Number> powreal(const RealDouble &other) const
414 {
415 if (i < 0) {
416 return number(std::pow(std::complex<double>(i), other.i));
417 }
419 }
420
422 RCP<const Number> pow(const Number &other) const override
423 {
424 if (is_a<Rational>(other)) {
426 } else if (is_a<Integer>(other)) {
428 } else if (is_a<Complex>(other)) {
430 } else if (is_a<RealDouble>(other)) {
432 } else {
433 return other.rpow(*this);
434 }
435 }
436
440 RCP<const Number> rpowreal(const Integer &other) const
441 {
442 if (other.is_negative()) {
443 return number(std::pow(mp_get_d(other.as_integer_class()),
445 }
447 std::pow(mp_get_d(other.as_integer_class()), i));
448 }
449
453 RCP<const Number> rpowreal(const Rational &other) const
454 {
455 if (other.is_negative()) {
456 return number(std::pow(std::complex<double>(i),
457 mp_get_d(other.as_rational_class())));
458 }
460 std::pow(mp_get_d(other.as_rational_class()), i));
461 }
462
466 RCP<const Number> rpowreal(const Complex &other) const
467 {
468 return number(std::pow(std::complex<double>(mp_get_d(other.real_),
469 mp_get_d(other.imaginary_)),
470 i));
471 }
472
474 RCP<const Number> rpow(const Number &other) const override
475 {
476 if (is_a<Rational>(other)) {
478 } else if (is_a<Integer>(other)) {
480 } else if (is_a<Complex>(other)) {
482 } else {
483 throw NotImplementedError("Not Implemented");
484 }
485 }
486};
487
488RCP<const RealDouble> real_double(double x);
489
490} // namespace SymEngine
491
492#endif
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition basic.h:340
The lowest unit of symbolic representation.
Definition basic.h:97
Complex Class.
Definition complex.h:33
A class that will evaluate functions numerically.
Definition number.h:200
Integer Class.
Definition integer.h:19
Rational Class.
Definition rational.h:16
RealDouble Class to hold double values.
Definition real_double.h:20
RCP< const Number > rpow(const Number &other) const override
Converts the param other appropriately and then calls powreal
bool is_minus_one() const override
Definition real_double.h:72
RCP< const Number > divreal(const Complex &other) const
RCP< const Number > addreal(const Complex &other) const
RCP< const Number > addreal(const RealDouble &other) const
bool is_zero() const override
Definition real_double.h:60
RCP< const Number > powreal(const Rational &other) const
RCP< const Number > subreal(const RealDouble &other) const
RCP< const Number > rdivreal(const Integer &other) const
RCP< const Number > powreal(const RealDouble &other) const
RCP< const Number > sub(const Number &other) const override
Converts the param other appropriately and then calls subreal
RCP< const Number > rdivreal(const Complex &other) const
RCP< const Number > div(const Number &other) const override
Converts the param other appropriately and then calls divreal
hash_t __hash__() const override
RCP< const Number > rsub(const Number &other) const override
Converts the param other appropriately and then calls subreal
bool is_one() const override
Definition real_double.h:66
RCP< const Number > rpowreal(const Integer &other) const
RCP< const Number > rpowreal(const Rational &other) const
Evaluate & get_eval() const override
Get Evaluate singleton to evaluate numerically.
bool is_positive() const override
Definition real_double.h:37
double as_double() const
Definition real_double.h:47
bool __eq__(const Basic &o) const override
RCP< const Number > add(const Number &other) const override
Converts the param other appropriately and then calls addreal
RCP< const Number > divreal(const Rational &other) const
RCP< const Number > mulreal(const RealDouble &other) const
RCP< const Number > divreal(const RealDouble &other) const
int compare(const Basic &o) const override
RCP< const Number > powreal(const Integer &other) const
RCP< const Number > rsubreal(const Integer &other) const
RCP< const Number > rdiv(const Number &other) const override
Converts the param other appropriately and then calls divreal
RCP< const Number > addreal(const Rational &other) const
Definition real_double.h:95
bool is_negative() const override
Definition real_double.h:42
bool is_complex() const override
Definition real_double.h:78
RCP< const Number > subreal(const Rational &other) const
RCP< const Number > rsubreal(const Complex &other) const
RCP< const Number > rpowreal(const Complex &other) const
RCP< const Number > mulreal(const Rational &other) const
RCP< const Number > subreal(const Complex &other) const
RCP< const Number > mul(const Number &other) const override
Converts the param other appropriately and then calls mulreal
RCP< const Number > powreal(const Complex &other) const
RCP< const Number > rdivreal(const Rational &other) const
RCP< const Number > mulreal(const Complex &other) const
RCP< const Number > pow(const Number &other) const override
Converts the param other appropriately and then calls powreal
bool is_exact() const override
Definition real_double.h:52
RCP< const Number > rsubreal(const Rational &other) const
RCP< const Number > addreal(const Integer &other) const
Definition real_double.h:86
RCP< const Number > mulreal(const Integer &other) const
RCP< const Number > subreal(const Integer &other) const
RCP< const Number > divreal(const Integer &other) const
Main namespace for SymEngine package.
Definition add.cpp:19
void hash_combine(hash_t &seed, const T &v)
Definition basic-inl.h:95
T pow(T... args)