From 36e7dbc7bdd7f0355ada99492050fc918bb6a93e Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sun, 27 Dec 2020 23:08:12 +0100 Subject: [PATCH] Shorter documentation --- common/angular_momentum.org | 169 +++++------- common/bitstring.org | 415 +++++++++++------------------- common/charge.org | 34 +-- common/command_line.org | 41 +-- common/constants.org | 54 ++-- common/coordinate.org | 196 +++++--------- common/lib/angular_momentum.ml | 129 ++++------ common/lib/angular_momentum.mli | 51 +--- common/lib/bitstring.ml | 220 +++++----------- common/lib/bitstring.mli | 145 ++--------- common/lib/charge.ml | 13 +- common/lib/charge.mli | 20 +- common/lib/command_line.ml | 28 +- common/lib/command_line.mli | 22 +- common/lib/coordinate.ml | 134 +++++----- common/lib/coordinate.mli | 105 ++------ common/lib/non_negative_float.ml | 15 +- common/lib/non_negative_float.mli | 21 +- common/non_negative_float.org | 54 ++++ common/test/bitstring.ml | 91 +++---- docs/config.el | 3 + 21 files changed, 698 insertions(+), 1262 deletions(-) create mode 100644 common/non_negative_float.org diff --git a/common/angular_momentum.org b/common/angular_momentum.org index 2f6d95e..6dc2f79 100644 --- a/common/angular_momentum.org +++ b/common/angular_momentum.org @@ -48,21 +48,40 @@ open Powers ** Conversions - -*** ~of_char~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) val of_char : char -> t - #+end_src +val to_char : t -> char - Returns an ~Angular_momentum.t~ when a shell is given as a character - (case insensitive): +val to_int : t -> int +val of_int : int -> t - #+begin_example -Angular_momentum.of_char 'p' -> Angular_momentum.P - #+end_example +val to_string : t -> string + #+end_src - #+begin_src ocaml :tangle (eval ml) :exports none + | ~of_char~ | Returns an ~Angular_momentum.t~ when a shell is given as a character (case insensitive) | + | ~to_char~ | Converts the angular momentum into a char | + | ~of_int~ | Returns a shell given an $l$ value. | + | ~to_int~ | Returns the $l_{max}$ value of the shell | + | ~to_string~ | Converts the angular momentum into a string | + + #+begin_example +Angular_momentum.of_char 'p';; +- : Angular_momentum.t = Qcaml.Common.Angular_momentum.P + +Angular_momentum.(to_char P);; +- : char = 'P' + +Angular_momentum.of_int 2;; +- : Angular_momentum.t = Qcaml.Common.Angular_momentum.D + +Angular_momentum.(to_int D);; +- : int = 2 + +Angular_momentum.(to_string D);; +- : string = "D" + #+end_example + + #+begin_src ocaml :tangle (eval ml) :exports none let of_char = function | 's' | 'S' -> S | 'p' | 'P' -> P | 'd' | 'D' -> D | 'f' | 'F' -> F @@ -72,21 +91,7 @@ let of_char = function | 'm' | 'M' -> M | 'n' | 'N' -> N | 'o' | 'O' -> O | c -> raise (Angular_momentum_error (String.make 1 c)) - #+end_src -*** ~to_string~ - - #+begin_src ocaml :tangle (eval mli) -val to_string : t -> string - #+end_src - - Converts the angular momentum into a string: - - #+begin_example -Angular_momentum.(to_string D) -> "D" - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let to_string = function | S -> "S" | P -> "P" | D -> "D" | F -> "F" @@ -95,21 +100,8 @@ let to_string = function | K -> "K" | L -> "L" | M -> "M" | N -> "N" | O -> "O" | Int i -> string_of_int i - #+end_src -*** ~to_char~ - #+begin_src ocaml :tangle (eval mli) -val to_char : t -> char - #+end_src - - Converts the angular momentum into a char: - - #+begin_example -Angular_momentum.(to_char D) -> 'D' - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let to_char = function | S -> 'S' | P -> 'P' | D -> 'D' | F -> 'F' @@ -118,21 +110,8 @@ let to_char = function | K -> 'K' | L -> 'L' | M -> 'M' | N -> 'N' | O -> 'O' | Int _ -> '_' - #+end_src -*** ~to_int~ - #+begin_src ocaml :tangle (eval mli) -val to_int : t -> int - #+end_src - - Returns the $l_{max}$ value of the shell: - - #+begin_example -Angular_momentum.(to_char D) -> 2 - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let to_int = function | S -> 0 | P -> 1 | D -> 2 | F -> 3 @@ -141,21 +120,8 @@ let to_int = function | K -> 8 | L -> 9 | M -> 10 | N -> 11 | O -> 12 | Int i -> i - #+end_src -*** ~of_int~ - #+begin_src ocaml :tangle (eval mli) -val of_int : int -> t - #+end_src - - Returns a shell given an $l$ value. - - #+begin_example -Angular_momentum.of_int 3 -> Angular_momentum.F - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let of_int = function | 0 -> S | 1 -> P | 2 -> D | 3 -> F @@ -164,59 +130,38 @@ let of_int = function | 8 -> K | 9 -> L | 10 -> M | 11 -> N | 12 -> O | i -> Int i - #+end_src + #+end_src ** Shell functions - -*** ~n_functions~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) val n_functions : t -> int - #+end_src +val zkey_array : kind -> Zkey.t array + #+end_src - Returns the number of cartesian functions in a shell. + | ~n_functions~ | Returns the number of cartesian functions in a shell. | + | ~zkey_array~ | Array of ~Zkey.t~, where each element is a a key associated with the the powers of $x,y,z$. | - #+begin_example -Angular_momentum.n_functions D -> 6 - #+end_example + #+begin_example +Angular_momentum.(n_functions D) ;; +- : int = 6 - #+begin_src ocaml :tangle (eval ml) :exports none +Angular_momentum.( zkey_array (Doublet (P,S)) );; +- : Zkey.t array = + [| {Zkey.left = 0; right = 1125899906842624} ; + {Zkey.left = 0; right = 1099511627776} ; + {Zkey.left = 0; right = 1073741824} |] + + #+end_example + + #+begin_src ocaml :tangle (eval ml) :exports none let n_functions a = let a = to_int a in (a*a + 3*a + 2)/2 - #+end_src -*** ~zkey_array~ - - #+begin_src ocaml :tangle (eval mli) -val zkey_array : kind -> Zkey.t array - #+end_src - - Array of ~Zkey.t~, where each element is a a key associated with the - the powers of $x,y,z$. - - #+begin_example - Angular_momentum.( zkey_array Doublet (P,S) ) -> - [| {Zkey.left = 0; right = 1125899906842624} ; - {Zkey.left = 0; right = 1099511627776} ; - {Zkey.left = 0; right = 1073741824} |] - = - - let s,x,y,z = - Powers.( of_int_tuple (0,0,0), - of_int_tuple (1,0,0), - of_int_tuple (0,1,0), - of_int_tuple (0,0,1) ) - in - Array.map (fun (a,b) -> {!Zkey.of_powers_six} a b) - [| (x,s) ; (y,s) ; (z,s) |] - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let zkey_array_memo : (kind, Zkey.t array) Hashtbl.t = Hashtbl.create 13 @@ -292,7 +237,7 @@ let zkey_array a = in Hashtbl.add zkey_array_memo a result; result - #+end_src + #+end_src ** Arithmetic @@ -301,6 +246,14 @@ val ( + ) : t -> t -> t val ( - ) : t -> t -> t #+end_src + #+begin_example +Angular_momentum.(D + P);; +- : Angular_momentum.t = Qcaml.Common.Angular_momentum.F + +Angular_momentum.(F - P);; +- : Angular_momentum.t = Qcaml.Common.Angular_momentum.D + #+end_example + #+begin_src ocaml :tangle (eval ml) :exports none let ( + ) a b = of_int ( (to_int a) + (to_int b) ) @@ -311,11 +264,12 @@ let ( - ) a b = ** Printers - Printers can print as a string (~pp_string~) or as an integer (~pp_int~). + Printers can print as a string (default) or as an integer. #+begin_src ocaml :tangle (eval mli) +val pp : Format.formatter -> t -> unit val pp_string : Format.formatter -> t -> unit -val pp_int : Format.formatter -> t -> unit +val pp_int : Format.formatter -> t -> unit #+end_src #+begin_src ocaml :tangle (eval ml) :exports none @@ -324,7 +278,8 @@ let pp_string ppf x = let pp_int ppf x = Format.fprintf ppf "@[%d@]" (to_int x) + +let pp = pp_string #+end_src - ** TODO Tests diff --git a/common/bitstring.org b/common/bitstring.org index 3c9d843..7f02cff 100644 --- a/common/bitstring.org +++ b/common/bitstring.org @@ -14,11 +14,11 @@ :header-args: :noweb yes :comments both :END: - We define here a data type to handle bit strings efficiently. - When the bit string contains less than 64 bits, it is stored - internally in a 63-bit integer and uses bitwise instructions. - When more than 63 bits are required, the =zarith= library is used to - consider the bit string as a multi-precision integer. + We define here a data type to handle bit strings efficiently. When + the bit string contains less than 64 bits, it is stored internally + in a 63-bit integer and uses bitwise instructions. When more than + 63 bits are required, the =zarith= library is used to consider the + bit string as a multi-precision integer. ** Single-integer implementation :noexport: @@ -110,7 +110,8 @@ type t = #+begin_src ocaml :tangle (eval test-ml) open Common.Bitstring -let check msg x = Alcotest.(check bool) msg true x +let check_bool = Alcotest.(check bool) +let check msg x = check_bool msg true x let test_all () = let x = 8745687 in let one_x = of_int x in @@ -120,362 +121,258 @@ let test_all () = ** General implementation -*** ~of_int~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) val of_int : int -> t - #+end_src +val of_z : Z.t -> t +val zero : int -> t - Creates a bit string from an ~int~. +val is_zero : t -> bool +val numbits : t -> int +val testbit : t -> int -> bool - #+begin_src ocaml :tangle (eval ml) :exports none +val neg : t -> t +val shift_left : t -> int -> t +val shift_right : t -> int -> t +val shift_left_one : int -> int -> t + +val logor : t -> t -> t +val logxor : t -> t -> t +val logand : t -> t -> t +val lognot : t -> t + +val plus_one : t -> t +val minus_one : t -> t + +val hamdist : t -> t -> int +val trailing_zeros : t -> int +val popcount : t -> int + +val to_list : ?accu:(int list) -> t -> int list +val permutations : int -> int -> t list + #+end_src + + | ~of_int~ | Creates a bit string from an ~int~ | + | ~of_z~ | Creates a bit string from an ~Z.t~ multi-precision integer | + | ~zero~ | ~zero n~ creates a zero bit string with ~n~ bits | + | ~is_zero~ | True if all the bits of the bit string are zero. | + | ~numbits~ | Returns the number of bits used to represent the bit string | + | ~testbit~ | ~testbit t n~ is true if the ~n~-th bit of the bit string ~t~ is set to ~1~ | + | ~neg~ | Returns the negative of the integer interpretation of the bit string | + | ~shift_left~ | ~shift_left t n~ returns a new bit strings with all the bits shifted ~n~ positions to the left | + | ~shift_right~ | ~shift_right t n~ returns a new bit strings with all the bits shifted ~n~ positions to the right | + | ~shift_left_one~ | ~shift_left_one size n~ returns a new bit strings with the ~n~-th bit set to one. It is equivalent as shifting ~1~ by ~n~ bits to the left, ~size~ is the total number of bits of the bit string | + | ~logor~ | Bitwise logical or | + | ~logxor~ | Bitwise logical exclusive or | + | ~logand~ | Bitwise logical and | + | ~lognot~ | Bitwise logical negation | + | ~plus_one~ | Takes the integer representation of the bit string and adds one | + | ~minus_one~ | Takes the integer representation of the bit string and removes one | + | ~hamdist~ | Returns the Hamming distance, i.e. the number of bits differing between two bit strings | + | ~trailing_zeros~ | Returns the number of trailing zeros in the bit string | + | ~permutations~ | ~permutations m n~ generates the list of all possible ~n~-bit strings with ~m~ bits set to ~1~. Algorithm adapted from [[https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation][Bit twiddling hacks]] | + | ~popcount~ | Returns the number of bits set to one in the bit string | + | ~to_list~ | Converts a bit string into a list of integers indicating the positions where the bits are set to ~1~. The first value for the position is not ~0~ but ~1~ | + + #+begin_src ocaml :tangle (eval ml) :exports none let of_int x = One (One.of_int x) - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "of_x" true (one_x = (of_int x)); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "of_x" true (one_x = (of_int x)); + #+end_src -*** ~of_z~ - #+begin_src ocaml :tangle (eval mli) -val of_z : Z.t -> t - #+end_src - - Creates a bit string from an ~Z.t~ multi-precision integer. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let of_z x = if Z.numbits x < 64 then One (Z.to_int x) else Many (Many.of_z x) - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "of_z" true (one_x = (of_z (Z.of_int x))); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "of_z" true (one_x = (of_z (Z.of_int x))); + #+end_src + -*** ~zero~ - - #+begin_src ocaml :tangle (eval mli) -val zero : int -> t - #+end_src - - ~zero n~ creates a zero bit string with ~n~ bits. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let zero = function | n when n < 64 -> One (One.zero) | _ -> Many (Many.zero) - #+end_src + #+end_src -*** ~numbits~ - #+begin_src ocaml :tangle (eval mli) -val numbits : t -> int - #+end_src - - Returns the number of bits used to represent the bit string. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let numbits = function | One x -> One.numbits x | Many x -> Many.numbits x - #+end_src + #+end_src -*** ~is_zero~ - #+begin_src ocaml :tangle (eval mli) -val is_zero : t -> bool - #+end_src - - True if all the bits of the bit string are zero. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let is_zero = function | One x -> One.is_zero x | Many x -> Many.is_zero x - #+end_src + #+end_src -*** ~neg~ - #+begin_src ocaml :tangle (eval mli) -val neg : t -> t - #+end_src - - Returns the negative of the integer interpretation of the bit string. - - #+begin_example -neg (of_int x) = neg (of_int (-x)) - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let neg = function | One x -> One (One.neg x) | Many x -> Many (Many.neg x) - #+end_src + #+end_src -*** ~shift_left~ - #+begin_src ocaml :tangle (eval mli) -val shift_left : t -> int -> t - #+end_src - - ~shift_left t n~ returns a new bit strings with all the bits - shifted ~n~ positions to the left. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let shift_left x i = match x with | One x -> One (One.shift_left x i) | Many x -> Many (Many.shift_left x i) - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "shift_left1" true (of_int (x lsl 3) = shift_left one_x 3); -Alcotest.(check bool) "shift_left2" true (of_z (Z.shift_left z 3) = shift_left many_x 3); -Alcotest.(check bool) "shift_left3" true (of_z (Z.shift_left z 100) = shift_left many_x 100); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "shift_left1" true (of_int (x lsl 3) = shift_left one_x 3); +check_bool "shift_left2" true (of_z (Z.shift_left z 3) = shift_left many_x 3); +check_bool "shift_left3" true (of_z (Z.shift_left z 100) = shift_left many_x 100); + #+end_src -*** ~shift_right~ - - #+begin_src ocaml :tangle (eval mli) -val shift_right : t -> int -> t - #+end_src - - ~shift_right t n~ returns a new bit strings with all the bits - shifted ~n~ positions to the right. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let shift_right x i = match x with | One x -> One (One.shift_right x i) | Many x -> Many (Many.shift_right x i) - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "shift_right1" true (of_int (x lsr 3) = shift_right one_x 3); -Alcotest.(check bool) "shift_right2" true (of_z (Z.shift_right z 3) = shift_right many_x 3); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "shift_right1" true (of_int (x lsr 3) = shift_right one_x 3); +check_bool "shift_right2" true (of_z (Z.shift_right z 3) = shift_right many_x 3); + #+end_src -*** ~shift_left_one~ - #+begin_src ocaml :tangle (eval mli) -val shift_left_one : int -> int -> t - #+end_src - - ~shift_left_one size n~ returns a new bit strings with the ~n~-th - bit set to one. - It is equivalent as shifting ~1~ by ~n~ bits to the left. - ~size~ is the total number of bits of the bit string. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let shift_left_one = function | n when n < 64 -> fun i -> One (One.shift_left_one i) | _ -> fun i -> Many (Many.shift_left_one i) - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "shift_left_one1" true (of_int (1 lsl 3) = shift_left_one 4 3); -Alcotest.(check bool) "shift_left_one2" true (of_z (Z.shift_left Z.one 200) = shift_left_one 300 200); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "shift_left_one1" true (of_int (1 lsl 3) = shift_left_one 4 3); +check_bool "shift_left_one2" true (of_z (Z.shift_left Z.one 200) = shift_left_one 300 200); + #+end_src -*** ~testbit~ - #+begin_src ocaml :tangle (eval mli) -val testbit : t -> int -> bool - #+end_src - - ~testbit t n~ is true if the ~n~-th bit of the bit string ~t~ is - set to ~1~. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let testbit = function | One x -> One.testbit x | Many x -> Many.testbit x - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "testbit1" true (testbit (of_int 8) 3); -Alcotest.(check bool) "testbit2" false (testbit (of_int 8) 2); -Alcotest.(check bool) "testbit3" false (testbit (of_int 8) 4); -Alcotest.(check bool) "testbit4" true (testbit (of_z (Z.of_int 8)) 3); -Alcotest.(check bool) "testbit5" false (testbit (of_z (Z.of_int 8)) 2); -Alcotest.(check bool) "testbit6" false (testbit (of_z (Z.of_int 8)) 4); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "testbit1" true (testbit (of_int 8) 3); +check_bool "testbit2" false (testbit (of_int 8) 2); +check_bool "testbit3" false (testbit (of_int 8) 4); +check_bool "testbit4" true (testbit (of_z (Z.of_int 8)) 3); +check_bool "testbit5" false (testbit (of_z (Z.of_int 8)) 2); +check_bool "testbit6" false (testbit (of_z (Z.of_int 8)) 4); + #+end_src -*** ~logor~ - #+begin_src ocaml :tangle (eval mli) -val logor : t -> t -> t - #+end_src - - Bitwise logical or. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let logor a b = match a,b with | One a, One b -> One (One.logor a b) | Many a, Many b -> Many (Many.logor a b) | _ -> invalid_arg "Bitstring.logor" - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "logor1" true (of_int (1 lor 2) = logor (of_int 1) (of_int 2)); -Alcotest.(check bool) "logor2" true (of_z (Z.of_int (1 lor 2)) = logor (of_z Z.one) (of_z (Z.of_int 2))); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "logor1" true (of_int (1 lor 2) = logor (of_int 1) (of_int 2)); +check_bool "logor2" true (of_z (Z.of_int (1 lor 2)) = logor (of_z Z.one) (of_z (Z.of_int 2))); + #+end_src -*** ~logxor~ - #+begin_src ocaml :tangle (eval mli) -val logxor : t -> t -> t - #+end_src - - Bitwise logical exclusive or. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let logxor a b = match a,b with | One a, One b -> One (One.logxor a b) | Many a, Many b -> Many (Many.logxor a b) | _ -> invalid_arg "Bitstring.logxor" - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "logxor1" true (of_int (1 lxor 2) = logxor (of_int 1) (of_int 2)); -Alcotest.(check bool) "logxor2" true (of_z (Z.of_int (1 lxor 2)) = logxor (of_z Z.one) (of_z (Z.of_int 2))); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "logxor1" true (of_int (1 lxor 2) = logxor (of_int 1) (of_int 2)); +check_bool "logxor2" true (of_z (Z.of_int (1 lxor 2)) = logxor (of_z Z.one) (of_z (Z.of_int 2))); + #+end_src -*** ~logand~ - #+begin_src ocaml :tangle (eval mli) -val logand : t -> t -> t - #+end_src - - Bitwise logical and. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let logand a b = match a,b with | One a, One b -> One (One.logand a b) | Many a, Many b -> Many (Many.logand a b) | _ -> invalid_arg "Bitstring.logand" - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "logand1" true (of_int (1 land 3) = logand (of_int 1) (of_int 3)); -Alcotest.(check bool) "logand2" true (of_z (Z.of_int (1 land 3)) = logand (of_z Z.one) (of_z (Z.of_int 3))); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "logand1" true (of_int (1 land 3) = logand (of_int 1) (of_int 3)); +check_bool "logand2" true (of_z (Z.of_int (1 land 3)) = logand (of_z Z.one) (of_z (Z.of_int 3))); + #+end_src -*** ~lognot~ - #+begin_src ocaml :tangle (eval mli) -val lognot : t -> t - #+end_src - - Bitwise logical negation. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let lognot = function | One x -> One (One.lognot x) | Many x -> Many (Many.lognot x) - #+end_src + #+end_src -*** ~minus_one~ - #+begin_src ocaml :tangle (eval mli) -val minus_one : t -> t - #+end_src - - Takes the integer representation of the bit string and removes one. - - #+begin_example + #+begin_example minus_one (of_int 10) = of_int 9 - #+end_example + #+end_example - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let minus_one = function | One x -> One (One.minus_one x) | Many x -> Many (Many.minus_one x) - #+end_src - -*** ~plus_one~ + #+end_src - #+begin_src ocaml :tangle (eval mli) -val plus_one : t -> t - #+end_src - - Takes the integer representation of the bit string and adds one. - - #+begin_example + #+begin_example plus_one (of_int 10) = of_int 11 - #+end_example - #+begin_src ocaml :tangle (eval ml) :exports none + #+end_example + #+begin_src ocaml :tangle (eval ml) :exports none let plus_one = function | One x -> One (One.plus_one x) | Many x -> Many (Many.plus_one x) - #+end_src + #+end_src -*** ~trailing_zeros~ - #+begin_src ocaml :tangle (eval mli) -val trailing_zeros : t -> int - #+end_src - - Returns the number of trailing zeros in the bit string. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let trailing_zeros = function | One x -> One.trailing_zeros x | Many x -> Many.trailing_zeros x - #+end_src + #+end_src -*** ~hamdist~ - #+begin_src ocaml :tangle (eval mli) -val hamdist : t -> t -> int - #+end_src - - Returns the Hamming distance, i.e. the number of bits differing - between two bit strings. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let hamdist a b = match a, b with | One a, One b -> One.hamdist a b | Many a, Many b -> Many.hamdist a b | _ -> invalid_arg "Bitstring.hamdist" - #+end_src + #+end_src -*** ~popcount~ - #+begin_src ocaml :tangle (eval mli) -val popcount : t -> int - #+end_src - - Returns the number of bits set to one in the bit string. - - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let popcount = function | One x -> One.popcount x | Many x -> Many.popcount x - #+end_src + #+end_src -*** ~to_list~ - #+begin_src ocaml :tangle (eval mli) -val to_list : ?accu:(int list) -> t -> int list - #+end_src - - Converts a bit string into a list of integers indicating the - positions where the bits are set to ~1~. The first value for the - position is not ~0~ but ~1~. - - #+begin_example + #+begin_example Bitstring.to_list (of_int 5);; - : int list = [1; 3] - #+end_example + #+end_example - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let rec to_list ?(accu=[]) = function | t when (is_zero t) -> List.rev accu | t -> let newlist = @@ -483,23 +380,13 @@ let rec to_list ?(accu=[]) = function in logand t @@ minus_one t |> (to_list [@tailcall]) ~accu:newlist - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none -Alcotest.(check bool) "to_list" true ([ 1 ; 3 ; 4 ; 6 ] = (to_list (of_int 45))); - #+end_src + #+begin_src ocaml :tangle (eval test-ml) :exports none +check_bool "to_list" true ([ 1 ; 3 ; 4 ; 6 ] = (to_list (of_int 45))); + #+end_src -*** ~permutations~ - - #+begin_src ocaml :tangle (eval mli) -val permutations : int -> int -> t list - #+end_src - - ~permutations m n~ generates the list of all possible ~n~-bit - strings with ~m~ bits set to ~1~. - Algorithm adapted from [[https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation][Bit twiddling hacks]]. - - #+begin_example + #+begin_example Bitstring.permutations 2 4 |> List.map (fun x -> Format.asprintf "%a" Bitstring.pp x) ;; - : string list = @@ -509,9 +396,9 @@ Bitstring.permutations 2 4 "+--+------------------------------------------------------------"; "-+-+------------------------------------------------------------"; "--++------------------------------------------------------------"] - #+end_example + #+end_example - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none let permutations m n = let rec aux k u rest = @@ -529,13 +416,13 @@ let permutations m n = (aux [@tailcall]) (k-1) (logor t' t'') (u :: rest) in aux (Util.binom n m) (minus_one (shift_left_one n m)) [] - #+end_src + #+end_src - #+begin_src ocaml :tangle (eval test-ml) :exports none + #+begin_src ocaml :tangle (eval test-ml) :exports none check "permutations" (permutations 2 4 = List.map of_int [ 3 ; 5 ; 6 ; 9 ; 10 ; 12 ]); - #+end_src + #+end_src ** Printers diff --git a/common/charge.org b/common/charge.org index 9935b24..fcc36f9 100644 --- a/common/charge.org +++ b/common/charge.org @@ -29,38 +29,24 @@ type t = float ** Conversions -*** ~of_float~ / ~to_float~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) val of_float : float -> t val to_float : t -> float - #+end_src - #+begin_src ocaml :tangle (eval ml) :exports none -external of_float : float -> t = "%identity" -external to_float : t -> float = "%identity" - #+end_src - -*** ~of_int~ / ~to_int~ - - #+begin_src ocaml :tangle (eval mli) val of_int : int -> t val to_int : t -> int - #+end_src - #+begin_src ocaml :tangle (eval ml) :exports none -let of_int = float_of_int -let to_int = int_of_float - #+end_src - -*** ~of_string~ / ~to_string~ - - #+begin_src ocaml :tangle (eval mli) val of_string: string -> t val to_string: t -> string - #+end_src + #+end_src + + #+begin_src ocaml :tangle (eval ml) :exports none +external of_float : float -> t = "%identity" +external to_float : t -> float = "%identity" + +let of_int = float_of_int +let to_int = int_of_float - #+begin_src ocaml :tangle (eval ml) :exports none let of_string = float_of_string let to_string x = @@ -70,7 +56,7 @@ let to_string x = Printf.sprintf "%f" x else "0.0" - #+end_src + #+end_src ** Simple operations diff --git a/common/command_line.org b/common/command_line.org index 1fc072a..786bc7d 100644 --- a/common/command_line.org +++ b/common/command_line.org @@ -214,23 +214,20 @@ let output_long max_width x = ** Query functions -*** ~anon_args~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) +val get : long_opt -> string option +val get_bool : long_opt -> bool val anon_args : unit -> string list - #+end_src + #+end_src - Returns the list of anonymous arguments + | ~get~ | Returns the argument associated with a long option | + | ~get_bool~ | True if the ~Optional~ argument is present in the command-line | + | ~anon_args~ | Returns the list of anonymous arguments | - #+begin_src ocaml :tangle (eval ml) :exports none + + #+begin_src ocaml :tangle (eval ml) :exports none let anon_args () = !anon_args_ref - #+end_src -*** ~help~ :noexport: - - Prints the documentation of the program. - - #+begin_src ocaml :tangle (eval ml) :exports none let help () = (* Print the header *) @@ -296,33 +293,15 @@ let help () = (* Print footer *) output_text !footer_doc; Format.printf "@." - #+end_src -*** ~get~ - #+begin_src ocaml :tangle (eval mli) -val get : long_opt -> string option - #+end_src - - Returns the argument associated with a long option. - - #+begin_src ocaml :tangle (eval ml) :exports none let get x = try Some (Hashtbl.find dict x) with Not_found -> None - #+end_src -*** ~get_bool~ - #+begin_src ocaml :tangle (eval mli) -val get_bool : long_opt -> bool - #+end_src - - True if the ~Optional~ argument is present in the command-line - - #+begin_src ocaml :tangle (eval ml) :exports none let get_bool x = Hashtbl.mem dict x - #+end_src + #+end_src ** Specification diff --git a/common/constants.org b/common/constants.org index 4d3dab6..9bb6af4 100644 --- a/common/constants.org +++ b/common/constants.org @@ -18,41 +18,21 @@ ** Thresholds -*** ~epsilon~ - - #+begin_src ocaml :tangle (eval mli) -val epsilon : float - #+end_src - - Value below which a float is considered null. Default is - \epsilon = 2.10^{-15}. - - #+begin_src ocaml :tangle (eval ml) :exports none -let epsilon = 2.e-15 - #+end_src - -*** ~integrals_cutoff~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) +val epsilon : float val integrals_cutoff : float - #+end_src + #+end_src - Cutoff value for integrals. Default is \epsilon . + | ~epsilon~ | Value below which a float is considered null. Default is \epsilon = 2.10^{-15} | + | ~integrals_cutoff~ | Cutoff value for integrals. Default is \epsilon | - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none +let epsilon = 2.e-15 let integrals_cutoff = epsilon - #+end_src - + #+end_src ** Mathematical constants - | ~pi~ | $\pi = 3.141~592~653~589~793~12$ | - | ~two_pi~ | $2 \pi$ | - | ~sq_pi~ | $\sqrt{\pi}$ | - | ~sq_pi_over_two~ | $\sqrt{\pi} / 2$ | - | ~pi_inv~ | $1 / \pi$ | - | ~two_over_sq_pi~ | $2 / \sqrt{\pi}$ | - #+begin_src ocaml :tangle (eval mli) val pi : float val two_pi : float @@ -62,6 +42,13 @@ val pi_inv : float val two_over_sq_pi : float #+end_src + | ~pi~ | $\pi = 3.141~592~653~589~793~12$ | + | ~two_pi~ | $2 \pi$ | + | ~sq_pi~ | $\sqrt{\pi}$ | + | ~sq_pi_over_two~ | $\sqrt{\pi} / 2$ | + | ~pi_inv~ | $1 / \pi$ | + | ~two_over_sq_pi~ | $2 / \sqrt{\pi}$ | + #+begin_src ocaml :tangle (eval ml) :exports none let pi = acos (-1.) let two_pi = 2. *. pi @@ -71,14 +58,8 @@ let pi_inv = 1. /. pi let two_over_sq_pi = 2. /. sq_pi #+end_src - ** Physical constants - | ~a0~ | Bohr's radius : $a_0 = 0.529~177~210~67(23)$ angstrom | - | ~a0_inv~ | $1 / a_0$ | - | ~ha_to_ev~ | Hartree to eV conversion factor : $27.211~386~02(17)$ | - | ~ev_to_ha~ | eV to Hartree conversion factor : 1 / ~ha_to_ev~ | - #+begin_src ocaml :tangle (eval mli) val a0 : float val a0_inv : float @@ -86,6 +67,11 @@ val ha_to_ev : float val ev_to_ha : float #+end_src + | ~a0~ | Bohr's radius : $a_0 = 0.529~177~210~67(23)$ angstrom | + | ~a0_inv~ | $1 / a_0$ | + | ~ha_to_ev~ | Hartree to eV conversion factor : $27.211~386~02(17)$ | + | ~ev_to_ha~ | eV to Hartree conversion factor : 1 / ~ha_to_ev~ | + #+begin_src ocaml :tangle (eval ml) :exports none let a0 = 0.529_177_210_67 let a0_inv = 1. /. a0 diff --git a/common/coordinate.org b/common/coordinate.org index 86ee98c..5b5445f 100644 --- a/common/coordinate.org +++ b/common/coordinate.org @@ -44,209 +44,139 @@ type axis = X | Y | Z <> #+end_src -** Creation / conversion +** Creation -*** ~make~ - - #+begin_src ocaml :tangle (eval mli) -val make : 'a point -> t - #+end_src - - Creates a point in atomic units. - - #+begin_src ocaml :tangle (eval ml) :exports none -external make : 'a point -> t = "%identity" - #+end_src - -*** ~make_angstrom~ - - #+begin_src ocaml :tangle (eval mli) + #+begin_src ocaml :tangle (eval mli) +val make : 'a point -> t val make_angstrom : 'a point -> angstrom point - #+end_src +val zero : bohr point + #+end_src - Creates a point in angstrom. + | ~make~ | Creates a point in atomic units | + | ~make_angstrom~ | Creates a point in angstrom | + | ~zero~ | $(0., 0., 0.)$ | - #+begin_src ocaml :tangle (eval ml) :exports none + #+begin_src ocaml :tangle (eval ml) :exports none +external make : 'a point -> t = "%identity" external make_angstrom : 'a point -> angstrom point = "%identity" - #+end_src -*** ~bohr_to_angstrom~ +let zero = + make { x = 0. ; y = 0. ; z = 0. } + #+end_src - #+begin_src ocaml :tangle (eval mli) +** Conversion + + #+begin_src ocaml :tangle (eval mli) val bohr_to_angstrom : bohr point -> angstrom point - #+end_src +val angstrom_to_bohr : angstrom point -> bohr point + #+end_src - Converts a point in bohr to angstrom. - - #+begin_src ocaml :tangle (eval ml) :exports none -let b_to_a b = Constants.a0 *. b + | ~bohr_to_angstrom~ | Converts a point in bohr to angstrom | + | ~angstrom_to_bohr~ | Converts a point in angstrom to bohr | + #+begin_src ocaml :tangle (eval ml) :exports none +let b_to_a b = Constants.a0 *. b let bohr_to_angstrom { x ; y ; z } = make { x = b_to_a x ; y = b_to_a y ; z = b_to_a z ; } - #+end_src -*** ~angstrom_to_bohr~ - - #+begin_src ocaml :tangle (eval mli) -val angstrom_to_bohr : angstrom point -> bohr point - #+end_src - - Converts a point in angstrom to bohr. - - #+begin_src ocaml :tangle (eval ml) :exports none -let a_to_b a = Constants.a0_inv *. a +let a_to_b a = Constants.a0_inv *. a let angstrom_to_bohr { x ; y ; z } = make { x = a_to_b x ; y = a_to_b y ; z = a_to_b z ; } - #+end_src + #+end_src -*** ~zero~ +** Vector operations - #+begin_src ocaml :tangle (eval mli) -val zero : bohr point - #+end_src + #+begin_src ocaml :tangle (eval mli) +val neg : t -> t +val get : axis -> bohr point -> float +val dot : t -> t -> float +val norm : t -> float +val ( |. ) : float -> t -> t +val ( |+ ) : t -> t -> t +val ( |- ) : t -> t -> t + #+end_src - ~zero~ = (0., 0., 0.) + | ~neg~ | Negative of a point | + | ~get~ | Extracts the projection of the coordinate on an axis | + | ~dot~ | Dot product | + | ~norm~ | $\ell{^2}$ norm of the vector | + | ~¦.~ | Scales the vector by a constant | + | ~¦+~ | Adds two vectors | + | ~¦-~ | Subtracts two vectors | - #+begin_src ocaml :tangle (eval ml) :exports none -let zero = - make { x = 0. ; y = 0. ; z = 0. } - #+end_src + #+begin_example +Coordinate.neg { x=1. ; y=2. ; z=3. } ;; +- : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -2.; z = -3.} -*** ~get~ - - #+begin_src ocaml :tangle (eval mli) -val get : axis -> bohr point -> float - #+end_src - - Extracts the projection of the coordinate on an axis. - - #+begin_example Coordinate.(get Y { x=1. ; y=2. ; z=3. }) ;; - : float = 2. - #+end_example - #+begin_src ocaml :tangle (eval ml) :exports none +Coordinate.( + 2. |. { x=1. ; y=2. ; z=3. } +) ;; +- : Coordinate.t = {Qcaml.Common.Coordinate.x = 2.; y = 4.; z = 6.} + +Coordinate.( + { x=1. ; y=2. ; z=3. } |+ { x=2. ; y=3. ; z=1. } +);; +- : Coordinate.t = {Qcaml.Common.Coordinate.x = 3.; y = 5.; z = 4.} + +Coordinate.( + { x=1. ; y=2. ; z=3. } |- { x=2. ; y=3. ; z=1. } +);; +- : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -1.; z = 2.} + #+end_example + + + #+begin_src ocaml :tangle (eval ml) :exports none let get axis { x ; y ; z } = match axis with | X -> x | Y -> y | Z -> z - #+end_src -** Vector operations -*** Scale - - #+begin_src ocaml :tangle (eval mli) -val ( |. ) : float -> t -> t - #+end_src - - #+begin_example -Coordinate.( - 2. |. { x=1. ; y=2. ; z=3. } -) ;; -- : Coordinate.t = {Qcaml.Common.Coordinate.x = 2.; y = 4.; z = 6.} - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let ( |. ) s { x ; y ; z } = make { x = s *. x ; y = s *. y ; z = s *. z ; } - #+end_src -*** Add - #+begin_src ocaml :tangle (eval mli) -val ( |+ ) : t -> t -> t - #+end_src - - #+begin_example -Coordinate.( - { x=1. ; y=2. ; z=3. } |+ { x=2. ; y=3. ; z=1. } -);; -- : Coordinate.t = {Qcaml.Common.Coordinate.x = 3.; y = 5.; z = 4.} - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let ( |+ ) { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = make { x = x1 +. x2 ; y = y1 +. y2 ; z = z1 +. z2 ; } - #+end_src -*** Subtract - #+begin_src ocaml :tangle (eval mli) -val ( |- ) : t -> t -> t - #+end_src - - #+begin_example -Coordinate.( - { x=1. ; y=2. ; z=3. } |- { x=2. ; y=3. ; z=1. } -);; -- : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -1.; z = 2.} - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let ( |- ) { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = make { x = x1 -. x2 ; y = y1 -. y2 ; z = z1 -. z2 ; } - #+end_src - -*** Negative - #+begin_src ocaml :tangle (eval mli) -val neg : t -> t - #+end_src - #+begin_example -Coordinate.neg { x=1. ; y=2. ; z=3. } ;; -- : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -2.; z = -3.} - #+end_example - - #+begin_src ocaml :tangle (eval ml) :exports none let neg a = -1. |. a - #+end_src -*** Dot product - #+begin_src ocaml :tangle (eval mli) -val dot : t -> t -> float - #+end_src - - #+begin_src ocaml :tangle (eval ml) :exports none let dot { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = x1 *. x2 +. y1 *. y2 +. z1 *. z2 - #+end_src -*** Norm - #+begin_src ocaml :tangle (eval mli) -val norm : t -> float - #+end_src - - $\ell{^2}$ norm of the vector. - - #+begin_src ocaml :tangle (eval ml) :exports none let norm u = sqrt ( dot u u ) - #+end_src - + #+end_src ** Printers diff --git a/common/lib/angular_momentum.ml b/common/lib/angular_momentum.ml index 2a9c00d..a40a5ed 100644 --- a/common/lib/angular_momentum.ml +++ b/common/lib/angular_momentum.ml @@ -25,15 +25,31 @@ open Powers -(* Returns an ~Angular_momentum.t~ when a shell is given as a character - * (case insensitive): +(* | ~of_char~ | Returns an ~Angular_momentum.t~ when a shell is given as a character (case insensitive) | + * | ~to_char~ | Converts the angular momentum into a char | + * | ~of_int~ | Returns a shell given an $l$ value. | + * | ~to_int~ | Returns the $l_{max}$ value of the shell | + * | ~to_string~ | Converts the angular momentum into a string | * - * #+begin_example - * Angular_momentum.of_char 'p' -> Angular_momentum.P - * #+end_example *) + * #+begin_example + * Angular_momentum.of_char 'p';; + * - : Angular_momentum.t = Qcaml.Common.Angular_momentum.P + * + * Angular_momentum.(to_char P);; + * - : char = 'P' + * + * Angular_momentum.of_int 2;; + * - : Angular_momentum.t = Qcaml.Common.Angular_momentum.D + * + * Angular_momentum.(to_int D);; + * - : int = 2 + * + * Angular_momentum.(to_string D);; + * - : string = "D" + * #+end_example *) -(* [[file:../angular_momentum.org::*~of_char~][~of_char~:2]] *) +(* [[file:../angular_momentum.org::*Conversions][Conversions:2]] *) let of_char = function | 's' | 'S' -> S | 'p' | 'P' -> P | 'd' | 'D' -> D | 'f' | 'F' -> F @@ -43,18 +59,7 @@ let of_char = function | 'm' | 'M' -> M | 'n' | 'N' -> N | 'o' | 'O' -> O | c -> raise (Angular_momentum_error (String.make 1 c)) -(* ~of_char~:2 ends here *) - - -(* Converts the angular momentum into a string: - * - * #+begin_example - * Angular_momentum.(to_string D) -> "D" - * #+end_example *) - - -(* [[file:../angular_momentum.org::*~to_string~][~to_string~:2]] *) let to_string = function | S -> "S" | P -> "P" | D -> "D" | F -> "F" @@ -63,18 +68,8 @@ let to_string = function | K -> "K" | L -> "L" | M -> "M" | N -> "N" | O -> "O" | Int i -> string_of_int i -(* ~to_string~:2 ends here *) - -(* Converts the angular momentum into a char: - * - * #+begin_example - * Angular_momentum.(to_char D) -> 'D' - * #+end_example *) - - -(* [[file:../angular_momentum.org::*~to_char~][~to_char~:2]] *) let to_char = function | S -> 'S' | P -> 'P' | D -> 'D' | F -> 'F' @@ -83,18 +78,8 @@ let to_char = function | K -> 'K' | L -> 'L' | M -> 'M' | N -> 'N' | O -> 'O' | Int _ -> '_' -(* ~to_char~:2 ends here *) - -(* Returns the $l_{max}$ value of the shell: - * - * #+begin_example - * Angular_momentum.(to_char D) -> 2 - * #+end_example *) - - -(* [[file:../angular_momentum.org::*~to_int~][~to_int~:2]] *) let to_int = function | S -> 0 | P -> 1 | D -> 2 | F -> 3 @@ -103,18 +88,8 @@ let to_int = function | K -> 8 | L -> 9 | M -> 10 | N -> 11 | O -> 12 | Int i -> i -(* ~to_int~:2 ends here *) - -(* Returns a shell given an $l$ value. - * - * #+begin_example - * Angular_momentum.of_int 3 -> Angular_momentum.F - * #+end_example *) - - -(* [[file:../angular_momentum.org::*~of_int~][~of_int~:2]] *) let of_int = function | 0 -> S | 1 -> P | 2 -> D | 3 -> F @@ -123,49 +98,34 @@ let of_int = function | 8 -> K | 9 -> L | 10 -> M | 11 -> N | 12 -> O | i -> Int i -(* ~of_int~:2 ends here *) +(* Conversions:2 ends here *) -(* Returns the number of cartesian functions in a shell. +(* | ~n_functions~ | Returns the number of cartesian functions in a shell. | + * | ~zkey_array~ | Array of ~Zkey.t~, where each element is a a key associated with the the powers of $x,y,z$. | * - * #+begin_example - * Angular_momentum.n_functions D -> 6 - * #+end_example *) + * #+begin_example + * Angular_momentum.(n_functions D) ;; + * - : int = 6 + * + * Angular_momentum.( zkey_array (Doublet (P,S)) );; + * - : Zkey.t array = + * [| {Zkey.left = 0; right = 1125899906842624} ; + * {Zkey.left = 0; right = 1099511627776} ; + * {Zkey.left = 0; right = 1073741824} |] + * + * #+end_example *) -(* [[file:../angular_momentum.org::*~n_functions~][~n_functions~:2]] *) +(* [[file:../angular_momentum.org::*Shell functions][Shell functions:2]] *) let n_functions a = let a = to_int a in (a*a + 3*a + 2)/2 -(* ~n_functions~:2 ends here *) - -(* Array of ~Zkey.t~, where each element is a a key associated with the - * the powers of $x,y,z$. - * - * #+begin_example - * Angular_momentum.( zkey_array Doublet (P,S) ) -> - * [| {Zkey.left = 0; right = 1125899906842624} ; - * {Zkey.left = 0; right = 1099511627776} ; - * {Zkey.left = 0; right = 1073741824} |] - * = - * - * let s,x,y,z = - * Powers.( of_int_tuple (0,0,0), - * of_int_tuple (1,0,0), - * of_int_tuple (0,1,0), - * of_int_tuple (0,0,1) ) - * in - * Array.map (fun (a,b) -> {!Zkey.of_powers_six} a b) - * [| (x,s) ; (y,s) ; (z,s) |] - * #+end_example *) - - -(* [[file:../angular_momentum.org::*~zkey_array~][~zkey_array~:2]] *) let zkey_array_memo : (kind, Zkey.t array) Hashtbl.t = Hashtbl.create 13 @@ -241,7 +201,18 @@ let zkey_array a = in Hashtbl.add zkey_array_memo a result; result -(* ~zkey_array~:2 ends here *) +(* Shell functions:2 ends here *) + + + +(* #+begin_example + * Angular_momentum.(D + P);; + * - : Angular_momentum.t = Qcaml.Common.Angular_momentum.F + * + * Angular_momentum.(F - P);; + * - : Angular_momentum.t = Qcaml.Common.Angular_momentum.D + * #+end_example *) + (* [[file:../angular_momentum.org::*Arithmetic][Arithmetic:2]] *) let ( + ) a b = @@ -257,4 +228,6 @@ let pp_string ppf x = let pp_int ppf x = Format.fprintf ppf "@[%d@]" (to_int x) + +let pp = pp_string (* Printers:2 ends here *) diff --git a/common/lib/angular_momentum.mli b/common/lib/angular_momentum.mli index 074ded9..d9f9bc6 100644 --- a/common/lib/angular_momentum.mli +++ b/common/lib/angular_momentum.mli @@ -16,54 +16,26 @@ type kind = | Quartet of (t * t * t * t) (* types ends here *) -(* ~of_char~ *) +(* Conversions *) -(* [[file:../angular_momentum.org::*~of_char~][~of_char~:1]] *) +(* [[file:../angular_momentum.org::*Conversions][Conversions:1]] *) val of_char : char -> t -(* ~of_char~:1 ends here *) - -(* ~to_string~ *) - - -(* [[file:../angular_momentum.org::*~to_string~][~to_string~:1]] *) -val to_string : t -> string -(* ~to_string~:1 ends here *) - -(* ~to_char~ *) - - -(* [[file:../angular_momentum.org::*~to_char~][~to_char~:1]] *) val to_char : t -> char -(* ~to_char~:1 ends here *) -(* ~to_int~ *) - - -(* [[file:../angular_momentum.org::*~to_int~][~to_int~:1]] *) val to_int : t -> int -(* ~to_int~:1 ends here *) - -(* ~of_int~ *) - - -(* [[file:../angular_momentum.org::*~of_int~][~of_int~:1]] *) val of_int : int -> t -(* ~of_int~:1 ends here *) -(* ~n_functions~ *) +val to_string : t -> string +(* Conversions:1 ends here *) + +(* Shell functions *) -(* [[file:../angular_momentum.org::*~n_functions~][~n_functions~:1]] *) +(* [[file:../angular_momentum.org::*Shell functions][Shell functions:1]] *) val n_functions : t -> int -(* ~n_functions~:1 ends here *) - -(* ~zkey_array~ *) - - -(* [[file:../angular_momentum.org::*~zkey_array~][~zkey_array~:1]] *) -val zkey_array : kind -> Zkey.t array -(* ~zkey_array~:1 ends here *) +val zkey_array : kind -> Zkey.t array +(* Shell functions:1 ends here *) (* Arithmetic *) @@ -75,10 +47,11 @@ val ( - ) : t -> t -> t (* Printers * - * Printers can print as a string (~pp_string~) or as an integer (~pp_int~). *) + * Printers can print as a string (default) or as an integer. *) (* [[file:../angular_momentum.org::*Printers][Printers:1]] *) +val pp : Format.formatter -> t -> unit val pp_string : Format.formatter -> t -> unit -val pp_int : Format.formatter -> t -> unit +val pp_int : Format.formatter -> t -> unit (* Printers:1 ends here *) diff --git a/common/lib/bitstring.ml b/common/lib/bitstring.ml index 6caac0c..7b98b90 100644 --- a/common/lib/bitstring.ml +++ b/common/lib/bitstring.ml @@ -81,249 +81,173 @@ type t = -(* Creates a bit string from an ~int~. *) +(* | ~of_int~ | Creates a bit string from an ~int~ | + * | ~of_z~ | Creates a bit string from an ~Z.t~ multi-precision integer | + * | ~zero~ | ~zero n~ creates a zero bit string with ~n~ bits | + * | ~is_zero~ | True if all the bits of the bit string are zero. | + * | ~numbits~ | Returns the number of bits used to represent the bit string | + * | ~testbit~ | ~testbit t n~ is true if the ~n~-th bit of the bit string ~t~ is set to ~1~ | + * | ~neg~ | Returns the negative of the integer interpretation of the bit string | + * | ~shift_left~ | ~shift_left t n~ returns a new bit strings with all the bits shifted ~n~ positions to the left | + * | ~shift_right~ | ~shift_right t n~ returns a new bit strings with all the bits shifted ~n~ positions to the right | + * | ~shift_left_one~ | ~shift_left_one size n~ returns a new bit strings with the ~n~-th bit set to one. It is equivalent as shifting ~1~ by ~n~ bits to the left, ~size~ is the total number of bits of the bit string | + * | ~logor~ | Bitwise logical or | + * | ~logxor~ | Bitwise logical exclusive or | + * | ~logand~ | Bitwise logical and | + * | ~lognot~ | Bitwise logical negation | + * | ~plus_one~ | Takes the integer representation of the bit string and adds one | + * | ~minus_one~ | Takes the integer representation of the bit string and removes one | + * | ~hamdist~ | Returns the Hamming distance, i.e. the number of bits differing between two bit strings | + * | ~trailing_zeros~ | Returns the number of trailing zeros in the bit string | + * | ~permutations~ | ~permutations m n~ generates the list of all possible ~n~-bit strings with ~m~ bits set to ~1~. Algorithm adapted from [[https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation][Bit twiddling hacks]] | + * | ~popcount~ | Returns the number of bits set to one in the bit string | + * | ~to_list~ | Converts a bit string into a list of integers indicating the positions where the bits are set to ~1~. The first value for the position is not ~0~ but ~1~ | *) -(* [[file:../bitstring.org::*~of_int~][~of_int~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:2]] *) let of_int x = One (One.of_int x) -(* ~of_int~:2 ends here *) +(* General implementation:2 ends here *) - - -(* Creates a bit string from an ~Z.t~ multi-precision integer. *) - - -(* [[file:../bitstring.org::*~of_z~][~of_z~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:4]] *) let of_z x = if Z.numbits x < 64 then One (Z.to_int x) else Many (Many.of_z x) -(* ~of_z~:2 ends here *) +(* General implementation:4 ends here *) - - -(* ~zero n~ creates a zero bit string with ~n~ bits. *) - - -(* [[file:../bitstring.org::*~zero~][~zero~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:6]] *) let zero = function | n when n < 64 -> One (One.zero) | _ -> Many (Many.zero) -(* ~zero~:2 ends here *) +(* General implementation:6 ends here *) - - -(* Returns the number of bits used to represent the bit string. *) - - -(* [[file:../bitstring.org::*~numbits~][~numbits~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:7]] *) let numbits = function | One x -> One.numbits x | Many x -> Many.numbits x -(* ~numbits~:2 ends here *) +(* General implementation:7 ends here *) - - -(* True if all the bits of the bit string are zero. *) - - -(* [[file:../bitstring.org::*~is_zero~][~is_zero~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:8]] *) let is_zero = function | One x -> One.is_zero x | Many x -> Many.is_zero x -(* ~is_zero~:2 ends here *) +(* General implementation:8 ends here *) - - -(* Returns the negative of the integer interpretation of the bit string. - * - * #+begin_example - * neg (of_int x) = neg (of_int (-x)) - * #+end_example *) - - -(* [[file:../bitstring.org::*~neg~][~neg~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:9]] *) let neg = function | One x -> One (One.neg x) | Many x -> Many (Many.neg x) -(* ~neg~:2 ends here *) +(* General implementation:9 ends here *) - - -(* ~shift_left t n~ returns a new bit strings with all the bits - * shifted ~n~ positions to the left. *) - - -(* [[file:../bitstring.org::*~shift_left~][~shift_left~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:10]] *) let shift_left x i = match x with | One x -> One (One.shift_left x i) | Many x -> Many (Many.shift_left x i) -(* ~shift_left~:2 ends here *) +(* General implementation:10 ends here *) - - -(* ~shift_right t n~ returns a new bit strings with all the bits - * shifted ~n~ positions to the right. *) - - -(* [[file:../bitstring.org::*~shift_right~][~shift_right~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:12]] *) let shift_right x i = match x with | One x -> One (One.shift_right x i) | Many x -> Many (Many.shift_right x i) -(* ~shift_right~:2 ends here *) +(* General implementation:12 ends here *) - - -(* ~shift_left_one size n~ returns a new bit strings with the ~n~-th - * bit set to one. - * It is equivalent as shifting ~1~ by ~n~ bits to the left. - * ~size~ is the total number of bits of the bit string. *) - - -(* [[file:../bitstring.org::*~shift_left_one~][~shift_left_one~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:14]] *) let shift_left_one = function | n when n < 64 -> fun i -> One (One.shift_left_one i) | _ -> fun i -> Many (Many.shift_left_one i) -(* ~shift_left_one~:2 ends here *) +(* General implementation:14 ends here *) - - -(* ~testbit t n~ is true if the ~n~-th bit of the bit string ~t~ is - * set to ~1~. *) - - -(* [[file:../bitstring.org::*~testbit~][~testbit~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:16]] *) let testbit = function | One x -> One.testbit x | Many x -> Many.testbit x -(* ~testbit~:2 ends here *) +(* General implementation:16 ends here *) - - -(* Bitwise logical or. *) - - -(* [[file:../bitstring.org::*~logor~][~logor~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:18]] *) let logor a b = match a,b with | One a, One b -> One (One.logor a b) | Many a, Many b -> Many (Many.logor a b) | _ -> invalid_arg "Bitstring.logor" -(* ~logor~:2 ends here *) +(* General implementation:18 ends here *) - - -(* Bitwise logical exclusive or. *) - - -(* [[file:../bitstring.org::*~logxor~][~logxor~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:20]] *) let logxor a b = match a,b with | One a, One b -> One (One.logxor a b) | Many a, Many b -> Many (Many.logxor a b) | _ -> invalid_arg "Bitstring.logxor" -(* ~logxor~:2 ends here *) +(* General implementation:20 ends here *) - - -(* Bitwise logical and. *) - - -(* [[file:../bitstring.org::*~logand~][~logand~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:22]] *) let logand a b = match a,b with | One a, One b -> One (One.logand a b) | Many a, Many b -> Many (Many.logand a b) | _ -> invalid_arg "Bitstring.logand" -(* ~logand~:2 ends here *) +(* General implementation:22 ends here *) - - -(* Bitwise logical negation. *) - - -(* [[file:../bitstring.org::*~lognot~][~lognot~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:24]] *) let lognot = function | One x -> One (One.lognot x) | Many x -> Many (Many.lognot x) -(* ~lognot~:2 ends here *) +(* General implementation:24 ends here *) -(* Takes the integer representation of the bit string and removes one. - * - * #+begin_example + +(* #+begin_example * minus_one (of_int 10) = of_int 9 * #+end_example *) -(* [[file:../bitstring.org::*~minus_one~][~minus_one~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:25]] *) let minus_one = function | One x -> One (One.minus_one x) | Many x -> Many (Many.minus_one x) -(* ~minus_one~:2 ends here *) +(* General implementation:25 ends here *) -(* Takes the integer representation of the bit string and adds one. - * - * #+begin_example + +(* #+begin_example * plus_one (of_int 10) = of_int 11 * #+end_example *) -(* [[file:../bitstring.org::*~plus_one~][~plus_one~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:26]] *) let plus_one = function | One x -> One (One.plus_one x) | Many x -> Many (Many.plus_one x) -(* ~plus_one~:2 ends here *) +(* General implementation:26 ends here *) - - -(* Returns the number of trailing zeros in the bit string. *) - - -(* [[file:../bitstring.org::*~trailing_zeros~][~trailing_zeros~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:27]] *) let trailing_zeros = function | One x -> One.trailing_zeros x | Many x -> Many.trailing_zeros x -(* ~trailing_zeros~:2 ends here *) +(* General implementation:27 ends here *) - - -(* Returns the Hamming distance, i.e. the number of bits differing - * between two bit strings. *) - - -(* [[file:../bitstring.org::*~hamdist~][~hamdist~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:28]] *) let hamdist a b = match a, b with | One a, One b -> One.hamdist a b | Many a, Many b -> Many.hamdist a b | _ -> invalid_arg "Bitstring.hamdist" -(* ~hamdist~:2 ends here *) +(* General implementation:28 ends here *) - - -(* Returns the number of bits set to one in the bit string. *) - - -(* [[file:../bitstring.org::*~popcount~][~popcount~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:29]] *) let popcount = function | One x -> One.popcount x | Many x -> Many.popcount x -(* ~popcount~:2 ends here *) +(* General implementation:29 ends here *) -(* Converts a bit string into a list of integers indicating the - * positions where the bits are set to ~1~. The first value for the - * position is not ~0~ but ~1~. - * - * #+begin_example + +(* #+begin_example * Bitstring.to_list (of_int 5);; * - : int list = [1; 3] * #+end_example *) -(* [[file:../bitstring.org::*~to_list~][~to_list~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:30]] *) let rec to_list ?(accu=[]) = function | t when (is_zero t) -> List.rev accu | t -> let newlist = @@ -331,15 +255,11 @@ let rec to_list ?(accu=[]) = function in logand t @@ minus_one t |> (to_list [@tailcall]) ~accu:newlist -(* ~to_list~:2 ends here *) +(* General implementation:30 ends here *) -(* ~permutations m n~ generates the list of all possible ~n~-bit - * strings with ~m~ bits set to ~1~. - * Algorithm adapted from [[https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation][Bit twiddling hacks]]. - * - * #+begin_example +(* #+begin_example * Bitstring.permutations 2 4 * |> List.map (fun x -> Format.asprintf "%a" Bitstring.pp x) ;; * - : string list = @@ -352,7 +272,7 @@ let rec to_list ?(accu=[]) = function * #+end_example *) -(* [[file:../bitstring.org::*~permutations~][~permutations~:2]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:32]] *) let permutations m n = let rec aux k u rest = @@ -370,7 +290,7 @@ let permutations m n = (aux [@tailcall]) (k-1) (logor t' t'') (u :: rest) in aux (Util.binom n m) (minus_one (shift_left_one n m)) [] -(* ~permutations~:2 ends here *) +(* General implementation:32 ends here *) (* [[file:../bitstring.org::*Printers][Printers:2]] *) let pp ppf = function diff --git a/common/lib/bitstring.mli b/common/lib/bitstring.mli index bcb7ba0..3f1afdf 100644 --- a/common/lib/bitstring.mli +++ b/common/lib/bitstring.mli @@ -5,153 +5,38 @@ type t (* Type:1 ends here *) -(* ~of_int~ *) +(* General implementation *) -(* [[file:../bitstring.org::*~of_int~][~of_int~:1]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:1]] *) val of_int : int -> t -(* ~of_int~:1 ends here *) +val of_z : Z.t -> t +val zero : int -> t -(* ~of_z~ *) - - -(* [[file:../bitstring.org::*~of_z~][~of_z~:1]] *) -val of_z : Z.t -> t -(* ~of_z~:1 ends here *) - -(* ~zero~ *) - - -(* [[file:../bitstring.org::*~zero~][~zero~:1]] *) -val zero : int -> t -(* ~zero~:1 ends here *) - -(* ~numbits~ *) - - -(* [[file:../bitstring.org::*~numbits~][~numbits~:1]] *) -val numbits : t -> int -(* ~numbits~:1 ends here *) - -(* ~is_zero~ *) - - -(* [[file:../bitstring.org::*~is_zero~][~is_zero~:1]] *) val is_zero : t -> bool -(* ~is_zero~:1 ends here *) - -(* ~neg~ *) - - -(* [[file:../bitstring.org::*~neg~][~neg~:1]] *) -val neg : t -> t -(* ~neg~:1 ends here *) - -(* ~shift_left~ *) - - -(* [[file:../bitstring.org::*~shift_left~][~shift_left~:1]] *) -val shift_left : t -> int -> t -(* ~shift_left~:1 ends here *) - -(* ~shift_right~ *) - - -(* [[file:../bitstring.org::*~shift_right~][~shift_right~:1]] *) -val shift_right : t -> int -> t -(* ~shift_right~:1 ends here *) - -(* ~shift_left_one~ *) - - -(* [[file:../bitstring.org::*~shift_left_one~][~shift_left_one~:1]] *) -val shift_left_one : int -> int -> t -(* ~shift_left_one~:1 ends here *) - -(* ~testbit~ *) - - -(* [[file:../bitstring.org::*~testbit~][~testbit~:1]] *) +val numbits : t -> int val testbit : t -> int -> bool -(* ~testbit~:1 ends here *) -(* ~logor~ *) +val neg : t -> t +val shift_left : t -> int -> t +val shift_right : t -> int -> t +val shift_left_one : int -> int -> t - -(* [[file:../bitstring.org::*~logor~][~logor~:1]] *) -val logor : t -> t -> t -(* ~logor~:1 ends here *) - -(* ~logxor~ *) - - -(* [[file:../bitstring.org::*~logxor~][~logxor~:1]] *) +val logor : t -> t -> t val logxor : t -> t -> t -(* ~logxor~:1 ends here *) - -(* ~logand~ *) - - -(* [[file:../bitstring.org::*~logand~][~logand~:1]] *) val logand : t -> t -> t -(* ~logand~:1 ends here *) - -(* ~lognot~ *) - - -(* [[file:../bitstring.org::*~lognot~][~lognot~:1]] *) val lognot : t -> t -(* ~lognot~:1 ends here *) -(* ~minus_one~ *) - - -(* [[file:../bitstring.org::*~minus_one~][~minus_one~:1]] *) +val plus_one : t -> t val minus_one : t -> t -(* ~minus_one~:1 ends here *) -(* ~plus_one~ *) - - - -(* [[file:../bitstring.org::*~plus_one~][~plus_one~:1]] *) -val plus_one : t -> t -(* ~plus_one~:1 ends here *) - -(* ~trailing_zeros~ *) - - -(* [[file:../bitstring.org::*~trailing_zeros~][~trailing_zeros~:1]] *) +val hamdist : t -> t -> int val trailing_zeros : t -> int -(* ~trailing_zeros~:1 ends here *) +val popcount : t -> int -(* ~hamdist~ *) - - -(* [[file:../bitstring.org::*~hamdist~][~hamdist~:1]] *) -val hamdist : t -> t -> int -(* ~hamdist~:1 ends here *) - -(* ~popcount~ *) - - -(* [[file:../bitstring.org::*~popcount~][~popcount~:1]] *) -val popcount : t -> int -(* ~popcount~:1 ends here *) - -(* ~to_list~ *) - - -(* [[file:../bitstring.org::*~to_list~][~to_list~:1]] *) -val to_list : ?accu:(int list) -> t -> int list -(* ~to_list~:1 ends here *) - -(* ~permutations~ *) - - -(* [[file:../bitstring.org::*~permutations~][~permutations~:1]] *) +val to_list : ?accu:(int list) -> t -> int list val permutations : int -> int -> t list -(* ~permutations~:1 ends here *) +(* General implementation:1 ends here *) (* Printers *) diff --git a/common/lib/charge.ml b/common/lib/charge.ml index f750101..918d22a 100644 --- a/common/lib/charge.ml +++ b/common/lib/charge.ml @@ -1,18 +1,19 @@ + + +(* This type should be used for all charges in the program (electrons, nuclei,...). *) + + (* [[file:../charge.org::*Type][Type:2]] *) type t = float (* Type:2 ends here *) -(* [[file:../charge.org::*~of_float~ / ~to_float~][~of_float~ / ~to_float~:2]] *) +(* [[file:../charge.org::*Conversions][Conversions:2]] *) external of_float : float -> t = "%identity" external to_float : t -> float = "%identity" -(* ~of_float~ / ~to_float~:2 ends here *) -(* [[file:../charge.org::*~of_int~ / ~to_int~][~of_int~ / ~to_int~:2]] *) let of_int = float_of_int let to_int = int_of_float -(* ~of_int~ / ~to_int~:2 ends here *) -(* [[file:../charge.org::*~of_string~ / ~to_string~][~of_string~ / ~to_string~:2]] *) let of_string = float_of_string let to_string x = @@ -22,7 +23,7 @@ let to_string x = Printf.sprintf "%f" x else "0.0" -(* ~of_string~ / ~to_string~:2 ends here *) +(* Conversions:2 ends here *) (* [[file:../charge.org::*Simple operations][Simple operations:2]] *) let gen_op op = diff --git a/common/lib/charge.mli b/common/lib/charge.mli index 85d32c7..ef53760 100644 --- a/common/lib/charge.mli +++ b/common/lib/charge.mli @@ -1,35 +1,23 @@ -(* Type - * - * This type should be used for all charges in the program (electrons, nuclei,...). *) +(* Type *) (* [[file:../charge.org::*Type][Type:1]] *) type t (* Type:1 ends here *) -(* ~of_float~ / ~to_float~ *) +(* Conversions *) -(* [[file:../charge.org::*~of_float~ / ~to_float~][~of_float~ / ~to_float~:1]] *) +(* [[file:../charge.org::*Conversions][Conversions:1]] *) val of_float : float -> t val to_float : t -> float -(* ~of_float~ / ~to_float~:1 ends here *) -(* ~of_int~ / ~to_int~ *) - - -(* [[file:../charge.org::*~of_int~ / ~to_int~][~of_int~ / ~to_int~:1]] *) val of_int : int -> t val to_int : t -> int -(* ~of_int~ / ~to_int~:1 ends here *) -(* ~of_string~ / ~to_string~ *) - - -(* [[file:../charge.org::*~of_string~ / ~to_string~][~of_string~ / ~to_string~:1]] *) val of_string: string -> t val to_string: t -> string -(* ~of_string~ / ~to_string~:1 ends here *) +(* Conversions:1 ends here *) (* Simple operations *) diff --git a/common/lib/command_line.ml b/common/lib/command_line.ml index 5979ea3..9420d9a 100644 --- a/common/lib/command_line.ml +++ b/common/lib/command_line.ml @@ -151,19 +151,15 @@ let output_long max_width x = -(* Returns the list of anonymous arguments *) +(* | ~get~ | Returns the argument associated with a long option | + * | ~get_bool~ | True if the ~Optional~ argument is present in the command-line | + * | ~anon_args~ | Returns the list of anonymous arguments | *) -(* [[file:../command_line.org::*~anon_args~][~anon_args~:2]] *) + +(* [[file:../command_line.org::*Query functions][Query functions:2]] *) let anon_args () = !anon_args_ref -(* ~anon_args~:2 ends here *) -(* ~help~ :noexport: - * - * Prints the documentation of the program. *) - - -(* [[file:../command_line.org::*~help~][~help~:1]] *) let help () = (* Print the header *) @@ -229,27 +225,15 @@ let help () = (* Print footer *) output_text !footer_doc; Format.printf "@." -(* ~help~:1 ends here *) - -(* Returns the argument associated with a long option. *) - - -(* [[file:../command_line.org::*~get~][~get~:2]] *) let get x = try Some (Hashtbl.find dict x) with Not_found -> None -(* ~get~:2 ends here *) - -(* True if the ~Optional~ argument is present in the command-line *) - - -(* [[file:../command_line.org::*~get_bool~][~get_bool~:2]] *) let get_bool x = Hashtbl.mem dict x -(* ~get_bool~:2 ends here *) +(* Query functions:2 ends here *) diff --git a/common/lib/command_line.mli b/common/lib/command_line.mli index f1e8a15..d574ebb 100644 --- a/common/lib/command_line.mli +++ b/common/lib/command_line.mli @@ -28,26 +28,14 @@ val set_footer_doc : string -> unit val anonymous : long_opt -> optional -> documentation -> description (* Mutable attributes:4 ends here *) -(* ~anon_args~ *) +(* Query functions *) -(* [[file:../command_line.org::*~anon_args~][~anon_args~:1]] *) +(* [[file:../command_line.org::*Query functions][Query functions:1]] *) +val get : long_opt -> string option +val get_bool : long_opt -> bool val anon_args : unit -> string list -(* ~anon_args~:1 ends here *) - -(* ~get~ *) - - -(* [[file:../command_line.org::*~get~][~get~:1]] *) -val get : long_opt -> string option -(* ~get~:1 ends here *) - -(* ~get_bool~ *) - - -(* [[file:../command_line.org::*~get_bool~][~get_bool~:1]] *) -val get_bool : long_opt -> bool -(* ~get_bool~:1 ends here *) +(* Query functions:1 ends here *) (* Specification *) diff --git a/common/lib/coordinate.ml b/common/lib/coordinate.ml index fed9d11..a70b823 100644 --- a/common/lib/coordinate.ml +++ b/common/lib/coordinate.ml @@ -15,130 +15,126 @@ type t = bohr point type axis = X | Y | Z (* Type:2 ends here *) -(* [[file:../coordinate.org::*~make~][~make~:2]] *) -external make : 'a point -> t = "%identity" -(* ~make~:2 ends here *) -(* [[file:../coordinate.org::*~make_angstrom~][~make_angstrom~:2]] *) + +(* | ~make~ | Creates a point in atomic units | + * | ~make_angstrom~ | Creates a point in angstrom | + * | ~zero~ | $(0., 0., 0.)$ | *) + + +(* [[file:../coordinate.org::*Creation][Creation:2]] *) +external make : 'a point -> t = "%identity" external make_angstrom : 'a point -> angstrom point = "%identity" -(* ~make_angstrom~:2 ends here *) -(* [[file:../coordinate.org::*~bohr_to_angstrom~][~bohr_to_angstrom~:2]] *) -let b_to_a b = Constants.a0 *. b +let zero = + make { x = 0. ; y = 0. ; z = 0. } +(* Creation:2 ends here *) + + +(* | ~bohr_to_angstrom~ | Converts a point in bohr to angstrom | + * | ~angstrom_to_bohr~ | Converts a point in angstrom to bohr | *) + + +(* [[file:../coordinate.org::*Conversion][Conversion:2]] *) +let b_to_a b = Constants.a0 *. b let bohr_to_angstrom { x ; y ; z } = make { x = b_to_a x ; y = b_to_a y ; z = b_to_a z ; } -(* ~bohr_to_angstrom~:2 ends here *) -(* [[file:../coordinate.org::*~angstrom_to_bohr~][~angstrom_to_bohr~:2]] *) -let a_to_b a = Constants.a0_inv *. a +let a_to_b a = Constants.a0_inv *. a let angstrom_to_bohr { x ; y ; z } = make { x = a_to_b x ; y = a_to_b y ; z = a_to_b z ; } -(* ~angstrom_to_bohr~:2 ends here *) - -(* [[file:../coordinate.org::*~zero~][~zero~:2]] *) -let zero = - make { x = 0. ; y = 0. ; z = 0. } -(* ~zero~:2 ends here *) - -(* [[file:../coordinate.org::*~get~][~get~:2]] *) -let get axis { x ; y ; z } = - match axis with - | X -> x - | Y -> y - | Z -> z -(* ~get~:2 ends here *) +(* Conversion:2 ends here *) -(* #+begin_example +(* | ~neg~ | Negative of a point | + * | ~get~ | Extracts the projection of the coordinate on an axis | + * | ~dot~ | Dot product | + * | ~norm~ | $\ell{^2}$ norm of the vector | + * | ~¦.~ | Scales the vector by a constant | + * | ~¦+~ | Adds two vectors | + * | ~¦-~ | Subtracts two vectors | + * + * #+begin_example + * Coordinate.neg { x=1. ; y=2. ; z=3. } ;; + * - : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -2.; z = -3.} + * + * Coordinate.(get Y { x=1. ; y=2. ; z=3. }) ;; + * - : float = 2. + * * Coordinate.( * 2. |. { x=1. ; y=2. ; z=3. } * ) ;; * - : Coordinate.t = {Qcaml.Common.Coordinate.x = 2.; y = 4.; z = 6.} - * #+end_example *) - - -(* [[file:../coordinate.org::*Scale][Scale:2]] *) -let ( |. ) s { x ; y ; z } = - make { x = s *. x ; - y = s *. y ; - z = s *. z ; } -(* Scale:2 ends here *) - - - -(* #+begin_example + * * Coordinate.( * { x=1. ; y=2. ; z=3. } |+ { x=2. ; y=3. ; z=1. } * );; * - : Coordinate.t = {Qcaml.Common.Coordinate.x = 3.; y = 5.; z = 4.} - * #+end_example *) + * + * Coordinate.( + * { x=1. ; y=2. ; z=3. } |- { x=2. ; y=3. ; z=1. } + * );; + * - : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -1.; z = 2.} + * #+end_example *) + + + +(* [[file:../coordinate.org::*Vector operations][Vector operations:2]] *) +let get axis { x ; y ; z } = + match axis with + | X -> x + | Y -> y + | Z -> z + + +let ( |. ) s { x ; y ; z } = + make { x = s *. x ; + y = s *. y ; + z = s *. z ; } -(* [[file:../coordinate.org::*Add][Add:2]] *) let ( |+ ) { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = make { x = x1 +. x2 ; y = y1 +. y2 ; z = z1 +. z2 ; } -(* Add:2 ends here *) - -(* #+begin_example - * Coordinate.( - * { x=1. ; y=2. ; z=3. } |- { x=2. ; y=3. ; z=1. } - * );; - * - : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -1.; z = 2.} - * #+end_example *) - - -(* [[file:../coordinate.org::*Subtract][Subtract:2]] *) let ( |- ) { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = make { x = x1 -. x2 ; y = y1 -. y2 ; z = z1 -. z2 ; } -(* Subtract:2 ends here *) - -(* #+begin_example - * Coordinate.neg { x=1. ; y=2. ; z=3. } ;; - * - : Coordinate.t = {Qcaml.Common.Coordinate.x = -1.; y = -2.; z = -3.} - * #+end_example *) - - -(* [[file:../coordinate.org::*Negative][Negative:2]] *) let neg a = -1. |. a -(* Negative:2 ends here *) -(* [[file:../coordinate.org::*Dot product][Dot product:2]] *) + let dot { x = x1 ; y = y1 ; z = z1 } { x = x2 ; y = y2 ; z = z2 } = x1 *. x2 +. y1 *. y2 +. z1 *. z2 -(* Dot product:2 ends here *) - -(* $\ell{^2}$ norm of the vector. *) - - -(* [[file:../coordinate.org::*Norm][Norm:2]] *) let norm u = sqrt ( dot u u ) -(* Norm:2 ends here *) +(* Vector operations:2 ends here *) + + + +(* Coordinates can be printed in bohr or angstrom. *) + (* [[file:../coordinate.org::*Printers][Printers:2]] *) open Format diff --git a/common/lib/coordinate.mli b/common/lib/coordinate.mli index 5c73384..3b4dae3 100644 --- a/common/lib/coordinate.mli +++ b/common/lib/coordinate.mli @@ -19,110 +19,37 @@ type t = bohr point type axis = X | Y | Z (* types ends here *) -(* ~make~ - * - * Creates a point in atomic units. *) +(* Creation *) -(* [[file:../coordinate.org::*~make~][~make~:1]] *) -val make : 'a point -> t -(* ~make~:1 ends here *) - -(* ~make_angstrom~ - * - * Creates a point in angstrom. *) - - -(* [[file:../coordinate.org::*~make_angstrom~][~make_angstrom~:1]] *) +(* [[file:../coordinate.org::*Creation][Creation:1]] *) +val make : 'a point -> t val make_angstrom : 'a point -> angstrom point -(* ~make_angstrom~:1 ends here *) +val zero : bohr point +(* Creation:1 ends here *) -(* ~bohr_to_angstrom~ - * - * Converts a point in bohr to angstrom. *) +(* Conversion *) -(* [[file:../coordinate.org::*~bohr_to_angstrom~][~bohr_to_angstrom~:1]] *) +(* [[file:../coordinate.org::*Conversion][Conversion:1]] *) val bohr_to_angstrom : bohr point -> angstrom point -(* ~bohr_to_angstrom~:1 ends here *) - -(* ~angstrom_to_bohr~ - * - * Converts a point in angstrom to bohr. *) - - -(* [[file:../coordinate.org::*~angstrom_to_bohr~][~angstrom_to_bohr~:1]] *) val angstrom_to_bohr : angstrom point -> bohr point -(* ~angstrom_to_bohr~:1 ends here *) +(* Conversion:1 ends here *) -(* ~zero~ - * - * ~zero~ = (0., 0., 0.) *) +(* Vector operations *) -(* [[file:../coordinate.org::*~zero~][~zero~:1]] *) -val zero : bohr point -(* ~zero~:1 ends here *) - -(* ~get~ - * - * Extracts the projection of the coordinate on an axis. - * - * #+begin_example - * Coordinate.(get Y { x=1. ; y=2. ; z=3. }) - * - float: 2. - * #+end_example *) - - -(* [[file:../coordinate.org::*~get~][~get~:1]] *) -val get : axis -> bohr point -> float -(* ~get~:1 ends here *) - -(* Scale *) - - -(* [[file:../coordinate.org::*Scale][Scale:1]] *) +(* [[file:../coordinate.org::*Vector operations][Vector operations:1]] *) +val neg : t -> t +val get : axis -> bohr point -> float +val dot : t -> t -> float +val norm : t -> float val ( |. ) : float -> t -> t -(* Scale:1 ends here *) - -(* Add *) - - -(* [[file:../coordinate.org::*Add][Add:1]] *) val ( |+ ) : t -> t -> t -(* Add:1 ends here *) - -(* Subtract *) - - -(* [[file:../coordinate.org::*Subtract][Subtract:1]] *) val ( |- ) : t -> t -> t -(* Subtract:1 ends here *) +(* Vector operations:1 ends here *) -(* Negative *) - - -(* [[file:../coordinate.org::*Negative][Negative:1]] *) -val neg : t -> t -(* Negative:1 ends here *) - -(* Dot product *) - - -(* [[file:../coordinate.org::*Dot product][Dot product:1]] *) -val dot : t -> t -> float -(* Dot product:1 ends here *) - -(* Norm *) - - -(* [[file:../coordinate.org::*Norm][Norm:1]] *) -val norm : t -> float -(* Norm:1 ends here *) - -(* Printers - * - * Coordinates can be printed in bohr or angstrom. *) +(* Printers *) (* [[file:../coordinate.org::*Printers][Printers:1]] *) diff --git a/common/lib/non_negative_float.ml b/common/lib/non_negative_float.ml index 43391c2..d6f34b8 100644 --- a/common/lib/non_negative_float.ml +++ b/common/lib/non_negative_float.ml @@ -1,10 +1,19 @@ -type t = float +(* [[file:../non_negative_float.org::*Type][Type:2]] *) +type t = float +(* Type:2 ends here *) + + +(* The ~of_float~ function checks that the float is non-negative. + * The unsafe variant doesn't do this check. *) + + +(* [[file:../non_negative_float.org::*Conversions][Conversions:2]] *) let of_float x = if x < 0. then invalid_arg (__FILE__^": of_float"); x -external to_float : t -> float = "%identity" +external to_float : t -> float = "%identity" external unsafe_of_float : float -> t = "%identity" let to_string x = @@ -12,4 +21,4 @@ let to_string x = let of_string x = let f = float_of_string x in of_float f - +(* Conversions:2 ends here *) diff --git a/common/lib/non_negative_float.mli b/common/lib/non_negative_float.mli index 1bd9c06..83f98f3 100644 --- a/common/lib/non_negative_float.mli +++ b/common/lib/non_negative_float.mli @@ -1,7 +1,18 @@ -(** Floats >= 0. *) +(* Type *) + + +(* [[file:../non_negative_float.org::*Type][Type:1]] *) type t = private float -val of_float : float -> t +(* Type:1 ends here *) + +(* Conversions *) + + +(* [[file:../non_negative_float.org::*Conversions][Conversions:1]] *) +val of_float : float -> t val unsafe_of_float : float -> t -val to_float : t -> float -val to_string : t -> string -val of_string : string -> t +val to_float : t -> float + +val of_string : string -> t +val to_string : t -> string +(* Conversions:1 ends here *) diff --git a/common/non_negative_float.org b/common/non_negative_float.org new file mode 100644 index 0000000..d8356a9 --- /dev/null +++ b/common/non_negative_float.org @@ -0,0 +1,54 @@ +#+begin_src elisp tangle: no :results none :exports none +(setq pwd (file-name-directory buffer-file-name)) +(setq name (file-name-nondirectory (substring buffer-file-name 0 -4))) +(setq lib (concat pwd "lib/")) +(setq testdir (concat pwd "test/")) +(setq mli (concat lib name ".mli")) +(setq ml (concat lib name ".ml")) +(setq test-ml (concat testdir name ".ml")) +(org-babel-tangle) +#+end_src + +* Non-negative float + :PROPERTIES: + :header-args: :noweb yes :comments both + :END: + +** Type + + #+begin_src ocaml :tangle (eval mli) +type t = private float + #+end_src + + #+begin_src ocaml :tangle (eval ml) :exports none +type t = float + #+end_src + +** Conversions + + #+begin_src ocaml :tangle (eval mli) +val of_float : float -> t +val unsafe_of_float : float -> t +val to_float : t -> float + +val of_string : string -> t +val to_string : t -> string + #+end_src + + The ~of_float~ function checks that the float is non-negative. + The unsafe variant doesn't do this check. + + #+begin_src ocaml :tangle (eval ml) :exports none +let of_float x = + if x < 0. then invalid_arg (__FILE__^": of_float"); + x + +external to_float : t -> float = "%identity" +external unsafe_of_float : float -> t = "%identity" + +let to_string x = + let f = to_float x in string_of_float f + +let of_string x = + let f = float_of_string x in of_float f + #+end_src diff --git a/common/test/bitstring.ml b/common/test/bitstring.ml index eb1b000..997be01 100644 --- a/common/test/bitstring.ml +++ b/common/test/bitstring.ml @@ -3,7 +3,8 @@ (* [[file:../bitstring.org::*Tests header][Tests header:1]] *) open Common.Bitstring -let check msg x = Alcotest.(check bool) msg true x +let check_bool = Alcotest.(check bool) +let check msg x = check_bool msg true x let test_all () = let x = 8745687 in let one_x = of_int x in @@ -11,63 +12,63 @@ let test_all () = let many_x = of_z z in (* Tests header:1 ends here *) -(* [[file:../bitstring.org::*~of_int~][~of_int~:3]] *) -Alcotest.(check bool) "of_x" true (one_x = (of_int x)); -(* ~of_int~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:3]] *) +check_bool "of_x" true (one_x = (of_int x)); +(* General implementation:3 ends here *) -(* [[file:../bitstring.org::*~of_z~][~of_z~:3]] *) -Alcotest.(check bool) "of_z" true (one_x = (of_z (Z.of_int x))); -(* ~of_z~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:5]] *) +check_bool "of_z" true (one_x = (of_z (Z.of_int x))); +(* General implementation:5 ends here *) -(* [[file:../bitstring.org::*~shift_left~][~shift_left~:3]] *) -Alcotest.(check bool) "shift_left1" true (of_int (x lsl 3) = shift_left one_x 3); -Alcotest.(check bool) "shift_left2" true (of_z (Z.shift_left z 3) = shift_left many_x 3); -Alcotest.(check bool) "shift_left3" true (of_z (Z.shift_left z 100) = shift_left many_x 100); -(* ~shift_left~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:11]] *) +check_bool "shift_left1" true (of_int (x lsl 3) = shift_left one_x 3); +check_bool "shift_left2" true (of_z (Z.shift_left z 3) = shift_left many_x 3); +check_bool "shift_left3" true (of_z (Z.shift_left z 100) = shift_left many_x 100); +(* General implementation:11 ends here *) -(* [[file:../bitstring.org::*~shift_right~][~shift_right~:3]] *) -Alcotest.(check bool) "shift_right1" true (of_int (x lsr 3) = shift_right one_x 3); -Alcotest.(check bool) "shift_right2" true (of_z (Z.shift_right z 3) = shift_right many_x 3); -(* ~shift_right~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:13]] *) +check_bool "shift_right1" true (of_int (x lsr 3) = shift_right one_x 3); +check_bool "shift_right2" true (of_z (Z.shift_right z 3) = shift_right many_x 3); +(* General implementation:13 ends here *) -(* [[file:../bitstring.org::*~shift_left_one~][~shift_left_one~:3]] *) -Alcotest.(check bool) "shift_left_one1" true (of_int (1 lsl 3) = shift_left_one 4 3); -Alcotest.(check bool) "shift_left_one2" true (of_z (Z.shift_left Z.one 200) = shift_left_one 300 200); -(* ~shift_left_one~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:15]] *) +check_bool "shift_left_one1" true (of_int (1 lsl 3) = shift_left_one 4 3); +check_bool "shift_left_one2" true (of_z (Z.shift_left Z.one 200) = shift_left_one 300 200); +(* General implementation:15 ends here *) -(* [[file:../bitstring.org::*~testbit~][~testbit~:3]] *) -Alcotest.(check bool) "testbit1" true (testbit (of_int 8) 3); -Alcotest.(check bool) "testbit2" false (testbit (of_int 8) 2); -Alcotest.(check bool) "testbit3" false (testbit (of_int 8) 4); -Alcotest.(check bool) "testbit4" true (testbit (of_z (Z.of_int 8)) 3); -Alcotest.(check bool) "testbit5" false (testbit (of_z (Z.of_int 8)) 2); -Alcotest.(check bool) "testbit6" false (testbit (of_z (Z.of_int 8)) 4); -(* ~testbit~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:17]] *) +check_bool "testbit1" true (testbit (of_int 8) 3); +check_bool "testbit2" false (testbit (of_int 8) 2); +check_bool "testbit3" false (testbit (of_int 8) 4); +check_bool "testbit4" true (testbit (of_z (Z.of_int 8)) 3); +check_bool "testbit5" false (testbit (of_z (Z.of_int 8)) 2); +check_bool "testbit6" false (testbit (of_z (Z.of_int 8)) 4); +(* General implementation:17 ends here *) -(* [[file:../bitstring.org::*~logor~][~logor~:3]] *) -Alcotest.(check bool) "logor1" true (of_int (1 lor 2) = logor (of_int 1) (of_int 2)); -Alcotest.(check bool) "logor2" true (of_z (Z.of_int (1 lor 2)) = logor (of_z Z.one) (of_z (Z.of_int 2))); -(* ~logor~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:19]] *) +check_bool "logor1" true (of_int (1 lor 2) = logor (of_int 1) (of_int 2)); +check_bool "logor2" true (of_z (Z.of_int (1 lor 2)) = logor (of_z Z.one) (of_z (Z.of_int 2))); +(* General implementation:19 ends here *) -(* [[file:../bitstring.org::*~logxor~][~logxor~:3]] *) -Alcotest.(check bool) "logxor1" true (of_int (1 lxor 2) = logxor (of_int 1) (of_int 2)); -Alcotest.(check bool) "logxor2" true (of_z (Z.of_int (1 lxor 2)) = logxor (of_z Z.one) (of_z (Z.of_int 2))); -(* ~logxor~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:21]] *) +check_bool "logxor1" true (of_int (1 lxor 2) = logxor (of_int 1) (of_int 2)); +check_bool "logxor2" true (of_z (Z.of_int (1 lxor 2)) = logxor (of_z Z.one) (of_z (Z.of_int 2))); +(* General implementation:21 ends here *) -(* [[file:../bitstring.org::*~logand~][~logand~:3]] *) -Alcotest.(check bool) "logand1" true (of_int (1 land 3) = logand (of_int 1) (of_int 3)); -Alcotest.(check bool) "logand2" true (of_z (Z.of_int (1 land 3)) = logand (of_z Z.one) (of_z (Z.of_int 3))); -(* ~logand~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:23]] *) +check_bool "logand1" true (of_int (1 land 3) = logand (of_int 1) (of_int 3)); +check_bool "logand2" true (of_z (Z.of_int (1 land 3)) = logand (of_z Z.one) (of_z (Z.of_int 3))); +(* General implementation:23 ends here *) -(* [[file:../bitstring.org::*~to_list~][~to_list~:3]] *) -Alcotest.(check bool) "to_list" true ([ 1 ; 3 ; 4 ; 6 ] = (to_list (of_int 45))); -(* ~to_list~:3 ends here *) +(* [[file:../bitstring.org::*General implementation][General implementation:31]] *) +check_bool "to_list" true ([ 1 ; 3 ; 4 ; 6 ] = (to_list (of_int 45))); +(* General implementation:31 ends here *) -(* [[file:../bitstring.org::*~permutations~][~permutations~:3]] *) +(* [[file:../bitstring.org::*General implementation][General implementation:33]] *) check "permutations" (permutations 2 4 = List.map of_int [ 3 ; 5 ; 6 ; 9 ; 10 ; 12 ]); -(* ~permutations~:3 ends here *) +(* General implementation:33 ends here *) (* Tests :noexport: *) diff --git a/docs/config.el b/docs/config.el index 84c9a19..6349c11 100755 --- a/docs/config.el +++ b/docs/config.el @@ -69,6 +69,9 @@ with class 'color and highest min-color value." (advice-add 'face-attribute :override #'my-face-attribute) +(setq org-html-htmlize-output-type 'css) ; default: 'inline-css +(setq org-html-htmlize-font-prefix "org-") ; default: "org-" + (setq ml "ml") (setq mli "mli") (setq test-ml "test-ml")