diff --git a/MathLib/LinearInterpolation.cpp b/MathLib/LinearInterpolation.cpp deleted file mode 100644 index 6853843b6a69800d2e8c44c54d2769c5b5860b4d..0000000000000000000000000000000000000000 --- a/MathLib/LinearInterpolation.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * LinearInterpolation.cpp - * - * Created on: Sep 7, 2010 - * Author: TF - */ - -#include "LinearInterpolation.h" -#include "binarySearch.h" -#include "swap.h" - -#include <iostream> - -namespace MathLib { - -LinearInterpolation::LinearInterpolation(const std::vector<double>& supporting_points, const std::vector<double>& values_at_supp_pnts) - : _supporting_points (supporting_points), _values_at_supp_pnts (values_at_supp_pnts) -{} - -LinearInterpolation::LinearInterpolation(const std::vector<double>& supporting_points, const std::vector<double>& values_at_supp_pnts, const std::vector<double>& points_to_interpolate, std::vector<double>& values_at_interpol_pnts) - : _supporting_points (supporting_points), _values_at_supp_pnts (values_at_supp_pnts) -{ -// std::cout << "LinearInterpolation::LinearInterpolation support_points, values_at_supp_pnts: " << std::endl; -// for (size_t k(0); k<supporting_points.size(); k++) { -// std::cout << supporting_points[k] << " " << values_at_supp_pnts[k] << std::endl; -// } -// std::cout << std::endl; - values_at_interpol_pnts.clear(); - for (size_t k(0); k<points_to_interpolate.size(); k++) - values_at_interpol_pnts.push_back (this->getValue (points_to_interpolate[k])); -} - -LinearInterpolation::~LinearInterpolation() -{} - -double LinearInterpolation::getValue ( double pnt_to_interpolate ) -{ - // search interval that has the point inside - size_t interval_idx (std::numeric_limits<size_t>::max()); - for (size_t k(1); k<_supporting_points.size() && interval_idx == std::numeric_limits<size_t>::max(); k++) { - if (_supporting_points[k-1] <= pnt_to_interpolate && pnt_to_interpolate <= _supporting_points[k]) { - interval_idx = k-1; - } - } - - // compute linear interpolation polynom: y = m * x + n - long double m ((_values_at_supp_pnts[interval_idx+1] - _values_at_supp_pnts[interval_idx]) / (_supporting_points[interval_idx+1] - _supporting_points[interval_idx])); -// double m ((_values_at_supp_pnts[interval_idx] - _values_at_supp_pnts[interval_idx+1]) / (_supporting_points[interval_idx] - _supporting_points[interval_idx+1])); -// double n (_values_at_supp_pnts[interval_idx+1] - m * _supporting_points[interval_idx+1]); - long double n (_values_at_supp_pnts[interval_idx] - m * _supporting_points[interval_idx]); - - return m * pnt_to_interpolate + n; -} - -} // end MathLib diff --git a/MathLib/LinearInterpolation.h b/MathLib/LinearInterpolation.h deleted file mode 100644 index 7e5f746cd621fcdadc530b3264ae578f05c61dfc..0000000000000000000000000000000000000000 --- a/MathLib/LinearInterpolation.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * LinearInterpolation.h - * - * Created on: Sep 7, 2010 - * Author: TF - */ - -#ifndef LINEARINTERPOLATION_H_ -#define LINEARINTERPOLATION_H_ - -#include <vector> -#include <limits> - -namespace MathLib { - -class LinearInterpolation { -public: - LinearInterpolation(const std::vector<double>& supporting_points, const std::vector<double>& values_at_supp_pnts); - LinearInterpolation(const std::vector<double>& supporting_points, const std::vector<double>& values_at_supp_pnts, const std::vector<double>& points_to_interpolate, std::vector<double>& values_at_interpol_pnts); - virtual ~LinearInterpolation(); - - double getValue ( double pnt_to_interpolate ); - -private: - const std::vector<double>& _supporting_points; - const std::vector<double>& _values_at_supp_pnts; -}; - -} // end namespace MathLib - -#endif /* LINEARINTERPOLATION_H_ */ diff --git a/MathLib/PiecewiseLinearInterpolation.cpp b/MathLib/PiecewiseLinearInterpolation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7c282ed11b91b0f919df73dd76d4ffd0d9b77fb --- /dev/null +++ b/MathLib/PiecewiseLinearInterpolation.cpp @@ -0,0 +1,63 @@ +/* + * PiecewiseLinearInterpolation.cpp + * + * Created on: Sep 7, 2010 + * Author: TF + */ + +#include "PiecewiseLinearInterpolation.h" +#include "binarySearch.h" +#include "swap.h" + +#include <iostream> + +namespace MathLib +{ +PiecewiseLinearInterpolation::PiecewiseLinearInterpolation(const std::vector<double>& supporting_points, + const std::vector<double>& values_at_supp_pnts) + : _supporting_points (supporting_points), _values_at_supp_pnts (values_at_supp_pnts) +{} + +PiecewiseLinearInterpolation::PiecewiseLinearInterpolation(const std::vector<double>& supporting_points, + const std::vector<double>& values_at_supp_pnts, + const std::vector<double>& points_to_interpolate, + std::vector<double>& values_at_interpol_pnts) + : _supporting_points (supporting_points), _values_at_supp_pnts (values_at_supp_pnts) +{ +// std::cout << "PiecewiseLinearInterpolation::PiecewiseLinearInterpolation support_points, values_at_supp_pnts: " << std::endl; +// for (size_t k(0); k<supporting_points.size(); k++) { +// std::cout << supporting_points[k] << " " << values_at_supp_pnts[k] << std::endl; +// } +// std::cout << std::endl; + values_at_interpol_pnts.clear(); + for (size_t k(0); k < points_to_interpolate.size(); k++) + values_at_interpol_pnts.push_back (this->getValue (points_to_interpolate[k])); +} + +PiecewiseLinearInterpolation::~PiecewiseLinearInterpolation() +{} + +double PiecewiseLinearInterpolation::getValue ( double pnt_to_interpolate ) +{ + // search interval that has the point inside + size_t interval_idx (std::numeric_limits<size_t>::max()); + for (size_t k(1); + k < _supporting_points.size() && interval_idx == std::numeric_limits<size_t>::max(); + k++) + if (_supporting_points[k - 1] <= pnt_to_interpolate && pnt_to_interpolate <= + _supporting_points[k]) + interval_idx = k - 1; + + // compute linear interpolation polynom: y = m * x + n + long double m ( + (_values_at_supp_pnts[interval_idx + + 1] - + _values_at_supp_pnts[interval_idx]) / + (_supporting_points[interval_idx + 1] - _supporting_points[interval_idx])); +// double m ((_values_at_supp_pnts[interval_idx] - _values_at_supp_pnts[interval_idx+1]) / (_supporting_points[interval_idx] - _supporting_points[interval_idx+1])); +// double n (_values_at_supp_pnts[interval_idx+1] - m * _supporting_points[interval_idx+1]); + long double n (_values_at_supp_pnts[interval_idx] - m * _supporting_points[interval_idx]); + + return m * pnt_to_interpolate + n; +} +} // end MathLib diff --git a/MathLib/PiecewiseLinearInterpolation.h b/MathLib/PiecewiseLinearInterpolation.h new file mode 100644 index 0000000000000000000000000000000000000000..e7f70d5a2a1138ed21d625fab01a27127f989aeb --- /dev/null +++ b/MathLib/PiecewiseLinearInterpolation.h @@ -0,0 +1,35 @@ +/* + * PiecewiseLinearInterpolation.h + * + * Created on: Sep 7, 2010 + * Author: TF + */ + +#ifndef PIECEWISELINEARINTERPOLATION_H_ +#define PIECEWISELINEARINTERPOLATION_H_ + +#include <limits> +#include <vector> + +namespace MathLib +{ +class PiecewiseLinearInterpolation +{ +public: + PiecewiseLinearInterpolation(const std::vector<double>& supporting_points, + const std::vector<double>& values_at_supp_pnts); + PiecewiseLinearInterpolation(const std::vector<double>& supporting_points, + const std::vector<double>& values_at_supp_pnts, + const std::vector<double>& points_to_interpolate, + std::vector<double>& values_at_interpol_pnts); + virtual ~PiecewiseLinearInterpolation(); + + double getValue ( double pnt_to_interpolate ); + +private: + const std::vector<double>& _supporting_points; + const std::vector<double>& _values_at_supp_pnts; +}; +} // end namespace MathLib + +#endif /* PIECEWISELINEARINTERPOLATION_H_ */