Skip to content
Snippets Groups Projects
Commit 4cf23e77 authored by wenqing's avatar wenqing
Browse files

[Alg] Added a function to get inverse value from curve

parent 6406d1a0
No related branches found
No related tags found
No related merge requests found
...@@ -58,7 +58,26 @@ public: ...@@ -58,7 +58,26 @@ public:
* this interval are set to \f$x_{\min}\f$ or \f$x_{\max}\f$. * this interval are set to \f$x_{\min}\f$ or \f$x_{\max}\f$.
* @return The interpolated value. * @return The interpolated value.
*/ */
double getValue(double pnt_to_interpolate) const; double getValue(double const pnt_to_interpolate) const;
/**
* \brief Calculates the interpolation of the variable by a known
* value of the piece wise linear curve. The curve must be
* monotonically increase or decrease. The monotonicity can be
* checked after create an instance of this class.
*
* @param value The variable for a point should be located within the range
* \f$[f(x_{\min}), f(x_{\max})]\f$, where \f$x_{\min} = \min_{1 \le j \le n}
* x_j\f$ and
* \f$x_{\max} = \max_{1 \le j \le n} x_j\f$. Points outside of this
* interval are
* set to f(x_{\min}) or f(x_{\max}).
* @return The interpolated value, \f$x\f$.
*
* \attention Check the monotonicity of the data beforehand if this function
* is used.
*/
double getInverseValue(double const value) const;
/** /**
* \brief Calculates derivative using quadratic interpolation * \brief Calculates derivative using quadratic interpolation
...@@ -76,9 +95,16 @@ public: ...@@ -76,9 +95,16 @@ public:
double getSupportMax() const; double getSupportMax() const;
double getSupportMin() const; double getSupportMin() const;
bool isMonotonic() const;
private: private:
std::vector<double> _supp_pnts; std::vector<double> _supp_pnts;
std::vector<double> _values_at_supp_pnts; std::vector<double> _values_at_supp_pnts;
double interpolate(std::vector<double> const& supp_pnts,
std::vector<double> const& values_at_supp_pnts,
std::size_t const interval_idx,
double const pnt_to_interpolate) const;
}; };
} // end namespace MathLib } // end namespace MathLib
......
...@@ -156,3 +156,30 @@ TEST(MathLibInterpolationAlgorithms, PiecewiseLinearInterpolationDerivative) ...@@ -156,3 +156,30 @@ TEST(MathLibInterpolationAlgorithms, PiecewiseLinearInterpolationDerivative)
ASSERT_NEAR(0, interpolation.getDerivative(1001), ASSERT_NEAR(0, interpolation.getDerivative(1001),
std::numeric_limits<double>::epsilon()); std::numeric_limits<double>::epsilon());
} }
TEST(MathLibInterpolationAlgorithms, PiecewiseLinearInterpolationGetInverseValue)
{
const std::size_t size = 1000;
std::vector<double> variables, values;
for (std::size_t k=0; k < size; ++k)
{
variables.push_back(static_cast<double>(k));
values.push_back(static_cast<double>(2*k));
}
std::vector<double> variables_cpy = variables;
std::vector<double> values_cpy = values;
MathLib::PiecewiseLinearInterpolation
interpolation{std::move(variables_cpy),
std::move(values_cpy)};
ASSERT_EQ(true, interpolation.isMonotonic());
// Get inverse values and compare them
for (std::size_t k=0; k < size; ++k)
{
ASSERT_NEAR(variables[k], interpolation.getInverseValue(values[k]),
std::numeric_limits<double>::epsilon());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment