10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-09 07:33:40 +01:00
QCaml/linear_algebra/lib/orthonormalization.ml
2020-09-26 12:02:53 +02:00

28 lines
672 B
OCaml

let canonical_ortho ?thresh:(thresh=1.e-6) ~overlap c =
let u, d, _ = Matrix.svd overlap in
let d_sqrt = Vector.sqrt d in
let n = (* Number of non-negligible singular vectors *)
Vector.fold (fun accu x -> if x > thresh then accu + 1 else accu) 0 d
in
let d_inv_sq = (* D^{-1/2} *)
Vector.map (fun x ->
if x >= thresh then 1. /. x
else 0. ) d_sqrt
in
if n < Vector.dim d_sqrt then
Printf.printf "Removed linear dependencies below %f\n" (1. /. d.{n})
;
Matrix.scale_cols_inplace u d_inv_sq ;
Matrix.gemm c u
let qr_ortho m =
(* Performed twice for precision *)
let q, _ = Matrix.qr m in
let q, _ = Matrix.qr q in
q