Loading...
Searching...
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.
 
 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 Member Functions

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

Data Fields

TypeID type_code_
 

Private Attributes

hash_t hash_
 Private variables.
 

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.

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

Definition at line 96 of file basic.h.

Constructor & Destructor Documentation

◆ Basic()

SymEngine::Basic::Basic ( )
inline

Constructor.

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 ( )
inlinevirtual

Definition at line 123 of file basic.h.

123{}

Member Function Documentation

◆ __cmp__()

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

Comparison operator.

Definition at line 31 of file basic.cpp.

32{
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 }
44}
virtual int compare(const Basic &o) const =0
void hash_combine(hash_t &seed, const T &v)
Definition basic-inl.h:95

◆ __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

Deprecated:
Use eq(const Basic &a, const Basic &b) non-member method
Parameters
oa constant reference to object to test against
Returns
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::ConjugateMatrix, SymEngine::DiagonalMatrix, SymEngine::HadamardProduct, SymEngine::IdentityMatrix, SymEngine::ImmutableDenseMatrix, SymEngine::MatrixAdd, SymEngine::MatrixMul, SymEngine::MatrixSymbol, SymEngine::Trace, SymEngine::Transpose, SymEngine::ZeroMatrix, SymEngine::Mul, SymEngine::NaN, SymEngine::MSymEnginePoly< Container, Poly >, SymEngine::MSymEnginePoly< MExprDict, MExprPoly >, SymEngine::MSymEnginePoly< MIntDict, MIntPoly >, SymEngine::UPolyBase< Container, Poly >, SymEngine::UPolyBase< Cont, Poly >, SymEngine::UPolyBase< GaloisFieldDict, GaloisField >, 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__()

virtual hash_t SymEngine::Basic::__hash__ ( ) const
pure virtual

Calculates the hash of the given SymEngine class. Use Basic.hash() which gives a cached version of the hash.

Returns
64-bit integer value for the hash

Implemented in SymEngine::Add, SymEngine::Complex, SymEngine::ComplexDouble, SymEngine::Constant, SymEngine::GaloisField, 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::ConjugateMatrix, SymEngine::DiagonalMatrix, SymEngine::HadamardProduct, SymEngine::IdentityMatrix, SymEngine::ImmutableDenseMatrix, SymEngine::MatrixAdd, SymEngine::MatrixMul, SymEngine::MatrixSymbol, SymEngine::Trace, SymEngine::Transpose, SymEngine::ZeroMatrix, SymEngine::Mul, SymEngine::NaN, SymEngine::MIntPoly, SymEngine::MExprPoly, SymEngine::Pow, SymEngine::Rational, SymEngine::RealDouble, SymEngine::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, SymEngine::Tuple, SymEngine::UPolyBase< Container, Poly >, SymEngine::UPolyBase< Cont, Poly >, and SymEngine::UPolyBase< GaloisFieldDict, GaloisField >.

◆ __neq__()

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

true if this is not equal to o.

Returns
true if not equal

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

16{
17 return not(eq(*this, o));
18}
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.

47{
48 return str(*this);
49}

◆ compare()

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

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.

Implemented in SymEngine::Add, SymEngine::Complex, SymEngine::ComplexDouble, SymEngine::Constant, SymEngine::GaloisField, 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::ConjugateMatrix, SymEngine::DiagonalMatrix, SymEngine::HadamardProduct, SymEngine::IdentityMatrix, SymEngine::ImmutableDenseMatrix, SymEngine::MatrixAdd, SymEngine::MatrixMul, SymEngine::MatrixSymbol, SymEngine::Trace, SymEngine::Transpose, SymEngine::ZeroMatrix, SymEngine::Mul, SymEngine::NaN, SymEngine::MSymEnginePoly< Container, Poly >, SymEngine::MSymEnginePoly< MExprDict, MExprPoly >, SymEngine::MSymEnginePoly< MIntDict, MIntPoly >, SymEngine::Pow, SymEngine::Rational, SymEngine::RealDouble, SymEngine::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, SymEngine::Tuple, SymEngine::UPolyBase< Container, Poly >, SymEngine::UPolyBase< Cont, Poly >, and SymEngine::UPolyBase< GaloisFieldDict, GaloisField >.

◆ diff()

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

Definition at line 810 of file derivative.cpp.

811{
812 return SymEngine::diff(this->rcp_from_this(), x, cache);
813}
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.

52{
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();
59}
T str(T... args)

◆ expand_as_exp()

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

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
inline

Definition at line 114 of file basic.h.

115 {
116 return type_code_;
117 };

◆ hash()

hash_t SymEngine::Basic::hash ( ) const
inline

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);
Returns
64-bit integer value for the hash

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

8{
9 if (hash_ == 0)
10 hash_ = __hash__();
11 return hash_;
12}
virtual hash_t __hash__() const =0

◆ loads()

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

Creates an instance of a serialized string.

Definition at line 61 of file basic.cpp.

62{
63 unsigned short major, minor;
64 RCP<const Basic> obj;
66 cereal::PortableBinaryInputArchive iarchive{iss};
70 << "SymEngine-" << SYMENGINE_MAJOR_VERSION
72 << " was asked to deserialize an object "
73 << "created using SymEngine-" << major << "."
74 << minor << ".");
75 }
77 return obj;
78}

◆ 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.

81{
82 return SymEngine::subs(this->rcp_from_this(), subs_dict);
83}

◆ xreplace()

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

Definition at line 85 of file basic.cpp.

86{
88}
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:347

Field Documentation

◆ hash_

hash_t SymEngine::Basic::hash_
mutableprivate

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: