Added Charge.org

This commit is contained in:
Anthony Scemama 2020-12-27 13:43:55 +01:00
parent dfadaf14c1
commit b08079c09d
5 changed files with 171 additions and 35 deletions

View File

@ -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:

View File

@ -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:

101
common/charge.org Normal file
View File

@ -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

View File

@ -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 *)

View File

@ -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 *)