Minsky
minsky::ScalarEvalOp Struct Referenceabstract

Legacy EvalOp base interface. More...

#include <evalOp.h>

Inheritance diagram for minsky::ScalarEvalOp:
Inheritance graph
Collaboration diagram for minsky::ScalarEvalOp:
Collaboration graph

Public Member Functions

virtual int numArgs () const =0
 
void deriv (double df[], std::size_t n, const double ds[], const double sv[], const double fv[]) override
 
void eval (double fv[], std::size_t, const double sv[]) override
 evaluate expression on sv and current value of fv, storing result in output variable (of fv) More...
 
virtual double evaluate (double in1=0, double in2=0) const =0
 evaluate expression on given arguments, returning result More...
 
virtual double d1 (double x1=0, double x2=0) const =0
 
virtual double d2 (double x1=0, double x2=0) const =0
 
- Public Member Functions inherited from minsky::EvalOpBase
void throw_error (const std::string &msg) const
 
virtual ~EvalOpBase ()
 
virtual void setTensorParams (const VariableValue &, const OperationBase &)
 set additional tensor operation related parameters More...
 

Static Public Member Functions

static ScalarEvalOpcreate (Type op, const ItemPtr &state)
 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

std::shared_ptr< VariableValueresult
 lifetime management of the resultant variableValue number of arguments to this operation More...
 
- Public Attributes inherited from minsky::EvalOpBase
int out =-1
 indexes into the flow/stock variables vector More...
 
std::vector< unsigned > in1
 
std::vector< std::vector< Support > > in2
 
bool flow1 =true
 indicate whether in1/in2 are flow variables (out is always a flow variable) More...
 
bool flow2 =true
 
bool xflow =true
 
std::shared_ptr< OperationBasestate
 state data (for those ops that need it) More...
 

Additional Inherited Members

- Public Types inherited from minsky::EvalOpBase
typedef OperationType::Type Type
 
- 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
}
 
- Static Public Attributes inherited from minsky::EvalOpBase
static double t
 
static std::string timeUnit
 

Detailed Description

Legacy EvalOp base interface.

Definition at line 111 of file evalOp.h.

Member Function Documentation

◆ create()

ScalarEvalOp * minsky::ScalarEvalOp::create ( Type  op,
const ItemPtr state 
)
static

factory method

Definition at line 617 of file evalOp.cc.

References minsky::anonymous_namespace{evalOp.cc}::evalOpFactory, f, and minsky::op.

618  {
619  switch (classify(op))
620  {
621  case general:
622  case binop:
623  case constop:
624  case function:
625  switch (op)
626  {
627  case constant:
628  return new ConstantEvalOp;
629  case numOps:
630  return nullptr;
631  case userFunction:
632  if (auto f=dynamic_cast<UserFunction*>(state.get()))
633  f->compile();
634  [[fallthrough]];
635  default:
636  auto r=evalOpFactory.create(op);
637  r->state=dynamic_pointer_cast<OperationBase>(state);
638  return r;
639  }
640  case reduction:
641  case scan:
642  case tensor:
643  case statistics:
644  return nullptr; //TODO should we be here?
645  }
646  assert(false); //shouldn't be here
647  return nullptr;
648  }
function f
Definition: canvas.m:1
std::shared_ptr< OperationBase > state
state data (for those ops that need it)
Definition: evalOp.h:77
OperationFactory< ScalarEvalOp, EvalOp, OperationType::sum-1 > evalOpFactory
Definition: evalOp.cc:615
static Group classify(Type t)

◆ d1()

virtual double minsky::ScalarEvalOp::d1 ( double  x1 = 0,
double  x2 = 0 
) const
pure virtual

derivatives with respect to 1st and second argument

◆ d2()

virtual double minsky::ScalarEvalOp::d2 ( double  x1 = 0,
double  x2 = 0 
) const
pure virtual

derivatives with respect to 1st and second argument

◆ deriv()

void minsky::ScalarEvalOp::deriv ( double  df[],
std::size_t  n,
const double  ds[],
const double  sv[],
const double  fv[] 
)
overridevirtual

total derivate with respect to a variable, which is a function of the stock variables.

Parameters
sv- stock variables
fv- flow variables (function of stock variables, computed by eval)
ds- derivative of stock variables
df- derivative of flow variables (updated by this function)
n- size of df array

To compute the partial derivatives with respect to stock variable i, seed ds with 1 in the ith position, 0 every else, and initialise df to zero.

Implements minsky::EvalOpBase.

Definition at line 93 of file evalOp.cc.

References minsky::anonymous_namespace{wire.cc}::d2(), and minsky::anonymous_namespace{userFunction.cc}::isfinite().

95  {
96  assert(out>=0 && size_t(out)<n);
97  switch (numArgs())
98  {
99  case 0:
100  df[out]=0;
101  return;
102  case 1:
103  {
104  assert((flow1 && in1[0]<ValueVector::flowVars.size()) ||
105  (!flow1 && in1[0]<ValueVector::stockVars.size()));
106  const double x1=flow1? fv[in1[0]]: sv[in1[0]];
107  const double dx1=flow1? df[in1[0]]: ds[in1[0]];
108  df[out] = dx1!=0? dx1 * d1(x1,0): 0;
109  break;
110  }
111  case 2:
112  {
113  assert((flow1 && in1[0]<ValueVector::flowVars.size()) ||
114  (!flow1 && in1[0]<ValueVector::stockVars.size()));
115  assert((flow2 && in2[0][0].idx<ValueVector::flowVars.size()) ||
116  (!flow2 && in2[0][0].idx<ValueVector::stockVars.size()));
117  const double x1=flow1? fv[in1[0]]: sv[in1[0]];
118  const double x2=flow2? fv[in2[0][0].idx]: sv[in2[0][0].idx];
119  const double dx1=flow1? df[in1[0]]: ds[in1[0]];
120  const double dx2=flow2? df[in2[0][0].idx]: ds[in2[0][0].idx];
121  df[out] = (dx1!=0? dx1 * d1(x1,x2): 0) +
122  (dx2!=0? dx2 * d2(x1,x2): 0);
123  break;
124  }
125  }
126  if (!std::isfinite(df[out]))
127  throw error("Invalid operation detected on a %s operation",
128  OperationBase::typeName(type()).c_str());
129  }
std::vector< std::vector< Support > > in2
Definition: evalOp.h:72
bool flow1
indicate whether in1/in2 are flow variables (out is always a flow variable)
Definition: evalOp.h:74
static std::vector< double, CIVITA_ALLOCATOR< double > > stockVars
vector of variables that are integrated via Runge-Kutta. These variables label the columns of the God...
int out
indexes into the flow/stock variables vector
Definition: evalOp.h:57
virtual int numArgs() const =0
virtual double d1(double x1=0, double x2=0) const =0
static std::string typeName(int type)
return the symbolic name of type
std::vector< unsigned > in1
Definition: evalOp.h:64
virtual double d2(double x1=0, double x2=0) const =0
static std::vector< double, CIVITA_ALLOCATOR< double > > flowVars
variables defined as a simple function of the stock variables, also known as lhs variables. These variables appear in the body of the Godley table
Here is the call graph for this function:

◆ eval()

void minsky::ScalarEvalOp::eval ( double  fv[],
std::size_t  n,
const double  sv[] 
)
overridevirtual

evaluate expression on sv and current value of fv, storing result in output variable (of fv)

Parameters
n- size of fv array

Implements minsky::EvalOpBase.

Definition at line 40 of file evalOp.cc.

41  {
42  assert(out>=0);
43  switch (numArgs())
44  {
45  case 0:
46  assert(size_t(out)<n);
47  fv[out]=evaluate(0,0);
48  break;
49  case 1:
50  assert(out+in1.size()<=n);
51  for (unsigned i=0; i<in1.size(); ++i)
52  fv[out+i]=evaluate(flow1? fv[in1[i]]: sv[in1[i]], 0);
53  break;
54  case 2:
55  assert(out+in1.size()<=n);
56  for (unsigned i=0; i<in1.size(); ++i)
57  {
58  double x2=0;
59  const double* v=flow2? fv: sv;
60  for (auto& j: in2[i])
61  x2+=j.weight*v[j.idx];
62  fv[out+i]=evaluate(flow1? fv[in1[i]]: sv[in1[i]], x2);
63  }
64  break;
65  }
66 
67  // This check may well be obsolete. ecolab::Plot will now elide
68  // non-finite data, and it appears that the RK solver can
69  // integrate through infinities. Leaving the code here for now,
70  // just in case we decide to enable it via a user preference.
71 // // check for NaNs only on scalars. For tensors, NaNs just means
72 // // element not present
73 // if (in1.size()==1)
74 // for (unsigned i=0; i<in1.size(); ++i)
75 // if (!std::isfinite(fv[out+i]))
76 // {
77 // if (state)
78 // cminsky().displayErrorItem(*state);
79 // string msg="Invalid: ";
80 // if (auto uf=dynamic_cast<UserFunction*>(state.get()))
81 // msg+=uf->description()+"(";
82 // else
83 // msg+=OperationBase::typeName(type())+"(";
84 // if (numArgs()>0)
85 // msg+=std::to_string(flow1? fv[in1[i]]: sv[in1[i]]);
86 // if (numArgs()>1)
87 // msg+=","+std::to_string(flow2? fv[in2[i][0].idx]: sv[in2[i][0].idx]);
88 // msg+=")";
89 // throw runtime_error(msg.c_str());
90 // }
91  };
std::vector< std::vector< Support > > in2
Definition: evalOp.h:72
bool flow1
indicate whether in1/in2 are flow variables (out is always a flow variable)
Definition: evalOp.h:74
virtual double evaluate(double in1=0, double in2=0) const =0
evaluate expression on given arguments, returning result
int out
indexes into the flow/stock variables vector
Definition: evalOp.h:57
virtual int numArgs() const =0
std::vector< unsigned > in1
Definition: evalOp.h:64

◆ evaluate()

virtual double minsky::ScalarEvalOp::evaluate ( double  in1 = 0,
double  in2 = 0 
) const
pure virtual

evaluate expression on given arguments, returning result

◆ numArgs()

virtual int minsky::ScalarEvalOp::numArgs ( ) const
pure virtual

Member Data Documentation

◆ result

std::shared_ptr<VariableValue> minsky::ScalarEvalOp::result

lifetime management of the resultant variableValue number of arguments to this operation

Definition at line 113 of file evalOp.h.


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