10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-18 20:12:26 +01:00
QCaml/common/powers.org

132 lines
3.2 KiB
Org Mode
Raw Normal View History

2020-12-28 01:08:55 +01:00
#+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)
2021-01-28 00:34:26 +01:00
#+end_src
2020-12-28 01:08:55 +01:00
* Powers
:PROPERTIES:
:header-args: :noweb yes :comments both
:END:
2021-01-28 00:34:26 +01:00
Contains powers of $x$, $y$ and $z$ describing the polynomials in atomic basis sets.
2020-12-28 01:08:55 +01:00
** Type
2021-01-28 00:34:26 +01:00
<<<~Powers.t~>>>
2020-12-28 01:08:55 +01:00
#+begin_src ocaml :tangle (eval mli)
type t = private {
x : int ;
y : int ;
z : int ;
2021-01-28 00:34:26 +01:00
tot : int ;
2020-12-28 01:08:55 +01:00
}
#+end_src
~tot~ always contains ~x+y+z~.
2021-01-28 00:34:26 +01:00
2020-12-28 01:08:55 +01:00
#+begin_src ocaml :tangle (eval ml) :exports none
type t = {
x : int ;
y : int ;
z : int ;
2021-01-28 00:34:26 +01:00
tot : int ;
2020-12-28 01:08:55 +01:00
}
#+end_src
** Conversions
#+begin_src ocaml :tangle (eval mli)
val of_int_tuple : int * int * int -> t
2021-01-28 00:34:26 +01:00
val to_int_tuple : t -> int * int * int
2020-12-28 01:08:55 +01:00
#+end_src
2021-01-28 00:34:26 +01:00
Example:
2020-12-28 01:08:55 +01:00
#+begin_example
Powers.of_int_tuple (2,3,1);;
2020-12-29 01:08:37 +01:00
- : Powers.t = x^2 + y^3 + z^1
2020-12-28 01:08:55 +01:00
Powers.(to_int_tuple (of_int_tuple (2,3,1)));;
- : int * int * int = (2, 3, 1)
#+end_example
#+begin_src ocaml :tangle (eval ml) :exports none
2021-01-28 00:34:26 +01:00
let of_int_tuple t =
2020-12-28 01:08:55 +01:00
let result =
2021-01-28 00:34:26 +01:00
match t with
2020-12-28 01:08:55 +01:00
| (x,y,z) -> { x ; y ; z ; tot=x+y+z }
in
if result.x < 0 ||
result.y < 0 ||
result.z < 0 ||
result.tot < 0 then
invalid_arg (__FILE__^": of_int_tuple");
result
2021-01-28 00:34:26 +01:00
let to_int_tuple { x ; y ; z ; _ } = (x,y,z)
2020-12-28 01:08:55 +01:00
#+end_src
** Operations
#+begin_src ocaml :tangle (eval mli)
val get : Coordinate.axis -> t -> int
2021-01-28 00:34:26 +01:00
val incr : Coordinate.axis -> t -> t
2020-12-28 01:08:55 +01:00
val decr : Coordinate.axis -> t -> t
#+end_src
| ~get~ | Returns the value of the power for $x$, $y$ or $z$
| ~incr~ | Returns a new ~Powers.t~ with the power on the given axis incremented |
| ~decr~ | Returns a new ~Powers.t~ with the power on the given axis decremented. As opposed to ~of_int_tuple~, the values may become negative|
2021-01-28 00:34:26 +01:00
Example:
2020-12-28 01:08:55 +01:00
#+begin_example
Powers.get Coordinate.Y (Powers.of_int_tuple (2,3,1));;
- : int = 3
Powers.incr Coordinate.Y (Powers.of_int_tuple (2,3,1));;
2020-12-29 01:08:37 +01:00
- : Powers.t = x^2 + y^4 + z^1
2020-12-28 01:08:55 +01:00
Powers.decr Coordinate.Y (Powers.of_int_tuple (2,3,1));;
2020-12-29 01:08:37 +01:00
- : Powers.t = x^2 + y^2 + z^1
2020-12-28 01:08:55 +01:00
#+end_example
#+begin_src ocaml :tangle (eval ml) :exports none
let get coord t =
match coord with
| Coordinate.X -> t.x
| Coordinate.Y -> t.y
| Coordinate.Z -> t.z
let incr coord t =
match coord with
| Coordinate.X -> let r = t.x+1 in { t with x = r ; tot = t.tot+1 }
| Coordinate.Y -> let r = t.y+1 in { t with y = r ; tot = t.tot+1 }
| Coordinate.Z -> let r = t.z+1 in { t with z = r ; tot = t.tot+1 }
let decr coord t =
match coord with
| Coordinate.X -> let r = t.x-1 in { t with x = r ; tot = t.tot-1 }
| Coordinate.Y -> let r = t.y-1 in { t with y = r ; tot = t.tot-1 }
| Coordinate.Z -> let r = t.z-1 in { t with z = r ; tot = t.tot-1 }
#+end_src
** Printers
#+begin_src ocaml :tangle (eval mli)
val pp : Format.formatter -> t -> unit
#+end_src
#+begin_src ocaml :tangle (eval ml) :exports none
let pp ppf t =
Format.fprintf ppf "@[x^%d + y^%d + z^%d@]" t.x t.y t.z
#+end_src