Minsky
MathDAG Namespace Reference

Namespaces

 anonymous_namespace{equationDisplayRender.cc}
 
 anonymous_namespace{equations.cc}
 
 anonymous_namespace{node_latex.cc}
 
 anonymous_namespace{node_matlab.cc}
 

Classes

struct  CachedOp
 
struct  ConstantDAG
 
struct  Expr
 
struct  GodleyColumnDAG
 represents a Godley column More...
 
struct  IntegralInputVariableDAG
 represents the input of an integration operation - differs from Variable DAG in that it doesn't refer to the VariableValue More...
 
struct  LaTeXManip
 a manipulator to make iostream expressions easy More...
 
struct  LockDAG
 
struct  MatlabManip
 
struct  Node
 
struct  OperationDAG
 
struct  OperationDAGBase
 
class  SubexpressionCache
 
class  SystemOfEquations
 
class  VariableDAG
 
struct  WeakNodePtr
 weak reference into subexpression cache More...
 

Typedefs

typedef std::shared_ptr< NodeNodePtr
 
typedef shared_ptr< VariableDAGVariableDAGPtr
 

Functions

string differentiateName (const std::string &x)
 creates a new name to represent the derivative of a variable More...
 
string latex (double)
 convert double to a LaTeX string representing that value More...
 
string latexInit (const string &)
 convert an initialisation string into a matlab expression More...
 
string mathrm (const string &nm)
 wraps in if nm has more than one letter - and also takes into account LaTeX subscript/superscripts More...
 
string matlabInit (const string &)
 convert an initialisation string into a matlab expression More...
 
ostream & operator<< (ostream &o, LaTeXManip m)
 
ostream & operator<< (ostream &o, MatlabManip m)
 
Expr operator+ (const NodePtr &x, const Expr &y)
 
Expr operator+ (const Expr &x, const Expr &y)
 
Expr operator+ (double x, const Expr &y)
 
Expr operator- (const NodePtr &x, const Expr &y)
 
Expr operator- (const Expr &x, const Expr &y)
 
Expr operator- (double x, const Expr &y)
 
Expr operator- (const Expr &x, double y)
 
Expr operator* (const NodePtr &x, const Expr &y)
 
Expr operator* (const Expr &x, const Expr &y)
 
Expr operator* (double x, const Expr &y)
 
Expr operator/ (const Expr &x, const Expr &y)
 
Expr operator/ (const NodePtr &x, const Expr &y)
 
Expr operator/ (double x, const Expr &y)
 
Expr log (const Expr &x)
 
Expr exp (const Expr &x)
 
Expr sin (const Expr &x)
 
Expr cos (const Expr &x)
 
Expr sinh (const Expr &x)
 
Expr cosh (const Expr &x)
 
Expr sqrt (const Expr &x)
 
Expr Gamma (const Expr &x)
 
Expr polygamma (const Expr &x, const Expr &y)
 
Expr fact (const Expr &x)
 
Expr operator<= (const Expr &x, const NodePtr &y)
 
Expr operator<= (const Expr &x, double y)
 
Expr operator<= (double x, const Expr &y)
 
Expr operator< (const Expr &x, const NodePtr &y)
 
Expr operator< (const Expr &x, double y)
 
Expr operator< (double x, const Expr &y)
 

Typedef Documentation

◆ NodePtr

typedef std::shared_ptr<Node> MathDAG::NodePtr

Definition at line 131 of file equations.h.

◆ VariableDAGPtr

typedef shared_ptr<VariableDAG> MathDAG::VariableDAGPtr

Definition at line 208 of file equations.h.

Function Documentation

◆ cos()

Expr MathDAG::cos ( const Expr x)
inline

Definition at line 137 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::cos, and MathDAG::Expr::newNode().

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), minsky::VariableBase::draw(), minsky::EvalOp< minsky::OperationType::constant >::evaluate(), minsky::RenderVariable::inImage(), minsky::Group::inIORegion(), minsky::PhillipsDiagram::init(), minsky::VariableBase::onMouseMotion(), and minsky::Group::rotFactor().

137  {
138  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cos));
139  r->arguments[0].push_back(x);
140  return Expr(x.cache,r);
141  }
Expr cos(const Expr &x)
Definition: expr.h:137
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cosh()

Expr MathDAG::cosh ( const Expr x)
inline

Definition at line 148 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::cosh, and MathDAG::Expr::newNode().

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), and minsky::EvalOp< minsky::OperationType::constant >::evaluate().

148  {
149  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::cosh));
150  r->arguments[0].push_back(x);
151  return Expr(x.cache,r);
152  }
Expr cosh(const Expr &x)
Definition: expr.h:148
Here is the call graph for this function:
Here is the caller graph for this function:

◆ differentiateName()

std::string MathDAG::differentiateName ( const string &  x)

creates a new name to represent the derivative of a variable

Definition at line 39 of file derivative.cc.

References minsky::str().

Referenced by MathDAG::SystemOfEquations::derivative().

40  {
41  int order=0; // current derivative order
42  string varName=x; // base variable name
43  const regex singleDeriv(R"(d(.*)/dt)"),
44  higherOrderDeriv(R"(d\^\{(\d*)\}(.*)/dt\^\{(\d*)\})");
45  smatch m;
46  if (regex_match(x,m,singleDeriv))
47  {
48  order=1;
49  varName=m[1];
50  }
51  else if(regex_match(x,m,higherOrderDeriv) && m[1]==m[3])
52  {
53  // for some reason, stoi is missing on MXE, so fake it
54  order=atoi(m[3].str().c_str());
55  varName=m[2];
56  }
57  order++;
58  ostringstream r;
59  if (order==1)
60  r<<"d"<<varName<<"/dt";
61  else
62  r<<"d^{"<<order<<"}"<<varName<<"/dt^{"<<order<<"}";
63  return r.str();
64  }
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ exp()

Expr MathDAG::exp ( const Expr x)
inline

Definition at line 126 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::exp, and MathDAG::Expr::newNode().

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), minsky::EvalOp< minsky::OperationType::constant >::evaluate(), and minsky::expMultiplier().

126  {
127  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::exp));
128  r->arguments[0].push_back(x);
129  return Expr(x.cache,r);
130  }
Expr exp(const Expr &x)
Definition: expr.h:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fact()

Expr MathDAG::fact ( const Expr x)
inline

Definition at line 173 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::fact, and MathDAG::Expr::newNode().

173  {
174  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::fact));
175  r->arguments[0].push_back(x);
176  return Expr(x.cache,r);
177  }
Expr fact(const Expr &x)
Definition: expr.h:173
Here is the call graph for this function:

◆ Gamma()

Expr MathDAG::Gamma ( const Expr x)
inline

Definition at line 160 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::Gamma, and MathDAG::Expr::newNode().

Referenced by MathDAG::SystemOfEquations::derivative().

160  {
161  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::Gamma));
162  r->arguments[0].push_back(x);
163  return Expr(x.cache,r);
164  }
Expr Gamma(const Expr &x)
Definition: expr.h:160
Here is the call graph for this function:
Here is the caller graph for this function:

◆ latex()

string MathDAG::latex ( double  x)

convert double to a LaTeX string representing that value

Definition at line 28 of file node_latex.cc.

References minsky::pow(), and minsky::str().

Referenced by MathDAG::OperationDAG< OperationType::subtract >::latex(), and MathDAG::Node::latexStr().

29  {
30  if (abs(x)>0 && (abs(x)>=1e5 || abs(x)<=1e-4))
31  {
32  int exponent=static_cast<int>(log10(abs(x)));
33  if (exponent<0) exponent++;
34  return str(x/pow(10.0,exponent))+"\\times10^{"+str(exponent)+"}";
35  }
36  return str(x);
37  }
UnitsExpressionWalker pow(const UnitsExpressionWalker &x, const UnitsExpressionWalker &y)
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ latexInit()

string MathDAG::latexInit ( const string &  init)

convert an initialisation string into a matlab expression

Definition at line 73 of file node_latex.cc.

References minsky::cminsky(), minsky::VariableValue::init(), TCLcmd::trap::init, minsky::VariableValues::initValue(), minsky::str(), and minsky::Minsky::variableValues.

Referenced by MathDAG::SystemOfEquations::latex(), MathDAG::SystemOfEquations::latexWrapped(), MathDAG::SystemOfEquations::renderEquations(), and MathDAG::anonymous_namespace{equationDisplayRender.cc}::variableRender().

74  {
75  if (init.empty()) return "0";
76  VariableValue v;
77  v.init(init);
78  auto t=cminsky().variableValues.initValue(v);
79  string r;
80  switch (t.rank())
81  {
82  case 0: return str(t[0]);
83  case 1: r="(";
84  for (size_t i=0; i<5 && i<t.size(); ++i)
85  {
86  if (i>0) r+=' ';
87  r+=str(t[i]);
88  }
89  if (t.size()>5)
90  r+="\\ldots";
91  return r+")";
92  default:
93  return "\\ldots"; //TODO can we represent this stuff reasonably?
94  }
95  }
TensorVal initValue(const VariableValue &, std::set< std::string > &visited) const
evaluates the initial value of a given variableValue in the context given by this. visited is used to check for circular definitions
VariableValues variableValues
Definition: minsky.h:200
const std::string & init() const
struct TCLcmd::trap::init_t init
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log()

Expr MathDAG::log ( const Expr x)
inline

Definition at line 120 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::ln, and MathDAG::Expr::newNode().

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), minsky::EvalOp< minsky::OperationType::constant >::d2(), MathDAG::SystemOfEquations::derivative(), minsky::engExp(), minsky::EvalOp< minsky::OperationType::constant >::evaluate(), minsky::loadValueFromCSVFileT(), and minsky::OperationBase::unitsBinOpCase().

120  {
121  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::ln));
122  r->arguments[0].push_back(x);
123  return Expr(x.cache,r);
124  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mathrm()

string MathDAG::mathrm ( const string &  nm)

wraps in if nm has more than one letter - and also takes into account LaTeX subscript/superscripts

Definition at line 41 of file node_latex.cc.

Referenced by MathDAG::OperationDAG< OperationType::subtract >::latex(), MathDAG::VariableDAG::latex(), MathDAG::SystemOfEquations::latex(), MathDAG::SystemOfEquations::latexWrapped(), MathDAG::VariableDAG::render(), MathDAG::OperationDAG< OperationType::subtract >::render(), and MathDAG::SystemOfEquations::renderEquations().

42  {
43  // process super/sub scripts
44  string::size_type ss;
45  if ((ss=nm.find_first_of("_^"))!=string::npos)
46  return mathrm(nm.substr(0, ss)) + nm[ss] + "{"+mathrm(nm.substr(ss+1))+"}";
47 
48  // process % chars
49  string::size_type pc;
50  if ((pc=nm.find_first_of('%'))!=string::npos)
51  if (pc>0 && nm[pc-1]!='\\')
52  return mathrm(nm.substr(0, pc)) + "\\" + nm[pc] + mathrm(nm.substr(pc+1));
53 
54  // if its a single letter variable, or contains LaTeX codes, process as is
55  if (nm.length()==1 || nm.find('\\')!=string::npos)
56  return nm;
57  return "\\mathrm{"+nm+"}";
58  }
string mathrm(const string &nm)
wraps in if nm has more than one letter - and also takes into account LaTeX subscript/superscripts ...
Definition: node_latex.cc:41
Here is the caller graph for this function:

◆ matlabInit()

string MathDAG::matlabInit ( const string &  init)

convert an initialisation string into a matlab expression

Definition at line 45 of file node_matlab.cc.

References minsky::cminsky(), minsky::VariableValue::init(), TCLcmd::trap::init, minsky::VariableValues::initValue(), minsky::str(), and minsky::Minsky::variableValues.

Referenced by MathDAG::SystemOfEquations::matlab().

46  {
47  if (init.empty()) return "0";
48  VariableValue v;
49  v.init(init);
50  auto t=cminsky().variableValues.initValue(v);
51  string r;
52  switch (t.rank())
53  {
54  case 0: return str(t[0]);
55  case 1: r="[";
56  for (auto i: t) r+=str(i)+",";
57  return r+"]";
58  case 2:
59  r="[";
60  for (size_t i=0; i<t.hypercube().xvectors[1].size(); ++i)
61  {
62  for (size_t j=0; j<t.hypercube().xvectors[0].size(); ++j)
63  r+=str(t[i*t.hypercube().xvectors[0].size()+j])+",";
64  r+=";";
65  }
66  return r+"]";
67  default:
68  throw error("high rank tensors not supported in Matlab");
69  }
70  }
TensorVal initValue(const VariableValue &, std::set< std::string > &visited) const
evaluates the initial value of a given variableValue in the context given by this. visited is used to check for circular definitions
VariableValues variableValues
Definition: minsky.h:200
const std::string & init() const
struct TCLcmd::trap::init_t init
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator*() [1/3]

◆ operator*() [2/3]

Expr MathDAG::operator* ( const Expr x,
const Expr y 
)
inline

Definition at line 103 of file expr.h.

103  {
104  return x*NodePtr(y);
105  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator*() [3/3]

Expr MathDAG::operator* ( double  x,
const Expr y 
)
inline

Definition at line 106 of file expr.h.

106  {
107  return y*NodePtr(new ConstantDAG(x));
108  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator+() [1/3]

Expr MathDAG::operator+ ( const NodePtr x,
const Expr y 
)
inline

Definition at line 77 of file expr.h.

77  {
78  return y+x;
79  }

◆ operator+() [2/3]

Expr MathDAG::operator+ ( const Expr x,
const Expr y 
)
inline

Definition at line 80 of file expr.h.

80  {
81  return x+NodePtr(y);
82  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator+() [3/3]

Expr MathDAG::operator+ ( double  x,
const Expr y 
)
inline

Definition at line 83 of file expr.h.

83  {
84  return y+NodePtr(new ConstantDAG(x));
85  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator-() [1/4]

Expr MathDAG::operator- ( const NodePtr x,
const Expr y 
)
inline

Definition at line 87 of file expr.h.

References MathDAG::Expr::cache.

87  {
88  return Expr(y.cache, x)-NodePtr(y);
89  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator-() [2/4]

Expr MathDAG::operator- ( const Expr x,
const Expr y 
)
inline

Definition at line 90 of file expr.h.

90  {
91  return x-NodePtr(y);
92  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator-() [3/4]

Expr MathDAG::operator- ( double  x,
const Expr y 
)
inline

Definition at line 93 of file expr.h.

93  {
94  return NodePtr(new ConstantDAG(x))-y;
95  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator-() [4/4]

Expr MathDAG::operator- ( const Expr x,
double  y 
)
inline

Definition at line 96 of file expr.h.

96  {
97  return x-NodePtr(new ConstantDAG(y));
98  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator/() [1/3]

Expr MathDAG::operator/ ( const Expr x,
const Expr y 
)
inline

Definition at line 110 of file expr.h.

110  {
111  return x/NodePtr(y);
112  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator/() [2/3]

Expr MathDAG::operator/ ( const NodePtr x,
const Expr y 
)
inline

Definition at line 113 of file expr.h.

References MathDAG::Expr::cache.

113  {
114  return Expr(y.cache, x)/y;
115  }

◆ operator/() [3/3]

Expr MathDAG::operator/ ( double  x,
const Expr y 
)
inline

Definition at line 116 of file expr.h.

116  {
117  return NodePtr(new ConstantDAG(x))/y;
118  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator<() [1/3]

Expr MathDAG::operator< ( const Expr x,
const NodePtr y 
)
inline

Definition at line 193 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::lt, and MathDAG::Expr::newNode().

193  {
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  }
Here is the call graph for this function:

◆ operator<() [2/3]

Expr MathDAG::operator< ( const Expr x,
double  y 
)
inline

Definition at line 200 of file expr.h.

References MathDAG::Expr::cache.

200  {
201  return x < Expr(x.cache, NodePtr(new ConstantDAG(y)));
202  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator<() [3/3]

Expr MathDAG::operator< ( double  x,
const Expr y 
)
inline

Definition at line 203 of file expr.h.

References MathDAG::Expr::cache.

203  {
204  return Expr(y.cache, NodePtr(new ConstantDAG(x))) < y;
205  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator<<() [1/2]

ostream& MathDAG::operator<< ( ostream &  o,
LaTeXManip  m 
)
inline

Definition at line 150 of file equations.h.

References MathDAG::Node::latex(), and MathDAG::LaTeXManip::node.

151  {return m.node.latex(o);}
Here is the call graph for this function:

◆ operator<<() [2/2]

ostream& MathDAG::operator<< ( ostream &  o,
MatlabManip  m 
)
inline

Definition at line 152 of file equations.h.

References MathDAG::Node::matlab(), and MathDAG::MatlabManip::node.

153  {return m.node.matlab(o);}
Here is the call graph for this function:

◆ operator<=() [1/3]

Expr MathDAG::operator<= ( const Expr x,
const NodePtr y 
)
inline

Definition at line 179 of file expr.h.

References MathDAG::Expr::cache, minsky::OperationType::le, and MathDAG::Expr::newNode().

179  {
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  }
Here is the call graph for this function:

◆ operator<=() [2/3]

Expr MathDAG::operator<= ( const Expr x,
double  y 
)
inline

Definition at line 186 of file expr.h.

References MathDAG::Expr::cache.

186  {
187  return x <= Expr(x.cache, NodePtr(new ConstantDAG(y)));
188  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ operator<=() [3/3]

Expr MathDAG::operator<= ( double  x,
const Expr y 
)
inline

Definition at line 189 of file expr.h.

References MathDAG::Expr::cache.

189  {
190  return Expr(y.cache, NodePtr(new ConstantDAG(x))) <= y;
191  }
std::shared_ptr< Node > NodePtr
Definition: equations.h:131

◆ polygamma()

Expr MathDAG::polygamma ( const Expr x,
const Expr y 
)
inline

Definition at line 166 of file expr.h.

References MathDAG::Expr::cache, MathDAG::Expr::newNode(), and minsky::OperationType::polygamma.

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), minsky::EvalOp< minsky::OperationType::constant >::evaluate(), and minsky::OperationBase::unitsBinOpCase().

166  {
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  }
Expr polygamma(const Expr &x, const Expr &y)
Definition: expr.h:166
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sin()

Expr MathDAG::sin ( const Expr x)
inline

Definition at line 131 of file expr.h.

References MathDAG::Expr::cache, MathDAG::Expr::newNode(), and minsky::OperationType::sin.

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), minsky::VariableBase::draw(), minsky::EvalOp< minsky::OperationType::constant >::evaluate(), minsky::RenderVariable::inImage(), minsky::Group::inIORegion(), and minsky::PhillipsDiagram::init().

131  {
132  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sin));
133  r->arguments[0].push_back(x);
134  return Expr(x.cache,r);
135  }
Expr sin(const Expr &x)
Definition: expr.h:131
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sinh()

Expr MathDAG::sinh ( const Expr x)
inline

Definition at line 142 of file expr.h.

References MathDAG::Expr::cache, MathDAG::Expr::newNode(), and minsky::OperationType::sinh.

Referenced by minsky::EvalOp< minsky::OperationType::constant >::d1(), MathDAG::SystemOfEquations::derivative(), and minsky::EvalOp< minsky::OperationType::constant >::evaluate().

142  {
143  shared_ptr<OperationDAGBase> r(x.newNode(OperationType::sinh));
144  r->arguments[0].push_back(x);
145  return Expr(x.cache,r);
146  }
Expr sinh(const Expr &x)
Definition: expr.h:142
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sqrt()