10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-14 10:03:39 +01:00
QCaml/linear_algebra/lib/matrix.mli

348 lines
13 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-12-07 13:52:56 +01:00
(** Creates a matrix initialized with the given value.
@param m: First dimension
@param n: Seconfd dimension
@param f: Value used to initialize the matrix elements
*)
2020-09-26 12:02:53 +02:00
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-18 01:58:22 +02:00
val reshape_inplace : int -> int -> ('a,'b) t -> ('c,'d) t
2020-09-26 12:02:53 +02:00
(** Changes the dimensions of the matrix *)
2020-10-18 01:58:22 +02:00
val reshape_vec_inplace : int -> int -> ('a*'b) Vector.t -> ('a,'b) t
(** Reshapres a vector into a 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. *)
val of_diag: 'a Vector.t -> ('a,'a) t
(** Creates a diagonal matrix. *)
val diag: ('a,'a) t -> 'a Vector.t
(** Returns the diagonal of a 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-18 01:58:22 +02:00
val amax : ('a,'b) t -> float
(** Maximum of the absolute values of the elements of the matrix. *)
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-03 15:40:06 +02:00
(*
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 *)
2020-10-03 15:40:06 +02:00
val of_bigarray : (float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t -> ('a,'b) t
(** Converts a [Bigarray.Array2] in Fortran layout into a matrix *)
*)
2020-09-26 12:02:53 +02:00
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-03 15:40:06 +02:00
val of_bigarray_inplace :
(float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.fortran_layout) Stdlib.Bigarray.Array2.t -> ('a,'b) t
(** Converts a [Bigarray.Array2] in Fortran layout into a matrix 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-18 01:58:22 +02:00
val to_array : ('a,'b) t -> float array array
(** Converts the matrix into an array of arrays *)
val of_array : float array array -> ('a,'b) t
(** Converts an array of arrays into a matrix *)
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-03 15:40:06 +02:00
(*
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-03 15:40:06 +02:00
*)
val col_inplace: ('a,'b) t -> int -> 'a Vector.t
(** Returns a column of the matrix as a vector *)
2020-09-26 12:02:53 +02:00
2020-12-16 12:03:20 +01:00
val transpose: ('a,'b) t -> ('b,'a) t
(** Returns the transposed matrix *)
val transpose_inplace: ('a,'a) t -> unit
(** Transposes the matrix in place. *)
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-12-17 19:52:09 +01:00
val scale_rows: 'a Vector.t -> ('a,'b) t -> ('a,'b) t
(** Multiplies the matrix by a constant *)
val scale_rows_inplace: 'a Vector.t -> ('a,'b) t -> unit
(** 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 gemv_n_inplace : ?m:int -> ?n:int -> ?beta:float -> 'a Vector.t ->
?alpha:float -> ?ar:int -> ?ac:int -> ('a,'b) t -> 'b Vector.t ->
unit
(** Performs the Lapack GEMV operation. Default values:
[beta=0.] [alpha=1.0].
[gemv ~beta y ~alpha m v]: %{ $Y = \beta Y + \alpha M V$
The vector Y is updated in-place.
*)
val gemv_t_inplace : ?m:int -> ?n:int -> ?beta:float -> 'b Vector.t ->
?alpha:float -> ?ar:int -> ?ac:int -> ('a,'b) t -> 'a Vector.t ->
unit
(** Performs the Lapack GEMV operation. Default values:
[beta=0.] [alpha=1.0].
[gemv ~beta y ~alpha m v]: %{ $Y = \beta Y + \alpha M^\dagger V$
The vector Y is updated in-place.
*)
val gemv_n : ?m:int -> ?n:int -> ?beta:float -> ?y:'a Vector.t ->
?alpha:float -> ?ar:int -> ?ac:int -> ('a,'b) t -> 'b Vector.t ->
'a Vector.t
(** Performs the Lapack GEMV operation. Default values:
[beta=0.] [alpha=1.0].
[gemv ~beta y ~alpha m v]: %{ $Y = \beta Y + \alpha M^\dagger V$ *)
val gemv_t : ?m:int -> ?n:int -> ?beta:float -> ?y:'b Vector.t ->
?alpha:float -> ?ar:int -> ?ac:int -> ('a,'b) t -> 'a Vector.t ->
'b Vector.t
(** Performs the Lapack GEMV operation. Default values:
[beta=0.] [alpha=1.0].
[gemv ~beta y ~alpha m v]: %{ $Y = \beta Y + \alpha M^\dagger V$
*)
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 ->
2020-10-18 01:58:22 +02:00
?c:('a,'c) t -> ?alpha:float -> ('a,'b) t -> ('c,'b) t -> ('a,'c) t
2020-10-02 18:55:19 +02:00
(** 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 ->
2020-10-18 01:58:22 +02:00
?c:('a,'c) t -> ?alpha:float -> ('b,'a) t -> ('c,'b) t -> ('a,'c) t
2020-10-02 18:55:19 +02:00
(** 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. *)
2020-10-03 15:40:06 +02:00
val svd': ('a,'b) t -> ('a,'a) t * 'a Vector.t * ('a,'b) t
2020-10-02 18:55:19 +02:00
(** 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-12-17 12:55:11 +01:00
val exponential : ('a,'a) t -> ('a,'a) t
(** Computes the exponential of a square matrix. *)
2020-12-17 19:52:09 +01:00
val exponential_iterative : ('a,'a) t -> ('a,'a) t
(** Computes the exponential of a square matrix with an iteratve algorithm. *)
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-18 01:58:22 +02:00
val 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
2020-10-18 01:58:22 +02:00
val sym_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 *)
val (%:) : ('a,'b) t -> (int*int) -> float
(** [t%.(i,j)] returns the element at i,j. *)
val set : ('a,'b) t -> int -> int -> float -> unit
(** [set t i j v] sets the (i,j)-th element to v *)
2020-10-19 18:33:02 +02:00
val to_file : filename:string -> ?sym:bool -> ?cutoff:float -> ('a,'b) t -> unit
(** Write the matrix to a file. *)
2020-10-02 18:55:19 +02:00
val pp : Format.formatter -> ('a,'b) t -> unit
2020-09-26 12:02:53 +02:00