Generate and return the expression 1 - exp(x)
robustly (without numerical underflow).
- Parameters
-
[in] | x | : The input scalar, or array of arbitrary rank, shape, and size, of either
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128), or
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
representing the x value whose 1 - exp(x) is to be returned.
|
[in] | control | : The input scalar object that can be,
-
the constant selection or equivalently, an object of type selection_type.
Specifying this option enables the runtime checks for underflow occurrence via branching and dynamic dispatch.
Enabling this option can aid runtime efficiency when the x ratio is expected to cause underflow.
This option avoids the computation of a logarithm term, leading to better runtime efficiency.
Note that logarithm is highly expensive (on the order of ~200 CPU cycles).
See the relevant benchmark here.
The presence of this argument is merely for compile-time resolution of the procedures of this generic interface.
(optional. If missing, a sequence control flow will be assumed without dynamic dispatch.) |
- Returns
onemexp
: The output scalar (or array) of the same type and kind (and shape) as the input x
representing 1 - exp(x)
without underflow.
Possible calling interfaces ⛓
onemexp
= get1mexp(x, control
= selection)
Generate and return the expression 1 - exp(x) robustly (without numerical underflow).
This module contains procedures and generic interfaces for computing 1 - exp(x) more precisely for ti...
- Warning
- The condition
x < log(huge(x))
must hold.
When the input arguments are of type complex
, this condition must hold for the real
components of the numbers.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
-
The
pure
procedure(s) documented herein become impure
when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1
.
By default, these procedures are pure
in release
build and impure
in debug
and testing
builds.
- See also
- getLog1p
get1mexp
getLogAddExp
getLogSubExp
getLogSumExp
Example usage ⛓
12 real(RKH),
allocatable :: inaccuracy(:)
13 real,
parameter :: EPS
= epsilon(
0.)
14 type(display_type) :: disp
21 call disp%show(
"ref = 1._RKH - exp(real(x, RKH)) ! reference high-precision value for comparison")
22 ref
= 1._RKH - exp(
real(x,
RKH))
25 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
29 call disp%show(
"inaccuracy = abs(ref - result)")
30 inaccuracy
= abs(ref
- result)
33 call disp%show(
"getRankDense(inaccuracy)")
42 call disp%show(
"ref = 1._RKH - exp(real(x, RKH)) ! reference high-precision value for comparison")
43 ref
= 1._RKH - exp(
real(x,
RKH))
46 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
50 call disp%show(
"inaccuracy = abs(ref - result)")
51 inaccuracy
= abs(ref
- result)
54 call disp%show(
"getRankDense(inaccuracy)")
63 call disp%show(
"ref = 1._RKH - exp(real(x, RKH))) ! reference high-precision value for comparison")
64 ref
= 1._RKH - exp(
real(x,
RKH))
67 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
71 call disp%show(
"inaccuracy = abs(ref - result)")
72 inaccuracy
= abs(ref
- result)
75 call disp%show(
"getRankDense(inaccuracy)")
84 call disp%show(
"ref = 1._RKH - exp(real(x, RKH)) ! reference high-precision value for comparison")
85 ref
= 1._RKH - exp(
real(x,
RKH))
88 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
92 call disp%show(
"inaccuracy = abs(ref - result)")
93 inaccuracy
= abs(ref
- result)
96 call disp%show(
"getRankDense(inaccuracy)")
105 call disp%show(
"ref = 1._RKH - exp(real(x, RKH)) ! reference high-precision value for comparison")
106 ref
= 1._RKH - exp(
real(x,
RKH))
109 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
113 call disp%show(
"inaccuracy = abs(ref - result)")
114 inaccuracy
= abs(ref
- result)
117 call disp%show(
"getRankDense(inaccuracy)")
122 call disp%show(
"x = -sqrt(tiny(x))")
126 call disp%show(
"ref = 1._RKH - exp(real(x, RKH)) ! reference high-precision value for comparison")
127 ref
= 1._RKH - exp(
real(x,
RKH))
130 call disp%show(
"result = [1. - exp(x), get1mexp(x), get1mexp(x, control = selection)]")
134 call disp%show(
"inaccuracy = abs(ref - result)")
135 inaccuracy
= abs(ref
- result)
138 call disp%show(
"getRankDense(inaccuracy)")
145 call disp%show(
"[(1._RKH + tiny(0._RKH)), get1mexp(tiny(0._RKH)), get1mexp(tiny(0._RKH), control = selection)]")
152 call disp%show(
"[log(1._RKH - tiny(0._RKH)), get1mexp(-tiny(0._RKH)), get1mexp(-tiny(0._RKH), control = selection)]")
153 call disp%show( [
log(
1._RKH - tiny(
0._RKH)),
get1mexp(
-tiny(
0._RKH)),
get1mexp(
-tiny(
0._RKH), control
= selection)] )
Generate and return the Dense rank of the input scalar string or contiguous array of rank 1 in ascend...
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
This module contains procedures and generic interfaces for obtaining various rankings of elements of ...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
integer, parameter RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
3ref
= 1._RKH - exp(
real(x,
RKH))
5-1.45960305251544247127912782270480705
8-1.45960307,
-1.45960307,
-1.45960307
9inaccuracy
= abs(ref
- result)
11+0.186973260834083721772951929513111672E-7,
+0.186973260834083721772951929513111672E-7,
+0.186973260834083721772951929513111672E-7
19ref
= 1._RKH - exp(
real(x,
RKH))
21-0.119209296656208889945326024661011389E-6
24-0.119209290E-6,
-0.119209290E-6,
-0.119209290E-6
25inaccuracy
= abs(ref
- result)
27+0.710542763994532602466101138934800795E-14,
+0.710542763994532602466101138934800795E-14,
+0.710542763994532602466101138934800795E-14
35ref
= 1._RKH - exp(
real(x,
RKH)))
37-0.596046465517474996932904594500639460E-7
40-0.119209290E-6,
-0.596046448E-7,
-0.596046448E-7
41inaccuracy
= abs(ref
- result)
43+0.596046429990337503067095405499360540E-7,
+0.177635687469329045945006394598704577E-14,
+0.177635687469329045945006394598704577E-14
51ref
= 1._RKH - exp(
real(x,
RKH))
53+0.596046429990338208927884783440999782E-7
56+0.596046448E-7,
+0.596046448E-7,
+0.596046448E-7
57inaccuracy
= abs(ref
- result)
59+0.177635680410721152165590002177307858E-14,
+0.177635680410721152165590002177307858E-14,
+0.177635680410721152165590002177307858E-14
67ref
= 1._RKH - exp(
real(x,
RKH))
69-0.108420217248550443400745280086994171E-18
72+0.00000000,
-0.108420217E-18,
-0.108420217E-18
73inaccuracy
= abs(ref
- result)
75+0.108420217248550443400745280086994171E-18,
+0.00000000000000000000000000000000000,
+0.00000000000000000000000000000000000
83ref
= 1._RKH - exp(
real(x,
RKH))
85+0.108420217248550443400745280086994171E-18
88+0.00000000,
+0.108420217E-18,
+0.108420217E-18
89inaccuracy
= abs(ref
- result)
91+0.108420217248550443400745280086994171E-18,
+0.00000000000000000000000000000000000,
+0.00000000000000000000000000000000000
97+0.336210314311209350626267781732175260E-4931
98[(
1._RKH + tiny(
0._RKH)),
get1mexp(
tiny(
0._RKH)),
get1mexp(
tiny(
0._RKH), control
= selection)]
99+1.00000000000000000000000000000000000,
-0.336210314311209350626267781732175260E-4931,
-0.336210314311209350626267781732175260E-4931
103+0.336210314311209350626267781732175260E-4931
104[
log(
1._RKH - tiny(
0._RKH)),
get1mexp(
-tiny(
0._RKH)),
get1mexp(
-tiny(
0._RKH), control
= selection)]
105+0.00000000000000000000000000000000000,
+0.336210314311209350626267781732175260E-4931,
+0.336210314311209350626267781732175260E-4931
- Benchmarks:
Benchmark :: The effects of control
on runtime efficiency ⛓
- The following program compares the runtime performance of get1mexp algorithm with and without checking for underflows.
7 use iso_fortran_env,
only:
error_unit
13 integer(IK) :: fileUnit
14 integer(IK) ,
parameter :: NBENCH
= 2_IK
15 real(RKG) :: dummySum
= 0._RKG
16 real(RKG) ,
allocatable :: X(:)
18 type(bench_type) :: bench(NBENCH)
19 logical(LK) :: underflowEnabled
21 bench(
1)
= bench_type(name
= SK_
"get1mexpSelection", exec
= get1mexpSelection, overhead
= setOverhead)
22 bench(
2)
= bench_type(name
= SK_
"get1mexpSequence", exec
= get1mexpSequence, overhead
= setOverhead)
24 write(
*,
"(*(g0,:,' '))")
25 write(
*,
"(*(g0,:,' '))")
"get1mexp(...) vs. get1mexp(..., control = selection)"
26 write(
*,
"(*(g0,:,' '))")
28 open(newunit
= fileUnit, file
= "main.out", status
= "replace")
30 write(fileUnit,
"(*(g0,:,','))")
"X", (bench(i)
%name, i
= 1, NBENCH)
31 X
= getLinSpace(x1
= -2 * log(
huge(onemexp)), x2
= log(
epsilon(onemexp)), count
= 20_IK)
32 loopOverArraySize:
do iarr
= 1,
size(X)
34 write(
*,
"(*(g0,:,' '))")
"Benchmarking with X", X(iarr)
36 bench(i)
%timing
= bench(i)
%getTiming(minsec
= 0.07_RK)
38 write(fileUnit,
"(*(g0,:,','))") X(iarr), (bench(i)
%timing
%mean, i
= 1, NBENCH)
40 end do loopOverArraySize
41 write(
*,
"(*(g0,:,' '))") dummySum
42 write(
*,
"(*(g0,:,' '))")
52 subroutine setOverhead()
57 dummySum
= dummySum
+ onemexp
60 subroutine get1mexpSelection()
62 onemexp
= get1mexp(X(iarr), selection)
66 subroutine get1mexpSequence()
Generate count evenly spaced points over the interval [x1, x2] if x1 < x2, or [x2,...
Generate and return an object of type timing_type containing the benchmark timing information and sta...
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains abstract interfaces and types that facilitate benchmarking of different procedur...
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
This is the class for creating benchmark and performance-profiling objects.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Postprocessing of the benchmark output ⛓
3import matplotlib.pyplot
as plt
8dirname = os.path.basename(os.getcwd())
12df = pd.read_csv(
"main.out", delimiter =
",")
13colnames = list(df.columns.values)
19ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
22for colname
in colnames[1:]:
23 plt.plot( df[colnames[0]].values
28plt.xticks(fontsize = fontsize)
29plt.yticks(fontsize = fontsize)
30ax.set_xlabel(colnames[0], fontsize = fontsize)
31ax.set_ylabel(
"Runtime [ seconds ]", fontsize = fontsize)
32ax.set_title(
" vs. ".join(colnames[1:])+
"\nLower is better.", fontsize = fontsize)
36plt.grid(visible =
True, which =
"both", axis =
"both", color =
"0.85", linestyle =
"-")
37ax.tick_params(axis =
"y", which =
"minor")
38ax.tick_params(axis =
"x", which =
"minor")
39ax.legend ( colnames[1:]
46plt.savefig(
"benchmark." + dirname +
".runtime.png")
52ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
55plt.plot( df[colnames[0]].values
56 , np.ones(len(df[colnames[0]].values))
61for colname
in colnames[2:]:
62 plt.plot( df[colnames[0]].values
63 , df[colname].values / df[colnames[1]].values
67plt.xticks(fontsize = fontsize)
68plt.yticks(fontsize = fontsize)
69ax.set_xlabel(colnames[0], fontsize = fontsize)
70ax.set_ylabel(
"Runtime compared to {}".format(colnames[1]), fontsize = fontsize)
71ax.set_title(
"Runtime Ratio Comparison. Lower means faster.\nLower than 1 means faster than {}().".format(colnames[1]), fontsize = fontsize)
75plt.grid(visible =
True, which =
"both", axis =
"both", color =
"0.85", linestyle =
"-")
76ax.tick_params(axis =
"y", which =
"minor")
77ax.tick_params(axis =
"x", which =
"minor")
78ax.legend ( colnames[1:]
85plt.savefig(
"benchmark." + dirname +
".runtime.ratio.png")
Visualization of the benchmark output ⛓
Benchmark moral ⛓
- If the input value
x
is known to be smaller than -log(huge)
then it is generally beneficial to call the interface with control
argument set to selection.
- Test:
- test_pm_math1mexp
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, Thursday 1:45 AM, August 22, 2019, Dallas, TX
Definition at line 133 of file pm_math1mexp.F90.