1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
Copyright (C) 2019 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef quantlib_ode_i
#define quantlib_ode_i
%include common.i
%include functions.i
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%{
class OdeFctDelegate {
public:
virtual ~OdeFctDelegate() {}
virtual std::vector<Real> value(
Real x, const std::vector<Real>& y) const {
QL_FAIL("implementation of OdeFctDelegate.value is missing");
}
};
class OdeFct {
public:
OdeFct(OdeFctDelegate* delegate)
: delegate_(delegate) { }
virtual ~OdeFct() { }
const std::vector<Real> operator()(Real x, const std::vector<Real>& y) const {
std::vector<Real> retVal = delegate_->value(x, y);
return retVal;
}
private:
OdeFctDelegate* delegate_;
};
%}
%feature("director") OdeFctDelegate;
class OdeFctDelegate {
public:
virtual ~OdeFctDelegate();
virtual std::vector<Real> value(Real x, const std::vector<Real>& y) const;
};
#elif defined(SWIGPYTHON)
%{
class OdeFct {
public:
OdeFct(PyObject* function)
: function_(PyPtr::fromBorrowed(function)) {}
const std::vector<Real> operator()(Real x, const std::vector<Real>& y) const {
auto pyY = PyPtr::fromResult(PyList_New(y.size()), "failed to convert arguments");
for (Size i=0; i < y.size(); ++i)
PyList_SetItem(pyY.get(), i, PyFloat_FromDouble(y[i]));
auto pyResult = PyPtr::fromResult(
PyObject_CallFunction(function_.get(), "dO", x, pyY.get()),
"failed to call Python function");
QL_ENSURE(
PyList_Check(pyResult.get()) && PyList_Size(pyResult.get()) == y.size(),
"Python function did not return a list of correct size");
std::vector<Real> retVal(y.size());
for (Size i=0; i < y.size(); ++i)
retVal[i] = PyFloat_AsDouble(PyList_GetItem(pyResult.get(), i));
return retVal;
}
private:
PyPtr function_;
};
%}
#endif
%{
using QuantLib::AdaptiveRungeKutta;
%}
template <class T = Real>
class AdaptiveRungeKutta {
public:
AdaptiveRungeKutta(const Real eps=1.0e-6,
const Real h1=1.0e-4,
const Real hmin=0.0);
%extend {
#if defined(SWIGPYTHON)
T operator()(PyObject* fct, T y1, Real x1, Real x2) {
BinaryFunction f(fct);
return self->operator()(f, y1, x1, x2);
}
std::vector<T> operator()(
PyObject* fct, const std::vector<T>& y1, Real x1, Real x2) {
OdeFct f(fct);
return self->operator()(f, y1, x1, x2);
}
#elif defined(SWIGJAVA) || defined(SWIGCSHARP)
T operator()(BinaryFunctionDelegate* fct, T y1, Real x1, Real x2) {
BinaryFunction f(fct);
return self->operator()(f, y1, x1, x2);
}
std::vector<T> operator()(
OdeFctDelegate* fct, const std::vector<T>& y1, Real x1, Real x2) {
OdeFct f(fct);
return self->operator()(f, y1, x1, x2);
}
#endif
}
};
%template(RungeKutta) AdaptiveRungeKutta<Real>;
#endif
|