Module Gg.M2

2D square matrices.

type t = m2

The type for 2D square matrices.

val dim : int

dim is the dimension of rows and columns.

type v = v2

The type for rows and columns as vectors.

Constructors, accessors and constants

val v : float -> float -> float -> float -> m2

v e00 e01 e10 e11 is a matrix whose components are specified in row-major order

val of_rows : v2 -> v2 -> m2

of_rows r0 r1 is the matrix whose rows are r0 and r1.

val of_cols : v2 -> v2 -> m2

of_cols c0 c1 is the matrix whose columns are c0 and c1.

val el : int -> int -> m2 -> float

el i j a is the element aij. See also the direct element accessors.

Raises Invalid_argument if i or j is not in [0;dim[.

val row : int -> m2 -> v2

row i a is the ith row of a.

Raises Invalid_argument if i is not in [0;dim[.

val col : int -> m2 -> v2

col j a is the jth column of a.

Raises Invalid_argument if j is not in [0;dim[.

val zero : m2

zero is the neutral element for add.

val id : m2

id is the identity matrix, the neutral element for mul.

val of_m3 : m3 -> m2

of_m3 m extracts the 2D linear part (top-left 2x2 matrix) of m.

val of_m4 : m4 -> m2

of_m4 m extracts the 2D linear part (top-left 2x2 matrix) of m.

Functions

val neg : m2 -> m2

neg a is the negated matrix -a.

val add : m2 -> m2 -> m2

add a b is the matrix addition a + b.

val sub : m2 -> m2 -> m2

sub a b is the matrix subtraction a - b.

val mul : m2 -> m2 -> m2

mul a b is the matrix multiplication a * b.

val emul : m2 -> m2 -> m2

emul a b is the element wise multiplication of a and b.

val ediv : m2 -> m2 -> m2

ediv a b is the element wise division of a and b.

val smul : float -> m2 -> m2

smul s a is a's elements multiplied by the scalar s.

val transpose : m2 -> m2

transpose a is the transpose aT.

val trace : m2 -> float

trace a is the matrix trace trace(a).

val det : m2 -> float

det a is the determinant |a|.

val inv : m2 -> m2

inv a is the inverse matrix a-1.

2D space transformations

val rot2 : float -> m2

rot2 theta rotates 2D space around the origin by theta radians.

val scale2 : v2 -> m2

scale2 s scales 2D space in the x and y dimensions according to s.

Traversal

val map : ( float -> float ) -> m2 -> m2

map f a is the element wise application of f to a.

val mapi : ( int -> int -> float -> float ) -> m2 -> m2

mapi f a is like map but the element indices are also given.

val fold : ( 'a -> float -> 'a ) -> 'a -> m2 -> 'a

fold f acc a is f (...(f (f acc a00) a10)...).

val foldi : ( 'a -> int -> int -> float -> 'a ) -> 'a -> m2 -> 'a

foldi f acc a is f (...(f (f acc 0 0 a00) 1 0 a10)...).

val iter : ( float -> unit ) -> m2 -> unit

iter f a is f a00; f a10; ...

val iteri : ( int -> int -> float -> unit ) -> m2 -> unit

iteri f a is f 0 0 a00; f 1 0 a10; ...

Predicates and comparisons

val for_all : ( float -> bool ) -> m2 -> bool

for_all p a is p a00 && p a10 && ...

val exists : ( float -> bool ) -> m2 -> bool

exists p a is p a00 || p a10 || ...

val equal : m2 -> m2 -> bool

equal a b is a = b.

val equal_f : ( float -> float -> bool ) -> m2 -> m2 -> bool

equal_f eq a b tests a and b like equal but uses eq to test floating point values.

val compare : m2 -> m2 -> int

compare a b is Stdlib.compare a b. That is lexicographic comparison in column-major order.

val compare_f : ( float -> float -> int ) -> m2 -> m2 -> int

compare_f cmp a b compares a and b like compare but uses cmp to compare floating point values.

Formatters

val pp : Stdlib.Format.formatter -> m2 -> unit

pp ppf a prints a textual representation of a on ppf.

val pp_f : ( Stdlib.Format.formatter -> float -> unit ) -> Stdlib.Format.formatter -> m2 -> unit

pp_f pp_e ppf a prints a like pp but uses pp_e to print floating point values.

Element accessors

val e00 : m2 -> float
val e01 : m2 -> float
val e10 : m2 -> float
val e11 : m2 -> float