The base class for representing addition in symbolic expressions. More...
#include <add.h>
Public Member Functions | |
void | accept (Visitor &v) const override |
void | accept (EvalRealDoubleVisitorFinal &v) const override |
Add (const RCP< const Number > &coef, umap_basic_num &&dict) | |
Default constructor. More... | |
hash_t | __hash__ () const override |
Generates the hash representation. More... | |
bool | __eq__ (const Basic &o) const override |
Test equality. More... | |
int | compare (const Basic &o) const override |
Compares Add objects. More... | |
void | as_two_terms (const Ptr< RCP< const Basic >> &a, const Ptr< RCP< const Basic >> &b) const |
Converts the Add into a sum of two Basic objects. More... | |
bool | is_canonical (const RCP< const Number > &coef, const umap_basic_num &dict) const |
Checks if a given dictionary and coeffient is in cannonical form. More... | |
vec_basic | get_args () const override |
Returns the arguments of the Add. More... | |
const RCP< const Number > & | get_coef () const |
const umap_basic_num & | get_dict () 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. | |
Basic & | operator= (const Basic &)=delete |
Assignment operator in continuation with above. | |
Basic (Basic &&)=delete | |
Delete the move constructor and assignment. | |
Basic & | operator= (Basic &&)=delete |
Assignment operator in continuation with above. | |
hash_t | hash () const |
bool | __neq__ (const Basic &o) const |
true if this is not equal to o . More... | |
int | __cmp__ (const Basic &o) const |
Comparison operator. | |
std::string | __str__ () const |
std::string | dumps () const |
Returns a string of the instance serialized. | |
RCP< const Basic > | subs (const map_basic_basic &subs_dict) const |
Substitutes 'subs_dict' into 'self'. | |
RCP< const Basic > | xreplace (const map_basic_basic &subs_dict) const |
virtual RCP< const Basic > | expand_as_exp () const |
expands the special function in terms of exp function | |
RCP< const Basic > | diff (const RCP< const Symbol > &x, bool cache=true) const |
Public Member Functions inherited from SymEngine::EnableRCPFromThis< Basic > | |
RCP< Basic > | rcp_from_this () |
Get RCP<T> pointer to self (it will cast the pointer to T) | |
RCP< const Basic > | rcp_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 Basic > | from_dict (const RCP< const Number > &coef, umap_basic_num &&d) |
Create an appropriate instance from dictionary quickly. More... | |
static void | dict_add_term (umap_basic_num &d, const RCP< const Number > &coef, const RCP< const Basic > &t) |
Adds a new term to the expression. More... | |
static void | coef_dict_add_term (const Ptr< RCP< const Number >> &coef, umap_basic_num &d, const RCP< const Number > &c, const RCP< const Basic > &term) |
Updates the numerical coefficient and the dictionary. More... | |
static void | as_coef_term (const RCP< const Basic > &self, const Ptr< RCP< const Number >> &coef, const Ptr< RCP< const Basic >> &term) |
Converts a Basic self into the form of coefficient * term . More... | |
Static Public Member Functions inherited from SymEngine::Basic | |
static RCP< const Basic > | loads (const std::string &) |
Creates an instance of a serialized string. | |
Static Public Attributes | |
static const TypeID | type_code_id = SYMENGINE_ADD |
Private Attributes | |
RCP< const Number > | coef_ |
umap_basic_num | dict_ |
Related Functions | |
(Note that these are not member functions.) | |
RCP< const Basic > | add (const RCP< const Basic > &a, const RCP< const Basic > &b) |
Adds two objects (safely). More... | |
RCP< const Basic > | add (const vec_basic &a) |
Sums the elements of a vector. More... | |
RCP< const Basic > | sub (const RCP< const Basic > &a, const RCP< const Basic > &b) |
Substracts b from a . More... | |
Additional Inherited Members | |
Data Fields inherited from SymEngine::Basic | |
TypeID | type_code_ |
The base class for representing addition in symbolic expressions.
Internally this is implemented in as a numeric coefficient coef_
and a dictionary dict_
of key-value pairs. Consider the following example:
Add(coef_, {{key1, value1}, {key2, value2}, ... })
This represents the following expression,
coef_ + key1*value1 + key2*value2 + ...
coef_
and the values of the dictionary may be numeric coefficients like Integer, RealDouble, Complex while their corresponding key
s can be any symbolic expression except numeric coefficients and Mul
objects with coefficient != 1.
For example, the following are valid representations
Add(1, {{x, 2}, {y, 5}}) Add(0, {{x, 1}, {y, 4}, {z, 3}})
The following representations are invalid (their valid equivalent is shown next to each of them)
Add(1, {{x, 1}, {2*y, 3}) -> Add(1, {{x, 1}, {y, 6}}) Add(0, {{x, 2}}) -> Mul(2, {{x, 1}}) Add(1, {{x, 2}, {4, 6}}) -> Add(25, {{x, 2}})
A visual aid (from the SymEngine Wiki) for understanding this class in the broader context of the data structure for mathematical expressions is:
SymEngine::Add::Add | ( | const RCP< const Number > & | coef, |
umap_basic_num && | dict | ||
) |
Default constructor.
coef | numeric coefficient. |
dict | dictionary of the expression without the coefficient. |
Constructs Add from a dictionary by copying the contents of the dictionary.
Definition at line 64 of file add.cpp.
|
overridevirtual |
Test equality.
o | a constant reference to object to test against. |
this
is equal to o
.This older implementation compares the elements of the coefficients and expressions for two objects.
Implements SymEngine::Basic.
Definition at line 88 of file add.cpp.
|
overridevirtual |
Generates the hash representation.
This uses Basic.hash()
to give a cached version of the hash.
Implements SymEngine::Basic.
|
static |
Converts a Basic self
into the form of coefficient * term
.
coef | numerical coefficient. |
term | the term. |
This function converts the its representation as per the following logic:
self
is a Mul
return the coefficient and the remaining term.self
is not Mul
or Add
the coefficient is set one and the term is unchanged.self
is a Number
the term is set one and the coefficient is unchanged. Definition at line 306 of file add.cpp.
void SymEngine::Add::as_two_terms | ( | const Ptr< RCP< const Basic >> & | a, |
const Ptr< RCP< const Basic >> & | b | ||
) | const |
Converts the Add into a sum of two Basic objects.
a | first basic object. |
b | second basic object. |
This implementation first converts a to a Mul
and then performs addition.
Definition at line 287 of file add.cpp.
|
static |
Updates the numerical coefficient and the dictionary.
coef | the numerical coefficient. |
d | the dictionary containing the expression. |
c | the numerical coefficient to be added. |
term | the new term. |
This implements the following logic:
c
and term
are numbers, then the term (c* term)
is added to the existing coeff
.term
is not a number then the pair (c, term
) is used to update the existing dict d
(as a pair c, term
).term
is Add
and c=1
, expands the Add
into the coeff
and d
. Definition at line 261 of file add.cpp.
|
overridevirtual |
Compares Add
objects.
o | object to test against. |
unified_compare()
for the actual implementation. this
is equal to o
otherwise (-1).This function takes a Basic
object, checks if it is an Add
object, and subsequently compares exhaustively:
map_basic_num
representation is not cached by Add
after being computed, this is slow. <
adict
and bdict
Implements SymEngine::Basic.
Definition at line 107 of file add.cpp.
|
static |
Adds a new term to the expression.
d | dictionary to be updated. |
coef | new coefficient. |
t | new term. |
Adds (coeff*t)
to the dict d inplace.
t
has no numerical coefficients, and coef
has only numerical coefficients. Definition at line 237 of file add.cpp.
|
static |
Create an appropriate instance from dictionary quickly.
coef | the numeric coefficient. |
d | the dictionary of the expression without the coefficient. |
coef
if the dictionary is empty (size 0). Mul
if the dictionary has one element which is a Mul
. Integer
if the dictionary has one element which is a Integer
. Symbol
if the dictionary has one element which is a Symbol
. Pow
if the dictionary has one element which is a Pow
. Add
if the size of the dictionary is greater than 1.Quick implementation which depends only on the size of d.
The speed benefits also arise from the fact that when using the SymEngine::RCP
in production mode (which is not thread safe) then when a single Mul
object is encountered, instead of copying its dict_
, it is reused instead.
That is, when WITH_SYMENGINE_THREAD_SAFE
is not defined and WITH_SYMENGINE_RCP
is defined, we can "steal" its dictionary by explictly casting away the const'ness. Since the refcount_
is 1, nothing else is using the Mul
.
Definition at line 140 of file add.cpp.
|
overridevirtual |
Returns the arguments of the Add.
Add
.For an Add
of the form:
Add(coef_, {{key1, value1}, {key2, value2}, ... })
If coef_ is non-zero it returns:
{coef_, key1*value1, key2*value2, ... }
otherwise it returns:
{key1*value1, key2*value2, ... }
Implements SymEngine::Basic.
Definition at line 397 of file add.cpp.
|
inline |
bool SymEngine::Add::is_canonical | ( | const RCP< const Number > & | coef, |
const umap_basic_num & | dict | ||
) | const |
Checks if a given dictionary and coeffient is in cannonical form.
coef | numerical coefficient. |
dict | dictionary of remaining expression terms. |
true
if canonical.This function ensures that each term in dict is in canonical form. The implementation in the form of a exclusion list (defaults to true).
coef
and dict
, so null
coefficients and purely numerical (empty dictionaries) are also not considered to be in canonical form. Also, the ordering is important, it must be (coeff, dict)
and not (dict, coeff)
.Some non-cannonical forms are:
Definition at line 348 of file add.cpp.
Sums the elements of a vector.
a | is a vector. |
a
.Definition at line 481 of file add.cpp.
Substracts b
from a
.
a | is the minuend. |
b | is the subtrahend. |
a-b
.Definition at line 495 of file add.cpp.
|
private |
|
private |
|
static |