basic.cpp
1 #include <symengine/printers.h>
2 #include <symengine/subs.h>
3 #if HAVE_SYMENGINE_RTTI
4 #include <symengine/serialize-cereal.h>
5 #endif
6 #include <array>
7 
8 namespace SymEngine
9 {
10 
11 std::string type_code_name(TypeID id)
12 {
13 #define STRINGIFY0(x) #x
14 #define STRINGIFY(x) STRINGIFY0(x)
15  const static std::array<std::string,
16  static_cast<int>(TypeID::TypeID_Count) + 1>
17  type_names{{
18 #define SYMENGINE_INCLUDE_ALL
19 #define SYMENGINE_ENUM(type, Class) STRINGIFY(Class),
20 #include "symengine/type_codes.inc"
21 #undef SYMENGINE_ENUM
22  "TypeID_Count"}};
23 #undef SYMENGINE_INCLUDE_ALL
24 #undef STRINGIFY0
25 #undef STRINGIFY
26 
27  if ((id < 0) || (id > TypeID::TypeID_Count)) {
28  throw std::runtime_error("type_id out of range");
29  }
30  return type_names[id];
31 }
32 
33 int Basic::__cmp__(const Basic &o) const
34 {
35  auto a = this->get_type_code();
36  auto b = o.get_type_code();
37  if (a == b) {
38  return this->compare(o);
39  } else {
40  // We return the order given by the numerical value of the TypeID enum
41  // type.
42  // The types don't need to be ordered in any given way, they just need
43  // to be ordered.
44  return a < b ? -1 : 1;
45  }
46 }
47 
49 {
50  return str(*this);
51 }
52 
54 {
55 #if HAVE_SYMENGINE_RTTI
57  unsigned short major = SYMENGINE_MAJOR_VERSION;
58  unsigned short minor = SYMENGINE_MINOR_VERSION;
60  major, minor, this->rcp_from_this());
61  return oss.str();
62 #else
63  throw NotImplementedError("Serialization not implemented in no-rtti mode");
64 #endif
65 }
66 
67 RCP<const Basic> Basic::loads(const std::string &serialized)
68 {
69 #if HAVE_SYMENGINE_RTTI
70  unsigned short major, minor;
71  RCP<const Basic> obj;
72  std::istringstream iss(serialized);
74  iarchive(major, minor);
75  if (major != SYMENGINE_MAJOR_VERSION or minor != SYMENGINE_MINOR_VERSION) {
76  throw SerializationError(StreamFmt()
77  << "SymEngine-" << SYMENGINE_MAJOR_VERSION
78  << "." << SYMENGINE_MINOR_VERSION
79  << " was asked to deserialize an object "
80  << "created using SymEngine-" << major << "."
81  << minor << ".");
82  }
83  iarchive(obj);
84  return obj;
85 #else
86  throw NotImplementedError("Serialization not implemented in no-rtti mode");
87 #endif
88 }
89 
90 RCP<const Basic> Basic::subs(const map_basic_basic &subs_dict) const
91 {
92  return SymEngine::subs(this->rcp_from_this(), subs_dict);
93 }
94 
95 RCP<const Basic> Basic::xreplace(const map_basic_basic &xreplace_dict) const
96 {
97  return SymEngine::xreplace(this->rcp_from_this(), xreplace_dict);
98 }
99 
100 const char *get_version()
101 {
102  return SYMENGINE_VERSION;
103 }
104 
105 bool is_a_Atom(const Basic &b)
106 {
107  return is_a_Number(b) or is_a<Symbol>(b) or is_a<Constant>(b);
108 }
109 
110 } // 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:90
std::string dumps() const
Returns a string of the instance serialized.
Definition: basic.cpp:53
std::string __str__() const
Definition: basic.cpp:48
static RCP< const Basic > loads(const std::string &)
Creates an instance of a serialized string.
Definition: basic.cpp:67
int __cmp__(const Basic &o) const
Comparison operator.
Definition: basic.cpp:33
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:105
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)
Code