10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-14 01:53:55 +01:00
quantum_package/ocaml/Symmetry.ml

174 lines
4.3 KiB
OCaml
Raw Normal View History

2014-08-27 16:38:13 +02:00
open Qptypes;;
open Core.Std;;
2014-08-13 17:54:32 +02:00
type t = S|P|D|F|G|H|I|J|K|L
let to_string = function
| S -> "S"
| P -> "P"
| D -> "D"
| F -> "F"
| G -> "G"
| H -> "H"
| I -> "I"
| J -> "J"
| K -> "K"
| L -> "L"
let of_string = function
| "S" -> S
| "P" -> P
| "D" -> D
| "F" -> F
| "G" -> G
| "H" -> H
| "I" -> I
| "J" -> J
| "K" -> K
| "L" -> L
| x -> raise (Failure ("Symmetry should be S|P|D|F|G|H|I|J|K|L,
not "^x^"."))
let of_char = function
| 'S' -> S
| 'P' -> P
| 'D' -> D
| 'F' -> F
| 'G' -> G
| 'H' -> H
| 'I' -> I
| 'J' -> J
| 'K' -> K
| 'L' -> L
| x -> raise (Failure ("Symmetry should be S|P|D|F|G|H|I|J|K|L"))
2014-08-27 16:38:13 +02:00
let to_l = function
| S -> Positive_int.of_int 0
| P -> Positive_int.of_int 1
| D -> Positive_int.of_int 2
| F -> Positive_int.of_int 3
| G -> Positive_int.of_int 4
| H -> Positive_int.of_int 5
| I -> Positive_int.of_int 6
| J -> Positive_int.of_int 7
| K -> Positive_int.of_int 8
| L -> Positive_int.of_int 9
2014-09-17 14:57:12 +02:00
type st = t
;;
2014-08-27 16:38:13 +02:00
module Xyz : sig
2014-09-18 17:01:43 +02:00
type t = { x: Positive_int.t ;
y: Positive_int.t ;
z: Positive_int.t }
2014-08-27 16:38:13 +02:00
val of_string : string -> t
val to_string : t -> string
val get_l : t -> Positive_int.t
2014-09-17 14:57:12 +02:00
val of_symmetry : st -> t list
2014-08-27 16:38:13 +02:00
end = struct
type t = { x: Positive_int.t ;
y: Positive_int.t ;
z: Positive_int.t }
type state_type = Null | X | Y | Z
2014-09-17 14:57:12 +02:00
(** Builds an XYZ triplet from a string.
* The input string is like "x2z3" *)
2014-08-27 16:38:13 +02:00
let of_string s =
let flush state accu number =
let n =
if (number = "") then 0
else (Int.of_string number)
in
match state with
| X -> { x= Positive_int.(of_int ( (to_int accu.x) +n));
y= accu.y ;
z= accu.z }
| Y -> { x= accu.x ;
y= Positive_int.(of_int ( (to_int accu.y) +n));
z= accu.z }
| Z -> { x= accu.x ;
y= accu.y ;
z= Positive_int.(of_int ( (to_int accu.z) +n))}
| Null -> accu
in
let rec do_work state accu number = function
| [] -> flush state accu number
| 'X'::rest | 'x'::rest ->
let new_accu = flush state accu number in
do_work X new_accu "" rest
| 'Y'::rest | 'y'::rest ->
let new_accu = flush state accu number in
do_work Y new_accu "" rest
| 'Z'::rest | 'z'::rest ->
let new_accu = flush state accu number in
do_work Z new_accu "" rest
| c::rest -> do_work state accu (number^(String.of_char c)) rest
in
String.to_list_rev s
|> List.rev
|> do_work Null
{ x=Positive_int.of_int 0 ;
y=Positive_int.of_int 0 ;
z=Positive_int.of_int 0 } ""
;;
2014-09-17 14:57:12 +02:00
(** Transforms an XYZ triplet to a string *)
2014-08-27 16:38:13 +02:00
let to_string t =
let x = match (Positive_int.to_int t.x) with
| 0 -> ""
| 1 -> "x"
| i -> Printf.sprintf "x%d" i
and y = match (Positive_int.to_int t.y) with
| 0 -> ""
| 1 -> "y"
| i -> Printf.sprintf "y%d" i
and z = match (Positive_int.to_int t.z) with
| 0 -> ""
| 1 -> "z"
| i -> Printf.sprintf "z%d" i
in
x^y^z
;;
2014-09-17 14:57:12 +02:00
(** Returns the l quantum number from a XYZ powers triplet *)
2014-08-27 16:38:13 +02:00
let get_l t =
let x = Positive_int.to_int t.x
and y = Positive_int.to_int t.y
and z = Positive_int.to_int t.z
in Positive_int.of_int (x+y+z)
;;
2014-09-17 14:57:12 +02:00
(** Returns a list of XYZ powers for a given symmetry *)
2014-08-27 16:38:13 +02:00
let of_symmetry sym =
let l = Positive_int.to_int (to_l sym) in
let create_z xyz =
{ x=xyz.x ;
y=xyz.y ;
z=Positive_int.(of_int (l-((to_int xyz.x)+(to_int xyz.y))))
}
in
let rec create_y accu xyz =
let {x ; y ; z} = xyz in
match (Positive_int.to_int y) with
| 0 -> (create_z xyz)::accu
| i ->
let ynew = Positive_int.( (to_int y)-1 |> of_int) in
create_y ( (create_z xyz)::accu) { x ; y=ynew ; z}
in
let rec create_x accu xyz =
let {x ; y ; z} = xyz in
match (Positive_int.to_int x) with
| 0 -> (create_y [] xyz)@accu
| i ->
let xnew = Positive_int.( (to_int x)-1 |> of_int) in
let ynew = Positive_int.(l-(to_int xnew) |> of_int)
in
create_x ((create_y [] xyz)@accu) { x=xnew ; y=ynew ; z}
in
create_x [] { x=(to_l sym) ; y=Positive_int.of_int 0 ;
z=Positive_int.of_int 0 }
;;
end