Skip to content
Snippets Groups Projects
Commit 9ed855b2 authored by Tom Fischer's avatar Tom Fischer
Browse files

renamed files MathLib/LinearInterpolation.{cpp,h} to MathLib/PiecewiseLinearInterpolation.{cpp,h}

parent 5e4dd036
No related branches found
No related tags found
No related merge requests found
/*
* 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_ */
/* /*
* LinearInterpolation.cpp * PiecewiseLinearInterpolation.cpp
* *
* Created on: Sep 7, 2010 * Created on: Sep 7, 2010
* Author: TF * Author: TF
*/ */
#include "LinearInterpolation.h" #include "PiecewiseLinearInterpolation.h"
#include "binarySearch.h" #include "binarySearch.h"
#include "swap.h" #include "swap.h"
#include <iostream> #include <iostream>
namespace MathLib { namespace MathLib
{
LinearInterpolation::LinearInterpolation(const std::vector<double>& supporting_points, const std::vector<double>& values_at_supp_pnts) 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) : _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) 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) : _supporting_points (supporting_points), _values_at_supp_pnts (values_at_supp_pnts)
{ {
// std::cout << "LinearInterpolation::LinearInterpolation support_points, values_at_supp_pnts: " << std::endl; // std::cout << "PiecewiseLinearInterpolation::PiecewiseLinearInterpolation support_points, values_at_supp_pnts: " << std::endl;
// for (size_t k(0); k<supporting_points.size(); k++) { // for (size_t k(0); k<supporting_points.size(); k++) {
// std::cout << supporting_points[k] << " " << values_at_supp_pnts[k] << std::endl; // std::cout << supporting_points[k] << " " << values_at_supp_pnts[k] << std::endl;
// } // }
// std::cout << std::endl; // std::cout << std::endl;
values_at_interpol_pnts.clear(); values_at_interpol_pnts.clear();
for (size_t k(0); k<points_to_interpolate.size(); k++) for (size_t k(0); k < points_to_interpolate.size(); k++)
values_at_interpol_pnts.push_back (this->getValue (points_to_interpolate[k])); values_at_interpol_pnts.push_back (this->getValue (points_to_interpolate[k]));
} }
LinearInterpolation::~LinearInterpolation() PiecewiseLinearInterpolation::~PiecewiseLinearInterpolation()
{} {}
double LinearInterpolation::getValue ( double pnt_to_interpolate ) double PiecewiseLinearInterpolation::getValue ( double pnt_to_interpolate )
{ {
// search interval that has the point inside // search interval that has the point inside
size_t interval_idx (std::numeric_limits<size_t>::max()); 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++) { for (size_t k(1);
if (_supporting_points[k-1] <= pnt_to_interpolate && pnt_to_interpolate <= _supporting_points[k]) { k < _supporting_points.size() && interval_idx == std::numeric_limits<size_t>::max();
interval_idx = k-1; 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 // 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])); 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 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]); // 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]); long double n (_values_at_supp_pnts[interval_idx] - m * _supporting_points[interval_idx]);
return m * pnt_to_interpolate + n; return m * pnt_to_interpolate + n;
} }
} // end MathLib } // end MathLib
/*
* 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_ */
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