msymenginepoly.cpp
1 #include <symengine/polys/msymenginepoly.h>
2 
3 namespace SymEngine
4 {
5 
6 RCP<const Basic> MIntPoly::as_symbolic() const
7 {
8  vec_basic args;
9  for (const auto &p : get_poly().dict_) {
10  RCP<const Basic> res = integer(p.second);
11  int whichvar = 0;
12  for (auto sym : get_vars()) {
13  if (0 != p.first[whichvar])
14  res = SymEngine::mul(res, pow(sym, integer(p.first[whichvar])));
15  whichvar++;
16  }
17  args.push_back(res);
18  }
19  return SymEngine::add(args);
20 }
21 
22 hash_t MIntPoly::__hash__() const
23 {
24  hash_t seed = SYMENGINE_MINTPOLY;
25  for (auto var : get_vars())
26  hash_combine<std::string>(seed, var->__str__());
27 
28  for (auto &p : get_poly().dict_) {
29  hash_t t = vec_hash<vec_uint>()(p.first);
30  hash_combine<hash_t>(t, mp_get_si(p.second));
31  seed ^= t;
32  }
33  return seed;
34 }
35 
36 integer_class MIntPoly::eval(
37  std::map<RCP<const Basic>, integer_class, RCPBasicKeyLess> &vals) const
38 {
39  // TODO : handle missing values
40  integer_class ans(0), temp, term;
41  for (auto bucket : get_poly().dict_) {
42  term = bucket.second;
43  unsigned int whichvar = 0;
44  for (auto sym : get_vars()) {
45  mp_pow_ui(temp, vals.find(sym)->second, bucket.first[whichvar]);
46  term *= temp;
47  whichvar++;
48  }
49  ans += term;
50  }
51  return ans;
52 }
53 
54 RCP<const Basic> MExprPoly::as_symbolic() const
55 {
56  vec_basic args;
57  for (const auto &p : get_poly().dict_) {
58  RCP<const Basic> res = (p.second.get_basic());
59  int whichvar = 0;
60  for (auto sym : get_vars()) {
61  if (0 != p.first[whichvar])
62  res = SymEngine::mul(res, pow(sym, integer(p.first[whichvar])));
63  whichvar++;
64  }
65  args.push_back(res);
66  }
67  return SymEngine::add(args);
68 }
69 
70 hash_t MExprPoly::__hash__() const
71 {
72  hash_t seed = SYMENGINE_MEXPRPOLY;
73  for (auto var : get_vars())
74  hash_combine<std::string>(seed, var->__str__());
75 
76  for (auto &p : get_poly().dict_) {
77  hash_t t = vec_hash<vec_int>()(p.first);
78  hash_combine<Basic>(t, *(p.second.get_basic()));
79  seed ^= t;
80  }
81  return seed;
82 }
83 
84 Expression MExprPoly::eval(
85  std::map<RCP<const Basic>, Expression, RCPBasicKeyLess> &vals) const
86 {
87  // TODO : handle missing values
88  Expression ans(0);
89  for (auto bucket : get_poly().dict_) {
90  Expression term = bucket.second;
91  unsigned int whichvar = 0;
92  for (auto sym : get_vars()) {
93  term *= pow(vals.find(sym)->second, bucket.first[whichvar]);
94  whichvar++;
95  }
96  ans += term;
97  }
98  return ans;
99 }
100 
101 unsigned int reconcile(vec_uint &v1, vec_uint &v2, set_basic &s,
102  const set_basic &s1, const set_basic &s2)
103 {
104  auto i = s1.begin();
105  auto j = s2.begin();
106  unsigned int pos = 0;
107 
108  // Performs a merge of s1 and s2, and builds up v1 and v2 as translators
109  // v1[i] and v2[i] is the position of the ith symbol in the new set
110 
111  // set union
112  s = s1;
113  s.insert(s2.begin(), s2.end());
114 
115  for (auto &it : s) {
116  if (i != s1.end() and eq(*it, **i)) {
117  v1.push_back(pos);
118  i++;
119  }
120  if (j != s2.end() and eq(*it, **j)) {
121  v2.push_back(pos);
122  j++;
123  }
124  pos++;
125  }
126  // return size of the new symbol set
127  return pos;
128 }
129 
130 } // namespace SymEngine
hash_t __hash__() const
hash_t __hash__() const
Main namespace for SymEngine package.
Definition: add.cpp:19
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
Definition: integer.h:200
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition: mul.cpp:358
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition: add.cpp:425
T pow(T... args)
Our less operator (<):
Definition: basic.h:220