Minsky: 3.17.0
expr.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2014
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 // some nice syntactic sugar for representing expressions
21 
22 #ifndef EXPR_H
23 #define EXPR_H
24 #include "equations.h"
25 namespace MathDAG
26 {
27  // some nice syntactic sugar for representing expressions
28  struct Expr: public NodePtr
29  {
30  SubexpressionCache& cache; //for anonymous nodes
33  Expr(SubexpressionCache& cache, const shared_ptr<OperationDAGBase>& x):
36  NodePtr(cache.reverseLookup(x)), cache(cache) {assert(*this);}
38  NodePtr(cache.reverseLookup(*x)), cache(cache) {assert(*this);}
39 
40  shared_ptr<OperationDAGBase> newNode(OperationType::Type type) const {
41  shared_ptr<OperationDAGBase> r(OperationDAGBase::create(type));
43  return r;
44  }
45 
46  Expr operator+(const NodePtr& x) const {
48  shared_ptr<OperationDAGBase> r=newNode(OperationType::add);
49  r->arguments[0].push_back(*this);
50  r->arguments[1].push_back(x);
51  return Expr(cache,r);
52  }
53  Expr operator-(const NodePtr& x) const {
55  shared_ptr<OperationDAGBase> r=newNode(OperationType::subtract);
56  r->arguments[0].push_back(*this);
57  r->arguments[1].push_back(x);
58  return Expr(cache,r);
59  }
60  Expr operator*(const NodePtr& x) const {
62  shared_ptr<OperationDAGBase> r=newNode(OperationType::multiply);
63  r->arguments[0].push_back(*this);
64  r->arguments[1].push_back(x);
65  return Expr(cache,r);
66  }
67  Expr operator/(const NodePtr& x) const {
69  shared_ptr<OperationDAGBase> r=newNode(OperationType::divide);
70  r->arguments[0].push_back(*this);
71  r->arguments[1].push_back(x);
72  return Expr(cache,r);
73  }
74  };
75 
76  inline Expr operator+(const NodePtr& x, const Expr& y) {
77  return y+x;
78  }
79  inline Expr operator+(const Expr& x, const Expr& y) {
80  return x+NodePtr(y);
81  }
82  inline Expr operator+(double x, const Expr& y) {
83  return y+NodePtr(new ConstantDAG(x));
84  }
85 
86  inline Expr operator-(const NodePtr& x, const Expr& y) {
87  return Expr(y.cache, x)-NodePtr(y);
88  }
89  inline Expr operator-(const Expr& x, const Expr& y) {
90  return x-NodePtr(y);
91  }
92  inline Expr operator-(double x, const Expr& y) {
93  return NodePtr(new ConstantDAG(x))-y;
94  }
95  inline Expr operator-(const Expr& x, double y) {
96  return x-NodePtr(new ConstantDAG(y));
97  }
98 
99  inline Expr operator*(const NodePtr& x, const Expr& y) {
100  return y*x;
101  }
102  inline Expr operator*(const Expr& x, const Expr& y) {
103  return x*NodePtr(y);
104  }
105  inline Expr operator*(double x, const Expr& y) {
106  return y*NodePtr(new ConstantDAG(x));
107  }
108 
109  inline Expr operator/(const Expr& x, const Expr& y) {
110  return x/NodePtr(y);
111  }
112  inline Expr operator/(const NodePtr& x, const Expr& y) {
113  return Expr(y.cache, x)/y;
114  }
115  inline Expr operator/(double x, const Expr& y) {
116  return NodePtr(new ConstantDAG(x))/y;
117  }
118 
119  inline Expr log(const Expr& x) {
120  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::ln));
121  r->arguments[0].push_back(x);
122  return Expr(x.cache,r);
123  }
124 
125  inline Expr exp(const Expr& x) {
126  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::exp));
127  r->arguments[0].push_back(x);
128  return Expr(x.cache,r);
129  }
130  inline Expr sin(const Expr& x) {
131  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sin));
132  r->arguments[0].push_back(x);
133  return Expr(x.cache,r);
134  }
135 
136  inline Expr cos(const Expr& x) {
137  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cos));
138  r->arguments[0].push_back(x);
139  return Expr(x.cache,r);
140  }
141  inline Expr sinh(const Expr& x) {
142  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sinh));
143  r->arguments[0].push_back(x);
144  return Expr(x.cache,r);
145  }
146 
147  inline Expr cosh(const Expr& x) {
148  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cosh));
149  r->arguments[0].push_back(x);
150  return Expr(x.cache,r);
151  }
152 
153  inline Expr sqrt(const Expr& x) {
154  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sqrt));
155  r->arguments[0].push_back(x);
156  return Expr(x.cache,r);
157  }
158 
159  inline Expr Gamma(const Expr& x) {
160  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::Gamma));
161  r->arguments[0].push_back(x);
162  return Expr(x.cache,r);
163  }
164 
165  inline Expr polygamma(const Expr& x, const Expr& y) {
166  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::polygamma));
167  r->arguments[0].push_back(x);
168  r->arguments[1].push_back(y);
169  return Expr(x.cache,r);
170  }
171 
172  inline Expr fact(const Expr& x) {
173  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::fact));
174  r->arguments[0].push_back(x);
175  return Expr(x.cache,r);
176  }
177 
178  inline Expr operator<=(const Expr& x, const NodePtr& y) {
179  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::le));
180  r->arguments[0].push_back(x);
181  r->arguments[1].push_back(y);
182  return Expr(x.cache,r);
183  }
184 
185  inline Expr operator<=(const Expr& x, double y) {
186  return x <= Expr(x.cache, NodePtr(new ConstantDAG(y)));
187  }
188  inline Expr operator<=(double x, const Expr& y) {
189  return Expr(y.cache, NodePtr(new ConstantDAG(x))) <= y;
190  }
191 
192  inline Expr operator<(const Expr& x, const NodePtr& y) {
193  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::lt));
194  r->arguments[0].push_back(x);
195  r->arguments[1].push_back(y);
196  return Expr(x.cache,r);
197  }
198 
199  inline Expr operator<(const Expr& x, double y) {
200  return x < Expr(x.cache, NodePtr(new ConstantDAG(y)));
201  }
202  inline Expr operator<(double x, const Expr& y) {
203  return Expr(y.cache, NodePtr(new ConstantDAG(x))) < y;
204  }
205 
206  template <OperationType::Type T>
207  struct CachedOp: std::shared_ptr<OperationDAGBase>
208  {
210  std::shared_ptr<OperationDAGBase>(OperationDAGBase::create(T))
211  {ec.insertAnonymous(*this);}
212  };
213 }
214 
215 #endif
Expr operator<=(const Expr &x, const NodePtr &y)
Definition: expr.h:178
Expr sin(const Expr &x)
Definition: expr.h:130
Expr cos(const Expr &x)
Definition: expr.h:136
Expr polygamma(const Expr &x, const Expr &y)
Definition: expr.h:165
Expr operator-(const NodePtr &x) const
Definition: expr.h:53
Expr sqrt(const Expr &x)
Definition: expr.h:153
Expr Gamma(const Expr &x)
Definition: expr.h:159
Expr operator+(const NodePtr &x) const
Definition: expr.h:46
STL namespace.
Expr operator/(const NodePtr &x) const
Definition: expr.h:67
Expr operator+(const NodePtr &x, const Expr &y)
Definition: expr.h:76
shared_ptr< OperationDAGBase > newNode(OperationType::Type type) const
Definition: expr.h:40
Expr operator<(const Expr &x, const NodePtr &y)
Definition: expr.h:192
Expr exp(const Expr &x)
Definition: expr.h:125
SubexpressionCache & cache
Definition: expr.h:30
Expr sinh(const Expr &x)
Definition: expr.h:141
Expr cosh(const Expr &x)
Definition: expr.h:147
Expr(SubexpressionCache &cache, const WeakNodePtr &x)
Definition: expr.h:37
Expr(SubexpressionCache &cache, const shared_ptr< OperationDAGBase > &x)
Definition: expr.h:33
CachedOp(SubexpressionCache &ec)
Definition: expr.h:209
weak reference into subexpression cache
Definition: equations.h:133
Expr(SubexpressionCache &cache, const Node &x)
Definition: expr.h:35
std::shared_ptr< Node > NodePtr
Definition: equations.h:131
Expr fact(const Expr &x)
Definition: expr.h:172
Expr operator*(const NodePtr &x, const Expr &y)
Definition: expr.h:99
Expr operator-(const NodePtr &x, const Expr &y)
Definition: expr.h:86
NodePtr insertAnonymous(NodePtr x)
Definition: equations.h:351
Expr(SubexpressionCache &cache, const NodePtr &x)
Definition: expr.h:31
Expr operator/(const Expr &x, const Expr &y)
Definition: expr.h:109
Expr log(const Expr &x)
Definition: expr.h:119
static OperationDAGBase * create(Type type, const string &name="")
factory method
Definition: equations.cc:176
Expr operator*(const NodePtr &x) const
Definition: expr.h:60