integer.h
Go to the documentation of this file.
1 
7 #ifndef SYMENGINE_INTEGER_H
8 #define SYMENGINE_INTEGER_H
9 
10 #include <symengine/number.h>
11 #include <symengine/symengine_exception.h>
12 #include <symengine/symengine_casts.h>
13 
14 namespace SymEngine
15 {
16 
18 class Integer : public Number
19 {
20 private:
22  integer_class i;
23 
24 public:
25  IMPLEMENT_TYPEID(SYMENGINE_INTEGER)
27  // explicit Integer(integer_class i);
28  Integer(const integer_class &_i)
29  : i(_i){SYMENGINE_ASSIGN_TYPEID()} Integer(integer_class && _i)
30  : i(std::move(_i))
31  {
32  SYMENGINE_ASSIGN_TYPEID()
33  }
35  virtual hash_t __hash__() const;
40  virtual bool __eq__(const Basic &o) const;
41  virtual int compare(const Basic &o) const;
42 
44  signed long int as_int() const;
46  unsigned long int as_uint() const;
48  inline const integer_class &as_integer_class() const
49  {
50  return this->i;
51  }
53  inline virtual bool is_zero() const
54  {
55  return this->i == 0u;
56  }
58  inline virtual bool is_one() const
59  {
60  return this->i == 1u;
61  }
63  inline virtual bool is_minus_one() const
64  {
65  return this->i == -1;
66  }
68  inline virtual bool is_positive() const
69  {
70  return this->i > 0u;
71  }
73  inline virtual bool is_negative() const
74  {
75  return this->i < 0u;
76  }
78  // False is returned because a pure integer cannot have an imaginary part
79  inline virtual bool is_complex() const
80  {
81  return false;
82  }
83 
84  /* These are very fast methods for add/sub/mul/div/pow on Integers only */
86  inline RCP<const Integer> addint(const Integer &other) const
87  {
88  return make_rcp<const Integer>(this->i + other.i);
89  }
91  inline RCP<const Integer> subint(const Integer &other) const
92  {
93  return make_rcp<const Integer>(this->i - other.i);
94  }
96  inline RCP<const Integer> mulint(const Integer &other) const
97  {
98  return make_rcp<const Integer>(this->i * other.i);
99  }
101  RCP<const Number> divint(const Integer &other) const;
103  RCP<const Number> pow_negint(const Integer &other) const;
105  inline RCP<const Number> powint(const Integer &other) const
106  {
107  if (not(mp_fits_ulong_p(other.i))) {
108  if (other.i > 0u)
109  throw SymEngineException(
110  "powint: 'exp' does not fit unsigned long.");
111  else
112  return pow_negint(other);
113  }
114  integer_class tmp;
115  mp_pow_ui(tmp, i, mp_get_ui(other.i));
116  return make_rcp<const Integer>(std::move(tmp));
117  }
119  inline RCP<const Integer> neg() const
120  {
121  return make_rcp<const Integer>(-i);
122  }
123 
124  /* These are general methods, overriden from the Number class, that need to
125  * check types to decide what operation to do, and so are a bit slower. */
127  virtual RCP<const Number> add(const Number &other) const
128  {
129  if (is_a<Integer>(other)) {
130  return addint(down_cast<const Integer &>(other));
131  } else {
132  return other.add(*this);
133  }
134  };
136  virtual RCP<const Number> sub(const Number &other) const
137  {
138  if (is_a<Integer>(other)) {
139  return subint(down_cast<const Integer &>(other));
140  } else {
141  return other.rsub(*this);
142  }
143  };
144 
145  virtual RCP<const Number> rsub(const Number &other) const
146  {
147  throw NotImplementedError("Not Implemented");
148  };
149 
151  virtual RCP<const Number> mul(const Number &other) const
152  {
153  if (is_a<Integer>(other)) {
154  return mulint(down_cast<const Integer &>(other));
155  } else {
156  return other.mul(*this);
157  }
158  };
160  virtual RCP<const Number> div(const Number &other) const
161  {
162  if (is_a<Integer>(other)) {
163  return divint(down_cast<const Integer &>(other));
164  } else {
165  return other.rdiv(*this);
166  }
167  };
168 
169  virtual RCP<const Number> rdiv(const Number &other) const;
170 
172  virtual RCP<const Number> pow(const Number &other) const
173  {
174  if (is_a<Integer>(other)) {
175  return powint(down_cast<const Integer &>(other));
176  } else {
177  return other.rpow(*this);
178  }
179  };
180 
181  virtual RCP<const Number> rpow(const Number &other) const
182  {
183  throw NotImplementedError("Not Implemented");
184  };
185 };
186 
190  bool operator()(const RCP<const Integer> &a,
191  const RCP<const Integer> &b) const
192  {
193  return a->as_integer_class() < b->as_integer_class();
194  }
195 };
197 template <typename T>
199  RCP<const Integer>>::type
201 {
202  return make_rcp<const Integer>(integer_class(i));
203 }
204 
206 inline RCP<const Integer> integer(integer_class i)
207 {
208  return make_rcp<const Integer>(std::move(i));
209 }
210 
212 RCP<const Integer> isqrt(const Integer &n);
214 int i_nth_root(const Ptr<RCP<const Integer>> &r, const Integer &a,
215  unsigned long int n);
217 bool perfect_square(const Integer &n);
219 bool perfect_power(const Integer &n);
221 RCP<const Integer> iabs(const Integer &n);
222 
223 } // namespace SymEngine
224 
225 #endif
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition: basic.h:340
Basic()
Constructor.
Definition: basic.h:118
Integer Class.
Definition: integer.h:19
unsigned long int as_uint() const
Convert to uint, raise an exception if it does not fit.
Definition: integer.cpp:45
virtual RCP< const Number > mul(const Number &other) const
Slower Multiplication.
Definition: integer.h:151
Integer(const integer_class &_i)
Constructor of Integer using integer_class
Definition: integer.h:28
virtual RCP< const Number > add(const Number &other) const
Slower Addition.
Definition: integer.h:127
virtual hash_t __hash__() const
Definition: integer.cpp:9
RCP< const Integer > subint(const Integer &other) const
Fast Integer Subtraction.
Definition: integer.h:91
virtual bool is_zero() const
Definition: integer.h:53
virtual bool __eq__(const Basic &o) const
Definition: integer.cpp:16
const integer_class & as_integer_class() const
Convert to integer_class.
Definition: integer.h:48
virtual RCP< const Number > sub(const Number &other) const
Slower Subtraction.
Definition: integer.h:136
virtual bool is_complex() const
Definition: integer.h:79
virtual bool is_one() const
Definition: integer.h:58
virtual int compare(const Basic &o) const
Definition: integer.cpp:25
RCP< const Integer > addint(const Integer &other) const
Fast Integer Addition.
Definition: integer.h:86
RCP< const Number > divint(const Integer &other) const
Integer Division.
Definition: integer.cpp:60
virtual RCP< const Number > div(const Number &other) const
Slower Division.
Definition: integer.h:160
RCP< const Number > pow_negint(const Integer &other) const
Fast Negative Power Evaluation.
Definition: integer.cpp:100
RCP< const Integer > neg() const
Definition: integer.h:119
virtual bool is_minus_one() const
Definition: integer.h:63
signed long int as_int() const
Convert to int, raise an exception if it does not fit.
Definition: integer.cpp:34
virtual bool is_positive() const
Definition: integer.h:68
RCP< const Number > powint(const Integer &other) const
Fast Power Evaluation.
Definition: integer.h:105
virtual RCP< const Number > pow(const Number &other) const
Slower power evaluation.
Definition: integer.h:172
RCP< const Integer > mulint(const Integer &other) const
Fast Integer Multiplication.
Definition: integer.h:96
integer_class i
i : object of integer_class
Definition: integer.h:22
virtual bool is_negative() const
Definition: integer.h:73
virtual RCP< const Number > mul(const Number &other) const =0
Multiplication.
virtual RCP< const Number > add(const Number &other) const =0
Addition.
T move(T... args)
Main namespace for SymEngine package.
Definition: add.cpp:19
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
Definition: integer.h:200
RCP< const Integer > isqrt(const Integer &n)
Integer Square root.
Definition: integer.cpp:118
bool perfect_square(const Integer &n)
Perfect Square.
Definition: integer.cpp:143
bool perfect_power(const Integer &n)
Perfect Square.
Definition: integer.cpp:148
int i_nth_root(const Ptr< RCP< const Integer >> &r, const Integer &a, unsigned long int n)
Integer nth root.
Definition: integer.cpp:128
RCP< const Integer > iabs(const Integer &n)
Integer Absolute value.
Definition: integer.cpp:123
STL namespace.
less operator (<) for Integers
Definition: integer.h:188
bool operator()(const RCP< const Integer > &a, const RCP< const Integer > &b) const
Definition: integer.h:190