ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_mathFactorial::getLogFactorial Interface Reference

Generate and return the natural logarithm of the factorial of the input positive whole real number. More...

Detailed Description

Generate and return the natural logarithm of the factorial of the input positive whole real number.

The factorial of an integer number \(n\) is defined as,

\begin{equation} \large n! = \prod_{i=1}^{n} i = \Gamma(n+1) ~. \end{equation}

Therefore,

\begin{equation} \large \log(n!) = \sum_{i=1}^{n} i = \log\bigg(\Gamma(n+1)\bigg) ~. \end{equation}

Note that the factorial of a number can readily overflow the maximum integer or even real values representable by computers. This is the primary reason for computing the natural logarithm of the factorial instead of the factorial.

Parameters
[in]x: The input scalar or array of arbitrary rank of type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128) containing the whole number (integer) whose \(\log(x!)\) is to be computed on return.
Returns
logFactorial : The output scalar or array of the same shape as the input x representing the natural logarithm of the factorial of x.


Possible calling interfaces

logFactorial = getLogFactorial(x)
Generate and return the natural logarithm of the factorial of the input positive whole real number.
This module contains procedures and generic interfaces for the Factorial function.
Warning
The input x must be a positive whole number.
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.
Remarks
The procedures under discussion are elemental.
See also
getFactorial


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_kind, only: RKS, RKD, RKH
5 use pm_io, only: display_type
7
8 implicit none
9
10 integer(IK) , parameter :: NP = 1000_IK
11 real(RKH) :: gamIncLow_RKH, x_RKH, shape_RKH
12 real(RKD) :: gamIncLow_RKD, x_RKD, shape_RKD
13 real(RKS) :: gamIncLow_RKS, x_RKS, shape_RKS
14 logical(LK) :: failed
15
16 type(display_type) :: disp
17 disp = display_type(file = "main.out.F90")
18
19 call disp%skip()
20 call disp%show("exp(getLogFactorial(x = 3._RKS))")
21 call disp%show( exp(getLogFactorial(x = 3._RKS)) )
22 call disp%skip()
23
24 call disp%skip()
25 call disp%show("exp(getLogFactorial(x = 4._RKD))")
26 call disp%show( exp(getLogFactorial(x = 4._RKD)) )
27 call disp%skip()
28
29 call disp%skip()
30 call disp%show("exp(getLogFactorial(x = 5._RKH))")
31 call disp%show( exp(getLogFactorial(x = 5._RKH)) )
32 call disp%skip()
33
34 call disp%skip()
35 call disp%show("exp(getLogFactorial(x = real([3., 5., 7., 10.],RKS)))")
36 call disp%show( exp(getLogFactorial(x = real([3., 5., 7., 10.],RKS))) )
37 call disp%skip()
38
39 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 ! Output an example array of the regularized Lower Incomplete Gamma function for visualization.
41 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
43 block
44
45 use pm_arrayRange, only: getRange
46 real(RKS), allocatable :: WholeNumber(:)
47 integer :: fileUnit, i
48
49 WholeNumber = getRange(start = 0_IK, stop = 30_IK, step = 5_IK)
50 open(newunit = fileUnit, file = "getLogFactorial.RK.txt")
51 write(fileUnit,"(2(g0,:,' '))") (WholeNumber(i), exp(getLogFactorial(WholeNumber(i))), i = 1, size(WholeNumber))
52 close(fileUnit)
53
54 end block
55
56end program example
Generate minimally-spaced character, integer, real sequences or sequences at fixed intervals of size ...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
This module contains procedures and generic interfaces for generating ranges of discrete character,...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
Definition: pm_kind.F90:568
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
Definition: pm_kind.F90:858
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2exp(getLogFactorial(x = 3._RKS))
3+6.00000000
4
5
6exp(getLogFactorial(x = 4._RKD))
7+24.000000000000004
8
9
10exp(getLogFactorial(x = 5._RKH))
11+119.999999999999999999999999999999963
12
13
14exp(getLogFactorial(x = real([3., 5., 7., 10.],RKS)))
15+6.00000000, +120.000008, +5040.00195, +3628801.75
16
17

Postprocessing of the example output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6import glob
7import sys
8
9fontsize = 17
10
11marker ={ "CK" : "*"
12 , "IK" : "*"
13 , "RK" : "*"
14 }
15linestyle = { "CK" : "-"
16 , "IK" : "-"
17 , "RK" : "-"
18 }
19xlab = { "CK" : r"Whole Complex Number: N"
20 , "IK" : r"Whole Integer Number: N"
21 , "RK" : r"Whole Real Number: N"
22 }
23
24for kind in ["IK", "CK", "RK"]:
25
26 pattern = "*." + kind + ".txt"
27 fileList = glob.glob(pattern)
28 if len(fileList) == 1:
29
30 df = pd.read_csv(fileList[0], delimiter = " ")
31
32 fig = plt.figure(figsize = 1.25 * np.array([6.4, 4.8]), dpi = 200)
33 ax = plt.subplot()
34
35 if kind == "CK":
36 plt.plot( df.values[:, 0]
37 , df.values[:,2]
38 , marker = marker[kind]
39 , linestyle = linestyle[kind]
40 , color = "r"
41 )
42 plt.plot( df.values[:, 1]
43 , df.values[:,3]
44 , marker = marker[kind]
45 , linestyle = linestyle[kind]
46 , color = "blue"
47 )
48 else:
49 plt.plot( df.values[:, 0]
50 , df.values[:, 1]
51 , marker = marker[kind]
52 , linestyle = linestyle[kind]
53 , color = "r"
54 )
55
56 plt.xticks(fontsize = fontsize - 2)
57 plt.yticks(fontsize = fontsize - 2)
58 ax.set_xlabel(xlab[kind], fontsize = fontsize)
59 ax.set_ylabel(r"Factorial: $N!$", fontsize = fontsize)
60 ax.set_yscale("log")
61
62 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
63 ax.tick_params(axis = "y", which = "minor")
64 ax.tick_params(axis = "x", which = "minor")
65
66 plt.savefig(fileList[0].replace(".txt",".png"))
67
68 elif len(fileList) > 1:
69
70 sys.exit("Ambiguous file list exists.")

Visualization of the example output
Test:
test_pm_mathFactorial


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.

  1. 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.
  2. 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.

Author:
Amir Shahmoradi, April 23, 2017, 1:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin

Definition at line 244 of file pm_mathFactorial.F90.


The documentation for this interface was generated from the following file: