// BicubicSpline.hpp
// Copyright (C) 2002, 2003 Roman Gitlin #ifndef bicubicspline_hpp #define bicubicspline_hpp #include <CubicSpline\CubicSpline.hpp> namespace QuantLib { namespace Math { template<class X1, class X2, typename X3> class BicubicSpline : public std::binary_function <QL_ITERATOR_TRAITS<X1>::value_type, QL_ITERATOR_TRAITS<X2>::value_type, X3> { public: typedef typename std::vector<result_type>::iterator iterator; typedef typename std::vector<result_type>::const_iterator const_iterator; typedef typename std::vector<std::vector<result_type> > data_table; BicubicSpline(int, const X1 &, const X2 &, const data_table &, const data_table &, int, int, bool); BicubicSpline(const X1 &, const X2 &, const data_table &, const data_table &, int, int, bool); virtual ~BicubicSpline(){} result_type operator()(first_argument_type, second_argument_type) const; private: // data const int m_; const int n_; const X1 &x1_; const X2 &x2_; const data_table &y_; const data_table &y2_; bool allow_extrapolation_; }; template<class X1, class X2, typename X3> BicubicSpline<X1, X2, X3>::BicubicSpline(const X1 &x1, const X2 &x2, const data_table &y, const data_table &y2, int m, int n, bool extrapolation) : m_(m), n_(n), x1_(x1), x2_(x2), y_(y), y2_(y2), allow_extrapolation_(extrapolation) { try { for(int i = 0; i < m_; ++i) CubicSpline<X2, const_iterator>(x2_, y_[i].begin(), (iterator)y2_[i].begin(), n_, allow_extrapolation_); } catch(std::exception &e) { std::cerr << "BicubicSpline<X1, X2, X3>::BicubicSpline(const " "X1 &, const X2 &, const std::vector<std::vector" << "<X3> > &, int, int, bool) : " << e.what() << std::endl; throw; } } template<class X1, class X2, typename X3> BicubicSpline<X1, X2, X3>::BicubicSpline(int, const X1 &x1, const X2 &x2, const data_table &y, const data_table &y2, int m, int n, bool extrapolation) : m_(m), n_(n), x1_(x1), x2_(x2), y_(y), y2_(y2), allow_extrapolation_(extrapolation) {} template<class X1, class X2, typename X3> BicubicSpline<X1, X2, X3>::result_type BicubicSpline<X1, X2, X3>::operator()(first_argument_type x, second_argument_type y) const { try { std::vector<result_type> temp1(m_), temp2(m_); iterator ya = temp1.begin(), y2 = temp2.begin(); for(int i = 0; i < m_; ++i) ya[i] = CubicSpline<X2, const_iterator>(0, x2_, y_[i].begin(), (iterator)y2_[i].begin(), n_, allow_extrapolation_)(y); return CubicSpline<X1, const_iterator>(x1_, ya, y2, m_, allow_extrapolation_)(x); } catch(std::exception &e) { std::cerr << "BiCubicInterpolation<X1, X2, X3>::operator() : " << e.what() << std::endl; throw; } } } } #endif // TricubicSpline.hpp // Copyright (C) 2002, 2003 Roman Gitlin #ifndef tricubicspline_hpp #define tricubicspline_hpp #include <BicubicSpline\BicubicSpline.hpp> namespace QuantLib { namespace Math { template<class X1, class X2, typename X3, class X4> class TricubicSpline { public: typedef typename QL_ITERATOR_TRAITS<X1>::value_type first_argument_type; typedef typename QL_ITERATOR_TRAITS<X2>::value_type second_argument_type; typedef typename QL_ITERATOR_TRAITS<X3>::value_type third_argument_type; typedef typename X4 result_type; typedef typename std::vector<result_type>::iterator iterator; typedef typename std::vector<result_type>::const_iterator const_iterator; typedef typename std::vector<std::vector<result_type> > D2data_table; typedef typename std::vector<D2data_table> D3data_table; TricubicSpline(int, const X1 &, const X2 &, const X3 &, const D3data_table &, const D3data_table &, int, int, int, bool); TricubicSpline(const X1 &, const X2 &, const X3 &, const D3data_table &, const D3data_table &, int, int, int, bool); virtual ~TricubicSpline(){} result_type operator()(first_argument_type, second_argument_type, third_argument_type) const; private: // data const int m_; const int n_; const int o_; const X1 &x1_; const X2 &x2_; const X3 &x3_; const D3data_table &y_; const D3data_table &y2_; bool allow_extrapolation_; }; template<class X1, class X2, class X3, typename X4> TricubicSpline<X1, X2, X3, X4>::TricubicSpline(const X1 &x1, const X2 &x2, const X3 &x3, const D3data_table &y, const D3data_table &y2, int m, int n, int o, bool extrapolation) : m_(m), n_(n), o_(o), x1_(x1), x2_(x2), x3_(x3), y_(y), y2_(y2), allow_extrapolation_(extrapolation) { try { for(int i = 0; i < m_; ++i) BicubicSpline<X2, X3, X4>(x2_, x3_, y_[i], y2_[i], n_, o_, allow_extrapolation_); } catch(std::exception &e) { std::cerr << "TricubicSpline<X1, X2, X3, X4>::TricubicSpline" "(const X1 &, const X2 &, const X3 &, conststd::vector<" "std::vector<std::vector<X3> > >&, const std::vector<" "std::vector<std::vector<X3> > >&, int, int, int, bool) : " << e.what() << std::endl; throw; } } template<class X1, class X2, class X3, typename X4> TricubicSpline<X1, X2, X3, X4>::TricubicSpline(int, const X1 &x1, const X2 &x2, const X3 &x3, const D3data_table &y, const D3data_table &y2, int m, int n, int o, bool extrapolation) : m_(m), n_(n), o_(o), x1_(x1), x2_(x2), x3_(x3), y_(y), y2_(y2), allow_extrapolation_(extrapolation) {} template<class X1, class X2, class X3, typename X4> TricubicSpline<X1, X2, X3, X4>::result_type TricubicSpline<X1, X2, X3, X4>::operator()(first_argument_type x, second_argument_type y, third_argument_type z) const { try { std::vector<result_type> temp1(m_), temp2(m_); iterator ya = temp1.begin(), y2 = temp2.begin(); for(int i = 0; i < m_; ++i) ya[i] = BicubicSpline<X2, X3, X4>(0, x2_, x3_, y_[i], y2_[i], n_, o_, allow_extrapolation_)(y, z); return CubicSpline<X1, const_iterator>(x1_, ya, y2, m_, allow_extrapolation_)(x); } catch(std::exception &e) { std::cerr << "BiCubicInterpolation<X1, X2, X3>::operator() : " << e.what() << std::endl; throw; } } } } #endif |
Free forum by Nabble | Edit this page |