| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb |
| 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 | |
| 20 | /*! \file grid.hpp |
| 21 | \brief Grid constructors |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_grid_hpp |
| 25 | #define quantlib_grid_hpp |
| 26 | |
| 27 | #include <ql/math/array.hpp> |
| 28 | |
| 29 | namespace QuantLib { |
| 30 | |
| 31 | Array CenteredGrid(Real center, Real dx, Size steps); |
| 32 | Array BoundedGrid(Real xMin, Real xMax, Size steps); |
| 33 | Array BoundedLogGrid(Real xMin, Real xMax, Size steps); |
| 34 | |
| 35 | // inline definitions |
| 36 | |
| 37 | inline Array CenteredGrid(Real center, Real dx, Size steps) { |
| 38 | Array result(steps+1); |
| 39 | for (Size i=0; i<steps+1; i++) |
| 40 | result[i] = center + (i - steps/2.0)*dx; |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | inline Array BoundedGrid(Real xMin, Real xMax, Size steps) { |
| 45 | Array result(steps+1); |
| 46 | Real x=xMin, dx=(xMax-xMin)/steps; |
| 47 | for (Size i=0; i<steps+1; i++, x+=dx) |
| 48 | result[i] = x; |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | inline Array BoundedLogGrid(Real xMin, Real xMax, Size steps) { |
| 53 | Array result(steps+1); |
| 54 | Real gridLogSpacing = (std::log(x: xMax) - std::log(x: xMin)) / |
| 55 | (steps); |
| 56 | Real edx = std::exp(x: gridLogSpacing); |
| 57 | result[0] = xMin; |
| 58 | for (Size j=1; j < steps+1; j++) { |
| 59 | result[j] = result[j-1]*edx; |
| 60 | } |
| 61 | return result; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | #endif |
| 67 | |