Chi-Tech
piecewise_linear_1D.cc
Go to the documentation of this file.
2
3#include "ChiObjectFactory.h"
4
6{
7
9
11{
13
14 // clang-format off
15 params.SetGeneralDescription("Piecewise linear function");
16 params.SetDocGroup("DocMathFunctions");
17 // clang-format on
18
20 "x_values", "The x-values used in the interpolation function.");
22 "y_values", "The x-values used in the interpolation function.");
23
24 params.ChangeExistingParamToOptional("input_dimension", size_t{1});
25 params.ChangeExistingParamToOptional("output_dimension", size_t{1});
26
27 return params;
28}
29
31 : FunctionDimAToDimB(params),
32 x_values_(params.GetParamVectorValue<double>("x_values")),
33 y_values_(params.GetParamVectorValue<double>("y_values")),
34 num_vals_(x_values_.size())
35{
37 y_values_.size() != num_vals_,
38 std::string("Number of y-values (") + std::to_string(y_values_.size()) +
39 ") must match number of x-values (" + std::to_string(num_vals_) + ".");
40
42 "Number of pairs must at least be 2");
43
44 delta_x_values_.assign(num_vals_ - 1, 0.0);
45 slopes_.assign(num_vals_ - 1, 0.0);
46 const size_t max_k = num_vals_ - 1;
47 for (size_t k = 0; k < max_k; ++k)
48 {
49 delta_x_values_[k] = x_values_[k + 1] - x_values_[k];
51 "x-values not monitonically "
52 "increasing");
53 slopes_[k] = (y_values_[k + 1] - y_values_[k]) / delta_x_values_[k];
54 }
55}
56
57std::vector<double>
58PiecewiseLinear1D::Evaluate(const std::vector<double>& values) const
59{
60 if (values.size() != 1)
61 ChiInvalidArgument("Can only be called with 1 argument.");
62
63 return {ScalarFunction1Parameter(values.front())};
64}
65
66std::vector<double>
67PiecewiseLinear1D::EvaluateSlope(const std::vector<double>& values) const
68{
69 if (values.size() != 1)
70 ChiInvalidArgument("Can only be called with 1 argument.");
71
72 return {ScalarFunctionSlope1Parameter(values.front())};
73}
74
76{
77 if (x < x_values_.front()) return y_values_.front();
78
79 if (x >= x_values_.back()) return y_values_.back();
80
81 const size_t max_k = num_vals_ - 1;
82 for (size_t k = 0; k < max_k; ++k)
83 if ((x >= x_values_[k]) and (x < x_values_[k + 1]))
84 {
85 return (y_values_[k] * (x_values_[k + 1] - x) +
86 y_values_[k + 1] * (x - x_values_[k])) /
88 }
89
90 ChiLogicalError("Bad trouble");
91}
92
94{
95 if (x < x_values_.front()) return 0.0;
96
97 if (x >= x_values_.back()) return 0.0;
98
99 const size_t max_k = num_vals_ - 1;
100 for (size_t k = 0; k < max_k; ++k)
101 if ((x >= x_values_[k]) and (x < x_values_[k + 1])) { return slopes_[k]; }
102
103 ChiLogicalError("Bad trouble");
104}
105
106} // namespace chi_math::functions
#define ChiLogicalError(message)
#define ChiInvalidArgumentIf(condition, message)
#define ChiInvalidArgument(message)
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameterArray(const std::string &name, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
void ChangeExistingParamToOptional(const std::string &name, T value, const std::string &doc_string="")
static chi::InputParameters GetInputParameters()
static chi::InputParameters GetInputParameters()
double ScalarFunctionSlope1Parameter(double x) const override
std::vector< double > Evaluate(const std::vector< double > &values) const override
std::vector< double > EvaluateSlope(const std::vector< double > &values) const override
const std::vector< double > x_values_
PiecewiseLinear1D(const chi::InputParameters &params)
const std::vector< double > y_values_
double ScalarFunction1Parameter(double x) const override
RegisterChiObject(chi_math::functions, LuaDimAToDimB)