Minsky
minsky::VariableValues Class Reference

#include <variableValues.h>

Inheritance diagram for minsky::VariableValues:
Inheritance graph
Collaboration diagram for minsky::VariableValues:
Collaboration graph

Public Member Functions

 VariableValues ()
 
void clear ()
 
std::string newName (const std::string &name) const
 generate a new valueId not otherwise in the system More...
 
void reset ()
 reset all variableValues to their initial conditions More...
 
void resetValue (VariableValue &) const
 reset a give variable value to it's initial condition, in this context More...
 
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 More...
 
TensorVal initValue (const VariableValue &v) const
 
std::vector< Summarysummarise () const
 
bool validEntries () const
 checks that all entry names are valid More...
 
void resetUnitsCache ()
 
- Public Member Functions inherited from minsky::ConstMap< std::string, VariableValuePtr >
 ConstMap ()
 
 ConstMap (const T &x)
 
 ConstMap (const T &x, const U &y)
 
VariableValuePtr operator[] (const std::string &k) const
 

Private Member Functions

 CLASSDESC_ACCESS (VariableValues)
 

Static Private Member Functions

static VariableValuePtrzero ()
 
static VariableValuePtrone ()
 

Detailed Description

Definition at line 46 of file variableValues.h.

Constructor & Destructor Documentation

◆ VariableValues()

minsky::VariableValues::VariableValues ( )
inline

Definition at line 52 of file variableValues.h.

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ CLASSDESC_ACCESS()

minsky::VariableValues::CLASSDESC_ACCESS ( VariableValues  )
private

◆ clear()

void minsky::VariableValues::clear ( )
inline

Definition at line 53 of file variableValues.h.

References one(), and zero().

Referenced by VariableValues().

53  {
54  ConstMap<std::string, mapped_type>::clear();
55  // add special values for zero and one, used for the derivative
56  // operator in SystemOfEquations
57  emplace("constant:zero", zero());
58  emplace("constant:one", one());
59  }
static VariableValuePtr & one()
static VariableValuePtr & zero()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initValue() [1/2]

TensorVal minsky::VariableValues::initValue ( const VariableValue v,
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

Definition at line 252 of file variableValue.cc.

References minsky::FlowCoef::coef, minsky::VariableValue::init(), minsky::VariableValueData::m_scope, minsky::VariableValueData::name, minsky::VariableValueData::tensorInit, minsky::trimWS(), and minsky::valueId().

Referenced by minsky::VariableValue::init(), initValue(), MathDAG::latexInit(), and MathDAG::matlabInit().

253  {
254  if (v.tensorInit.rank()>0)
255  return v.tensorInit;
256 
257  const FlowCoef fc(v.init());
258  if (trimWS(fc.name).empty())
259  return fc.coef;
260 
261  // special generator functions handled here
262  auto p=fc.name.find('(');
263  if (p!=string::npos)
264  {
265  // const string fn=fc.name.substr(0,p);
266  // unpack args
267  const char* x=fc.name.c_str()+p+1;
268  char* e;
269  vector<unsigned> dims;
270  for (;;)
271  {
272  auto tmp=strtol(x,&e,10);
273  if (tmp>0 && e>x && *e)
274  {
275  x=e+1;
276  dims.push_back(tmp);
277  }
278  else
279  break;
280  }
281  TensorVal r(dims);
282  r.allocVal();
283 
284  if (fc.name.starts_with("iota"))
285  for (size_t i=0; i<r.size(); ++i)
286  r[i]=i;
287  else if (fc.name.starts_with("one"))
288  for (size_t i=0; i<r.size(); ++i)
289  r[i]=1;
290  else if (fc.name.starts_with("zero") || fc.name.starts_with("eye"))
291  {
292  for (size_t i=0; i<r.size(); ++i)
293  r[i]=0;
294  if (fc.name.starts_with("eye"))
295  {
296  // diagonal elements set to 1
297  // find minimum dimension, and stride of diagonal elements
298  size_t mind=r.size(), stride=1;
299  for (auto i: dims)
300  mind=min(mind, size_t(i));
301  for (size_t i=0; i<dims.size()-1; ++i)
302  stride*=(dims[i]+1);
303  for (size_t i=0; i<mind; ++i)
304  r[stride*i]=1;
305  }
306  }
307  else if (fc.name.starts_with("rand"))
308  {
309  for (size_t i=0; i<r.size(); ++i)
310  r[i]=double(rand())/RAND_MAX; // NOLINT
311  }
312  return r;
313  }
314 
315  // resolve name
316  auto valueId=minsky::valueId(v.m_scope.lock(), fc.name);
317  if (visited.contains(valueId))
318  throw error("circular definition of initial value for %s",
319  fc.name.c_str());
320  const VariableValues::const_iterator vv=find(valueId);
321  if (vv==end())
322  throw error("Unknown variable %s in initialisation of %s",fc.name.c_str(), v.name.c_str());
323 
324  visited.insert(valueId);
325  return fc.coef*initValue(*vv->second, visited);
326  }
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
string valueId(const string &name)
construct a valueId from fully qualified name @ name should not be canonicalised
Definition: valueId.cc:75
std::string trimWS(const std::string &s)
Definition: str.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initValue() [2/2]

TensorVal minsky::VariableValues::initValue ( const VariableValue v) const
inline

Definition at line 71 of file variableValues.h.

References initValue().

71  {
72  std::set<std::string> visited;
73  return initValue(v, visited);
74  }
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
Here is the call graph for this function:

◆ newName()

string minsky::VariableValues::newName ( const std::string &  name) const

generate a new valueId not otherwise in the system

Definition at line 355 of file variableValue.cc.

References minsky::to_string(), and minsky::valueId().

Referenced by minsky::IntOp::description().

356  {
357  int i=1;
358  string trialName;
359  do
360  trialName=utf_to_utf<char>(name)+to_string(i++);
361  while (count(valueId(trialName)));
362  return trialName;
363  }
string valueId(const string &name)
construct a valueId from fully qualified name @ name should not be canonicalised
Definition: valueId.cc:75
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ one()

VariableValuePtr & minsky::VariableValues::one ( )
staticprivate

Definition at line 84 of file variableValue.cc.

Referenced by clear().

84  {
85  static VariableValuePtr s_one(make_shared<SpecialConst>("constant:one","1"));
86  return s_one;
87  }
Here is the caller graph for this function:

◆ reset()

void minsky::VariableValues::reset ( )

reset all variableValues to their initial conditions

Definition at line 365 of file variableValue.cc.

References minsky::VariableValue::allocValue(), minsky::VariableValue::idxInRange(), and minsky::VariableValue::reset_idx().

366  {
367  // reallocate all variables
368  ValueVector::stockVars.clear();
369  ValueVector::flowVars.clear();
370  for (auto& v: *this) {
371  v.second->reset_idx(); // Set idx of all flowvars and stockvars to -1 on reset. For ticket 1049
372  resetValue(v.second->allocValue());
373  assert(v.second->idxInRange());
374  }
375  }
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...
void resetValue(VariableValue &) const
reset a give variable value to it&#39;s initial condition, in this context
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:

◆ resetUnitsCache()

void minsky::VariableValues::resetUnitsCache ( )
inline

Definition at line 85 of file variableValues.h.

Referenced by minsky::VariableBase::units().

85  {
86  for (auto& i: *this)
87  i.second->unitsCached=false;
88  }
Here is the caller graph for this function:

◆ resetValue()

void minsky::VariableValues::resetValue ( VariableValue v) const

reset a give variable value to it's initial condition, in this context

Definition at line 328 of file variableValue.cc.

References minsky::VariableValue::allocValue(), minsky::cminsky(), minsky::Minsky::definingVar(), minsky::Minsky::dimensions, minsky::VariableValue::idx(), minsky::VariableValue::idxInRange(), minsky::VariableValue::isFlowVar(), minsky::VariableValueData::tensorInit, and minsky::VariableValue::valueId().

Referenced by MathDAG::ConstantDAG::addEvalOps(), and minsky::VariableBase::init().

329  {
330  if (v.idx()<0) v.allocValue();
331  // initialise variable only if its variable is not defined or it is a stock
332  if (!v.isFlowVar() || !cminsky().definingVar(v.valueId()))
333  {
334  if (v.tensorInit.rank())
335  {
336  // ensure dimensions are correct
337  auto hc=v.tensorInit.hypercube();
338  for (auto& xv: hc.xvectors)
339  {
340  auto dim=cminsky().dimensions.find(xv.name);
341  if (dim!=cminsky().dimensions.end())
342  xv.dimension=dim->second;
343  }
344  v.tensorInit.hypercube(hc);
345  }
346  if (v.tensorInit.rank())
347  v=v.tensorInit;
348  else
349  v=initValue(v);
350  }
351  assert(v.idxInRange());
352  }
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
VariablePtr definingVar(const std::string &valueId) const
returns reference to variable defining (ie input wired) for valueId
Definition: minsky.cc:205
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
Dimensions dimensions
Definition: minsky.h:201
Here is the call graph for this function:
Here is the caller graph for this function:

◆ summarise()

std::vector<Summary> minsky::VariableValues::summarise ( ) const
inline

Definition at line 76 of file variableValues.h.

76  {
77  std::vector<Summary> summary;
78  for (auto& v: *this)
79  summary.emplace_back(v.second->summary());
80  return summary;
81  }

◆ validEntries()

bool minsky::VariableValues::validEntries ( ) const

checks that all entry names are valid

Definition at line 377 of file variableValue.cc.

References minsky::isValueId().

378  {
379  return all_of(begin(), end(), [](const value_type& v){return isValueId(v.first);});
380  }
bool isValueId(const string &name)
check that name is a valid valueId (useful for assertions)
Definition: valueId.cc:33
Here is the call graph for this function:

◆ zero()

VariableValuePtr & minsky::VariableValues::zero ( )
staticprivate

Definition at line 80 of file variableValue.cc.

Referenced by clear().

80  {
81  static VariableValuePtr s_zero(make_shared<SpecialConst>("constant:zero","0"));
82  return s_zero;
83  }
Here is the caller graph for this function:

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