10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-06 22:23:42 +01:00
QCaml/common/lib/powers.ml

86 lines
2.3 KiB
OCaml
Raw Normal View History

2018-02-19 11:24:15 +01:00
2020-12-28 01:08:55 +01:00
(* ~tot~ always contains ~x+y+z~. *)
2020-12-28 01:55:03 +01:00
(* [[file:~/QCaml/common/powers.org::*Type][Type:2]] *)
2020-12-28 01:08:55 +01:00
type t = {
x : int ;
y : int ;
z : int ;
tot : int ;
}
(* Type:2 ends here *)
(* #+begin_example
* Powers.of_int_tuple (2,3,1);;
* - : Powers.t = {Qcaml.Common.Powers.x = 2; y = 3; z = 1; tot = 6}
*
* Powers.(to_int_tuple (of_int_tuple (2,3,1)));;
* - : int * int * int = (2, 3, 1)
* #+end_example *)
2020-12-28 01:55:03 +01:00
(* [[file:~/QCaml/common/powers.org::*Conversions][Conversions:2]] *)
2018-02-19 11:24:15 +01:00
let of_int_tuple t =
let result =
match t with
| (x,y,z) -> { x ; y ; z ; tot=x+y+z }
in
2018-02-25 00:53:09 +01:00
if result.x < 0 ||
result.y < 0 ||
result.z < 0 ||
result.tot < 0 then
invalid_arg (__FILE__^": of_int_tuple");
2018-02-19 11:24:15 +01:00
result
2020-12-28 01:08:55 +01:00
let to_int_tuple { x ; y ; z ; _ } = (x,y,z)
(* Conversions:2 ends here *)
(* | ~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|
*
* #+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));;
* - : Powers.t = {Qcaml.Common.Powers.x = 2; y = 4; z = 1; tot = 7}
*
* Powers.decr Coordinate.Y (Powers.of_int_tuple (2,3,1));;
* - : Powers.t = {Qcaml.Common.Powers.x = 2; y = 2; z = 1; tot = 5}
* #+end_example *)
2020-12-28 01:55:03 +01:00
(* [[file:~/QCaml/common/powers.org::*Operations][Operations:2]] *)
2018-02-19 11:24:15 +01:00
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
2018-02-19 16:01:13 +01:00
| 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 }
2018-02-19 11:24:15 +01:00
let decr coord t =
match coord with
2018-02-19 16:01:13 +01:00
| 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 }
2020-12-28 01:08:55 +01:00
(* Operations:2 ends here *)
2018-02-19 11:24:15 +01:00
2020-12-28 01:55:03 +01:00
(* [[file:~/QCaml/common/powers.org::*Printers][Printers:2]] *)
2020-12-28 01:08:55 +01:00
let pp ppf t =
Format.fprintf ppf "@[x^%d + y^%d + z^%d@]" t.x t.y t.z
(* Printers:2 ends here *)