77 lines
2.1 KiB
C++
Executable File
77 lines
2.1 KiB
C++
Executable File
#include "functions/functions.h"
|
|
|
|
#include <map>
|
|
#include "simulation/errors.h"
|
|
|
|
namespace D1ToD1 {
|
|
std::function<double(double)> PiecewiseLinear(std::vector<double> positions,
|
|
std::vector<double> values) {
|
|
if (positions.size() != values.size()) {
|
|
ERROR("Values and positions must match in size");
|
|
}
|
|
|
|
std::map<double, double> coordinates;
|
|
for (size_t i = 0; i < positions.size(); ++i) {
|
|
coordinates[positions[i]] = values[i];
|
|
}
|
|
|
|
return [=](double x) {
|
|
const auto lb = coordinates.lower_bound(x);
|
|
const auto ub = coordinates.upper_bound(x);
|
|
|
|
if (lb == coordinates.end()) {
|
|
return coordinates.rbegin()->second;
|
|
}
|
|
if (ub == coordinates.begin()) {
|
|
return coordinates.begin()->second;
|
|
}
|
|
if (lb == coordinates.begin()) {
|
|
return (*lb).second;
|
|
}
|
|
const double x1 = (*lb).first;
|
|
const double y1 = (*lb).second;
|
|
const double x0 = std::prev(lb)->first;
|
|
const double y0 = std::prev(lb)->second;
|
|
|
|
return ((y1 - y0) * x + x1 * y0 - x0 * y1) / (x1 - x0);
|
|
};
|
|
}
|
|
|
|
std::function<double(double)> Constant(double constant) {
|
|
return [=](double x) { return constant; };
|
|
}
|
|
|
|
std::function<double(double)> Product(
|
|
std::vector<std::function<double(double)>> functions) {
|
|
if (functions.size() == 0) {
|
|
ERROR("The product needs at least one function");
|
|
}
|
|
|
|
return [=](double x) {
|
|
double product = 1.;
|
|
for (auto it = functions.begin(); it != functions.end(); ++it) {
|
|
product *= (*it)(x);
|
|
}
|
|
return product;
|
|
};
|
|
}
|
|
|
|
std::function<double(double)> Exponential(double prefactor, double scale) {
|
|
return [=](double x) { return prefactor * std::exp(scale * x); };
|
|
}
|
|
}; // namespace D1ToD1
|
|
|
|
namespace D3ToD1 {
|
|
std::function<double(double, double, double)> Constant(double value) {
|
|
return [=](double x, double y, double z) { return value; };
|
|
}
|
|
|
|
std::function<double(double, double, double)> Product(
|
|
std::function<double(double)> fi,
|
|
std::function<double(double)> fj,
|
|
std::function<double(double)> fk) {
|
|
return [=](double x, double y, double z) { return fi(x) * fj(y) * fk(z); };
|
|
}
|
|
|
|
}; // namespace D3ToD1
|