10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-22 20:33:36 +01:00
QCaml/common/lib/util.mli
2024-01-17 10:30:24 +01:00

158 lines
5.0 KiB
OCaml

(** Error function *)
external erf_float : float -> float
= "erf_float_bytecode" "erf_float" [@@unboxed] [@@noalloc]
external erfc_float : float -> float
= "erfc_float_bytecode" "erfc_float" [@@unboxed] [@@noalloc]
external gamma_float : float -> float
= "gamma_float_bytecode" "gamma_float" [@@unboxed] [@@noalloc]
(** Bit manipulation functions *)
val popcnt : int64 -> int
val trailz : int64 -> int
val leadz : int64 -> int
(** General functions *)
val fact : int -> float
(** Factorial function.
* @raise Invalid_argument for negative arguments or arguments >100. *)
val binom : int -> int -> int
(** Binomial coefficient. ~binom n k~ = $C_n^k$ *)
val binom_float : int -> int -> float
(** Binomial coefficient (float). ~binom n k~ = $C_n^k$ *)
val chop : float -> (unit -> float) -> float
(** In ~chop a f~, evaluate ~f~ only if the absolute value of ~a~ is larger than ~Constants.epsilon~, and return ~a *. f ()~. *)
val pow : float -> int -> float
(** Fast implementation of the power function for small integer powers *)
val float_of_int_fast : int -> float
(** Faster implementation of float_of_int for small positive ints *)
val of_some : 'a option -> 'a
(** Extracts the value of an option *)
exception Not_implemented of string
val not_implemented : string -> 'a
(** Fails if some functionality is not implemented
* @raise Not_implemented. *)
(** Functions related to the Boys function *)
val incomplete_gamma : alpha:float -> float -> float
(** The lower [[https://en.wikipedia.org/wiki/Incomplete_gamma_function][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].
*
* @raise Failure when the calculation doesn't converge. *)
val boys_function : maxm:int -> float -> float array
(** Functions related to the Boys function:3 ends here
The [[https://link.springer.com/article/10.1007/s10910-005-9023-3][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
(** Filters out all ~None~ elements of the list, and returns the elements without the ~Some~ *)
val list_range : int -> int -> int list
(** Creates a list of consecutive integers *)
val list_pack : int -> 'a list -> 'a list list
(** ~list_pack n l~ Creates a list of ~n~-elements lists *)
(** Array functions *)
val array_range : int -> int -> int array
(** Creates an array of consecutive integers *)
val array_sum : float array -> float
(** Returns the sum of all the elements of the array *)
val array_product : float array -> float
(** Returns the product of all the elements of the array *)
(** Seq functions *)
val seq_range : int -> int -> int Seq.t
(** Creates a sequence returning consecutive integers *)
val seq_to_list : 'a Seq.t -> 'a list
(** Read a sequence and put items in a list *)
val seq_fold : ('a -> 'b -> 'a) -> 'a -> 'b Seq.t -> 'a
(** Apply a fold to the elements of the sequence *)
(** Printers *)
(**
| ~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:
#+begin_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:
+++++------+--
#+end_example
*)
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