10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-07-11 13:53:36 +02:00
QCaml/linear_algebra/lib/orthonormalization.ml

30 lines
713 B
OCaml
Raw Normal View History

2020-10-02 18:55:19 +02:00
let canonical_ortho ?thresh:(thresh=1.e-6) ~overlap c =
2020-09-26 12:02:53 +02:00
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
2020-10-02 15:49:09 +02:00
let dx = Vector.to_bigarray d in
2020-09-26 12:02:53 +02:00
if n < Vector.dim d_sqrt then
2020-10-02 15:49:09 +02:00
Printf.printf "Removed linear dependencies below %f\n" (1. /. dx.{n})
2020-09-26 12:02:53 +02:00
;
Matrix.scale_cols_inplace u d_inv_sq ;
2020-10-02 18:55:19 +02:00
Matrix.gemm_nn u c
2020-09-26 12:02:53 +02:00
let qr_ortho m =
(* Performed twice for precision *)
let q, _ = Matrix.qr m in
let q, _ = Matrix.qr q in
q