Minsky
minsky::Units Struct Reference

represents the units (in sense of dimensional analysis) of a variable. More...

#include <units.h>

Inheritance diagram for minsky::Units:
Inheritance graph
Collaboration diagram for minsky::Units:
Collaboration graph

Public Member Functions

 Units ()
 
 Units (const std::string &)
 
std::string str () const
 
std::string latexStr () const
 insert braces around exponents for LaTeX processing More...
 
void normalise ()
 

Detailed Description

represents the units (in sense of dimensional analysis) of a variable.

first argument is the base unit (eg metre, second), and the second is it's power (eg {{"m",1},{"s",-1}} => m/s)

Definition at line 34 of file units.h.

Constructor & Destructor Documentation

◆ Units() [1/2]

minsky::Units::Units ( )
inline

Definition at line 36 of file units.h.

36 {}

◆ Units() [2/2]

minsky::Units::Units ( const std::string &  x)

Definition at line 56 of file variableType.cc.

57  {
58  if (x.empty()) return;
59  // split into numerator and denominator components if / found
60  auto divPos=x.find('/');
61  vector<string> components;
62  components.push_back(x.substr(0,divPos));
63  if (divPos!=string::npos)
64  components.push_back(x.substr(divPos+1));
65 
66  if (components.back().find('/')!=string::npos)
67  throw runtime_error("too many division signs: "+x);
68 
69  int coef=1;
70  for (auto& i: components)
71  {
72  if (i.empty())
73  throw runtime_error("empty numerator or denominator: "+x);
74  const char* b=i.c_str();
75  for (const char*j=b;;)
76  {
77  if (*j=='^'||*j=='\0'||isspace(*j))
78  {
79  const string name(b,j); // stash end of unit name
80  if (name.empty())
81  throw runtime_error("empty unit name: "+x);
82  while (isspace(*j)) ++j;
83  if (*j=='^')
84  {
85  auto k=j+1;
86  const int v=strtol(k,const_cast<char**>(&j),10);
87  if (j==k)
88  throw runtime_error("invalid exponent: "+x);
89  if (name!="1")
90  (*this)[name]+=coef*v;
91  }
92  else if (name!="1")
93  (*this)[name]+=coef;
94  if ((*this)[name]==0)
95  erase(name);
96  while (isspace(*j)) ++j;
97  b=j; // update to next unit name
98  if (*j=='\0') break; //loop exits here
99  }
100  else
101  ++j;
102  }
103  if (b==i.c_str()) // we haven't found any exponents
104  emplace(i,coef);
105  coef*=-1;
106  }
107  }

Member Function Documentation

◆ latexStr()

string minsky::Units::latexStr ( ) const

insert braces around exponents for LaTeX processing

Definition at line 40 of file variableType.cc.

References minsky::to_string().

41  {
42  string s;
43  for (auto& i: *this)
44  if (!i.first.empty() && i.second!=0)
45  {
46  s+=i.first;
47  if (i.second!=1)
48  s+="^{"+to_string(i.second)+"}";
49  else
50  s+=" ";
51  }
52  return s;
53  }
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33
Here is the call graph for this function:

◆ normalise()

void minsky::Units::normalise ( )
inline

Definition at line 42 of file units.h.

Referenced by minsky::UnitsExpressionWalker::operator*=(), minsky::UnitsExpressionWalker::operator/=(), minsky::Derivative::units(), and minsky::OperationBase::unitsBinOpCase().

42  {
43  for (auto i=begin(); i!=end(); ) {
44  auto j=i; ++i;
45  if (j->second==0 || j->first.empty()) erase(j);
46  }
47  }
Here is the caller graph for this function:

◆ str()

string minsky::Units::str ( ) const

Definition at line 33 of file variableType.cc.

Referenced by minsky::GodleyIcon::stockVarUnits(), minsky::SwitchIcon::units(), minsky::VariableBase::units(), and minsky::VariableBase::unitsStr().

34  {
35  ostringstream s;
36  s<<*this;
37  return s.str();
38  }
Here is the caller graph for this function:

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