From b08079c09d95508264ac54134328b64de7331ad4 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sun, 27 Dec 2020 13:43:55 +0100 Subject: [PATCH] Added Charge.org --- common/angular_momentum.org | 5 +- common/bitstring.org | 6 +-- common/charge.org | 101 ++++++++++++++++++++++++++++++++++++ common/lib/charge.ml | 48 ++++++++++------- common/lib/charge.mli | 46 ++++++++++++---- 5 files changed, 171 insertions(+), 35 deletions(-) create mode 100644 common/charge.org diff --git a/common/angular_momentum.org b/common/angular_momentum.org index ea6b92d..26cc3c5 100644 --- a/common/angular_momentum.org +++ b/common/angular_momentum.org @@ -1,6 +1,7 @@ -#+TITLE: Angular Momentum +#+begin_src elisp tangle: no :results none +(org-babel-tangle) +#+end_src -[[elisp:(org-babel-tangle)]] * Angular Momentum :PROPERTIES: diff --git a/common/bitstring.org b/common/bitstring.org index 2dee502..5d17f8e 100644 --- a/common/bitstring.org +++ b/common/bitstring.org @@ -1,6 +1,6 @@ -#+TITLE: Bit string - -[[elisp:(org-babel-tangle)]] +#+begin_src elisp tangle: no :results none +(org-babel-tangle) +#+end_src * Bit string :PROPERTIES: diff --git a/common/charge.org b/common/charge.org new file mode 100644 index 0000000..610a3e3 --- /dev/null +++ b/common/charge.org @@ -0,0 +1,101 @@ +#+begin_src elisp tangle: no :results none +(org-babel-tangle) +#+end_src + +* Charge + :PROPERTIES: + :ml: lib/charge.ml + :mli: lib/charge.mli + :test-ml: test/charge.ml + :header-args: :noweb yes :comments both + :END: + +** Type + + This type should be used for all charges in the program (electrons, nuclei,...). + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +type t + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +type t = float + #+end_src + +** Conversions + +*** ~of_float~ / ~to_float~ + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +val of_float : float -> t +val to_float : t -> float + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +external of_float : float -> t = "%identity" +external to_float : t -> float = "%identity" + #+end_src + +*** ~of_int~ / ~to_int~ + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +val of_int : int -> t +val to_int : t -> int + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +let of_int = float_of_int +let to_int = int_of_float + #+end_src + +*** ~of_string~ / ~to_string~ + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +val of_string: string -> t +val to_string: t -> string + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +let of_string = float_of_string + +let to_string x = + if x > 0. then + Printf.sprintf "+%f" x + else if x < 0. then + Printf.sprintf "%f" x + else + "0.0" + #+end_src + +** Simple operations + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +val ( + ) : t -> t -> t +val ( - ) : t -> t -> t +val ( * ) : t -> float -> t +val ( / ) : t -> float -> t + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +let gen_op op = + fun a b -> + op (to_float a) (to_float b) + |> of_float + +let ( + ) = gen_op ( +. ) +let ( - ) = gen_op ( -. ) +let ( * ) = gen_op ( *. ) +let ( / ) = gen_op ( /. ) + #+end_src + +** Printers + + #+begin_src ocaml :tangle (org-entry-get nil "mli" t) +val pp : Format.formatter -> t -> unit + #+end_src + + #+begin_src ocaml :tangle (org-entry-get nil "ml" t) +let pp ppf x = + Format.fprintf ppf "@[+%s@]" (to_string x) + #+end_src + diff --git a/common/lib/charge.ml b/common/lib/charge.ml index e587b48..f750101 100644 --- a/common/lib/charge.ml +++ b/common/lib/charge.ml @@ -1,32 +1,42 @@ +(* [[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]] *) external of_float : float -> t = "%identity" external to_float : t -> float = "%identity" +(* ~of_float~ / ~to_float~:2 ends here *) -let of_int i = float_of_int i -let of_string s = float_of_string s +(* [[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_int x = int_of_float x let to_string x = - if x >= 0. then + if x > 0. then Printf.sprintf "+%f" x - else + else if x < 0. then Printf.sprintf "%f" x + else + "0.0" +(* ~of_string~ / ~to_string~:2 ends here *) -let ( + ) a b = - (to_float a) +. (to_float b) |> of_float +(* [[file:../charge.org::*Simple operations][Simple operations:2]] *) +let gen_op op = + fun a b -> + op (to_float a) (to_float b) + |> of_float -let ( - ) a b = - (to_float a) -. (to_float b) |> of_float - -let ( * ) a b = - (to_float a) *. b |> of_float - -let ( / ) a b = - (to_float a) /. b |> of_float +let ( + ) = gen_op ( +. ) +let ( - ) = gen_op ( -. ) +let ( * ) = gen_op ( *. ) +let ( / ) = gen_op ( /. ) +(* Simple operations:2 ends here *) +(* [[file:../charge.org::*Printers][Printers:2]] *) let pp ppf x = - if x > 0. then - Format.fprintf ppf "@[+%f@]" (to_float x) - else - Format.fprintf ppf "@[%f@]" (to_float x) + Format.fprintf ppf "@[+%s@]" (to_string x) +(* Printers:2 ends here *) diff --git a/common/lib/charge.mli b/common/lib/charge.mli index 0d6a25d..85d32c7 100644 --- a/common/lib/charge.mli +++ b/common/lib/charge.mli @@ -1,25 +1,49 @@ -(** This type should be used for all charges in the program (electrons, nuclei,...). *) +(* Type + * + * This type should be used for all charges in the program (electrons, nuclei,...). *) -type t -(** {2 Conversions} *) +(* [[file:../charge.org::*Type][Type:1]] *) +type t +(* Type:1 ends here *) +(* ~of_float~ / ~to_float~ *) + + +(* [[file:../charge.org::*~of_float~ / ~to_float~][~of_float~ / ~to_float~:1]] *) val of_float : float -> t -val of_int : int -> t -val of_string: string -> 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 -val to_string: t -> string +(* ~of_int~ / ~to_int~:1 ends here *) -(** {2 Operations} *) +(* ~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 *) + +(* Simple operations *) + + +(* [[file:../charge.org::*Simple operations][Simple operations:1]] *) val ( + ) : t -> t -> t val ( - ) : t -> t -> t val ( * ) : t -> float -> t -val ( / ) : t -> float -> t +val ( / ) : t -> float -> t +(* Simple operations:1 ends here *) -(** {2 Printers } *) +(* Printers *) + +(* [[file:../charge.org::*Printers][Printers:1]] *) val pp : Format.formatter -> t -> unit - +(* Printers:1 ends here *)