Minsky
userFunction.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2020
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef USERFUNCTION_H
21 #define USERFUNCTION_H
22 #include "operation.h"
23 #include "unitsExpressionWalker.h"
24 #include "callableFunction.h"
25 namespace minsky
26 {
27  class UserFunction: public ItemT<UserFunction, Operation<OperationType::userFunction>>, public NamedOp, public CallableFunction
28  {
29  struct Impl;
30  std::shared_ptr<Impl> impl;
31  void updateBB() override {bb.update(*this);}
33  public:
34  static int nextId;
35  std::vector<std::string> argNames;
36  std::vector<double> argVals;
37  std::string expression;
38  UserFunction(): UserFunction("uf"+std::to_string(nextId++)+"(x,y)") {}
39  UserFunction(const std::string& name, const std::string& expression="");
40  std::vector<std::string> symbolNames() const;
41  void compile();
42  double evaluate(double x, double y);
43 
45  double operator()(const std::vector<double>& p) override;
46 
47  Units units(bool check=false) const override;
48  void displayTooltip(cairo_t* cr, const std::string& tt) const override
49  {Item::displayTooltip(cr,tt.empty()? expression: tt+" "+expression);}
50 
52  std::string description(const std::string&) override;
54  std::string name() const override;
55 
56  // required by the compiler
58  {return (t==OperationType::userFunction)? new UserFunction: nullptr;}
59 
60  void draw(cairo_t* cairo) const override {drawUserFunction(cairo);}
61 
62  };
63 
64  // static UnitExpressionWalker that is initialised to the time unit
65  extern UnitsExpressionWalker timeUnit;
66 
67 }
68 #include "userFunction.cd"
69 #include "userFunction.xcd"
70 #endif
std::vector< double > argVals
Definition: userFunction.h:36
virtual void displayTooltip(cairo_t *, const std::string &) const
display tooltip text, eg on mouseover
Definition: item.cc:398
void displayTooltip(cairo_t *cr, const std::string &tt) const override
display tooltip text, eg on mouseover
Definition: userFunction.h:48
std::vector< std::string > argNames
Definition: userFunction.h:35
virtual float x() const
Definition: item.cc:107
double operator()(const std::vector< double > &p) override
evaluate function on arbitrary number of arguments (exprtk support)
virtual float y() const
Definition: item.cc:114
void drawUserFunction(cairo_t *cairo) const
Definition: operation.cc:143
STL namespace.
static UserFunction * create(OperationType::Type t)
Definition: userFunction.h:57
void draw(cairo_t *cairo) const override
draw this item into a cairo context
Definition: userFunction.h:60
Units units(bool check=false) const override
compute the dimensional units
base class for operations that have names
Definition: operation.h:101
BoundingBox bb
canvas bounding box.
Definition: item.h:195
CLASSDESC_ACCESS(UserFunction)
void update(const Item &x)
Definition: item.cc:46
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
void updateBB() override
Definition: userFunction.h:31
std::vector< std::string > symbolNames() const
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
virtual std::string description() const
name of the associated data operation
Definition: operation.cc:573
double evaluate(double x, double y)
std::shared_ptr< Impl > impl
Definition: userFunction.h:29
std::string name() const override
function name, shorn of argument decorators
std::string expression
Definition: userFunction.h:37
UnitsExpressionWalker timeUnit
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33