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)){SYMENGINE_ASSIGN_TYPEID()}
32  hash_t __hash__() const override;
37  bool __eq__(const Basic &o) const override;
38  int compare(const Basic &o) const override;
39 
41  signed long int as_int() const;
43  unsigned long int as_uint() const;
45  inline const integer_class &as_integer_class() const
46  {
47  return this->i;
48  }
50  inline bool is_zero() const override
51  {
52  return this->i == 0u;
53  }
55  inline bool is_one() const override
56  {
57  return this->i == 1u;
58  }
60  inline bool is_minus_one() const override
61  {
62  return this->i == -1;
63  }
65  inline bool is_positive() const override
66  {
67  return this->i > 0u;
68  }
70  inline bool is_negative() const override
71  {
72  return this->i < 0u;
73  }
75  // False is returned because a pure integer cannot have an imaginary part
76  inline bool is_complex() const override
77  {
78  return false;
79  }
80 
81  /* These are very fast methods for add/sub/mul/div/pow on Integers only */
83  inline RCP<const Integer> addint(const Integer &other) const
84  {
85  return make_rcp<const Integer>(this->i + other.i);
86  }
88  inline RCP<const Integer> subint(const Integer &other) const
89  {
90  return make_rcp<const Integer>(this->i - other.i);
91  }
93  inline RCP<const Integer> mulint(const Integer &other) const
94  {
95  return make_rcp<const Integer>(this->i * other.i);
96  }
98  RCP<const Number> divint(const Integer &other) const;
100  RCP<const Number> pow_negint(const Integer &other) const;
102  inline RCP<const Number> powint(const Integer &other) const
103  {
104  if (not(mp_fits_ulong_p(other.i))) {
105  if (other.i > 0u)
106  throw SymEngineException(
107  "powint: 'exp' does not fit unsigned long.");
108  else
109  return pow_negint(other);
110  }
111  integer_class tmp;
112  mp_pow_ui(tmp, i, mp_get_ui(other.i));
113  return make_rcp<const Integer>(std::move(tmp));
114  }
116  inline RCP<const Integer> neg() const
117  {
118  return make_rcp<const Integer>(-i);
119  }
120 
121  /* These are general methods, overriden from the Number class, that need to
122  * check types to decide what operation to do, and so are a bit slower. */
124  RCP<const Number> add(const Number &other) const override
125  {
126  if (is_a<Integer>(other)) {
127  return addint(down_cast<const Integer &>(other));
128  } else {
129  return other.add(*this);
130  }
131  };
133  RCP<const Number> sub(const Number &other) const override
134  {
135  if (is_a<Integer>(other)) {
136  return subint(down_cast<const Integer &>(other));
137  } else {
138  return other.rsub(*this);
139  }
140  };
141 
142  RCP<const Number> rsub(const Number &other) const override
143  {
144  throw NotImplementedError("Not Implemented");
145  };
146 
148  RCP<const Number> mul(const Number &other) const override
149  {
150  if (is_a<Integer>(other)) {
151  return mulint(down_cast<const Integer &>(other));
152  } else {
153  return other.mul(*this);
154  }
155  };
157  RCP<const Number> div(const Number &other) const override
158  {
159  if (is_a<Integer>(other)) {
160  return divint(down_cast<const Integer &>(other));
161  } else {
162  return other.rdiv(*this);
163  }
164  };
165 
166  RCP<const Number> rdiv(const Number &other) const override;
167 
169  RCP<const Number> pow(const Number &other) const override
170  {
171  if (is_a<Integer>(other)) {
172  return powint(down_cast<const Integer &>(other));
173  } else {
174  return other.rpow(*this);
175  }
176  };
177 
178  RCP<const Number> rpow(const Number &other) const override
179  {
180  throw NotImplementedError("Not Implemented");
181  };
182 };
183 
187  bool operator()(const RCP<const Integer> &a,
188  const RCP<const Integer> &b) const
189  {
190  return a->as_integer_class() < b->as_integer_class();
191  }
192 };
194 template <typename T>
196  RCP<const Integer>>::type
198 {
199  return make_rcp<const Integer>(integer_class(i));
200 }
201 
203 inline RCP<const Integer> integer(integer_class i)
204 {
205  return make_rcp<const Integer>(std::move(i));
206 }
207 
209 RCP<const Integer> isqrt(const Integer &n);
211 int i_nth_root(const Ptr<RCP<const Integer>> &r, const Integer &a,
212  unsigned long int n);
214 bool perfect_square(const Integer &n);
216 bool perfect_power(const Integer &n);
218 RCP<const Integer> iabs(const Integer &n);
219 
220 } // namespace SymEngine
221 
222 #endif
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition: basic.h:340
Basic()
Constructor.
Definition: basic.h:120
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
RCP< const Number > div(const Number &other) const override
Slower Division.
Definition: integer.h:157
Integer(const integer_class &_i)
Constructor of Integer using integer_class
Definition: integer.h:28
RCP< const Number > mul(const Number &other) const override
Slower Multiplication.
Definition: integer.h:148
RCP< const Integer > subint(const Integer &other) const
Fast Integer Subtraction.
Definition: integer.h:88
RCP< const Number > add(const Number &other) const override
Slower Addition.
Definition: integer.h:124
bool is_negative() const override
Definition: integer.h:70
const integer_class & as_integer_class() const
Convert to integer_class.
Definition: integer.h:45
RCP< const Number > sub(const Number &other) const override
Slower Subtraction.
Definition: integer.h:133
RCP< const Integer > addint(const Integer &other) const
Fast Integer Addition.
Definition: integer.h:83
RCP< const Number > divint(const Integer &other) const
Integer Division.
Definition: integer.cpp:60
bool __eq__(const Basic &o) const override
Definition: integer.cpp:16
bool is_positive() const override
Definition: integer.h:65
hash_t __hash__() const override
Definition: integer.cpp:9
bool is_minus_one() const override
Definition: integer.h:60
bool is_one() const override
Definition: integer.h:55
int compare(const Basic &o) const override
Definition: integer.cpp:25
bool is_zero() const override
Definition: integer.h:50
bool is_complex() const override
Definition: integer.h:76
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:116
signed long int as_int() const
Convert to int, raise an exception if it does not fit.
Definition: integer.cpp:34
RCP< const Number > powint(const Integer &other) const
Fast Power Evaluation.
Definition: integer.h:102
RCP< const Integer > mulint(const Integer &other) const
Fast Integer Multiplication.
Definition: integer.h:93
RCP< const Number > pow(const Number &other) const override
Slower power evaluation.
Definition: integer.h:169
integer_class i
i : object of integer_class
Definition: integer.h:22
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:197
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:185
bool operator()(const RCP< const Integer > &a, const RCP< const Integer > &b) const
Definition: integer.h:187