Loading...
Searching...
No Matches
SymEngine::Integer Class Reference

Integer Class. More...

#include <integer.h>

+ Inheritance diagram for SymEngine::Integer:
+ Collaboration diagram for SymEngine::Integer:

Public Member Functions

void accept (Visitor &v) const override
 
void accept (EvalRealDoubleVisitorFinal &v) const override
 
 Integer (const integer_class &_i)
 Constructor of Integer using integer_class
 
 Integer (integer_class &&_i)
 
hash_t __hash__ () const override
 
bool __eq__ (const Basic &o) const override
 
int compare (const Basic &o) const override
 
signed long int as_int () const
 Convert to int, raise an exception if it does not fit.
 
unsigned long int as_uint () const
 Convert to uint, raise an exception if it does not fit.
 
const integer_class & as_integer_class () const
 Convert to integer_class.
 
bool is_zero () const override
 
bool is_one () const override
 
bool is_minus_one () const override
 
bool is_positive () const override
 
bool is_negative () const override
 
bool is_complex () const override
 
RCP< const Integeraddint (const Integer &other) const
 Fast Integer Addition.
 
RCP< const Integersubint (const Integer &other) const
 Fast Integer Subtraction.
 
RCP< const Integermulint (const Integer &other) const
 Fast Integer Multiplication.
 
RCP< const Numberdivint (const Integer &other) const
 Integer Division.
 
RCP< const Numberpow_negint (const Integer &other) const
 Fast Negative Power Evaluation.
 
RCP< const Numberpowint (const Integer &other) const
 Fast Power Evaluation.
 
RCP< const Integerneg () const
 
RCP< const Numberadd (const Number &other) const override
 Slower Addition.
 
RCP< const Numbersub (const Number &other) const override
 Slower Subtraction.
 
RCP< const Numberrsub (const Number &other) const override
 
RCP< const Numbermul (const Number &other) const override
 Slower Multiplication.
 
RCP< const Numberdiv (const Number &other) const override
 Slower Division.
 
RCP< const Numberrdiv (const Number &other) const override
 
RCP< const Numberpow (const Number &other) const override
 Slower power evaluation.
 
RCP< const Numberrpow (const Number &other) const override
 
- Public Member Functions inherited from SymEngine::Number
virtual bool is_zero () const =0
 
virtual bool is_one () const =0
 
virtual bool is_minus_one () const =0
 
virtual bool is_negative () const =0
 
virtual bool is_positive () const =0
 
virtual bool is_complex () const =0
 
virtual RCP< const Basicconjugate () const
 
virtual bool is_exact () const
 return true if the number is an exact representation
 
bool is_exact_zero () const
 
virtual Evaluateget_eval () const
 Get Evaluate singleton to evaluate numerically.
 
virtual RCP< const Numberadd (const Number &other) const =0
 Addition.
 
virtual RCP< const Numbersub (const Number &other) const
 Subtraction.
 
virtual RCP< const Numberrsub (const Number &other) const
 
virtual RCP< const Numbermul (const Number &other) const =0
 Multiplication.
 
virtual RCP< const Numberdiv (const Number &other) const
 Division.
 
virtual RCP< const Numberrdiv (const Number &other) const
 
virtual RCP< const Numberpow (const Number &other) const =0
 Power.
 
virtual RCP< const Numberrpow (const Number &other) const =0
 
vec_basic get_args () const override
 Returns the list of arguments.
 
virtual bool is_perfect_power (bool is_expected=false) const
 
virtual bool nth_root (const Ptr< RCP< const Number > > &, unsigned long n) const
 
- Public Member Functions inherited from SymEngine::Basic
TypeID get_type_code () const
 
 Basic ()
 Constructor.
 
 Basic (const Basic &)=delete
 Delete the copy constructor and assignment.
 
Basicoperator= (const Basic &)=delete
 Assignment operator in continuation with above.
 
 Basic (Basic &&)=delete
 Delete the move constructor and assignment.
 
Basicoperator= (Basic &&)=delete
 Assignment operator in continuation with above.
 
virtual hash_t __hash__ () const =0
 
hash_t hash () const
 
virtual bool __eq__ (const Basic &o) const =0
 Test equality.
 
bool __neq__ (const Basic &o) const
 true if this is not equal to o.
 
int __cmp__ (const Basic &o) const
 Comparison operator.
 
virtual int compare (const Basic &o) const =0
 
std::string __str__ () const
 
std::string dumps () const
 Returns a string of the instance serialized.
 
RCP< const Basicsubs (const map_basic_basic &subs_dict) const
 Substitutes 'subs_dict' into 'self'.
 
RCP< const Basicxreplace (const map_basic_basic &subs_dict) const
 
virtual RCP< const Basicexpand_as_exp () const
 expands the special function in terms of exp function
 
virtual vec_basic get_args () const =0
 Returns the list of arguments.
 
virtual void accept (Visitor &v) const =0
 
virtual void accept (EvalRealDoubleVisitorFinal &v) const =0
 
RCP< const Basicdiff (const RCP< const Symbol > &x, bool cache=true) const
 
- Public Member Functions inherited from SymEngine::EnableRCPFromThis< Basic >
RCP< Basicrcp_from_this ()
 Get RCP<T> pointer to self (it will cast the pointer to T)
 
RCP< const Basicrcp_from_this () const
 Get RCP<const T> pointer to self (it will cast the pointer to const T)
 
RCP< const T2 > rcp_from_this_cast () const
 Get RCP<T2> pointer to self (it will cast the pointer to T2)
 
unsigned int use_count () const
 

Static Public Attributes

static const TypeID type_code_id = SYMENGINE_INTEGER
 

Private Attributes

integer_class i
 i : object of integer_class
 

Additional Inherited Members

- Static Public Member Functions inherited from SymEngine::Basic
static RCP< const Basicloads (const std::string &)
 Creates an instance of a serialized string.
 
- Data Fields inherited from SymEngine::Basic
TypeID type_code_
 

Detailed Description

Integer Class.

Definition at line 18 of file integer.h.

Constructor & Destructor Documentation

◆ Integer() [1/2]

SymEngine::Integer::Integer ( const integer_class &  _i)
inline

Constructor of Integer using integer_class

Definition at line 28 of file integer.h.

29 : i(_i){SYMENGINE_ASSIGN_TYPEID()} Integer(integer_class && _i)
Integer(const integer_class &_i)
Constructor of Integer using integer_class
Definition: integer.h:28
integer_class i
i : object of integer_class
Definition: integer.h:22

◆ Integer() [2/2]

SymEngine::Integer::Integer ( integer_class &&  _i)
inline

Definition at line 29 of file integer.h.

29 : i(_i){SYMENGINE_ASSIGN_TYPEID()} Integer(integer_class && _i)
30 : i(std::move(_i)){SYMENGINE_ASSIGN_TYPEID()}
T move(T... args)
STL namespace.

Member Function Documentation

◆ __eq__()

bool SymEngine::Integer::__eq__ ( const Basic o) const
overridevirtual

Equality comparator

Parameters
o- Object to be compared with
Returns
whether the 2 objects are equal

Implements SymEngine::Basic.

Definition at line 16 of file integer.cpp.

17{
18 if (is_a<Integer>(o)) {
19 const Integer &s = down_cast<const Integer &>(o);
20 return this->i == s.i;
21 }
22 return false;
23}

◆ __hash__()

hash_t SymEngine::Integer::__hash__ ( ) const
overridevirtual
Returns
size of the hash

Implements SymEngine::Basic.

Definition at line 9 of file integer.cpp.

10{
11 // only the least significant bits that fit into "long long int" are
12 // hashed:
13 return ((hash_t)mp_get_ui(this->i)) * (hash_t)(mp_sign(this->i));
14}

◆ accept() [1/2]

void SymEngine::Integer::accept ( EvalRealDoubleVisitorFinal v) const
overridevirtual

Implements SymEngine::Basic.

◆ accept() [2/2]

void SymEngine::Integer::accept ( Visitor v) const
overridevirtual

Implements SymEngine::Basic.

◆ add()

RCP< const Number > SymEngine::Integer::add ( const Number other) const
inlineoverridevirtual

Slower Addition.

Implements SymEngine::Number.

Definition at line 124 of file integer.h.

125 {
126 if (is_a<Integer>(other)) {
127 return addint(down_cast<const Integer &>(other));
128 } else {
129 return other.add(*this);
130 }
131 };
RCP< const Integer > addint(const Integer &other) const
Fast Integer Addition.
Definition: integer.h:83

◆ addint()

RCP< const Integer > SymEngine::Integer::addint ( const Integer other) const
inline

Fast Integer Addition.

Definition at line 83 of file integer.h.

84 {
85 return make_rcp<const Integer>(this->i + other.i);
86 }

◆ as_int()

signed long int SymEngine::Integer::as_int ( ) const

Convert to int, raise an exception if it does not fit.

Definition at line 34 of file integer.cpp.

35{
36 // mp_get_si() returns "signed long int", so that's what we return from
37 // "as_int()" and we leave it to the user to do any possible further integer
38 // conversions.
39 if (not(mp_fits_slong_p(this->i))) {
40 throw SymEngineException("as_int: Integer larger than int");
41 }
42 return mp_get_si(this->i);
43}

◆ as_integer_class()

const integer_class & SymEngine::Integer::as_integer_class ( ) const
inline

Convert to integer_class.

Definition at line 45 of file integer.h.

46 {
47 return this->i;
48 }

◆ as_uint()

unsigned long int SymEngine::Integer::as_uint ( ) const

Convert to uint, raise an exception if it does not fit.

Definition at line 45 of file integer.cpp.

46{
47 // mp_get_ui() returns "unsigned long int", so that's what we return from
48 // "as_uint()" and we leave it to the user to do any possible further
49 // integer
50 // conversions.
51 if (this->i < 0u) {
52 throw SymEngineException("as_uint: negative Integer");
53 }
54 if (not(mp_fits_ulong_p(this->i))) {
55 throw SymEngineException("as_uint: Integer larger than uint");
56 }
57 return mp_get_ui(this->i);
58}

◆ compare()

int SymEngine::Integer::compare ( const Basic o) const
overridevirtual

Returns -1, 0, 1 for this < o, this == o, this > o. This method is used when you want to sort things like x+y+z into canonical order. This function assumes that o is the same type as this. Use __cmp__ if you want general comparison.

Implements SymEngine::Basic.

Definition at line 25 of file integer.cpp.

26{
27 SYMENGINE_ASSERT(is_a<Integer>(o))
28 const Integer &s = down_cast<const Integer &>(o);
29 if (i == s.i)
30 return 0;
31 return i < s.i ? -1 : 1;
32}

◆ div()

RCP< const Number > SymEngine::Integer::div ( const Number other) const
inlineoverridevirtual

Slower Division.

Reimplemented from SymEngine::Number.

Definition at line 157 of file integer.h.

158 {
159 if (is_a<Integer>(other)) {
160 return divint(down_cast<const Integer &>(other));
161 } else {
162 return other.rdiv(*this);
163 }
164 };
RCP< const Number > divint(const Integer &other) const
Integer Division.
Definition: integer.cpp:60

◆ divint()

RCP< const Number > SymEngine::Integer::divint ( const Integer other) const

Integer Division.

Definition at line 60 of file integer.cpp.

61{
62 if (other.i == 0) {
63 if (this->i == 0) {
64 return Nan;
65 } else {
66 return ComplexInf;
67 }
68 }
69 rational_class q(this->i, other.i);
70
71 // This is potentially slow, but has to be done, since q might not
72 // be in canonical form.
73 canonicalize(q);
74
76}
static RCP< const Number > from_mpq(const rational_class &i)
Definition: rational.cpp:23

◆ is_complex()

bool SymEngine::Integer::is_complex ( ) const
inlineoverridevirtual
Returns
false

Implements SymEngine::Number.

Definition at line 76 of file integer.h.

77 {
78 return false;
79 }

◆ is_minus_one()

bool SymEngine::Integer::is_minus_one ( ) const
inlineoverridevirtual
Returns
true if -1

Implements SymEngine::Number.

Definition at line 60 of file integer.h.

61 {
62 return this->i == -1;
63 }

◆ is_negative()

bool SymEngine::Integer::is_negative ( ) const
inlineoverridevirtual
Returns
true if negative

Implements SymEngine::Number.

Definition at line 70 of file integer.h.

71 {
72 return this->i < 0u;
73 }

◆ is_one()

bool SymEngine::Integer::is_one ( ) const
inlineoverridevirtual
Returns
true if 1

Implements SymEngine::Number.

Definition at line 55 of file integer.h.

56 {
57 return this->i == 1u;
58 }

◆ is_positive()

bool SymEngine::Integer::is_positive ( ) const
inlineoverridevirtual
Returns
true if positive

Implements SymEngine::Number.

Definition at line 65 of file integer.h.

66 {
67 return this->i > 0u;
68 }

◆ is_zero()

bool SymEngine::Integer::is_zero ( ) const
inlineoverridevirtual
Returns
true if 0

Implements SymEngine::Number.

Definition at line 50 of file integer.h.

51 {
52 return this->i == 0u;
53 }

◆ mul()

RCP< const Number > SymEngine::Integer::mul ( const Number other) const
inlineoverridevirtual

Slower Multiplication.

Implements SymEngine::Number.

Definition at line 148 of file integer.h.

149 {
150 if (is_a<Integer>(other)) {
151 return mulint(down_cast<const Integer &>(other));
152 } else {
153 return other.mul(*this);
154 }
155 };
RCP< const Integer > mulint(const Integer &other) const
Fast Integer Multiplication.
Definition: integer.h:93

◆ mulint()

RCP< const Integer > SymEngine::Integer::mulint ( const Integer other) const
inline

Fast Integer Multiplication.

Definition at line 93 of file integer.h.

94 {
95 return make_rcp<const Integer>(this->i * other.i);
96 }

◆ neg()

RCP< const Integer > SymEngine::Integer::neg ( ) const
inline
Returns
negative of self.

Definition at line 116 of file integer.h.

117 {
118 return make_rcp<const Integer>(-i);
119 }

◆ pow()

RCP< const Number > SymEngine::Integer::pow ( const Number other) const
inlineoverridevirtual

Slower power evaluation.

Implements SymEngine::Number.

Definition at line 169 of file integer.h.

170 {
171 if (is_a<Integer>(other)) {
172 return powint(down_cast<const Integer &>(other));
173 } else {
174 return other.rpow(*this);
175 }
176 };
RCP< const Number > powint(const Integer &other) const
Fast Power Evaluation.
Definition: integer.h:102

◆ pow_negint()

RCP< const Number > SymEngine::Integer::pow_negint ( const Integer other) const

Fast Negative Power Evaluation.

Definition at line 100 of file integer.cpp.

101{
102 RCP<const Number> tmp = powint(*other.neg());
103 if (is_a<Integer>(*tmp)) {
104 const integer_class &j = down_cast<const Integer &>(*tmp).i;
105#if SYMENGINE_INTEGER_CLASS == SYMENGINE_BOOSTMP
106 // boost::multiprecision::cpp_rational lacks an (int, cpp_int)
107 // constructor. must use cpp_rational(cpp_int,cpp_int)
108 rational_class q(integer_class(mp_sign(j)), mp_abs(j));
109#else
110 rational_class q(mp_sign(j), mp_abs(j));
111#endif
112 return Rational::from_mpq(std::move(q));
113 } else {
114 throw SymEngineException("powint returned non-integer");
115 }
116}

◆ powint()

RCP< const Number > SymEngine::Integer::powint ( const Integer other) const
inline

Fast Power Evaluation.

Definition at line 102 of file integer.h.

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 }
RCP< const Number > pow_negint(const Integer &other) const
Fast Negative Power Evaluation.
Definition: integer.cpp:100

◆ rdiv()

RCP< const Number > SymEngine::Integer::rdiv ( const Number other) const
overridevirtual

Reimplemented from SymEngine::Number.

Definition at line 78 of file integer.cpp.

79{
80 if (is_a<Integer>(other)) {
81 if (this->i == 0) {
82 if (other.is_zero()) {
83 return Nan;
84 } else {
85 return ComplexInf;
86 }
87 }
88 rational_class q((down_cast<const Integer &>(other)).i, this->i);
89
90 // This is potentially slow, but has to be done, since q might not
91 // be in canonical form.
92 canonicalize(q);
93
95 } else {
96 throw NotImplementedError("Not Implemented");
97 }
98}

◆ rpow()

RCP< const Number > SymEngine::Integer::rpow ( const Number other) const
inlineoverridevirtual

Implements SymEngine::Number.

Definition at line 178 of file integer.h.

179 {
180 throw NotImplementedError("Not Implemented");
181 };

◆ rsub()

RCP< const Number > SymEngine::Integer::rsub ( const Number other) const
inlineoverridevirtual

Reimplemented from SymEngine::Number.

Definition at line 142 of file integer.h.

143 {
144 throw NotImplementedError("Not Implemented");
145 };

◆ sub()

RCP< const Number > SymEngine::Integer::sub ( const Number other) const
inlineoverridevirtual

Slower Subtraction.

Reimplemented from SymEngine::Number.

Definition at line 133 of file integer.h.

134 {
135 if (is_a<Integer>(other)) {
136 return subint(down_cast<const Integer &>(other));
137 } else {
138 return other.rsub(*this);
139 }
140 };
RCP< const Integer > subint(const Integer &other) const
Fast Integer Subtraction.
Definition: integer.h:88

◆ subint()

RCP< const Integer > SymEngine::Integer::subint ( const Integer other) const
inline

Fast Integer Subtraction.

Definition at line 88 of file integer.h.

89 {
90 return make_rcp<const Integer>(this->i - other.i);
91 }

Field Documentation

◆ i

integer_class SymEngine::Integer::i
private

i : object of integer_class

Definition at line 22 of file integer.h.

◆ type_code_id

const TypeID SymEngine::Integer::type_code_id = SYMENGINE_INTEGER
static

Type_code_id shared by all instances

Definition at line 25 of file integer.h.


The documentation for this class was generated from the following files: