Minsky
dataOp.cc
Go to the documentation of this file.
1 
2 /*
3  @copyright Steve Keen 2021
4  @author Russell Standish
5  This file is part of Minsky.
6 
7  Minsky is free software: you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Minsky is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "cairoItems.h"
21 #include "pango.h"
22 #include "dataOp.h"
23 #include "selection.h"
24 #include "lasso.h"
25 #include "dataOp.rcd"
26 #include "itemT.rcd"
27 #include "minsky_epilogue.h"
28 
29 using namespace std;
30 namespace minsky
31 {
32  void DataOp::readData(const string& fileName)
33  {
34  ifstream f(fileName.c_str());
35  data.clear();
36  // for now, we just read pairs of numbers, separated by
37  // whitespace. Later, we need to add in the smarts to handle a
38  // variety of CSV formats
39  double x, y;
40  while (f>>x>>y)
41  data[x]=y; // TODO: throw if more than one equal value of x provided?
42 
43  // trim any leading directory
44  const size_t p=fileName.rfind('/');
45  // '/' is guaranteed not to be in fileName, so we can use that as
46  // a delimiter
47  description("\\verb/"+
48  ((p!=string::npos)? fileName.substr(p+1): fileName) + "/");
49  }
50 
51  void DataOp::initRandom(double xmin, double xmax, unsigned numSamples)
52  {
53  data.clear();
54  const double dx=(xmax-xmin)/numSamples;
55  for (double x=xmin; x<xmax; x+=dx)
56  data[x]=double(rand())/RAND_MAX; //NOLINT
57  }
58 
59  double DataOp::interpolate(double x) const
60  {
61  // not terribly sensible, but need to return something
62  if (data.empty()) return 0;
63 
64  const map<double, double>::const_iterator v=data.lower_bound(x);
65  if (v==data.end())
66  return data.rbegin()->second;
67  if (v==data.begin())
68  return v->second;
69  if (v->first > x)
70  {
71  map<double, double>::const_iterator v0=v;
72  --v0;
73  return (x-v0->first)*(v->second-v0->second)/
74  (v->first-v0->first)+v0->second;
75  }
76  assert(v->first==x);
77  return v->second;
78  }
79 
80  double DataOp::deriv(double x) const
81  {
82  const map<double, double>::const_iterator v=data.lower_bound(x);
83  if (v==data.end() || v==data.begin())
84  return 0;
85  map<double, double>::const_iterator v1=v, v2=v;
86  --v1;
87  if (v->first==x)
88  {
89  ++v2;
90  if (v2==data.end()) v2=v;
91  return (v2->second-v1->second)/(v2->first-v1->first);
92  }
93  return (v->second-v1->second)/(v->first-v1->first);
94  }
95 
96  void DataOp::pack(classdesc::pack_t& x, const string& d) const
97  {::pack(x,d,*this);}
98 
99  void DataOp::unpack(classdesc::unpack_t& x, const string& d)
100  {::unpack(x,d,*this);}
101 
102 }
103 
function f
Definition: canvas.m:1
void pack(classdesc::pack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
void unpack(classdesc::pack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
initRandom
Definition: wiring.tcl:1370
STL namespace.
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::DataOp)
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
legacy data importer object
Definition: dataOp.h:28