No Matches
SymEngine::Basic Class Referenceabstract

The lowest unit of symbolic representation. More...

#include <basic.h>

+ Inheritance diagram for SymEngine::Basic:
+ Collaboration diagram for SymEngine::Basic:

Public Member Functions

TypeID get_type_code () const
 Basic ()
 Constructor. More...
 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. More...
bool __neq__ (const Basic &o) const
 true if this is not equal to o. More...
int __cmp__ (const Basic &o) const
 Comparison operator. More...
virtual int compare (const Basic &o) const =0
std::string __str__ () const
std::string dumps () const
 Returns a string of the instance serialized. More...
RCP< const Basicsubs (const map_basic_basic &subs_dict) const
 Substitutes 'subs_dict' into 'self'. More...
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 More...
virtual vec_basic get_args () const =0
 Returns the list of arguments. More...
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) More...
RCP< const Basicrcp_from_this () const
 Get RCP<const T> pointer to self (it will cast the pointer to const T) More...
RCP< const T2 > rcp_from_this_cast () const
 Get RCP<T2> pointer to self (it will cast the pointer to T2) More...
unsigned int use_count () const

Static Public Member Functions

static RCP< const Basicloads (const std::string &)
 Creates an instance of a serialized string. More...

Data Fields

TypeID type_code_

Private Attributes

hash_t hash_
 Private variables. More...

Detailed Description

The lowest unit of symbolic representation.

Classes like Add, Mul, Pow are initialized through their constructor using their internal representation. Add, Mul have a 'coeff' and 'dict', while Pow has 'base' and 'exp'. There are restrictions on what 'coeff' and 'dict' can be (for example 'coeff' cannot be zero in Mul, and if Mul is used inside Add, then Mul's coeff must be one, etc.). All these restrictions are checked when WITH_SYMENGINE_ASSERT is enabled inside the constructors using the is_canonical() method. That way, you don't have to worry about creating Add / Mul / Pow with wrong arguments, as it will be caught by the tests. In the Release mode no checks are done, so you can construct Add / Mul / Pow very quickly. The idea is that depending on the algorithm, you sometimes know that things are already canonical, so you simply pass it directly to the constructors of the Basic classes and you avoid expensive type checking and canonicalization. At the same time, you need to make sure that tests are still running with WITH_SYMENGINE_ASSERT enabled, so that the Basic classes are never in an inconsistent state.

Summary: always try to construct the expressions Add / Mul / Pow directly using their constructors and all the knowledge that you have for the given algorithm, that way things will be very fast. If you want slower but simpler code, you can use the add(), mul(), pow() functions that peform general and possibly slow canonicalization first.

Any Basic class can be used in a "dictionary", due to the methods:
Subclasses must implement __hash__() and __eq__()

Definition at line 96 of file basic.h.

Constructor & Destructor Documentation

◆ Basic()

SymEngine::Basic::Basic ( )


Definition at line 120 of file basic.h.

120: hash_{0} {}
hash_t hash_
Private variables.
Definition: basic.h:107

◆ ~Basic()

virtual SymEngine::Basic::~Basic ( )

Definition at line 123 of file basic.h.


Member Function Documentation

◆ __cmp__()

int SymEngine::Basic::__cmp__ ( const Basic o) const

Comparison operator.

Definition at line 31 of file basic.cpp.

33 auto a = this->get_type_code();
34 auto b = o.get_type_code();
35 if (a == b) {
36 return this->compare(o);
37 } else {
38 // We return the order given by the numerical value of the TypeID enum
39 // type.
40 // The types don't need to be ordered in any given way, they just need
41 // to be ordered.
42 return a < b ? -1 : 1;
43 }
virtual int compare(const Basic &o) const =0

◆ __eq__()

virtual bool SymEngine::Basic::__eq__ ( const Basic o) const
pure virtual

Test equality.

A virtual function for testing the equality of two Basic objects

Use eq(const Basic &a, const Basic &b) non-member method
oa constant reference to object to test against
True if this is equal to o

Implemented in SymEngine::Add, SymEngine::Complex, SymEngine::ComplexDouble, SymEngine::Constant, SymEngine::OneArgFunction, SymEngine::TwoArgBasic< Boolean >, SymEngine::MultiArgFunction, SymEngine::FunctionSymbol, SymEngine::Derivative, SymEngine::Subs, SymEngine::Infty, SymEngine::Integer, SymEngine::BooleanAtom, SymEngine::Contains, SymEngine::Piecewise, SymEngine::And, SymEngine::Or, SymEngine::Not, SymEngine::Xor, SymEngine::Mul, SymEngine::NaN, SymEngine::MSymEnginePoly< Container, Poly >, SymEngine::MSymEnginePoly< MExprDict, MExprPoly >, SymEngine::MSymEnginePoly< MIntDict, MIntPoly >, SymEngine::UPolyBase< Container, Poly >, SymEngine::UPolyBase< GaloisFieldDict, GaloisField >, SymEngine::UPolyBase< Cont, Poly >, SymEngine::Pow, SymEngine::Rational, SymEngine::RealDouble, SymEngine::SeriesBase< Poly, Coeff, Series >, SymEngine::SeriesBase< UExprDict, Expression, UnivariateSeries >, SymEngine::EmptySet, SymEngine::UniversalSet, SymEngine::FiniteSet, SymEngine::Interval, SymEngine::Complexes, SymEngine::Reals, SymEngine::Rationals, SymEngine::Integers, SymEngine::Naturals, SymEngine::Naturals0, SymEngine::Union, SymEngine::Intersection, SymEngine::Complement, SymEngine::ConditionSet, SymEngine::ImageSet, SymEngine::Symbol, SymEngine::Dummy, and SymEngine::Tuple.

◆ __hash__()

◆ __neq__()

bool SymEngine::Basic::__neq__ ( const Basic o) const

true if this is not equal to o.

true if not equal

Definition at line 15 of file basic-inl.h.

17 return not(eq(*this, o));
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21

◆ __str__()

std::string SymEngine::Basic::__str__ ( ) const

Returns string representation of self.

Definition at line 46 of file basic.cpp.

48 return str(*this);

◆ compare()

virtual int SymEngine::Basic::compare ( const Basic o) const
pure virtual

◆ diff()

RCP< const Basic > SymEngine::Basic::diff ( const RCP< const Symbol > &  x,
bool  cache = true 
) const

Definition at line 755 of file derivative.cpp.

757 return SymEngine::diff(this->rcp_from_this(), x, cache);
RCP< Basic > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)

◆ dumps()

std::string SymEngine::Basic::dumps ( ) const

Returns a string of the instance serialized.

Definition at line 51 of file basic.cpp.

54 unsigned short major = SYMENGINE_MAJOR_VERSION;
55 unsigned short minor = SYMENGINE_MINOR_VERSION;
56 cereal::PortableBinaryOutputArchive{oss}(major, minor,
57 this->rcp_from_this());
58 return oss.str();
T str(T... args)

◆ expand_as_exp()

virtual RCP< const Basic > SymEngine::Basic::expand_as_exp ( ) const

expands the special function in terms of exp function

Definition at line 196 of file basic.h.

197 {
198 throw NotImplementedError("Not Implemented");
199 }

◆ get_args()

◆ get_type_code()

TypeID SymEngine::Basic::get_type_code ( ) const

Definition at line 114 of file basic.h.

115 {
116 return type_code_;
117 };

◆ hash()

hash_t SymEngine::Basic::hash ( ) const

Returns the hash of the SymEngine class: This method caches the value

Use std::hash to get the hash. Example:

     RCP<const Symbol> x = symbol("x");
  std::hash<Basic> hash_fn;
  std::cout << hash_fn(*x);
64-bit integer value for the hash

Definition at line 7 of file basic-inl.h.

9 if (hash_ == 0)
10 hash_ = __hash__();
11 return hash_;
virtual hash_t __hash__() const =0

◆ loads()

RCP< const Basic > SymEngine::Basic::loads ( const std::string serialized)

Creates an instance of a serialized string.

Definition at line 61 of file basic.cpp.

63 unsigned short major, minor;
64 RCP<const Basic> obj;
65 std::istringstream iss(serialized);
66 cereal::PortableBinaryInputArchive iarchive{iss};
67 iarchive(major, minor);
69 throw SerializationError(StreamFmt()
72 << " was asked to deserialize an object "
73 << "created using SymEngine-" << major << "."
74 << minor << ".");
75 }
76 iarchive(obj);
77 return obj;

◆ subs()

RCP< const Basic > SymEngine::Basic::subs ( const map_basic_basic subs_dict) const

Substitutes 'subs_dict' into 'self'.

Definition at line 80 of file basic.cpp.

82 return SymEngine::subs(this->rcp_from_this(), subs_dict);

◆ xreplace()

RCP< const Basic > SymEngine::Basic::xreplace ( const map_basic_basic subs_dict) const

Definition at line 85 of file basic.cpp.

87 return SymEngine::xreplace(this->rcp_from_this(), xreplace_dict);
RCP< const Basic > xreplace(const RCP< const Basic > &x, const map_basic_basic &subs_dict, bool cache=true)
Mappings in the subs_dict are applied to the expression tree of x
Definition: subs.h:338

Field Documentation

◆ hash_

hash_t SymEngine::Basic::hash_

Private variables.

Definition at line 107 of file basic.h.

◆ type_code_

TypeID SymEngine::Basic::type_code_

Definition at line 113 of file basic.h.

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