Generate and return the natural logarithm of the subtraction of the exponential of the smaller input logarithmic value from the larger input logarithmic value robustly (without causing overflow).
More...
Generate and return the natural logarithm of the subtraction of the exponential of the smaller input logarithmic value from the larger input logarithmic value robustly (without causing overflow).
- Parameters
-
[in] | smaller | : The input scalar, or array of the same rank, shape, and size as other array-like input arguments, 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 smaller of the two logarithmic values to be added.
If it is complex , then the real component of the complex value must be smaller than the real component of the (larger ) complex value.
(optional, it must be present if and only if the input argument larger is also present.) |
[in] | larger | : The input scalar, or array of the same rank, shape, and size as other array-like input arguments, of the same type and kind as the input argument smaller , representing the larger of the two logarithmic values to be added.
If it is complex , then the real component of the complex value must be larger than the real component of the (larger ) complex value.
(optional, it must be present if and only if the input argument smaller is also present.) |
[in] | minMax | : The input vector of length 2 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 smaller and the larger of the two logarithmic values to be added as minMax(1) and minMax(2) .
If it is complex , then the real component of the complex value must be smaller than the real component of the (larger ) complex value.
If the smaller and larger values are not known a priori, the vector minMax can be instead constructed by calling getMinMax.
(optional, it must be present if and only if the input arguments smaller and larger are missing.) |
| 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 smaller to larger ratio is expected to cause underflow.
In such cases, the exponentiation is avoided if control = selection, leading to faster runtime by avoiding exponentiation since it 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
logSubExp
: The output scalar of the same type and kind as the input larger
and smaller
containing the natural logarithm of the subtraction of the exponential of the larger input logarithmic value from the smaller input logarithmic value, computed robustly.
Possible calling interfaces ⛓
!
Generate and return the natural logarithm of the subtraction of the exponential of the smaller input ...
This module contains procedures and generic interfaces for subtracting two real or complex values wit...
- Warning
- As the argument names suggest, the value of the input argument
larger
must be larger than or equal to the value of the input argument smaller
.
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.
- Note
- The smaller and larger input values can be quickly obtained by a one-line call to setMinMax.
Alternatively, the smaller and larger values can be sorted in a minMax(1:2)
array by calling getMinMax and passing the result directly to getLogSubExp.
- See also
- getLog1p
get1mexp
getMinMax
setMinMax
getLogAddExp
getLogSubExp
getLogSumExp
Example usage ⛓
12 type(display_type) :: disp
22 call disp%show(
"exp( getLogSubExp(smaller = log(4._RKS), larger = log(10._RKS)) )")
31 call disp%show(
"exp( getLogSubExp(smaller = log(4._RKD), larger = log(10._RKD)) )")
40 call disp%show(
"exp( getLogSubExp(smaller = log(4._RKH), larger = log(10._RKH)) )")
49 call disp%show(
"exp( getLogSubExp(smaller = log((4._CKS, -4._CKS)), larger = log((10._CKS, -10._CKS))) )")
50 call disp%show(
exp(
getLogSubExp(smaller
= log((
4._CKS,
-4._CKS)), larger
= log((
10._CKS,
-10._CKS))) ) )
58 call disp%show(
"exp( getLogSubExp(smaller = log((4._CKD, -4._CKD)), larger = log((10._CKD, -10._CKD))) )")
59 call disp%show(
exp(
getLogSubExp(smaller
= log((
4._CKD,
-4._CKD)), larger
= log((
10._CKD,
-10._CKD))) ) )
67 call disp%show(
"exp( getLogSubExp(smaller = log((4._CKH, -4._CKH)), larger = log((10._CKH, -10._CKH))) )")
68 call disp%show(
exp(
getLogSubExp(smaller
= log((
4._CKH,
-4._CKH)), larger
= log((
10._CKH,
-10._CKH))) ) )
71 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
72 call disp%show(
"!Input arguments as a vector of [minimum, maximum] by calling getMinMax()")
73 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
76 call disp%show(
"exp(getLogSubExp(getMinMax(getUnifRand(-1., 1., 2_IK))))")
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
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.
Generate and return an array of size two containing the minimum and maximum of the two input values i...
This module contains classes and procedures for computing various statistical quantities related to t...
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 CKH
The scalar integer constant of intrinsic default kind, representing the highest-precision complex kin...
integer, parameter CKS
The single-precision complex kind in Fortran mode. On most platforms, this is a 32-bit real kind.
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter CKD
The double precision complex kind in Fortran mode. On most platforms, this is a 64-bit real kind.
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
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...
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
This module contains procedures and generic interfaces for finding the minimum and maximum of two inp...
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 ⛓
6exp(
getLogSubExp(smaller
= log(
4._RKS), larger
= log(
10._RKS)) )
13exp(
getLogSubExp(smaller
= log(
4._RKD), larger
= log(
10._RKD)) )
20exp(
getLogSubExp(smaller
= log(
4._RKH), larger
= log(
10._RKH)) )
21+5.99999999999999999999999999999999923
27exp(
getLogSubExp(smaller
= log((
4._CKS,
-4._CKS)), larger
= log((
10._CKS,
-10._CKS))) )
28(
+6.00000048,
-6.00000048)
34exp(
getLogSubExp(smaller
= log((
4._CKD,
-4._CKD)), larger
= log((
10._CKD,
-10._CKD))) )
35(
+5.9999999999999973,
-5.9999999999999964)
41exp(
getLogSubExp(smaller
= log((
4._CKH,
-4._CKH)), larger
= log((
10._CKH,
-10._CKH))) )
42(
+6.00000000000000000000000000000000000,
-5.99999999999999999999999999999999923)
- Benchmarks:
Benchmark :: The effects of the control
argument on runtime efficiency ⛓
- The following program compares the runtime performance of getLogSubExp algorithm with and without checking for underflows.
4 use iso_fortran_env,
only:
error_unit
12 integer(IK) :: fileUnitN
13 integer(IK) :: fileUnitU
14 integer(IK) ,
parameter :: NARR
= 11_IK
15 integer(IK) ,
parameter :: NBENCH
= 2_IK
16 integer(IK) :: arraySize(NARR)
17 real(RK) :: dummySum
= 0._RK
19 real(RK) ,
allocatable :: Larger(:)
20 real(RK) ,
allocatable :: Smaller(:)
21 real(RK) ,
allocatable :: LogSubExp(:)
22 type(bench_type) :: bench(
2,NBENCH)
23 logical(LK) :: underflowEnabled
25 bench(
1:
2,
1)
= bench_type(name
= SK_
"getLogSubExpSequence ", exec
= getLogSubExpSequence , overhead
= setOverhead)
26 bench(
1:
2,
2)
= bench_type(name
= SK_
"getLogSubExpSelection ", exec
= getLogSubExpSelection , overhead
= setOverhead)
28 arraySize
= [(
2_IK**iarr, iarr
= 1_IK, NARR )]
30 write(
*,
"(*(g0,:,' '))")
31 write(
*,
"(*(g0,:,' '))")
"getLogSubExp(...) vs. getLogSubExp(..., control) vs. direct method."
32 write(
*,
"(*(g0,:,' '))")
34 open(newunit
= fileUnitN, file
= "main.normal.out", status
= "replace")
35 open(newunit
= fileUnitU, file
= "main.underflow.out", status
= "replace")
37 write(fileUnitN,
"(*(g0,:,','))")
"arraySize", (bench(
1,i)
%name, i
= 1, NBENCH)
38 write(fileUnitU,
"(*(g0,:,','))")
"arraySize", (bench(
2,i)
%name, i
= 1, NBENCH)
40 loopOverArraySize:
do iarr
= 1, NARR
42 allocate(Larger(arraySize(iarr)))
43 allocate(Smaller(arraySize(iarr)))
44 allocate(LogSubExp(arraySize(iarr)))
45 write(
*,
"(*(g0,:,' '))")
"Benchmarking with array size", arraySize(iarr)
47 underflowEnabled
= .false._LK
49 bench(
1,i)
%timing
= bench(
1,i)
%getTiming(minsec
= 0.07_RK)
51 write(fileUnitN,
"(*(g0,:,','))") arraySize(iarr), (bench(
1,i)
%timing
%mean, i
= 1, NBENCH)
53 underflowEnabled
= .true._LK
55 bench(
2,i)
%timing
= bench(
2,i)
%getTiming(minsec
= 0.07_RK)
57 write(fileUnitU,
"(*(g0,:,','))") arraySize(iarr), (bench(
2,i)
%timing
%mean, i
= 1, NBENCH)
59 deallocate(LogSubExp, Smaller, Larger)
61 end do loopOverArraySize
62 write(
*,
"(*(g0,:,' '))") dummySum
63 write(
*,
"(*(g0,:,' '))")
74 subroutine setOverhead()
81 call random_number(Larger)
82 call random_number(Smaller)
83 if (underflowEnabled)
then
84 Larger
= Larger
* (
maxexponent(
0._RK)
- minexponent(
0._RK))
+ minexponent(
0._RK)
85 Smaller
= Smaller
* (
maxexponent(
0._RK)
- minexponent(
0._RK))
+ minexponent(
0._RK)
91 dummySum
= dummySum
+ LogSubExp(
1)
94 subroutine getLogSubExpSequence()
101 subroutine getLogSubExpSelection()
Generate and return an object of type timing_type containing the benchmark timing information and sta...
Return the minimum and maximum of the two input scalar values a and b in a and b, respectively,...
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 LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, 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())
16df = pd.read_csv(
"main.normal.out")
17colnames = 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
25 , df[colname].values / df[colnames[1]].values
30plt.xticks(fontsize = fontsize)
31plt.yticks(fontsize = fontsize)
32ax.set_xlabel(
"Array Size", fontsize = fontsize)
33ax.set_ylabel(
"Runtime Ratio ( W.R.T. cenabled = .false. )", fontsize = fontsize)
34ax.set_title(
"Performance when the input arrays\ncause no underflow instances.\nLower is better.", fontsize = fontsize)
38plt.grid(visible =
True, which =
"both", axis =
"both", color =
"0.85", linestyle =
"-")
39ax.tick_params(axis =
"y", which =
"minor")
40ax.tick_params(axis =
"x", which =
"minor")
41ax.legend ( colnames[1:]
48plt.savefig(
"benchmark." + dirname +
".normal.png")
54df = pd.read_csv(
"main.underflow.out")
55colnames = list(df.columns.values)
57ax = plt.figure(figsize = 1.25 * np.array([6.4,4.6]), dpi = 200)
60for colname
in colnames[1:]:
61 plt.plot( df[colnames[0]].values
63 , df[colname].values / df[colnames[1]].values
68plt.xticks(fontsize = fontsize)
69plt.yticks(fontsize = fontsize)
70ax.set_xlabel(
"Array Size", fontsize = fontsize)
71ax.set_ylabel(
"Runtime Ratio W.R.T. {})".format(colnames[1]), fontsize = fontsize)
72ax.set_title(
"Performance when the input arrays\ncause many underflow instances.\nLower is better.", fontsize = fontsize)
76plt.grid(visible =
True, which =
"both", axis =
"both", color =
"0.85", linestyle =
"-")
77ax.tick_params(axis =
"y", which =
"minor")
78ax.tick_params(axis =
"x", which =
"minor")
79ax.legend ( colnames[1:]
86plt.savefig(
"benchmark." + dirname +
".underflow.png")
Visualization of the benchmark output ⛓
Benchmark moral ⛓
- If the ratio of the
smaller
to larger
input arguments causes frequent underflows, then setting control =
selection to allow branching will likely result in a faster runtime.
Conversely, if the divisions are not expected to cause any or too many underflows, then removing the input argument control
when calling getLogSubExp will lead to a better runtime performance (at the expense of occasional expensive but redundant exponentiation operations).
- If the procedure getLogSubExp is to be called billions of times, then it would make sense to manually inline the procedure implementation in your code as procedure call and processing of optional arguments will have a non-negligible performance overhead.
- Test:
- test_pm_mathLogSubExp
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 166 of file pm_mathLogSubExp.F90.