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 <http://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
24namespace QuantLib {
25
26 std::vector<std::vector<BigNatural> > PascalTriangle::coefficients_;
27
28 const std::vector<BigNatural>& PascalTriangle::get(Size order) {
29 if (coefficients_.empty()) {
30 // order zero mandatory for bootstrap
31 coefficients_.emplace_back(args: 1, args: 1);
32
33 coefficients_.emplace_back(args: 2, args: 1);
34 coefficients_.emplace_back(args: 3, args: 1);
35 coefficients_[2][1] = 2;
36 coefficients_.emplace_back(args: 4, args: 1);
37 coefficients_[3][1] = coefficients_[3][2] = 3;
38 }
39 while (coefficients_.size()<=order)
40 nextOrder();
41 return coefficients_[order];
42 }
43
44 void PascalTriangle::nextOrder() {
45 Size order = coefficients_.size();
46 coefficients_.emplace_back(args: order + 1);
47 coefficients_[order][0] = coefficients_[order][order] = 1;
48 for (Size i=1; i<order/2+1; ++i) {
49 coefficients_[order][i] = coefficients_[order][order-i] =
50 coefficients_[order-1][i-1] + coefficients_[order-1][i];
51 }
52 }
53
54}
55

source code of quantlib/ql/math/pascaltriangle.cpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.