|
| 1 | +/* pcmsolver_copyright_start */ |
| 2 | +/* |
| 3 | + * PCMSolver, an API for the Polarizable Continuum Model |
| 4 | + * Copyright (C) 2013 Roberto Di Remigio, Luca Frediani and contributors |
| 5 | + * |
| 6 | + * This file is part of PCMSolver. |
| 7 | + * |
| 8 | + * PCMSolver is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * PCMSolver is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with PCMSolver. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + * For information on the complete list of contributors to the |
| 22 | + * PCMSolver API, see: <http://pcmsolver.github.io/pcmsolver-doc> |
| 23 | + */ |
| 24 | +/* pcmsolver_copyright_end */ |
| 25 | + |
| 26 | +#ifndef SPLINEFUNCTION_HPP |
| 27 | +#define SPLINEFUNCTION_HPP |
| 28 | + |
| 29 | +#include "Config.hpp" |
| 30 | + |
| 31 | +#include <Eigen/Core> |
| 32 | +#include <unsupported/Eigen/Splines> |
| 33 | + |
| 34 | +/*! \file SplineFunction.hpp |
| 35 | + * \class SplineFunction |
| 36 | + * \brief Spline interpolation of a function |
| 37 | + * \author Roberto Di Remigio |
| 38 | + * \date 2015 |
| 39 | + * |
| 40 | + * Taken from StackOverflow http://stackoverflow.com/a/29825204/2528668 |
| 41 | + */ |
| 42 | + |
| 43 | +/*! Scale abscissa value to [0, 1] */ |
| 44 | +inline double scale(double x) { |
| 45 | + return (x - xMin_) / (xMax_ - xMin_); |
| 46 | +} |
| 47 | + |
| 48 | +/*! Scale abscissa values to [0, 1] */ |
| 49 | +inline Eigen::RowVectorXd scale(const Eigen::VectorXd & x_vec) { |
| 50 | + return x_vec.unaryExpr([](double x) -> double { return scale(x); }).transpose(); |
| 51 | +} |
| 52 | + |
| 53 | +class SplineFunction |
| 54 | +{ |
| 55 | +private: |
| 56 | + typedef Eigen::Spline<double, 1, 3> SplineFit; |
| 57 | +public: |
| 58 | + /*! \param[in] x vector with abscissa values |
| 59 | + * \param[in] y vector with function values |
| 60 | + */ |
| 61 | + SplineFunction(const Eigen::VectorXd & x, const Eigen::VectorXd & y) |
| 62 | + : xMin_(x.minCoeff()), xMax_(x.maxCoeff()), |
| 63 | + spline_(Eigen::SplineFitting<SplineFit>::Interpolate(y.transpose(), |
| 64 | + std::min<int>(x.rows() - 1, 3), scale(x)) |
| 65 | + ) |
| 66 | + {} |
| 67 | + /*! \brief Evaluate spline at given point |
| 68 | + * \param[in] x evaluation point |
| 69 | + */ |
| 70 | + double operator()(double x) const { |
| 71 | + return spline_(scale(x))(0); |
| 72 | + } |
| 73 | +private: |
| 74 | + double xMin_; |
| 75 | + double xMax_; |
| 76 | + SplineFit spline_; |
| 77 | +}; |
| 78 | +#endif // SPLINEFUNCTION_HPP |
0 commit comments