QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
expm1.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) 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 <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#include <ql/math/expm1.hpp>
22
23#include <cmath>
24
25namespace QuantLib {
26 std::complex<Real> expm1(const std::complex<Real>& z) {
27 if (std::abs(z) < 1.0) {
28 const Real a = z.real(), b = z.imag();
29 const Real exp_1 = std::expm1(a);
30 const Real cos_1 = -2*squared(std::sin(0.5*b));
31
32 return std::complex<Real>(
33 exp_1*cos_1 + exp_1 + cos_1,
34 std::sin(b)*std::exp(a)
35 );
36 }
37 else {
38 return std::exp(z)-1.0;
39 }
40 }
41
42 std::complex<Real> log1p(const std::complex<Real>& z) {
43 const Real a = z.real(), b = z.imag();
44 if (std::abs(a) < 0.5 && std::abs(b) < 0.5) {
45 return std::complex<Real>(
46 0.5*std::log1p(a*a + 2*a + b*b),
47 std::arg(1.0 + z)
48 );
49 }
50 else {
51 return std::log(1.0+z);
52 }
53 }
54}
complex versions of expm1 and logp1
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
functionals and combinators not included in the STL
Definition: any.hpp:35
std::complex< Real > log1p(const std::complex< Real > &z)
Definition: expm1.cpp:42
T squared(T x)
Definition: functional.hpp:37
std::complex< Real > expm1(const std::complex< Real > &z)
Definition: expm1.cpp:26