Minsky
MathDAG::OperationDAGBase Struct Referenceabstract

#include <equations.h>

Inheritance diagram for MathDAG::OperationDAGBase:
Inheritance graph
Collaboration diagram for MathDAG::OperationDAGBase:
Collaboration graph

Public Member Functions

 OperationDAGBase (const string &name="")
 
virtual Type type () const =0
 
int order (unsigned maxOrder) const override
 returns evaluation order in sequence of variable defintions More...
 
bool tensorEval (std::set< const Node *> &) const override
 returns true if the evaluation of this involves tensor processing More...
 
VariableValuePtr addEvalOps (EvalOpVector &, const VariableValuePtr &) override
 adds EvalOps to an EvalOpVector representing this node. More...
 
void checkArg (unsigned i, unsigned j) const
 
virtual bool tensorEval (std::set< const Node * > &visited) const=0
 returns true if the evaluation of this involves tensor processing More...
 
bool tensorEval ()
 returns true if the evaluation of this involves tensor processing More...
 
- Public Member Functions inherited from MathDAG::Node
virtual ~Node ()
 
virtual int BODMASlevel () const =0
 algebraic heirarchy level, used for working out whether brackets are necessary. More...
 
virtual ostream & latex (ostream &) const =0
 writes LaTeX representation of this DAG to the stream More...
 
std::string latexStr () const
 
virtual ostream & matlab (ostream &) const =0
 writes a matlab representation of this DAG to the stream More...
 
std::string matlabStr () const
 
virtual void render (ecolab::cairo::Surface &surf) const =0
 renders a visual representation of this node to surf graphic extends right from the current pen position (which needs to be defined), and pen is moved to the right edge of the graphic. The determine bounding box, render into a recording surface, and use width()/height() More...
 
bool tensorEval ()
 returns true if the evaluation of this involves tensor processing More...
 
LaTeXManip latex () const
 used within io streaming More...
 
MatlabManip matlab () const
 
virtual std::shared_ptr< Nodederivative (SystemOfEquations &) const =0
 support for the derivative operator. More...
 

Static Public Member Functions

static OperationDAGBasecreate (Type type, const string &name="")
 factory method More...
 
- Static Public Member Functions inherited from minsky::OperationType
static std::string typeName (int type)
 return the symbolic name of type More...
 
static Group classify (Type t)
 

Public Attributes

vector< vector< WeakNodePtr > > arguments
 
string name
 
string init ="0"
 
ItemPtr state
 
- Public Attributes inherited from MathDAG::Node
int cachedOrder =-1
 
VariableValuePtr result
 reference to where this node's value is stored More...
 

Additional Inherited Members

- Public Types inherited from minsky::OperationType
enum  Type {
  constant, time, integrate, differentiate,
  data, ravel, euler, pi,
  zero, one, inf, percent,
  add, subtract, multiply, divide,
  min, max, and_, or_,
  log, pow, polygamma, lt,
  le, eq, userFunction, copy,
  sqrt, exp, ln, sin,
  cos, tan, asin, acos,
  atan, sinh, cosh, tanh,
  abs, floor, frac, not_,
  Gamma, fact, sum, product,
  infimum, supremum, any, all,
  infIndex, supIndex, runningSum, runningProduct,
  difference, differencePlus, innerProduct, outerProduct,
  index, gather, meld, merge,
  slice, size, shape, mean,
  median, stdDev, moment, histogram,
  covariance, correlation, linearRegression, numOps
}
 
enum  Group {
  general, constop, binop, function,
  reduction, scan, tensor, statistics
}
 

Detailed Description

Definition at line 217 of file equations.h.

Constructor & Destructor Documentation

◆ OperationDAGBase()

MathDAG::OperationDAGBase::OperationDAGBase ( const string &  name = "")
inline

Definition at line 223 of file equations.h.

223  :
224  name(name) {}

Member Function Documentation

◆ addEvalOps()

VariableValuePtr MathDAG::OperationDAGBase::addEvalOps ( EvalOpVector ,
const VariableValuePtr result 
)
overridevirtual

adds EvalOps to an EvalOpVector representing this node.

Returns
a variable where the result is stored. If a flowVariable has been provided in result, that may be used directly, otherwise a copy operation is added to ensure it receives the result.

Implements MathDAG::Node.

Definition at line 317 of file equations.cc.

References MathDAG::anonymous_namespace{equations.cc}::addTensorOp(), MathDAG::anonymous_namespace{equations.cc}::cumulate(), minsky::Minsky::displayErrorItem(), minsky::doOneEvent(), minsky::minsky(), minsky::VariableType::tempFlow, and minsky::VariableType::undefined.

318  {
319  assert(result);
320  if (result->type()==VariableType::undefined)
321  {
322  assert(!dynamic_cast<IntOp*>(state.get()));
323  if (r->type()!=VariableType::undefined && r->isFlowVar())
324  result=r;
325  else
326  {
328  result->allocValue();
329  }
330  if (tensorEval() && addTensorOp(result, *this, ev))
331  return result;
332  assert(result->type()!=VariableType::undefined);
333  assert(result->idx()>=0);
334 
335  // prepare argument expressions
336  vector<vector<VariableValuePtr> > argIdx(arguments.size());
337  for (size_t i=0; type()!=integrate && i<arguments.size(); ++i)
338  for (size_t j=0; j<arguments[i].size(); ++j)
339  if (arguments[i][j])
340  argIdx[i].push_back(arguments[i][j]->addEvalOps(ev));
341  else
342  {
343  argIdx[i].emplace_back(VariableType::tempFlow);
344  argIdx[i].back()->allocValue();
345  }
346 
347  try
348  {
349  // basic arithmetic is handled in a cumulative fashion
350  switch (type())
351  {
352  case add:
353  cumulate(ev, state, result, argIdx, add, add, 0);
354  break;
355  case subtract:
356  cumulate(ev, state, result, argIdx, subtract, add, 0);
357  break;
358  case multiply:
359  cumulate(ev, state, result, argIdx, multiply, multiply, 1);
360  break;
361  case divide:
362  cumulate(ev, state, result, argIdx, divide, multiply, 1);
363  break;
364  case min:
365  cumulate(ev, state, result, argIdx, min, min, numeric_limits<double>::max());
366  break;
367  case max:
368  cumulate(ev, state, result, argIdx, max, max, -numeric_limits<double>::max());
369  break;
370  case and_:
371  cumulate(ev, state, result, argIdx, and_, and_, 1);
372  break;
373  case or_:
374  cumulate(ev, state, result, argIdx, or_, or_, 0);
375  break;
376  case constant:
378  throw error("Constant deprecated");
379  case lt: case le: case eq:
380  for (size_t i=0; i<arguments.size(); ++i)
381  if (arguments[i].empty())
382  {
383  argIdx[i].emplace_back(VariableType::tempFlow);
384  argIdx[i].back()->allocValue();
385  // ensure units are compatible (as we're doing comparisons with zero)
386  if (i>0)
387  argIdx[i][0]->units=argIdx[i-1][0]->units;
388  else if (arguments.size()>1 && !argIdx[1].empty())
389  argIdx[0][0]->units=argIdx[1][0]->units;
390  }
391  ev.push_back(EvalOpPtr(type(), state, result, *argIdx[0][0], *argIdx[1][0]));
392  break;
393  case userFunction:
394  for (size_t i=0; i<arguments.size(); ++i)
395  if (arguments[i].empty())
396  {
397  argIdx[i].emplace_back(VariableType::tempFlow);
398  argIdx[i].back()->allocValue();
399  }
400  ev.push_back(EvalOpPtr(type(), state, result, *argIdx[0][0], *argIdx[1][0]));
401  break;
402  case data:
403  if (!argIdx.empty() && argIdx[0].size()==1)
404  ev.push_back(EvalOpPtr(type(), state, result, *argIdx[0][0]));
405  else
406  throw error("inputs for highlighted operations incorrectly wired");
407  break;
408  default:
409  switch (classify(type()))
410  {
411  case reduction: case scan: case tensor:
412  if (result->idx()==-1) result->allocValue();
413  break; // TODO handle tensor properly later
414  default:
415  {
416  // sanity check that the correct number of arguments is provided
417  bool correctlyWired=true;
418  for (size_t i=0; i<arguments.size(); ++i)
419  correctlyWired &= arguments[i].size()==1;
420  if (!correctlyWired)
421  throw error("inputs for highlighted operations incorrectly wired");
422 
423  switch (arguments.size())
424  {
425  case 0:
426  ev.push_back(EvalOpPtr(type(), state, result));
427  break;
428  case 1:
429  ev.push_back(EvalOpPtr(type(), state, result, *argIdx[0][0]));
430  break;
431  case 2:
432  ev.push_back(EvalOpPtr(type(), state, result, *argIdx[0][0], *argIdx[1][0]));
433  break;
434  default:
435  throw error("Too many arguments");
436  }
437  }
438  }
439  }
440  }
441  catch (const std::exception&)
442  {
444  throw;
445  }
446  }
447  if (type()!=integrate && r->type()!=VariableType::undefined && r->isFlowVar() && result!=r)
448  ev.emplace_back(EvalOpPtr(new TensorEval(r,result)));
449  if (state && state->portsSize()>0)
450  if (auto statePort=state->ports(0).lock())
451  statePort->setVariableValue(result);
452  assert(result->idx()>=0);
453  doOneEvent(true);
454  return result;
455  }
VariableValuePtr addEvalOps(EvalOpVector &, const VariableValuePtr &) override
adds EvalOps to an EvalOpVector representing this node.
Definition: equations.cc:317
bool addTensorOp(const shared_ptr< VariableValue > &result, OperationDAGBase &nodeOp, EvalOpVector &ev)
Definition: equations.cc:88
bool tensorEval()
returns true if the evaluation of this involves tensor processing
Definition: equations.h:116
void doOneEvent(bool idletasksOnly)
checks if any GUI events are waiting, and proces an event if so
Definition: tclmain.cc:161
A helper to evaluate a variable value.
void displayErrorItem(const Item &op) const
indicate operation item has error, if visible, otherwise contining group
Definition: minsky.cc:1230
virtual Type type() const =0
VariableValuePtr result
reference to where this node&#39;s value is stored
Definition: equations.h:128
a shared_ptr that default constructs a default target, and is always valid
void cumulate(EvalOpVector &ev, const ItemPtr &state, VariableValuePtr &r, const vector< vector< VariableValuePtr > > &argIdx, OperationType::Type op, OperationType::Type accum, double groupIdentity)
Definition: equations.cc:236
vector< vector< WeakNodePtr > > arguments
Definition: equations.h:219
Minsky & minsky()
global minsky object
Definition: minskyTCL.cc:51
static Group classify(Type t)
Here is the call graph for this function:

◆ checkArg()

void MathDAG::OperationDAGBase::checkArg ( unsigned  i,
unsigned  j 
) const

Definition at line 227 of file equations.cc.

228  {
229  if (arguments.size()<=i || arguments[i].size()<=j || !arguments[i][j])
230  throw NoArgument(state, i, j);
231  }
vector< vector< WeakNodePtr > > arguments
Definition: equations.h:219

◆ create()

OperationDAGBase * MathDAG::OperationDAGBase::create ( Type  type,
const string &  name = "" 
)
static

factory method

Definition at line 176 of file equations.cc.

References MathDAG::anonymous_namespace{equations.cc}::operationDAGFactory.

Referenced by MathDAG::SystemOfEquations::makeDAG(), and MathDAG::Expr::newNode().

177  {
178  auto r=operationDAGFactory.create(type);
179  r->name=name;
180  return r;
181  }
virtual Type type() const =0
OperationFactory< OperationDAGBase, OperationDAG, OperationType::numOps-1 > operationDAGFactory
Definition: equations.cc:174
Here is the caller graph for this function:

◆ order()

int MathDAG::OperationDAGBase::order ( unsigned  maxOrder) const
overridevirtual

returns evaluation order in sequence of variable defintions

Parameters
maxOrderis used to limit the recursion depth

Implements MathDAG::Node.

Reimplemented in MathDAG::GodleyColumnDAG.

Definition at line 185 of file equations.cc.

186  {
187  if (type()==integrate)
188  return 0; //integrals have already been evaluated
189  if (cachedOrder>=0) return cachedOrder;
190 
191  if (maxOrder==0)
192  throw error("maximum order recursion reached");
193 
194 
195  // constants have order one, as they must be ordered after the
196  // "fake" variables have been initialised
197  int order=type()==constant? 1: 0;
198  for (size_t i=0; i<arguments.size(); ++i)
199  for (size_t j=0; j<arguments[i].size(); ++j)
200  {
201  checkArg(i,j);
202  order=std::max(order, arguments[i][j]->order(maxOrder-1));
203  }
204  return cachedOrder=order;
205  }
void checkArg(unsigned i, unsigned j) const
Definition: equations.cc:227
virtual Type type() const =0
vector< vector< WeakNodePtr > > arguments
Definition: equations.h:219
int order(unsigned maxOrder) const override
returns evaluation order in sequence of variable defintions
Definition: equations.cc:185

◆ tensorEval() [1/3]

bool MathDAG::OperationDAGBase::tensorEval ( std::set< const Node *> &  visited) const
overridevirtual

returns true if the evaluation of this involves tensor processing

Parameters
visitedset to break graph cycles

Implements MathDAG::Node.

Definition at line 207 of file equations.cc.

References minsky::OperationType::classify().

208  {
209  if (!visited.insert(this).second)
210  return false; // cycle detected, break
211  switch (OperationType::classify(type()))
212  {
213  case reduction: case scan: case tensor: case statistics:
214  return true;
215  case general: case binop: case constop: case function:
216  for (auto& i: arguments)
217  for (auto j: i)
218  if (j && j->tensorEval(visited)) return true;
219  return false;
220  default:
221  assert(false);// above cases should exhaust
222  return false;
223  }
224  }
virtual Type type() const =0
vector< vector< WeakNodePtr > > arguments
Definition: equations.h:219
static Group classify(Type t)
Here is the call graph for this function:

◆ tensorEval() [2/3]

bool MathDAG::Node::tensorEval
inline

returns true if the evaluation of this involves tensor processing

Definition at line 116 of file equations.h.

116 {std::set<const Node*> visited; return tensorEval(visited);}
bool tensorEval()
returns true if the evaluation of this involves tensor processing
Definition: equations.h:116

◆ tensorEval() [3/3]

virtual bool MathDAG::Node::tensorEval

returns true if the evaluation of this involves tensor processing

Parameters
visitedset to break graph cycles

◆ type()

virtual Type MathDAG::OperationDAGBase::type ( ) const
pure virtual

Member Data Documentation

◆ arguments

◆ init

string MathDAG::OperationDAGBase::init ="0"

Definition at line 221 of file equations.h.

◆ name

string MathDAG::OperationDAGBase::name

Definition at line 220 of file equations.h.

◆ state

ItemPtr MathDAG::OperationDAGBase::state

The documentation for this struct was generated from the following files: