-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhypercube.cc
More file actions
191 lines (172 loc) · 5.41 KB
/
hypercube.cc
File metadata and controls
191 lines (172 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
@copyright Steve Keen 2019
@author Russell Standish
This file is part of Minsky.
Minsky is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Minsky is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hypercube.h"
#include <set>
#ifdef CLASSDESC
#include "dimension.jcd"
#include "xvector.jcd"
#include "hypercube.jcd"
#include <classdesc_epilogue.h>
#endif
using namespace std;
namespace civita
{
std::vector<unsigned> Hypercube::dims() const
{
std::vector<unsigned> d;
for (auto& i: xvectors) d.push_back(i.size());
return d;
}
std::vector<string> Hypercube::dimLabels() const
{
std::vector<std::string> l;
for (auto& i: xvectors) l.push_back(static_cast<std::string>(i.name));
return l;
}
std::vector<unsigned> const& Hypercube::dims(const std::vector<unsigned>& d) {
xvectors.clear();
for (size_t i=0; i<d.size(); ++i)
{
xvectors.emplace_back(std::to_string(i));
xvectors.back().dimension.type=Dimension::value;
for (size_t j=0; j<d[i]; ++j)
xvectors.back().emplace_back(double(j));
}
return d;
}
size_t Hypercube::numElements() const
{
size_t s=1;
for (auto& i: xvectors)
s*=i.size();
return s;
}
double Hypercube::logNumElements() const
{
double r=0;
for (auto& i: xvectors)
r+=log(i.size());
return r;
}
bool Hypercube::dimsAreDistinct() const
{
set<string> names;
for (auto& xv: xvectors)
if (!names.insert(xv.name).second)
return false;
return true;
}
/// split lineal index into components along each dimension
vector<size_t> Hypercube::splitIndex(size_t i) const
{
std::vector<size_t> splitIndex;
splitIndex.reserve(xvectors.size());
for (auto& xv: xvectors)
{
auto res=div(ssize_t(i),ssize_t(xv.size()));
splitIndex.push_back(res.rem);
i=res.quot;
}
return splitIndex;
}
template
size_t Hypercube::linealIndex<vector<size_t>>(const vector<size_t>& splitIndex) const;
void unionHypercube(Hypercube& result, const Hypercube& x, bool intersection)
{
if (x.logNumElements()==1)
{
result.xvectors.clear(); // intersection empty anyway
return;
}
map<string, set<any, AnyLess>> indexedData;
vector<XVector> extraDims;
for (auto& xvector: result.xvectors)
{
auto& xvectorData=indexedData[xvector.name];
for (auto& i: xvector)
xvectorData.insert(i);
}
for (auto& xvector: x.xvectors)
{
auto xvectorData=indexedData.find(xvector.name);
if (xvectorData==indexedData.end())
extraDims.emplace_back(xvector);
else
{
if (xvector.dimension.type==Dimension::string)
{
// compute the intersection of the two x-vectors.
set<any, AnyLess> xvectorAsSet(xvector.begin(), xvector.end());
for (auto i=xvectorData->second.begin(); i!=xvectorData->second.end(); )
if (xvectorAsSet.count(*i))
++i;
else
i=xvectorData->second.erase(i);
}
else if (intersection)
{
if (xvectorData->second.empty())
{
result.xvectors.clear(); // intersection empty anyway
return;
}
// trim to intersection of the two
// TODO use structured binding when we go C++17.
auto xRange=std::minmax_element(xvector.begin(),xvector.end());
auto minX=*xRange.first, maxX=*xRange.second;
auto rRange=std::minmax_element(xvectorData->second.begin(),xvectorData->second.end());
if (minX<*rRange.first) minX=*rRange.first;
if (*rRange.second<maxX) maxX=*rRange.second;
for (auto i=xvectorData->second.begin(); i!=xvectorData->second.end(); )
{
auto j=i++;
if (*j<minX || maxX<*j)
xvectorData->second.erase(j);
}
for (auto& i: xvector)
if (diff(minX,i)<=0 && diff(i,maxX)<=0)
xvectorData->second.insert(i);
}
else
xvectorData->second.insert(xvector.begin(), xvector.end());
}
}
for (auto& xvector: result.xvectors)
{
auto& xvectorData=indexedData[xvector.name];
xvector.clear();
xvector.insert(xvector.end(), xvectorData.begin(), xvectorData.end());
}
for (auto& i: extraDims)
result.xvectors.push_back(i);
}
string Hypercube::json() const
{
#ifdef CLASSDESC
return classdesc::json(*this);
#else
return "";
#endif
}
Hypercube Hypercube::fromJson(const std::string& s)
{
Hypercube hc;
#ifdef CLASSDESC
classdesc::json(hc,s);
#endif
return hc;
}
}