/src/quantlib/ql/math/expm1.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2023 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 | | <https://www.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 | | #include <ql/math/expm1.hpp> |
21 | | #include <ql/math/functional.hpp> |
22 | | |
23 | | #include <cmath> |
24 | | |
25 | | namespace QuantLib { |
26 | 0 | std::complex<Real> expm1(const std::complex<Real>& z) { |
27 | 0 | if (std::abs(z) < 1.0) { |
28 | 0 | const Real a = z.real(), b = z.imag(); |
29 | 0 | const Real exp_1 = std::expm1(a); |
30 | 0 | const Real cos_1 = -2*squared(std::sin(0.5*b)); |
31 | |
|
32 | 0 | return std::complex<Real>( |
33 | 0 | exp_1*cos_1 + exp_1 + cos_1, |
34 | 0 | std::sin(b)*std::exp(a) |
35 | 0 | ); |
36 | 0 | } |
37 | 0 | else { |
38 | 0 | return std::exp(z)-1.0; |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | std::complex<Real> log1p(const std::complex<Real>& z) { |
43 | 0 | const Real a = z.real(), b = z.imag(); |
44 | 0 | if (std::abs(a) < 0.5 && std::abs(b) < 0.5) { |
45 | 0 | return std::complex<Real>( |
46 | 0 | 0.5*std::log1p(a*a + 2*a + b*b), |
47 | 0 | std::arg(1.0 + z) |
48 | 0 | ); |
49 | 0 | } |
50 | 0 | else { |
51 | 0 | return std::log(1.0+z); |
52 | 0 | } |
53 | 0 | } |
54 | | } |