# package phantom-algebra

Legend:
Library
Module
Module type
Parameter
Class
Class type
type (+'dim, +'rank) t

Tensor core type:

• rank is either 2 (for matrix), 1 (for vector) or zero for scalars
• dim belongs to {1,2,3,4}.
val pp : Stdlib.Format.formatter -> ('dim, 'rank) t -> unit

Printer function

type +'x scalar = (, ) t constraint 'x = 'a * 'b

Type abreviations

type +'x vec2 = (, ) t constraint 'x = 'a * 'b
type +'x vec3 = t constraint 'x = 'a * 'b
type +'x vec4 = t constraint 'x = 'a * 'b
type +'x mat2 = (, ) t constraint 'x = 'a * 'b
type +'x mat3 = t constraint 'x = 'a * 'b
type +'x mat4 = t constraint 'x = 'a * 'b
val scalar : k ->

Constructors

val (~+) : k ->

scalar f build a scalar from the naked scalar type. It can be abbreviated to (+s).

val vec2 : k -> k -> _ vec2
val vec3 : k -> k -> k -> _ vec3
val vec4 : k -> k -> k -> k -> _ vec4
val mat2 : _ vec2 -> _ vec2 -> _ mat2
val mat3 : _ vec3 -> _ vec3 -> _ vec3 -> _ mat3
val mat4 : _ vec4 -> _ vec4 -> _ vec4 -> _ vec4 -> _ mat4

## Vector stretching and concatenation

val vec2' : ([< ], [< ]) t -> _ vec2

vec\$n' v extends a vector of dimension d <= n to a vector of dimension n by repeating the last value of the vector

val vec3' : (, [< ]) t -> _ vec3
val vec4' : (, [< ]) t -> _ vec4
val (|+|) : (('dim1, 'dim2, 'dim3, _) Type_functions.nat_sum, [< ]) t -> ('dim2, [< ]) t -> ('dim3, ) t

Vector concatenation : v |+| w is (v_0, v_1, … , v_{dim-1}, w_0, …, w_{dim-1})

## Map functions

val map : (k -> k) -> ('dim, 'rank) t -> ('dim, 'rank) t
val map2 : (k -> k -> k) -> ('dim, 'rank) t -> ('dim, 'rank) t -> ('dim, 'rank) t

### Core linear algebra functions

val (+) : ('dim1, ('rank1, 'rank2, 'rank3, 'dim1, 'dim2, 'dim3, _) Type_functions.sum) t -> ('dim2, 'rank2) t -> ('dim3, 'rank3) t

x + y is the standard vector sum, except for scalar argument which are broadcasted to a constant tensor

val (<+>) : ('dim, 'rank) t -> ('dim, 'rank) t -> ('dim, 'rank) t

x <+> y is the standard vector sum, without broadcasting

val (~-) : ('dim, 'rank) t -> ('dim, 'rank) t

~-x is the standard addition inverse

val (-) : ('a, ('rank1, 'rank2, 'rank3, 'dim1, 'dim2, 'dim3, _) Type_functions.sum) t -> ('a, 'rank2) t -> ('a, 'rank3) t

x - y is the standard vector difference, except for scalar argument which are broadcasted to a constant tensor

val (<->) : ('dim, 'rank) t -> ('dim, 'rank) t -> ('dim, 'rank) t

x <-> y is the standard vector difference, without broadcasting

val (*) : ('dim1, ('rank1, 'rank2, 'rank3, 'dim1, 'dim2, 'dim3, _) Type_functions.product) t -> ('dim2, 'rank2) t -> ('dim3, 'rank3) t

x * y is:

• the external product if x or y is a scalar
• the matrix product if x or y is a matrix
• the element-wise (hadamard) product otherwise
val (/) : ('dim1, ('rank1, 'rank2, 'rank3, 'dim1, 'dim2, 'dim3, _) Type_functions.div) t -> ('dim2, 'rank2) t -> ('dim3, 'rank3) t

x / y is:

• the external product division if x or y is a scalar
• the right matrix division if y is a matrix and x is either a matrix or a vector
• the element-wise division if both x and y are vectors

### Exponentiation functions

val (**) : ('dim, 'rank) t -> int -> ('dim, 'rank) t

t ** k is t * … * t k-times

val exp : ('dim, 'rank) t -> ('dim, 'rank) t

exp is the algebraic exponential: exp m = 1 + m + m ** 2 / 2 + m **3 / 3! + …

### Scalar products and norms

val (|*|) : ('dim, 'rank) t -> ('dim, 'rank) t -> k

(x|*|y) is the canonical scalar product

val norm : ('dim, 'rank) t -> k

norm x is the canonical 2-norm of x

val normalize : ('dim, 'rank) t -> ('dim, 'rank) t

normalize x is x / scalar (norm x)

val orthonormalize : ('dim, ) t list -> ('dim, ) t list

orthonomalize [v_0;...;v_n] returns a list of orthonormal vectors [w_0;...;w_k] that spans the same vector subspace as v_0;...;v_n. If the family [v_0;...;v_n] was free then k = n, otherwise k<n.

val distance : ('dim, 'rank) t -> ('dim, 'rank) t -> k

distance x y is norm (x - y)

val norm_1 : ('dim, 'rank) t -> k

norm_1 x is ∑ |x_i|

val norm_q : float -> ('dim, 'rank) t -> k

norm_q q x is (∑ |x_i|^q) ^ 1/q

### Cross and external product

val cross : (('dim, 'dim2 * 'rank2, _) Type_functions.cross, ) t -> ('dim, ) t -> ('dim2, 'rank2) t

cross v w is the cross product, it maps either two 3d vectors to a 3d pseudo-vector, or two 2d vectors to a scalar

val (^) : ('dim, ) t -> ('dim, ) t -> ('dim, ) t

See cross for the 2d and 3d cross-product for vectors v ^ w is the infinitesimal rotation matrix in the plane generated by v and w with an amplitude |v||w| sin θ . In other words the matrix representation of the 2-form dv ^ dw in the corresponding graded algebra.

### More linear algebra functions

val commutator : ('dim, ) t -> ('dim, ) t -> ('dim, ) t

commutator m n is m * n - n * m

val anticommutator : ('dim, ) t -> ('dim, ) t -> ('dim, ) t

anticommutator m n is m * n + n * m

val trace : ('dim, ) t -> k

trace m is ∑_i m_ii

val det : ('dim, ) t -> k

det m is the signed volume of the convex hull of of the matrix rows

val transpose : ('dim, ) t -> ('dim, ) t

transpose m is the matrix with row and column reversed

Innovation. Community. Security.

##### Ecosystem
Packages Community Events OCaml Planet Jobs
##### Policies
Carbon Footprint Governance Privacy Code of Conduct