10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-07-17 16:33:28 +02:00
QCaml/linear_algebra/lib/matrix.mli

263 lines
9.8 KiB
OCaml
Raw Normal View History

2020-10-02 18:55:19 +02:00
(** Type for matrices. The ['a] and ['b] types are labels for the rows and columns. *)
2020-09-26 12:02:53 +02:00
2020-10-02 18:55:19 +02:00
type ('a,'b) t
val dim1: ('a,'b) t -> int
2020-09-26 12:02:53 +02:00
(** First dimension of the matrix *)
2020-10-02 18:55:19 +02:00
val dim2: ('a,'b) t -> int
2020-09-26 12:02:53 +02:00
(** Second dimension of the matrix *)
2020-10-02 18:55:19 +02:00
val make : int -> int -> float -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates a matrix initialized with the given value. *)
2020-10-02 18:55:19 +02:00
val make0 : int -> int -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates a zero-initialized matrix. *)
2020-10-02 18:55:19 +02:00
val create : int -> int -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates an uninitialized matrix. *)
2020-10-02 18:55:19 +02:00
val reshape : ('a,'b) t -> int -> int -> ('c,'d) t
2020-09-26 12:02:53 +02:00
(** Changes the dimensions of the matrix *)
2020-10-02 18:55:19 +02:00
val init_cols : int -> int -> (int -> int -> float) -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates an uninitialized matrix. *)
2020-10-02 18:55:19 +02:00
val identity: int -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates an identity matrix. *)
2020-10-02 18:55:19 +02:00
val fill_inplace: ('a,'b) t -> float -> unit
2020-09-26 12:02:53 +02:00
(** Fills the matrix with the give value. *)
2020-10-02 18:55:19 +02:00
val add_const_diag : float -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Adds a constant to the diagonal *)
2020-10-02 18:55:19 +02:00
val add_const_diag_inplace : float -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Adds a constant to the diagonal *)
2020-10-02 18:55:19 +02:00
val add_const_inplace : float -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Adds a constant to the diagonal *)
2020-10-02 18:55:19 +02:00
val add_const : float -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Adds a constant to the diagonal *)
2020-10-02 18:55:19 +02:00
val add : ('a,'b) t -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Adds two matrices *)
2020-10-02 18:55:19 +02:00
val sub : ('a,'b) t -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Subtracts two matrices *)
2020-10-02 18:55:19 +02:00
val mul : ('a,'b) t -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Multiplies two matrices element-wise *)
2020-10-02 18:55:19 +02:00
val div : ('a,'b) t -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Divides two matrices element-wise *)
2020-10-02 18:55:19 +02:00
val add_inplace : c:('a,'b) t -> ('a,'b) t -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** [add_inplace c a b] : performs [c = a+b] in-place. *)
2020-10-02 18:55:19 +02:00
val sub_inplace : c:('a,'b) t -> ('a,'b) t -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** [sub_inplace c a b] : performs [c = a+b] in-place. *)
2020-10-02 18:55:19 +02:00
val mul_inplace : c:('a,'b) t -> ('a,'b) t -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** [mul_inplace c a b] : performs [c = a*b] element-wise in-place. *)
2020-10-02 18:55:19 +02:00
val div_inplace : c:('a,'b) t -> ('a,'b) t -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** [div_inplace c a b] : performs [c = a/b] element-wise in-place. *)
2020-10-02 18:55:19 +02:00
val at : ('a,'b) t -> int -> int -> float
2020-09-26 12:02:53 +02:00
(** [at i j] returns the element at i,j. *)
2020-10-02 18:55:19 +02:00
val to_bigarray : ('a,'b) t -> (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t
2020-09-26 12:02:53 +02:00
(** Converts the matrix into a Bigarray in Fortran layout *)
val to_bigarray_inplace :
2020-10-02 18:55:19 +02:00
('a,'b) t -> (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t
2020-09-26 12:02:53 +02:00
(** Converts the matrix into a Bigarray in Fortran layout in place*)
2020-10-02 18:55:19 +02:00
val to_col_vecs : ('a,'b) t -> 'a Vector.t array
2020-09-26 12:02:53 +02:00
(** Converts the matrix into an array of vectors *)
2020-10-02 18:55:19 +02:00
val to_col_vecs_list : ('a,'b) t -> 'a Vector.t list
2020-09-26 12:02:53 +02:00
(** Converts the matrix into a list of vectors *)
2020-10-02 18:55:19 +02:00
val of_col_vecs : 'a Vector.t array -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Converts an array of vectors into a matrix *)
2020-10-02 18:55:19 +02:00
val of_col_vecs_list : 'a Vector.t list -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Converts a list of vectors into a matrix *)
2020-10-02 18:55:19 +02:00
val of_bigarray : (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Converts a [Bigarray.Array2] in Fortran layout into a matrix *)
val of_bigarray_inplace :
2020-10-02 18:55:19 +02:00
(float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Converts a [Bigarray.Array2] in Fortran layout into a matrix in place*)
2020-10-02 18:55:19 +02:00
val copy: ?m:int -> ?n:int -> ?br:int -> ?bc:int -> ?ar:int -> ?ac:int -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Copies all or part of a two-dimensional matrix A to a new matrix B *)
2020-10-02 18:55:19 +02:00
val copy_inplace: ?m:int -> ?n:int -> ?br:int -> ?bc:int -> b:('a,'b) t -> ?ar:int -> ?ac:int -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Copies all or part of a two-dimensional matrix A to an existing matrix B *)
2020-10-02 18:55:19 +02:00
val col: ('a,'b) t -> int -> 'a Vector.t
2020-09-26 12:02:53 +02:00
(** Returns a column of the matrix as a vector *)
2020-10-02 18:55:19 +02:00
val detri: ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Takes an upper-triangular matrix, and makes it a symmetric matrix
by mirroring the defined triangle along the diagonal. *)
2020-10-02 18:55:19 +02:00
val detri_inplace: ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Takes an upper-triangular matrix, and makes it a symmetric matrix
by mirroring the defined triangle along the diagonal. *)
2020-10-02 18:55:19 +02:00
val as_vec_inplace: ('a,'b) t -> ('a*'b) Vector.t
2020-09-26 12:02:53 +02:00
(** Interpret the matrix as a vector (reshape). *)
2020-10-02 18:55:19 +02:00
val as_vec: ('a,'b) t -> ('a*'b) Vector.t
2020-09-26 12:02:53 +02:00
(** Return a copy of the reshaped matrix into a vector *)
2020-10-02 18:55:19 +02:00
val random: ?rnd_state:Random.State.t -> ?from:float -> ?range:float -> int -> int -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Creates a random matrix, similarly to [Vector.random] *)
2020-10-02 18:55:19 +02:00
val map: (float -> float) -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Apply the function to all elements of the matrix *)
2020-10-02 18:55:19 +02:00
val map_inplace: (float -> float) -> b:('a,'b) t -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** [map_inplace f b a] : Apply the function to all elements of the
matrix [a] and store the results in [b] *)
2020-10-02 18:55:19 +02:00
val scale: float -> ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Multiplies the matrix by a constant *)
2020-10-02 18:55:19 +02:00
val scale_inplace: float -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Multiplies the matrix by a constant *)
2020-10-02 18:55:19 +02:00
val scale_cols: ('a,'b) t -> 'b Vector.t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Multiplies the matrix by a constant *)
2020-10-02 18:55:19 +02:00
val scale_cols_inplace: ('a,'b) t -> 'b Vector.t -> unit
2020-09-26 12:02:53 +02:00
(** Multiplies the matrix by a constant *)
2020-10-02 18:55:19 +02:00
val sycon: ('a,'b) t -> float
2020-09-26 12:02:53 +02:00
(** Returns the condition number of a matrix *)
2020-10-02 18:55:19 +02:00
val outer_product : ?alpha:float -> 'a Vector.t -> 'b Vector.t -> ('a,'b) t
2020-09-26 16:45:52 +02:00
(** Computes M = %{ $\alpha u.v^t$ %} *)
2020-10-02 18:55:19 +02:00
val outer_product_inplace : ('a,'b) t -> ?alpha:float -> 'a Vector.t -> 'b Vector.t -> unit
2020-09-26 16:45:52 +02:00
(** Computes M = %{ $\alpha u.v^t$ %} *)
2020-10-02 18:55:19 +02:00
val gemm_inplace : ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
c:('a,'b) t -> ?transa:[`N | `T] -> ?alpha:float ->
('c,'d) t -> ?transb:[`N | `T] -> ('e,'f) t -> unit
2020-09-26 16:45:52 +02:00
(** Performs the Lapack GEMM operation. Default values:
2020-09-26 12:02:53 +02:00
[beta=0.] [transa=`N] [alpha=1.0] [transb=`N].
[gemm ~beta c ~alpha a b]: %{ $C = \beta C + \alpha A B$ *)
2020-10-02 18:55:19 +02:00
val gemm_nn_inplace : ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
c:('a,'c) t -> ?alpha:float -> ('a,'b) t -> ('b,'c) t -> unit
(** Performs gemm with [~transa=`N] and [~transb=`N]. *)
val gemm_nt_inplace : ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
c:('a,'c) t -> ?alpha:float -> ('a,'b) t -> ('c,'b) t -> unit
(** Performs gemm with [~transa=`N] and [~transb=`T]. *)
val gemm_tt_inplace : ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
c:('a,'c) t -> ?alpha:float -> ('b,'a) t -> ('c,'b) t -> unit
(** Performs gemm with [~transa=`T] and [~transb=`T]. *)
val gemm_tn_inplace : ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
c:('a,'c) t -> ?alpha:float -> ('b,'a) t -> ('b,'c) t -> unit
(** Performs gemm with [~transa=`T] and [~transb=`N]. *)
val gemm: ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
?c:('a,'b) t -> ?transa:[`N | `T] -> ?alpha:float ->
('c,'d) t -> ?transb:[`N | `T] -> ('e,'f) t -> ('a,'b) t
2020-09-26 16:45:52 +02:00
(** Performs the Lapack GEMM operation. Default values:
2020-09-26 12:02:53 +02:00
[beta=0.] [transa=`N] [alpha=1.0] [transb=`N]
[gemm ~beta ~alpha a b]: %{ $C = \beta C + \alpha A B$ *)
2020-10-02 18:55:19 +02:00
val gemm_nn: ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
?c:('a,'c) t -> ?alpha:float -> ('a,'b) t -> ('b,'c) t -> ('a,'c) t
(** Performs gemm with [~transa=`N] and [~transb=`N]. *)
val gemm_nt: ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
?c:('a,'c) t -> ?alpha:float -> ('a,'b) t -> ('c,'b) t -> ('c,'a) t
(** Performs gemm with [~transa=`N] and [~transb=`T]. *)
val gemm_tn: ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
?c:('a,'c) t -> ?alpha:float -> ('b,'a) t -> ('b,'c) t -> ('a,'c) t
(** Performs gemm with [~transa=`T] and [~transb=`N]. *)
val gemm_tt: ?m:int -> ?n:int -> ?k:int -> ?beta:float ->
?c:('a,'c) t -> ?alpha:float -> ('b,'a) t -> ('c,'b) t -> ('c,'b) t
(** Performs gemm with [~transa=`T] and [~transb=`T]. *)
val gemm_trace: ?transa:[`N | `T] -> ('a,'b) t -> ?transb:[`N | `T] -> ('c,'d) t -> float
2020-09-26 16:45:52 +02:00
(** Computes the trace of a GEMM. Default values:
[transa=`N] [transb=`N]
[gemm_trace a b]: %{ $C = Tr(A B)$ *)
2020-10-02 18:55:19 +02:00
val gemm_nn_trace: ('a,'b) t -> ('b,'c) t -> float
(** Computes the trace of a GEMM with [~transa=`N] and [~transb=`N]. *)
val gemm_nt_trace: ('a,'b) t -> ('c,'b) t -> float
(** Computes the trace of a GEMM with [~transa=`N] and [~transb=`T]. *)
val gemm_tn_trace: ('b,'a) t -> ('b,'c) t -> float
(** Computes the trace of a GEMM with [~transa=`T] and [~transb=`N]. *)
val gemm_tt_trace: ('b,'a) t -> ('c,'b) t -> float
(** Computes the trace of a GEMM with [~transa=`T] and [~transb=`T]. *)
2020-09-26 12:02:53 +02:00
2020-10-02 18:55:19 +02:00
val svd: ('a,'b) t -> ('a,'b) t * 'b Vector.t * ('b,'b) t
(** Singular value decomposition of A(m,n) when m >= n. *)
val svd_t: ('a,'b) t -> ('a,'a) t * 'a Vector.t * ('a,'b) t
(** Singular value decomposition of A(m,n) when m < n. *)
val qr: ('a,'b) t -> ('a,'b) t * ('b,'b) t
2020-09-26 12:02:53 +02:00
(** QR factorization *)
2020-10-02 18:55:19 +02:00
val normalize_mat : ('a,'b) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Returns the matrix with all the column vectors normalized *)
2020-10-02 18:55:19 +02:00
val normalize_mat_inplace : ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Returns the matrix with all the column vectors normalized *)
2020-10-02 18:55:19 +02:00
val diagonalize_symm : ('a,'a) t -> ('a,'a) t * 'a Vector.t
2020-09-26 12:02:53 +02:00
(** Diagonalize a symmetric matrix. Returns the eigenvectors and the eigenvalues. *)
2020-10-02 18:55:19 +02:00
val xt_o_x : o:('a,'a) t -> x:('a,'b) t -> ('b,'b) t
2020-09-26 12:02:53 +02:00
(** Computes {% $\mathbf{X^\dag\, O\, X}$ %} *)
2020-10-02 18:55:19 +02:00
val x_o_xt : o:('b,'b) t -> x:('a,'b) t -> ('a,'a) t
2020-09-26 12:02:53 +02:00
(** Computes {% $\mathbf{X\, O\, X^\dag}$ %} *)
2020-10-02 18:55:19 +02:00
val debug_matrix: string -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00
(** Prints a matrix in stdout for debug *)
2020-10-02 18:55:19 +02:00
val matrix_of_file : string -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Reads a matrix from a file with format "%d %d %f" corresponding to
[i, j, A.{i,j}]. *)
2020-10-02 18:55:19 +02:00
val relabel : ('a,'b) t -> ('c,'d) t
val sym_matrix_of_file : string -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Reads a symmetric matrix from a file with format "%d %d %f" corresponding to
[i, j, A.{i,j}]. *)
2020-10-02 18:55:19 +02:00
val sysv_inplace : b:('a,'b) t -> ('a,'a) t -> unit
2020-09-26 12:02:53 +02:00
(** Solves %{ $AX=B$ %} when A is symmetric, and stores the result in B. *)
2020-10-02 18:55:19 +02:00
val sysv : b:('a,'b) t -> ('a,'a) t -> ('a,'b) t
2020-09-26 12:02:53 +02:00
(** Solves %{ $AX=B$ %} when A is symmetric *)
2020-10-02 18:55:19 +02:00
val pp : Format.formatter -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00