QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
nthorderderivativeop.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2018 Klaus Spanderen
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/operators/numericaldifferentiation.hpp>
26#include <ql/methods/finitedifferences/operators/nthorderderivativeop.hpp>
27
28#include <set>
29
30namespace QuantLib {
31
33 Size direction, Size order, Integer nPoints,
34 const ext::shared_ptr<FdmMesher>& mesher)
35 : m_(mesher->layout()->size(), mesher->layout()->size()) {
36
37 const Integer hPoints = nPoints/2;
38 const bool isEven = (nPoints == 2*hPoints);
39
40 Array xValues = mesher->locations(direction);
41 std::set<Real> tmp(xValues.begin(), xValues.end());
42 xValues = Array(tmp.begin(), tmp.end()); //unique vector
43
44 const Integer nx(mesher->layout()->dim()[direction]);
45
46 QL_REQUIRE(Integer(xValues.size()) == nx,
47 "inconsistent set of grid values in direction " << direction);
48
49 QL_REQUIRE(nPoints > 1 && Integer(nPoints) <= nx,
50 "inconsistent number of points");
51
52 Array xOffsets(nPoints);
53 const ext::function<Real(Real)> emptyFct;
54
55 for (const auto& iter : *mesher->layout()) {
56 const auto ix = Integer(iter.coordinates()[direction]);
57 const Integer offset = std::max(0, hPoints - ix)
58 - std::max(0, hPoints - (nx-((isEven)? 0 : 1) - ix));
59
60 const Integer ilx = ix - hPoints + offset;
61
62 for (Integer j=0; j < nPoints; ++j) {
63 const Integer idx = ilx + j;
64 xOffsets[j] = xValues[idx] - xValues[ix];
65 }
66
67 const Array weights =
68 NumericalDifferentiation(emptyFct, order, xOffsets).weights();
69
70 const Size i = iter.index();
71 for (Integer j=0; j < nPoints; ++j) {
72 const Size k = mesher->layout()->neighbourhood(iter, direction, ilx - ix + j);
73
74 m_(i, k) = weights[j];
75 }
76 }
77 }
78
80 return prod(m_, r);
81 }
82
83
85 return m_;
86 }
87
88}
89
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
NthOrderDerivativeOp(Size direction, Size order, Integer nPoints, const ext::shared_ptr< FdmMesher > &mesher)
array_type apply(const array_type &r) const override
SparseMatrix toMatrix() const override
Numerical Differentiation on arbitrarily spaced grids.
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Array prod(const SparseMatrix &A, const Array &x)
boost::numeric::ublas::compressed_matrix< Real > SparseMatrix