10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-09 07:33:40 +01:00
QCaml/Utils/Util.mli

216 lines
5.9 KiB
OCaml
Raw Normal View History

2019-02-27 14:56:59 +01:00
open Lacaml.D
2018-02-24 23:57:38 +01:00
(** All utilities which should be included in all source files are defined here *)
2018-03-06 18:25:48 +01:00
(** {2 Functions from libm} *)
2018-02-24 23:57:38 +01:00
external erf_float : float -> float = "erf_float_bytecode" "erf_float"
[@@unboxed] [@@noalloc]
(** Error function [erf] from [libm] *)
external erfc_float : float -> float = "erfc_float_bytecode" "erfc_float"
[@@unboxed] [@@noalloc]
(** Complementary error function [erfc] from [libm] *)
external gamma_float : float -> float = "gamma_float_bytecode" "gamma_float"
[@@unboxed] [@@noalloc]
(** Gamma function [gamma] from [libm] *)
2019-10-24 11:25:49 +02:00
external vfork : unit -> int = "unix_vfork" "unix_vfork"
2019-03-03 01:43:04 +01:00
(*
external popcnt : int64 -> int32 = "popcnt_bytecode" "popcnt"
[@@unboxed] [@@noalloc]
(** popcnt instruction *)
external trailz : int64 -> int32 = "trailz_bytecode" "trailz"
[@@unboxed] [@@noalloc]
(** ctz instruction *)
*)
val popcnt : int64 -> int
(** popcnt instruction *)
val trailz : int64 -> int
(** ctz instruction *)
2019-03-26 01:20:17 +01:00
val leadz : int64 -> int
(** ctz instruction *)
2018-02-24 23:57:38 +01:00
2018-03-06 18:25:48 +01:00
(** {2 General functions} *)
2018-02-24 23:57:38 +01:00
val fact : int -> float
(** Factorial function.
@raise Invalid_argument for negative arguments or arguments >100.
*)
2019-02-19 17:36:07 +01:00
val binom : int -> int -> int
(** Binomial coefficient. [binom n k] {% $= C_n^k$ %}. *)
2020-02-17 19:45:53 +01:00
val binom_float : int -> int -> float
(** Binomial coefficient. [binom n k] {% $= C_n^k$ %}. *)
2018-02-24 23:57:38 +01:00
val pow : float -> int -> float
(** Fast implementation of the power function for small integer powers *)
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 ()].
*)
2020-02-17 19:45:53 +01:00
val float_of_int_fast : int -> float
(* Faster implementation of float_of_int for small positive ints *)
2018-02-24 23:57:38 +01:00
2020-02-17 19:45:53 +01:00
val of_some : 'a option -> 'a
2018-02-24 23:57:38 +01:00
2018-03-06 18:25:48 +01:00
(** {2 Functions related to the Boys function} *)
2018-02-24 23:57:38 +01:00
val incomplete_gamma : alpha:float -> float -> float
(** {{:https://en.wikipedia.org/wiki/Incomplete_gamma_function}
Lower incomplete gamma function}
@raise Failure when the calculation doesn't converge.
*)
val boys_function : maxm:int -> float -> float array
(** {{:https://link.springer.com/article/10.1007/s10910-005-9023-3}
Generalized Boys function}.
@param maxm Maximum total angular momentum.
*)
2018-03-06 18:25:48 +01:00
(** {2 Extension of the Array module} *)
2018-02-24 23:57:38 +01:00
2020-02-17 19:45:53 +01:00
val array_range : int -> int -> int array
(** [array_range first last] returns an array [| first; first+1 ; ... ; last-1 ; last |]. *)
2018-02-24 23:57:38 +01:00
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 *)
2018-03-22 00:29:14 +01:00
(** {2 Extension of the List module} *)
2018-05-30 18:07:05 +02:00
2018-03-22 00:29:14 +01:00
val list_some : 'a option list -> 'a list
2018-05-30 18:07:05 +02:00
(** Filters out all [None] elements of the list, and returns the elements without
the [Some]. *)
2018-03-22 00:29:14 +01:00
2019-02-20 18:15:15 +01:00
val list_range : int -> int -> int list
(** [list_range first last] returns a list [first; first+1 ; ... ; last-1 ; last ]. *)
2018-07-20 16:09:06 +02:00
2019-02-27 14:56:59 +01:00
val list_pack : int -> 'a list -> 'a list list
(** Example:
{[
list_pack 3 [ 1; 2; 3; ...; 18; 19; 20 ] =
[[1; 2; 3]; [4; 5; 6]; [7; 8; 9]; [10; 11; 12]; [13; 14; 15];
[16; 17; 18]; [19; 20]]
]}
*)
2018-06-28 14:43:24 +02:00
(** {2 Useful streams} *)
2019-02-20 18:15:15 +01:00
val stream_range : int -> int -> int Stream.t
(** [stream_range first last] returns a stream <first ; first+1 ; ... ; last-1 ; last>. *)
2018-07-20 16:09:06 +02:00
2019-02-25 14:37:20 +01:00
val stream_to_list : 'a Stream.t -> 'a list
(** Read a stream and put items in a list. *)
2018-03-22 00:29:14 +01:00
2019-03-20 23:10:53 +01:00
val stream_fold : ('a -> 'b -> 'a) -> 'a -> 'b Stream.t -> 'a
(** Apply a fold to the elements of the stream. *)
2018-03-06 18:25:48 +01:00
(** {2 Linear algebra } *)
2018-02-24 23:57:38 +01:00
2019-02-27 14:56:59 +01:00
val normalize : Vec.t -> Vec.t
(** Returns a the vector normalized *)
val normalize_mat : Mat.t -> Mat.t
(** Returns the matrix with all the column vectors normalized *)
val diagonalize_symm : Mat.t -> Mat.t * Vec.t
2018-02-24 23:57:38 +01:00
(** Diagonalize a symmetric matrix. Returns the eigenvectors and the eigenvalues. *)
2019-02-27 14:56:59 +01:00
val xt_o_x : o:Mat.t -> x:Mat.t -> Mat.t
2018-05-30 09:19:49 +02:00
(** Computes {% $\mathbf{X^\dag\, O\, X}$ %} *)
2018-02-24 23:57:38 +01:00
2019-02-27 14:56:59 +01:00
val x_o_xt : o:Mat.t -> x:Mat.t -> Mat.t
2019-02-26 11:58:53 +01:00
(** Computes {% $\mathbf{X\, O\, X^\dag}$ %} *)
2019-11-20 16:42:28 +01:00
val remove_epsilons : Mat.t -> Mat.t
(** Remove values below machine precision *)
2019-02-27 14:56:59 +01:00
val qr_ortho : Mat.t -> Mat.t
(** QR orthogonalization of the input matrix *)
val canonical_ortho: ?thresh:float -> overlap:Mat.t -> Mat.t -> Mat.t
2018-05-30 09:19:49 +02:00
(** Canonical orthogonalization. [overlap] is the overlap matrix {% $\mathbf{S}$ %},
and the last argument contains the vectors {% $\mathbf{C}$ %} to orthogonalize.
2018-05-30 18:07:05 +02:00
2018-05-30 09:19:49 +02:00
{%
2018-05-30 18:07:05 +02:00
$$
\mathbf{C_\bot} = \mathbf{C\, U\, D^{-1/2}}, \;
\mathbf{U\, D\, V^\dag} = \mathbf{S}
$$
2018-05-30 09:19:49 +02:00
%}
*)
2019-02-27 14:56:59 +01:00
val debug_matrix: string -> Mat.t -> unit
2018-05-30 01:15:45 +02:00
(** Prints a matrix in stdout for debug *)
2018-03-15 15:25:49 +01:00
2019-02-27 14:56:59 +01:00
val matrix_of_file : string -> Mat.t
2018-07-05 00:39:17 +02:00
(** Reads a matrix from a file with format "%d %d %f" corresponding to
[i, j, A.{i,j}]. *)
2019-02-27 14:56:59 +01:00
val sym_matrix_of_file : string -> Mat.t
2018-07-05 00:39:17 +02:00
(** Reads a symmetric matrix from a file with format "%d %d %f" corresponding to
[i, j, A.{i,j}]. *)
2019-02-19 17:36:07 +01:00
2018-03-15 15:25:49 +01:00
(** {2 Printers} *)
val pp_float_array_size : Format.formatter -> float array -> unit
(** Example:
{[
[ 6: 1.000000 1.732051 1.732051 1.000000 1.732051 1.000000
]
]}
*)
val pp_float_array : Format.formatter -> float array -> unit
(** Example:
{[
[ 1.000000 1.732051 1.732051 1.000000 1.732051 1.000000
]
]}
*)
2018-03-20 14:11:31 +01:00
val pp_float_2darray_size : Format.formatter -> float array array -> unit
(** Example:
{[
[
2:[ 6: 1.000000 1.732051 1.732051 1.000000 1.732051 1.000000 ]
[ 4: 1.000000 2.000000 3.000000 4.000000 ] ]
]}
*)
val pp_float_2darray : Format.formatter -> float array array -> unit
(** Example:
{[
[ [ 1.000000 1.732051 1.732051 1.000000 1.732051 1.000000 ]
[ 1.000000 2.000000 3.000000 4.000000 ] ]
]}
*)
2019-02-20 19:43:16 +01:00
val pp_bitstring : int -> Format.formatter -> Z.t -> unit
(** Example: [ pp_bitstring 14 ppf z -> +++++------+-- ] *)
2019-02-18 19:45:41 +01:00
2019-02-27 14:56:59 +01:00
val pp_matrix : Format.formatter -> Mat.t -> unit
2018-06-27 13:13:59 +02:00
2019-03-03 01:43:04 +01:00
(** {1 Unit tests} *)
val test_case : unit -> (string * [> `Quick ] * (unit -> unit)) list