/src/quantlib/ql/math/pascaltriangle.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2015 Ferdinando Ametrano |
5 | | Copyright (C) 2015 Paolo Mazzocchi |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <https://www.quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | #include <ql/math/pascaltriangle.hpp> |
22 | | #include <iterator> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | std::vector<std::vector<BigNatural> > PascalTriangle::coefficients_; |
27 | | |
28 | 0 | const std::vector<BigNatural>& PascalTriangle::get(Size order) { |
29 | 0 | if (coefficients_.empty()) { |
30 | | // order zero mandatory for bootstrap |
31 | 0 | coefficients_.emplace_back(1, 1); |
32 | |
|
33 | 0 | coefficients_.emplace_back(2, 1); |
34 | 0 | coefficients_.emplace_back(3, 1); |
35 | 0 | coefficients_[2][1] = 2; |
36 | 0 | coefficients_.emplace_back(4, 1); |
37 | 0 | coefficients_[3][1] = coefficients_[3][2] = 3; |
38 | 0 | } |
39 | 0 | while (coefficients_.size()<=order) |
40 | 0 | nextOrder(); |
41 | 0 | return coefficients_[order]; |
42 | 0 | } |
43 | | |
44 | 0 | void PascalTriangle::nextOrder() { |
45 | 0 | Size order = coefficients_.size(); |
46 | 0 | coefficients_.emplace_back(order + 1); |
47 | 0 | coefficients_[order][0] = coefficients_[order][order] = 1; |
48 | 0 | for (Size i=1; i<order/2+1; ++i) { |
49 | 0 | coefficients_[order][i] = coefficients_[order][order-i] = |
50 | 0 | coefficients_[order-1][i-1] + coefficients_[order-1][i]; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | } |