16#include <serializer.h>
17#include <initializer_list>
23 : DataTable(false, false)
27DataTable::DataTable(
bool allowDuplicates)
28 : DataTable(allowDuplicates, false)
32DataTable::DataTable(
bool allowDuplicates,
bool allowIncompleteGrid)
33 : allowDuplicates(allowDuplicates),
34 allowIncompleteGrid(allowIncompleteGrid),
40DataTable::DataTable(
const char* fileName)
41 : DataTable(std::string(fileName))
45DataTable::DataTable(
const std::string& fileName)
50void DataTable::addSample(
double x,
double y)
52 addSample(DataPoint(x, y));
55void DataTable::addSample(std::vector<double> x,
double y)
57 addSample(DataPoint(x, y));
60void DataTable::addSample(DenseVector x,
double y)
62 addSample(DataPoint(x, y));
65void DataTable::addSample(
const DataPoint& sample)
67 if (getNumSamples() == 0)
69 numVariables = sample.getDimX();
73 if (sample.getDimX() != numVariables)
75 throw Exception(
"Datatable::addSample: Dimension of new sample is inconsistent with previous samples!");
79 if (samples.count(sample) > 0)
84 std::cout <<
"Discarding duplicate sample because allowDuplicates is false!" <<
86 std::cout <<
"Initialise with DataTable(true) to set it to true." << std::endl;
94 samples.insert(sample);
95 recordGridPoint(sample);
98void DataTable::addSample(std::initializer_list<DataPoint> samples)
100 for (
auto& sample : samples)
106void DataTable::recordGridPoint(
const DataPoint& sample)
108 for (
unsigned int i = 0;
i < getNumVariables();
i++)
110 grid.at(
i).insert(sample.getX().at(
i));
114unsigned int DataTable::getNumSamplesRequired()
const
116 unsigned long samplesRequired = 1;
119 for (
auto& variable : grid)
121 samplesRequired *= (
unsigned long) variable.size();
125 return (
i > 0 ? samplesRequired : (unsigned long) 0);
128bool DataTable::isGridComplete()
const
130 return samples.size() > 0
131 && samples.size() - numDuplicates == getNumSamplesRequired();
134void DataTable::initDataStructures()
136 for (
unsigned int i = 0;
i < getNumVariables();
i++)
138 grid.push_back(std::set<double>());
142void DataTable::gridCompleteGuard()
const
144 if (!(isGridComplete() || allowIncompleteGrid))
146 throw Exception(
"DataTable::gridCompleteGuard: The grid is not complete yet!");
150void DataTable::save(
const std::string& fileName)
const
154 s.saveToFile(fileName);
157void DataTable::load(
const std::string& fileName)
159 Serializer s(fileName);
160 s.deserialize(*
this);
166std::multiset<DataPoint>::const_iterator DataTable::cbegin()
const
168 return samples.cbegin();
171std::multiset<DataPoint>::const_iterator DataTable::cend()
const
173 return samples.cend();
180std::vector< std::vector<double>> DataTable::getTableX()
const
184 std::vector<std::vector<double>> table;
186 for (
unsigned int i = 0;
i < numVariables;
i++)
188 std::vector<double> xi(getNumSamples(), 0.0);
195 for (
auto& sample : samples)
197 std::vector<double> x = sample.getX();
199 for (
unsigned int j = 0; j < numVariables; j++)
201 table.at(j).at(
i) = x.at(j);
211std::vector<double> DataTable::getVectorY()
const
213 std::vector<double> y;
215 for (std::multiset<DataPoint>::const_iterator it = cbegin(); it != cend(); ++it)
217 y.push_back(it->getY());
223DataTable
operator+(
const DataTable& lhs,
const DataTable& rhs)
225 if (lhs.getNumVariables() != rhs.getNumVariables())
227 throw Exception(
"operator+(DataTable, DataTable): trying to add two DataTable's of different dimensions!");
232 for (
auto it = lhs.cbegin(); it != lhs.cend(); it++)
234 result.addSample(*it);
237 for (
auto it = rhs.cbegin(); it != rhs.cend(); it++)
239 result.addSample(*it);
245DataTable
operator-(
const DataTable& lhs,
const DataTable& rhs)
247 if (lhs.getNumVariables() != rhs.getNumVariables())
249 throw Exception(
"operator-(DataTable, DataTable): trying to subtract two DataTable's of different dimensions!");
253 auto rhsSamples = rhs.getSamples();
256 for (
auto it = lhs.cbegin(); it != lhs.cend(); it++)
258 if (rhsSamples.count(*it) == 0)
260 result.addSample(*it);
DataTable operator-(const DataTable &lhs, const DataTable &rhs)
DataTable operator+(const DataTable &lhs, const DataTable &rhs)
Eigen::Matrix< T, -1, dim > load(Eigen::Matrix< T, -1, dim > &mat, const std::string fname, std::string order="rowMajor")