Commit f340b484 authored by Martin Beseda's avatar Martin Beseda

FIX: Fixed generating of isotropic data

parent 5907699f
......@@ -33,13 +33,24 @@ DataSet::DataSet(std::vector<std::pair<std::vector<double>, std::vector<double>>
DataSet::DataSet(double lower_bound, double upper_bound, unsigned int size, double output) {
std::vector<std::pair<std::vector<double>, std::vector<double>>> new_data_vec;
this->data = new_data_vec;
this->n_elements = size;
this->n_elements = 0;
this->input_dim = 1;
this->output_dim = 1;
this->add_isotropic_data(lower_bound, upper_bound, size, output);
}
DataSet::DataSet(std::vector<double> bounds, unsigned int no_elems_in_one_dim, std::vector<double> (*output_func)(std::vector<double>), unsigned int output_dim) {
std::vector<std::pair<std::vector<double>, std::vector<double>>> new_data_vec;
this->data = new_data_vec;
this->input_dim = bounds.size()/2;
this->output_dim = output_dim;
this->n_elements = 0;
this->add_isotropic_data(bounds, no_elems_in_one_dim, output_func);
}
void DataSet::add_data_pair(std::vector<double> inputs, std::vector<double> outputs) {
if(inputs.size() != this->input_dim) {
throw InvalidDimension("Bad input dimension.");
......@@ -66,6 +77,33 @@ void DataSet::add_isotropic_data(double lower_bound, double upper_bound, unsigne
inp = {frac*i};
this->data.emplace_back(std::make_pair(inp, out));
}
this->n_elements += size;
}
void DataSet::add_isotropic_data(std::vector<double> bounds, unsigned int no_elems_in_one_dim, std::vector<double> (*output_func)(std::vector<double>)) {
// TODO add check of dataset dimensions
std::vector<std::vector<double>> grid;
std::vector<double> tmp;
double frac;
for(unsigned int i = 0; i < bounds.size(); i += 2) {
frac = (bounds[i] + bounds[i+1]) / (no_elems_in_one_dim - 1);
tmp.clear();
for(double j = bounds[i]; j <= bounds[i+1]; j += frac) {
tmp.emplace_back(j);
}
grid.emplace_back(tmp);
}
grid = this->cartesian_product(&grid);
for(const auto& vec : grid) {
this->n_elements++;
this->data.emplace_back(std::make_pair(vec, output_func(vec)));
}
}
std::vector<std::pair<std::vector<double>, std::vector<double>>>* DataSet::get_data() {
......@@ -110,4 +148,34 @@ void DataSet::store_text(std::string file_path) {
boost::archive::text_oarchive oa(ofs);
oa << *this;
ofs.close();
}
template <class T>
std::vector<std::vector<T>> DataSet::cartesian_product(const std::vector<std::vector<T>>* v) {
std::vector<std::vector<double>> v_combined_old, v_combined, v_tmp;
std::vector<double> tmp;
for(const auto& e : v->at(0)) {
tmp = {e};
v_combined.emplace_back(tmp);
}
for(unsigned int i = 1; i < v->size(); i++) { // Iterate through remaining vectors of 'v'
v_combined_old = v_combined;
v_combined.clear();
for(const auto& e : v->at(i)) {
for(const auto& vec : v_combined_old) {
tmp = vec;
tmp.emplace_back(e);
/* Add only unique elements */
if(std::find(v_combined.begin(), v_combined.end(), tmp) == v_combined.end()) {
v_combined.emplace_back(tmp);
}
}
}
}
return v_combined;
}
\ No newline at end of file
......@@ -12,6 +12,7 @@
#include <boost/range/size_type.hpp>
#include <exception>
#include <string>
#include <functional>
/**
* Class representing an error caused by an incorrect
......@@ -61,6 +62,9 @@ private:
*/
std::vector<std::pair<std::vector<double>, std::vector<double>>> data;
template <class T>
std::vector<std::vector<T>> cartesian_product(const std::vector<std::vector<T>>* v);
protected:
/**
* Serialization function
......@@ -165,6 +169,8 @@ public:
*/
DataSet(double lower_bound, double upper_bound, unsigned int size, double output);
DataSet(std::vector<double> bounds, unsigned int no_elems_in_one_dim, std::vector<double> (*output_func)(std::vector<double>), unsigned int output_dim);
/**
* Getter for number of elements
* @return Number of elements in the data set
......@@ -212,6 +218,20 @@ public:
*/
void add_isotropic_data(double lower_bound, double upper_bound, unsigned int size, double output);
/**
* Adds a new data with input values equidistantly positioned
* over the certain interval and the output value
* being constant
*
* Input can have arbitrary many dimensions,
* output can be an arbitrary function
*
* @param bounds Odd values are lower bounds and even values are corresponding upper bounds
* @param size Number of input-output pairs generated
* @param output_func Function determining output value
*/
void add_isotropic_data(std::vector<double> bounds, unsigned int no_elems_in_one_dim, std::vector<double> (*output_func)(std::vector<double>));
//TODO Chebyshev - ch. interpolation points, i-th point = cos(i*alpha) from 0 to pi
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment