From 3c40aff42379f97af42fcf9f7e7d7b37af3ffb03 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Tue, 22 May 2012 13:49:43 +0200 Subject: [PATCH] - added folder InterpolationAlgorithms within the folder MathLib - added class template LinearIntervalInterpolation --- MathLib/CMakeLists.txt | 3 + .../LinearIntervalInterpolation.h | 76 +++++++++++++++++++ .../PiecewiseLinearInterpolation.cpp | 0 .../PiecewiseLinearInterpolation.h | 0 4 files changed, 79 insertions(+) create mode 100644 MathLib/InterpolationAlgorithms/LinearIntervalInterpolation.h rename MathLib/{ => InterpolationAlgorithms}/PiecewiseLinearInterpolation.cpp (100%) rename MathLib/{ => InterpolationAlgorithms}/PiecewiseLinearInterpolation.h (100%) diff --git a/MathLib/CMakeLists.txt b/MathLib/CMakeLists.txt index d7eb5f51a97..f6e6b904d90 100644 --- a/MathLib/CMakeLists.txt +++ b/MathLib/CMakeLists.txt @@ -2,6 +2,9 @@ GET_SOURCE_FILES(SOURCES_MATHLIB) SET ( SOURCES ${SOURCES_MATHLIB}) +GET_SOURCE_FILES(SOURCES_INTERPOLATIONALGORITHMS InterpolationAlgorithms) +SET ( SOURCES ${SOURCES} ${SOURCES_INTERPOLATIONALGORITHMS}) + GET_SOURCE_FILES(SOURCES_LINALG LinAlg) SET ( SOURCES ${SOURCES} ${SOURCES_LINALG}) diff --git a/MathLib/InterpolationAlgorithms/LinearIntervalInterpolation.h b/MathLib/InterpolationAlgorithms/LinearIntervalInterpolation.h new file mode 100644 index 00000000000..40f728ad800 --- /dev/null +++ b/MathLib/InterpolationAlgorithms/LinearIntervalInterpolation.h @@ -0,0 +1,76 @@ +/* + * @file LinearIntervalInterpolation.h + * + * Created on: May 22, 2012 + * Author: fischeth + */ + +#ifndef LINEARINTERVALINTERPOLATION_H_ +#define LINEARINTERVALINTERPOLATION_H_ + +#include <stdexcept> + +namespace MathLib { + +/** + * @brief Class (template) LinearIntervalInterpolation is a functional object performing + * an interval mapping \f$f: [a,b] \to [c,d]\f$. + * + * Input numeric type has to be a floating point type and must behave well under the + * operations addition, subtraction, multiplication and division. Let \f$a, b, c, d\f$ + * objects supporting the mentioned operations. Under the condition + * \f$a \neq b\f$ an instance of the class computes a value within the interval + * \f$[c, d]\f$, i.e., \f$f: [a,b] \to [c,d]\f$. + */ +template <typename NUMERIC_TYPE> +class LinearIntervalInterpolation { +public: + /** + * Constructor of class template for a linear map \f$y = m \cdot x + n\f$. + * Under the prerequisite \f$a \neq b\f$ it initializes the coefficients + * \f$m\f$ and \f$n\f$ in a correct way. + * @param a first endpoint of the first interval + * @param b second endpoint of the first interval + * @param c first endpoint of the second interval + * @param d second endpoint of the second interval + */ + LinearIntervalInterpolation(NUMERIC_TYPE a, NUMERIC_TYPE b, NUMERIC_TYPE c, NUMERIC_TYPE d); + /** + * Method computes the value at point \f$x\f$ obtained by linear interpolation. + * @param x the point the interpolation value is searched for + * @return the interpolation value at point \f$x\f$ + */ + inline NUMERIC_TYPE operator() (NUMERIC_TYPE x) const; + +private: + /** + * the slope of the linear map + */ + NUMERIC_TYPE _m; + /** + * the offset of the linear map for \f$x\f$ equals zero + */ + NUMERIC_TYPE _n; +}; + +template <typename NUMERIC_TYPE> +LinearIntervalInterpolation<NUMERIC_TYPE>::LinearIntervalInterpolation(NUMERIC_TYPE a, NUMERIC_TYPE b, + NUMERIC_TYPE c, NUMERIC_TYPE d) : + _m (d-c), _n(0.0) +{ + if (b == a) { + throw std::runtime_error("LinearIntervalInterpolation::LinearIntervalInterpolation: a == b, empty interval"); + } + _m /= (b-a); + _n = c - _m * a; +} + +template <typename NUMERIC_TYPE> +inline NUMERIC_TYPE LinearIntervalInterpolation<NUMERIC_TYPE>::operator() (NUMERIC_TYPE x) const +{ + return _m * x + _n; +} + +} // end namespace MathLib + +#endif /* LINEARINTERPOLATION_H_ */ diff --git a/MathLib/PiecewiseLinearInterpolation.cpp b/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp similarity index 100% rename from MathLib/PiecewiseLinearInterpolation.cpp rename to MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp diff --git a/MathLib/PiecewiseLinearInterpolation.h b/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.h similarity index 100% rename from MathLib/PiecewiseLinearInterpolation.h rename to MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.h -- GitLab