10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-23 04:43:32 +01:00
QCaml/common/util.org

21 KiB

Util

Utility functions.

External C functions

erf_float Error function erf from libm
erfc_float Complementary error function erfc from libm
gamma_float Gamma function gamma from libm
popcnt popcnt instruction
trailz ctz instruction
leadz bsf instruction

Erf

external erf_float : float -> float
= "erf_float_bytecode" "erf_float" [@@unboxed] [@@noalloc]

Erfc

external erfc_float : float -> float
= "erfc_float_bytecode" "erfc_float" [@@unboxed] [@@noalloc]

Gamma

external gamma_float : float -> float
= "gamma_float_bytecode" "gamma_float" [@@unboxed] [@@noalloc]

Popcnt

val popcnt : int64 -> int

Trailz

val trailz : int64 -> int

Leadz

val leadz : int64 -> int

Test

General functions

val fact : int -> float
(* @raise Invalid_argument for negative arguments or arguments >100. *)
val binom       : int -> int -> int
val binom_float : int -> int -> float

val chop              : float -> (unit -> float) -> float
val pow               : float -> int -> float
val float_of_int_fast : int -> float

val of_some : 'a option -> 'a

exception Not_implemented of string
val not_implemented : string -> 'a
(* @raise Not_implemented. *)
fact Factorial function.
binom Binomial coefficient. binom n k = $C_n^k$
binom_float float variant of binom
pow Fast implementation of the power function for small integer powers
chop In chop a f, evaluate f only if the absolute value of a is larger than Constants.epsilon, and return a *. f ().
float_of_int_fast Faster implementation of float_of_int for small positive ints
not_implemented Fails if some functionality is not implemented
of_some Extracts the value of an option

Functions related to the Boys function

val incomplete_gamma : alpha:float -> float -> float
(* @raise Failure when the calculation doesn't converge. *)

The lower Incomplete Gamma function is implemented : \[ \gamma(\alpha,x) = \int_0^x e^{-t} t^{\alpha-1} dt \]

p: $\frac{1}{\Gamma(\alpha)} \int_0^x e^{-t} t^{\alpha-1} dt$

q: $\frac{1}{\Gamma(\alpha)} \int_x^\infty e^{-t} t^{\alpha-1} dt$

Reference : Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten (New Algorithm handbook in C language) (Gijyutsu hyouron sha, Tokyo, 1991) p.227 [in Japanese]

val boys_function : maxm:int -> float -> float array

The Generalized Boys function is implemented, maxm is the maximum total angular momentum.

\[ F_m(x) = \frac{\gamma(m+1/2,x)}{2x^{m+1/2}} \] where $\gamma$ is the incomplete gamma function.

  • $F_0(0.) = 1$
  • $F_0(t) = \frac{\sqrt{\pi}}{2\sqrt{t}} \text{erf} ( \sqrt{t} )$
  • $F_m(0.) = \frac{1}{2m+1}$
  • $F_m(t) = \frac{\gamma{m+1/2,t}}{2t^{m+1/2}}$
  • $F_m(t) = \frac{ 2t\, F_{m+1}(t) + e^{-t} }{2m+1}$

List functions

val list_some  : 'a option list -> 'a list
val list_range : int -> int -> int list
val list_pack  : int -> 'a list -> 'a list list
list_some Filters out all None elements of the list, and returns the elements without the Some
list_range Creates a list of consecutive integers
list_pack list_pack n l Creates a list of n-elements lists

Array functions

val array_range   : int -> int -> int array
val array_sum     : float array -> float
val array_product : float array -> float
array_range Creates an array of consecutive integers
array_sum Returns the sum of all the elements of the array
array_product Returns the product of all the elements of the array

Seq functions

val seq_range   : int -> int -> int Seq.t
val seq_to_list : 'a Seq.t -> 'a list
val seq_fold    : ('a -> 'b -> 'a) -> 'a -> 'b Seq.t -> 'a
seq_range Creates a sequence returning consecutive integers
seq_to_list Read a sequence and put items in a list
seq_fold Apply a fold to the elements of the sequence

Printers

val pp_float_array_size   : Format.formatter -> float array -> unit
val pp_float_array        : Format.formatter -> float array -> unit
val pp_float_2darray_size : Format.formatter -> float array array -> unit
val pp_float_2darray      : Format.formatter -> float array array -> unit
val pp_bitstring          : int -> Format.formatter -> Z.t -> unit
pp_float_array Printer for float arrays
pp_float_array_size Printer for float arrays with size
pp_float_2darray Printer for matrices
pp_float_2darray_size Printer for matrices with size
pp_bitstring Printer for bit strings (used by Bitstring module)

Example:

pp_float_array_size:
[ 6:   1.000000   1.732051   1.732051   1.000000   1.732051   1.000000 ]

pp_float_array: 
[    1.000000   1.732051   1.732051   1.000000   1.732051   1.000000 ]

pp_float_2darray_size
[
2:[ 6:  1.000000   1.732051   1.732051   1.000000   1.732051   1.000000 ]
 [ 4:  1.000000   2.000000   3.000000   4.000000 ] ]

pp_float_2darray:
[ [   1.000000   1.732051   1.732051   1.000000   1.732051   1.000000 ]
[   1.000000   2.000000   3.000000   4.000000 ] ]

pp_bitstring 14:
+++++------+--