QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
math
pascaltriangle.cpp
Go to the documentation of this file.
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
24
namespace
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(1, 1);
32
33
coefficients_
.emplace_back(2, 1);
34
coefficients_
.emplace_back(3, 1);
35
coefficients_
[2][1] = 2;
36
coefficients_
.emplace_back(4, 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(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
}
QuantLib::PascalTriangle::coefficients_
static std::vector< std::vector< BigNatural > > coefficients_
Definition:
pascaltriangle.hpp:41
QuantLib::PascalTriangle::nextOrder
static void nextOrder()
Definition:
pascaltriangle.cpp:44
QuantLib::PascalTriangle::get
static const std::vector< BigNatural > & get(Size order)
Get and store one vector of coefficients after another.
Definition:
pascaltriangle.cpp:28
QuantLib::Size
std::size_t Size
size of a container
Definition:
types.hpp:58
QuantLib
Definition:
any.hpp:35
pascaltriangle.hpp
Pascal triangle coefficients calculator.
Generated by
Doxygen
1.9.5