Minsky
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 #include <ecolab_epilogue.h>
26 namespace MathDAG
27 {
28  // some nice syntactic sugar for representing expressions
29  struct Expr: public NodePtr
30  {
31  SubexpressionCache& cache; //for anonymous nodes
34  Expr(SubexpressionCache& cache, const shared_ptr<OperationDAGBase>& x):
37  NodePtr(cache.reverseLookup(x)), cache(cache) {assert(*this);}
39  NodePtr(cache.reverseLookup(*x)), cache(cache) {assert(*this);}
40 
41  shared_ptr<OperationDAGBase> newNode(OperationType::Type type) const {
42  shared_ptr<OperationDAGBase> r(OperationDAGBase::create(type));
44  return r;
45  }
46 
47  Expr operator+(const NodePtr& x) const {
49  shared_ptr<OperationDAGBase> r=newNode(OperationType::add);
50  r->arguments[0].push_back(*this);
51  r->arguments[1].push_back(x);
52  return Expr(cache,r);
53  }
54  Expr operator-(const NodePtr& x) const {
56  shared_ptr<OperationDAGBase> r=newNode(OperationType::subtract);
57  r->arguments[0].push_back(*this);
58  r->arguments[1].push_back(x);
59  return Expr(cache,r);
60  }
61  Expr operator*(const NodePtr& x) const {
63  shared_ptr<OperationDAGBase> r=newNode(OperationType::multiply);
64  r->arguments[0].push_back(*this);
65  r->arguments[1].push_back(x);
66  return Expr(cache,r);
67  }
68  Expr operator/(const NodePtr& x) const {
70  shared_ptr<OperationDAGBase> r=newNode(OperationType::divide);
71  r->arguments[0].push_back(*this);
72  r->arguments[1].push_back(x);
73  return Expr(cache,r);
74  }
75  };
76 
77  inline Expr operator+(const NodePtr& x, const Expr& y) {
78  return y+x;
79  }
80  inline Expr operator+(const Expr& x, const Expr& y) {
81  return x+NodePtr(y);
82  }
83  inline Expr operator+(double x, const Expr& y) {
84  return y+NodePtr(new ConstantDAG(x));
85  }
86 
87  inline Expr operator-(const NodePtr& x, const Expr& y) {
88  return Expr(y.cache, x)-NodePtr(y);
89  }
90  inline Expr operator-(const Expr& x, const Expr& y) {
91  return x-NodePtr(y);
92  }
93  inline Expr operator-(double x, const Expr& y) {
94  return NodePtr(new ConstantDAG(x))-y;
95  }
96  inline Expr operator-(const Expr& x, double y) {
97  return x-NodePtr(new ConstantDAG(y));
98  }
99 
100  inline Expr operator*(const NodePtr& x, const Expr& y) {
101  return y*x;
102  }
103  inline Expr operator*(const Expr& x, const Expr& y) {
104  return x*NodePtr(y);
105  }
106  inline Expr operator*(double x, const Expr& y) {
107  return y*NodePtr(new ConstantDAG(x));
108  }
109 
110  inline Expr operator/(const Expr& x, const Expr& y) {
111  return x/NodePtr(y);
112  }
113  inline Expr operator/(const NodePtr& x, const Expr& y) {
114  return Expr(y.cache, x)/y;
115  }
116  inline Expr operator/(double x, const Expr& y) {
117  return NodePtr(new ConstantDAG(x))/y;
118  }
119 
120  inline Expr log(const Expr& x) {
121  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::ln));
122  r->arguments[0].push_back(x);
123  return Expr(x.cache,r);
124  }
125 
126  inline Expr exp(const Expr& x) {
127  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::exp));
128  r->arguments[0].push_back(x);
129  return Expr(x.cache,r);
130  }
131  inline Expr sin(const Expr& x) {
132  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sin));
133  r->arguments[0].push_back(x);
134  return Expr(x.cache,r);
135  }
136 
137  inline Expr cos(const Expr& x) {
138  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cos));
139  r->arguments[0].push_back(x);
140  return Expr(x.cache,r);
141  }
142  inline Expr sinh(const Expr& x) {
143  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sinh));
144  r->arguments[0].push_back(x);
145  return Expr(x.cache,r);
146  }
147 
148  inline Expr cosh(const Expr& x) {
149  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cosh));
150  r->arguments[0].push_back(x);
151  return Expr(x.cache,r);
152  }
153 
154  inline Expr sqrt(const Expr& x) {
155  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sqrt));
156  r->arguments[0].push_back(x);
157  return Expr(x.cache,r);
158  }
159 
160  inline Expr Gamma(const Expr& x) {
161  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::Gamma));
162  r->arguments[0].push_back(x);
163  return Expr(x.cache,r);
164  }
165 
166  inline Expr polygamma(const Expr& x, const Expr& y) {
167  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::polygamma));
168  r->arguments[0].push_back(x);
169  r->arguments[1].push_back(y);
170  return Expr(x.cache,r);
171  }
172 
173  inline Expr fact(const Expr& x) {
174  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::fact));
175  r->arguments[0].push_back(x);
176  return Expr(x.cache,r);
177  }
178 
179  inline Expr operator<=(const Expr& x, const NodePtr& y) {
180  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::le));
181  r->arguments[0].push_back(x);
182  r->arguments[1].push_back(y);
183  return Expr(x.cache,r);
184  }
185 
186  inline Expr operator<=(const Expr& x, double y) {
187  return x <= Expr(x.cache, NodePtr(new ConstantDAG(y)));
188  }
189  inline Expr operator<=(double x, const Expr& y) {
190  return Expr(y.cache, NodePtr(new ConstantDAG(x))) <= y;
191  }
192 
193  inline Expr operator<(const Expr& x, const NodePtr& y) {
194  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::lt));
195  r->arguments[0].push_back(x);
196  r->arguments[1].push_back(y);
197  return Expr(x.cache,r);
198  }
199 
200  inline Expr operator<(const Expr& x, double y) {
201  return x < Expr(x.cache, NodePtr(new ConstantDAG(y)));
202  }
203  inline Expr operator<(double x, const Expr& y) {
204  return Expr(y.cache, NodePtr(new ConstantDAG(x))) < y;
205  }
206 
207  template <OperationType::Type T>
208  struct CachedOp: std::shared_ptr<OperationDAGBase>
209  {
211  std::shared_ptr<OperationDAGBase>(OperationDAGBase::create(T))
212  {ec.insertAnonymous(*this);}
213  };
214 }
215 
216 #endif
Expr operator<=(const Expr &x, const NodePtr &y)
Definition: expr.h:179
Expr sin(const Expr &x)
Definition: expr.h:131
Expr cos(const Expr &x)
Definition: expr.h:137
Expr polygamma(const Expr &x, const Expr &y)
Definition: expr.h:166
Expr operator-(const NodePtr &x) const
Definition: expr.h:54
Expr sqrt(const Expr &x)
Definition: expr.h:154
Expr Gamma(const Expr &x)
Definition: expr.h:160
Expr operator+(const NodePtr &x) const
Definition: expr.h:47
STL namespace.
Expr operator/(const NodePtr &x) const
Definition: expr.h:68
Expr operator+(const NodePtr &x, const Expr &y)
Definition: expr.h:77
shared_ptr< OperationDAGBase > newNode(OperationType::Type type) const
Definition: expr.h:41
Expr operator<(const Expr &x, const NodePtr &y)
Definition: expr.h:193
Expr exp(const Expr &x)
Definition: expr.h:126
SubexpressionCache & cache
Definition: expr.h:31
Expr sinh(const Expr &x)
Definition: expr.h:142
Expr cosh(const Expr &x)
Definition: expr.h:148
Expr(SubexpressionCache &cache, const WeakNodePtr &x)
Definition: expr.h:38
Expr(SubexpressionCache &cache, const shared_ptr< OperationDAGBase > &x)
Definition: expr.h:34
CachedOp(SubexpressionCache &ec)
Definition: expr.h:210
weak reference into subexpression cache
Definition: equations.h:133
Expr(SubexpressionCache &cache, const Node &x)
Definition: expr.h:36
std::shared_ptr< Node > NodePtr
Definition: equations.h:131
Expr fact(const Expr &x)
Definition: expr.h:173
Expr operator*(const NodePtr &x, const Expr &y)
Definition: expr.h:100
Expr operator-(const NodePtr &x, const Expr &y)
Definition: expr.h:87
NodePtr insertAnonymous(NodePtr x)
Definition: equations.h:351
Expr(SubexpressionCache &cache, const NodePtr &x)
Definition: expr.h:32
Expr operator/(const Expr &x, const Expr &y)
Definition: expr.h:110
Expr log(const Expr &x)
Definition: expr.h:120
static OperationDAGBase * create(Type type, const string &name="")
factory method
Definition: equations.cc:176
Expr operator*(const NodePtr &x) const
Definition: expr.h:61