From b7add6b2b6edfc7a22d55675ff4edb5922f9a02d Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 16 Dec 2020 12:03:20 +0100 Subject: [PATCH] Added transpose --- linear_algebra/lib/matrix.ml | 19 ++++++++++++++----- linear_algebra/lib/matrix.mli | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/linear_algebra/lib/matrix.ml b/linear_algebra/lib/matrix.ml index b30c853..74095a3 100644 --- a/linear_algebra/lib/matrix.ml +++ b/linear_algebra/lib/matrix.ml @@ -68,12 +68,21 @@ let col_inplace t j = Mat.col t j |> Vector.of_bigarray_inplace -(* -let col t j = - Mat.col t j - |> Vector.of_bigarray -*) +let transpose t = + Mat.transpose_copy t +let transpose_inplace t = + let mat = to_bigarray_inplace t in + let d1 = dim1 t in + let d2 = dim2 t in + assert (d1 = d2); + for j=1 to d1 do + for i=1 to (j-1) do + let ij, ji = mat.{i,j}, mat.{j,i} in + mat.{i,j} <- ji; + mat.{j,i} <- ij + done; + done let to_col_vecs t = Mat.to_col_vecs t diff --git a/linear_algebra/lib/matrix.mli b/linear_algebra/lib/matrix.mli index c99c0f8..46f3090 100644 --- a/linear_algebra/lib/matrix.mli +++ b/linear_algebra/lib/matrix.mli @@ -129,6 +129,12 @@ val col: ('a,'b) t -> int -> 'a Vector.t val col_inplace: ('a,'b) t -> int -> 'a Vector.t (** Returns a column of the matrix as a vector *) +val transpose: ('a,'b) t -> ('b,'a) t +(** Returns the transposed matrix *) + +val transpose_inplace: ('a,'a) t -> unit +(** Transposes the matrix in place. *) + val detri: ('a,'b) t -> ('a,'b) t (** Takes an upper-triangular matrix, and makes it a symmetric matrix by mirroring the defined triangle along the diagonal. *)