#include "functions/functions.h" #include #include "simulation/errors.h" namespace D1ToD1 { std::function PiecewiseLinear(std::vector positions, std::vector values) { if (positions.size() != values.size()) { ERROR("Values and positions must match in size"); } std::map 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 Constant(double constant) { return [=](double x) { return constant; }; } std::function Product( std::vector> 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 Exponential(double prefactor, double scale) { return [=](double x) { return prefactor * std::exp(scale * x); }; } }; // namespace D1ToD1 namespace D3ToD1 { std::function Constant(double value) { return [=](double x, double y, double z) { return value; }; } std::function Product( std::function fi, std::function fj, std::function fk) { return [=](double x, double y, double z) { return fi(x) * fj(y) * fk(z); }; } }; // namespace D3ToD1