10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-07 14:43:41 +01:00
QCaml/linear_algebra/lib/conventions.ml

31 lines
595 B
OCaml
Raw Normal View History

2020-10-03 15:46:21 +02:00
(** Contains the conventions relative to the program.
The phase convention is given by:
{% $\sum_i c_i > 0$ %} or {% $\min_i c_i \ge 0$ %}
*)
open Vector
2020-10-03 15:46:21 +02:00
let in_phase vec =
let s = Vector.sum vec in
if s = 0. then
let rec first_non_zero k =
if k > Vector.dim vec then
k-1
else if vec%.(k) = 0. then
2020-10-03 15:46:21 +02:00
first_non_zero (k+1)
else k
in
let k = first_non_zero 1 in
vec%.(k) >= 0.
2020-10-03 15:46:21 +02:00
else
s > 0.
let rephase mat =
Matrix.to_col_vecs mat
|> Array.map (fun v -> if in_phase v then v else Vector.neg v)
|> Matrix.of_col_vecs