View Source Graphmath.Mat22 (graphmath v3.0.0)

2x2 matrices and linear transformations of 2D vectors.

Matrices store rows in a flat tuple {a11, a12, a21, a22}:

a11  a12
a21  a22

The matrix and vector types use floating-point entries. Integer and mixed numeric inputs are also accepted, following Elixir's numeric promotion rules; round/2 and inverse/1 return floats.

As in Graphmath.Mat33, apply(a, v) computes the column-vector product A**v. Graphics constructors use row vectors: transform_vector(a, v) and apply_left(v, a) compute v**A, as does apply_transpose(a, v). For row vectors, multiply(a, b) applies a first, then b.

These matrices support scale, rotation, reflection, and shear. Use Graphmath.Mat33 for 2D transformations that also include translation.

Summary

Functions

Adds corresponding entries of a and b.

Returns the column-vector product A**v**.

Returns the row-vector product v**A**.

Returns v**A**<sup>T</sup>, equivalent to apply(a, v).

Returns A<sup>T</sup>v, equivalent to apply_left(v, a).

Returns the entry at zero-based row i and column j.

Returns the first column, {a11, a21}.

Returns the second column, {a12, a22}.

Returns the determinant, a11 * a22 - a12 * a21.

Returns the diagonal entries, {a11, a22}.

Returns the identity matrix {1.0, 0.0, 0.0, 1.0}.

Returns the inverse matrix, with floating-point entries.

Creates a 2D linear reflection across the line through the origin with normal {nx, ny}.

Returns a matrix that rotates row vectors counterclockwise by theta radians about +Z. Apply it with transform_vector/2 or apply_left/2.

Returns a matrix that uniformly scales both vector coordinates by k.

Returns a matrix that scales X by sx and Y by sy.

Creates a 2D linear X shear: x' = x + k*y.

Creates a 2D linear Y shear: y' = y + k*x.

Returns the matrix product A**B**.

Returns A**B**<sup>T</sup>, multiplying a by the transpose of b.

Rounds every entry to sigfigs decimal places, returning floats.

Returns the first row, {a11, a12}.

Returns the second row, {a21, a22}.

Multiplies every entry of a by the scalar k.

Subtracts each entry of b from the corresponding entry of a.

Returns the trace, the sum of the diagonal entries a11 + a22.

Transforms a 2D vector using the row-vector convention, computing v**A**.

Returns the zero matrix {0.0, 0.0, 0.0, 0.0}.

Types

@type mat22() :: {float(), float(), float(), float()}
@type vec2() :: {float(), float()}

Functions

@spec add(mat22(), mat22()) :: mat22()

Adds corresponding entries of a and b.

@spec apply(mat22(), vec2()) :: vec2()

Returns the column-vector product A**v**.

@spec apply_left(vec2(), mat22()) :: vec2()

Returns the row-vector product v**A**.

Link to this function

apply_left_transpose(arg1, arg2)

View Source
@spec apply_left_transpose(vec2(), mat22()) :: vec2()

Returns v**A**<sup>T</sup>, equivalent to apply(a, v).

Link to this function

apply_transpose(arg1, arg2)

View Source
@spec apply_transpose(mat22(), vec2()) :: vec2()

Returns A<sup>T</sup>v, equivalent to apply_left(v, a).

@spec at(mat22(), 0..1, 0..1) :: float()

Returns the entry at zero-based row i and column j.

Both indices must be 0 or 1; invalid indices raise FunctionClauseError.

@spec column0(mat22()) :: vec2()

Returns the first column, {a11, a21}.

@spec column1(mat22()) :: vec2()

Returns the second column, {a12, a22}.

@spec determinant(mat22()) :: float()

Returns the determinant, a11 * a22 - a12 * a21.

@spec diag(mat22()) :: vec2()

Returns the diagonal entries, {a11, a22}.

@spec identity() :: mat22()

Returns the identity matrix {1.0, 0.0, 0.0, 1.0}.

@spec inverse(mat22()) :: mat22()

Returns the inverse matrix, with floating-point entries.

Raises ArithmeticError when the determinant is zero.

@spec make_reflect(vec2()) :: mat22()

Creates a 2D linear reflection across the line through the origin with normal {nx, ny}.

The normal may have any nonzero length; a zero normal raises ArithmeticError. Apply using transform_vector/2 with a full 2-component vector.

@spec make_rotate(float()) :: mat22()

Returns a matrix that rotates row vectors counterclockwise by theta radians about +Z. Apply it with transform_vector/2 or apply_left/2.

@spec make_scale(float()) :: mat22()

Returns a matrix that uniformly scales both vector coordinates by k.

@spec make_scale(float(), float()) :: mat22()

Returns a matrix that scales X by sx and Y by sy.

@spec make_shear_x(float()) :: mat22()

Creates a 2D linear X shear: x' = x + k*y.

The other spatial coordinates are unchanged. Apply using transform_vector/2 with a full 2-component vector.

@spec make_shear_y(float()) :: mat22()

Creates a 2D linear Y shear: y' = y + k*x.

The other spatial coordinates are unchanged. Apply using transform_vector/2 with a full 2-component vector.

@spec multiply(mat22(), mat22()) :: mat22()

Returns the matrix product A**B**.

Link to this function

multiply_transpose(arg1, arg2)

View Source
@spec multiply_transpose(mat22(), mat22()) :: mat22()

Returns A**B**<sup>T</sup>, multiplying a by the transpose of b.

@spec round(mat22(), 0..15) :: mat22()

Rounds every entry to sigfigs decimal places, returning floats.

sigfigs must be an integer from 0 through 15, as required by Float.round/2.

@spec row0(mat22()) :: vec2()

Returns the first row, {a11, a12}.

@spec row1(mat22()) :: vec2()

Returns the second row, {a21, a22}.

@spec scale(mat22(), float()) :: mat22()

Multiplies every entry of a by the scalar k.

@spec subtract(mat22(), mat22()) :: mat22()

Subtracts each entry of b from the corresponding entry of a.

@spec trace(mat22()) :: float()

Returns the trace, the sum of the diagonal entries a11 + a22.

Link to this function

transform_vector(arg1, arg2)

View Source
@spec transform_vector(mat22(), vec2()) :: vec2()

Transforms a 2D vector using the row-vector convention, computing v**A**.

This agrees with the scale and rotation constructors and is equivalent to apply_left(v, a).

@spec zero() :: mat22()

Returns the zero matrix {0.0, 0.0, 0.0, 0.0}.