Loading...
Searching...
No Matches
basic.cpp
1#include <symengine/printers.h>
2#include <symengine/subs.h>
3#include <symengine/serialize-cereal.h>
4#include <array>
5
6namespace SymEngine
7{
8
9std::string type_code_name(TypeID id)
10{
11#define STRINGIFY0(x) #x
12#define STRINGIFY(x) STRINGIFY0(x)
13 const static std::array<std::string,
14 static_cast<int>(TypeID::TypeID_Count) + 1>
15 type_names{{
16#define SYMENGINE_INCLUDE_ALL
17#define SYMENGINE_ENUM(type, Class) STRINGIFY(Class),
18#include "symengine/type_codes.inc"
19#undef SYMENGINE_ENUM
20 "TypeID_Count"}};
21#undef SYMENGINE_INCLUDE_ALL
22#undef STRINGIFY0
23#undef STRINGIFY
24
25 if ((id < 0) || (id > TypeID::TypeID_Count)) {
26 throw std::runtime_error("type_id out of range");
27 }
28 return type_names[id];
29}
30
31int Basic::__cmp__(const Basic &o) const
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}
45
47{
48 return str(*this);
49}
50
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}
60
61RCP<const Basic> Basic::loads(const std::string &serialized)
62{
63 unsigned short major, minor;
64 RCP<const Basic> obj;
65 std::istringstream iss(serialized);
66 cereal::PortableBinaryInputArchive iarchive{iss};
67 iarchive(major, minor);
68 if (major != SYMENGINE_MAJOR_VERSION or minor != SYMENGINE_MINOR_VERSION) {
69 throw SerializationError(StreamFmt()
70 << "SymEngine-" << SYMENGINE_MAJOR_VERSION
71 << "." << SYMENGINE_MINOR_VERSION
72 << " was asked to deserialize an object "
73 << "created using SymEngine-" << major << "."
74 << minor << ".");
75 }
76 iarchive(obj);
77 return obj;
78}
79
80RCP<const Basic> Basic::subs(const map_basic_basic &subs_dict) const
81{
82 return SymEngine::subs(this->rcp_from_this(), subs_dict);
83}
84
85RCP<const Basic> Basic::xreplace(const map_basic_basic &xreplace_dict) const
86{
87 return SymEngine::xreplace(this->rcp_from_this(), xreplace_dict);
88}
89
90const char *get_version()
91{
92 return SYMENGINE_VERSION;
93}
94
95bool is_a_Atom(const Basic &b)
96{
97 return is_a_Number(b) or is_a<Symbol>(b) or is_a<Constant>(b);
98}
99
100} // namespace SymEngine
The lowest unit of symbolic representation.
Definition: basic.h:97
RCP< const Basic > subs(const map_basic_basic &subs_dict) const
Substitutes 'subs_dict' into 'self'.
Definition: basic.cpp:80
std::string dumps() const
Returns a string of the instance serialized.
Definition: basic.cpp:51
std::string __str__() const
Definition: basic.cpp:46
static RCP< const Basic > loads(const std::string &)
Creates an instance of a serialized string.
Definition: basic.cpp:61
int __cmp__(const Basic &o) const
Comparison operator.
Definition: basic.cpp:31
virtual int compare(const Basic &o) const =0
RCP< Basic > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
Main namespace for SymEngine package.
Definition: add.cpp:19
bool is_a_Number(const Basic &b)
Definition: number.h:130
@ TypeID_Count
Definition: basic.h:52
bool is_a_Atom(const Basic &b)
Returns true if b is an atom. i.e. b.get_args returns an empty vector.
Definition: basic.cpp:95
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
T str(T... args)