Documentation
¶
Overview ¶
Package matrix implements several different matrix data structures, each optimised for different purposes.
In general, this package is for matrices of arbitrary size and dimension. Fixed-size matrices (e.g. 4x4) would benefit from a special-purpose package.
Index ¶
- Variables
- func Reduce[T any](m Interface[T], identity T, sum func(a, b T) T) T
- type Bit
- func (m Bit) Capacity() int
- func (m *Bit) Clear()
- func (m Bit) Depth() int
- func (m Bit) DimensionN(n int) int
- func (m Bit) Dimensionality() int
- func (m Bit) Dimensions1D() int
- func (m Bit) Dimensions2D() (int, int)
- func (m Bit) Dimensions3D() (int, int, int)
- func (m Bit) Dimensions4D() (int, int, int, int)
- func (m Bit) Extent() int
- func (m Bit) Get1D(x int) int
- func (m Bit) Get2D(x, y int) int
- func (m Bit) Get3D(x, y, z int) int
- func (m Bit) Get4D(x, y, z, w int) int
- func (m Bit) GetN(offsets ...int) int
- func (m Bit) Height() int
- func (m *Bit) Resize1D(w int)
- func (m *Bit) Resize2D(w, h int)
- func (m *Bit) Resize3D(w, h, d int)
- func (m *Bit) Resize4D(w, h, d, x int)
- func (m *Bit) ResizeN(lengths ...int)
- func (m *Bit) Set1D(value int, x int)
- func (m *Bit) Set2D(value int, x, y int)
- func (m *Bit) Set3D(value int, x, y, z int)
- func (m *Bit) Set4D(value int, x, y, z, w int)
- func (m *Bit) SetN(value int, offsets ...int)
- func (m Bit) Width() int
- type Bool
- func (m Bool) Capacity() int
- func (m *Bool) Clear()
- func (m Bool) Depth() int
- func (m Bool) DimensionN(n int) int
- func (m Bool) Dimensionality() int
- func (m Bool) Dimensions1D() int
- func (m Bool) Dimensions2D() (int, int)
- func (m Bool) Dimensions3D() (int, int, int)
- func (m Bool) Dimensions4D() (int, int, int, int)
- func (m Bool) Extent() int
- func (m Bool) Get1D(x int) bool
- func (m Bool) Get2D(x, y int) bool
- func (m Bool) Get3D(x, y, z int) bool
- func (m Bool) Get4D(x, y, z, w int) bool
- func (m Bool) GetN(offsets ...int) bool
- func (m Bool) Height() int
- func (m *Bool) Resize1D(w int)
- func (m *Bool) Resize2D(w, h int)
- func (m *Bool) Resize3D(w, h, d int)
- func (m *Bool) Resize4D(w, h, d, x int)
- func (m *Bool) ResizeN(lengths ...int)
- func (m *Bool) Set1D(value bool, x int)
- func (m *Bool) Set2D(value bool, x, y int)
- func (m *Bool) Set3D(value bool, x, y, z int)
- func (m *Bool) Set4D(value bool, x, y, z, w int)
- func (m *Bool) SetN(value bool, offsets ...int)
- func (m Bool) Width() int
- type Constructor
- type Diagonal
- func (m Diagonal) Capacity() int
- func (m *Diagonal[T]) Clear()
- func (m Diagonal) Depth() int
- func (m Diagonal) DimensionN(n int) int
- func (m Diagonal) Dimensionality() int
- func (m Diagonal) Dimensions1D() int
- func (m Diagonal) Dimensions2D() (int, int)
- func (m Diagonal) Dimensions3D() (int, int, int)
- func (m Diagonal) Dimensions4D() (int, int, int, int)
- func (m Diagonal) Extent() int
- func (m Diagonal[T]) Get1D(x int) T
- func (m Diagonal[T]) Get2D(x, y int) T
- func (m Diagonal[T]) Get3D(x, y, z int) T
- func (m Diagonal[T]) Get4D(x, y, z, w int) T
- func (m Diagonal[T]) GetN(offset ...int) T
- func (m Diagonal) Height() int
- func (m *Diagonal[T]) Resize1D(w int)
- func (m *Diagonal[T]) Resize2D(w, h int)
- func (m *Diagonal[T]) Resize3D(w, h, d int)
- func (m *Diagonal[T]) Resize4D(w, h, d, x int)
- func (m *Diagonal[T]) ResizeN(lengths ...int)
- func (m *Diagonal[T]) Set1D(value T, x int)
- func (m *Diagonal[T]) Set2D(value T, x, y int)
- func (m *Diagonal[T]) Set3D(value T, x, y, z int)
- func (m *Diagonal[T]) Set4D(value T, x, y, z, w int)
- func (m *Diagonal[T]) SetN(value T, offsets ...int)
- func (m Diagonal) Width() int
- type ErrDimension
- type ErrIndexOutOfRange
- type ErrOffDiagonal
- type Grid
- func (m Grid) Capacity() int
- func (m *Grid[T]) Clear()
- func (m Grid) Depth() int
- func (m Grid) DimensionN(n int) int
- func (m Grid) Dimensionality() int
- func (m Grid) Dimensions1D() int
- func (m Grid) Dimensions2D() (int, int)
- func (m Grid) Dimensions3D() (int, int, int)
- func (m Grid) Dimensions4D() (int, int, int, int)
- func (m Grid) Extent() int
- func (m Grid[T]) Get1D(x int) T
- func (m Grid[T]) Get2D(x, y int) T
- func (m Grid[T]) Get3D(x, y, z int) T
- func (m Grid[T]) Get4D(x, y, z, w int) T
- func (m Grid[T]) GetN(offsets ...int) T
- func (m Grid) Height() int
- func (m *Grid[T]) Resize1D(w int)
- func (m *Grid[T]) Resize2D(w, h int)
- func (m *Grid[T]) Resize3D(w, h, d int)
- func (m *Grid[T]) Resize4D(w, h, d, x int)
- func (m *Grid[T]) ResizeN(lengths ...int)
- func (m *Grid[T]) Set1D(value T, x int)
- func (m *Grid[T]) Set2D(value T, x, y int)
- func (m *Grid[T]) Set3D(value T, x, y, z int)
- func (m *Grid[T]) Set4D(value T, x, y, z, w int)
- func (m Grid[T]) SetN(value T, offsets ...int)
- func (m Grid) Width() int
- type Hash
- func (m Hash) Capacity() int
- func (m *Hash[T]) Clear()
- func (m Hash) Depth() int
- func (m Hash) DimensionN(n int) int
- func (m Hash) Dimensionality() int
- func (m Hash) Dimensions1D() int
- func (m Hash) Dimensions2D() (int, int)
- func (m Hash) Dimensions3D() (int, int, int)
- func (m Hash) Dimensions4D() (int, int, int, int)
- func (m Hash) Extent() int
- func (m Hash[T]) Get1D(x int) T
- func (m Hash[T]) Get2D(x, y int) T
- func (m Hash[T]) Get3D(x, y, z int) T
- func (m Hash[T]) Get4D(x, y, z, w int) T
- func (m *Hash[T]) GetN(offsets ...int) T
- func (m Hash) Height() int
- func (m Hash[T]) Reduce(identity T, sum func(a, b T) T) T
- func (m *Hash[T]) Resize1D(width int)
- func (m *Hash[T]) Resize2D(width, height int)
- func (m *Hash[T]) Resize3D(width, height, depth int)
- func (m *Hash[T]) Resize4D(width, height, depth, extent int)
- func (m *Hash[T]) ResizeN(lengths ...int)
- func (m *Hash[T]) Set1D(value T, x int)
- func (m *Hash[T]) Set2D(value T, x, y int)
- func (m *Hash[T]) Set3D(value T, x, y, z int)
- func (m *Hash[T]) Set4D(value T, x, y, z, w int)
- func (m *Hash[T]) SetN(value T, offsets ...int)
- func (m Hash) Width() int
- type Interface
- func New1D[T any](implementation Constructor[T], width int) Interface[T]
- func New2D[T any](implementation Constructor[T], width, height int) Interface[T]
- func New3D[T any](implementation Constructor[T], width, height, depth int) Interface[T]
- func New4D[T any](implementation Constructor[T], width, height, depth, extent int) Interface[T]
- func NewBit() Interface[int]
- func NewBool() Interface[bool]
- func NewDiagonal[T any]() Interface[T]
- func NewGrid[T any]() Interface[T]
- func NewHash[T any]() Interface[T]
- func NewN[T any](implementation Constructor[T], lengths ...int) Interface[T]
Constants ¶
This section is empty.
Variables ¶
var ErrDiagonalSize = errors.New("matrix must be diagonal")
ErrDiagonalSize is the type of error raised by a panic if a Diagonal matrix is resized without equal length sides in each supported dimension.
var ErrNotImplemented = errors.New("not implemented")
ErrNotImplemented is the type of error raised by a panic if a matrix method is not implemented. For example, the Hash implementation does not support an arbitrary dimension.
Functions ¶
func Reduce ¶
Reduce applies the given function pairwise to each value, using the result of the previous application as the first input to the next application, or, for the first application, using the identity value provided as the first input. Returns the last result returned by the application of the sum function or, if there are no elements, returns the provided identity value without calling the sum function.
The reduce function is applied in arbitrary order.
Types ¶
type Bit ¶
type Bit struct {
// contains filtered or unexported fields
}
Bit implements a bit matrix, implementing the matrix Interface, as a contiguous sequence of bits with integer value 1 or 0.
This type is more efficient than Grid for storing single-bit values.
When setting a value in the matrix, any non-zero integer is treated as true.
func (Bit) DimensionN ¶
func (Bit) Dimensionality ¶
func (m Bit) Dimensionality() int
func (Bit) Dimensions1D ¶
func (m Bit) Dimensions1D() int
func (Bit) Dimensions2D ¶
func (Bit) Dimensions3D ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool implements a boolean matrix, implementing the matrix Interface, as a contiguous sequence of true or false bits.
This type is more efficient than Grid for storing single-bit values.
func (Bool) DimensionN ¶
func (Bool) Dimensionality ¶
func (m Bool) Dimensionality() int
func (Bool) Dimensions1D ¶
func (m Bool) Dimensions1D() int
func (Bool) Dimensions2D ¶
func (Bool) Dimensions3D ¶
type Constructor ¶
Constructor is any function that returns a new matrix - e.g. NewGrid.
type Diagonal ¶
type Diagonal[T any] struct { // contains filtered or unexported fields }
Diagonal implements the matrix Interface as a contiguous slice of values making up only the diagonal entries. All entries off the diagonal are zero, and the matrix must have equal length sides in every dimension.
func (Diagonal) DimensionN ¶
func (Diagonal) Dimensionality ¶
func (m Diagonal) Dimensionality() int
func (Diagonal) Dimensions1D ¶
func (m Diagonal) Dimensions1D() int
func (Diagonal) Dimensions2D ¶
func (Diagonal) Dimensions3D ¶
type ErrDimension ¶
type ErrDimension struct {
Requested, Actual int // 1, 2, 3 or 4.
}
ErrDimension is the type of error raised by a panic if a matrix is accessed using coordinates with the wrong dimensionality, for example (x, y) coordinates in a 3D matrix expecting (x, y, z) coordinates.
func (ErrDimension) Error ¶
func (e ErrDimension) Error() string
type ErrIndexOutOfRange ¶
type ErrIndexOutOfRange struct { Index [4]int // x, y, w, z index attempted to access Range [4]int // width, height, depth, extent of each dimension }
ErrIndexOutOfRange is the type of error raised by a panic if a matrix is accessed with a coordinate lying outside its range.
func (ErrIndexOutOfRange) Error ¶
func (e ErrIndexOutOfRange) Error() string
type ErrOffDiagonal ¶
type ErrOffDiagonal struct { Index [4]int // x, y, w, z index attempted to access Width int Dimensionality int }
ErrOffDiagonal is the type of error raised by a panic if a value is set in a Diagonal matrix at an index that does not lie on the diagonal.
func (ErrOffDiagonal) Error ¶
func (e ErrOffDiagonal) Error() string
type Grid ¶
type Grid[T any] struct { // contains filtered or unexported fields }
Grid implements the matrix Interface as a contiguous slice of values.
This type is more efficient for representing dense matrices (i.e. one where few values are zero).
func (Grid) DimensionN ¶
func (Grid) Dimensionality ¶
func (m Grid) Dimensionality() int
func (Grid) Dimensions1D ¶
func (m Grid) Dimensions1D() int
func (Grid) Dimensions2D ¶
func (Grid) Dimensions3D ¶
type Hash ¶
type Hash[T any] struct { // contains filtered or unexported fields }
Hash implements the matrix Interface as a hash map with coordinate pairs forming keys.
This type is more efficient for representing sparse matrices (i.e. one where most values are zero). As a rule-of-thumb, prefer this type if less than 1/64th of the matrix is set.
This type only implements 1- to 4-dimensional matrices.
func (Hash) DimensionN ¶
func (Hash) Dimensionality ¶
func (m Hash) Dimensionality() int
func (Hash) Dimensions1D ¶
func (m Hash) Dimensions1D() int
func (Hash) Dimensions2D ¶
func (Hash) Dimensions3D ¶
type Interface ¶
type Interface[T any] interface { // Resize1D (and variants) updates a matrix, if necessary, so that it has // at least capacity for the given number of elements, and has the // appropriate dimensionality. It may return a new matrix, but reuses // underlying memory from the existing matrix where possible. All values // are cleared. Resize1D(index int) Resize2D(width, height int) Resize3D(width, height, depth int) Resize4D(width, height, depth, extent int) ResizeN(lengths ...int) // Width and Height and Depth, and Extent return the number of elements // in the x, y, z and w dimensions respectively. Width() int Height() int Depth() int Extent() int // Dimensions2D and Dimensions3D, and Dimensions4D return the number of // elements in the (x, y), (x, y, z), and (x, y, z, w) dimensions, // respectively. Dimensions2D() (width, height int) Dimensions3D() (width, height, depth int) Dimensions4D() (width, height, depth, extent int) // DimensionN returns the number of elements in the nth dimension. DimensionN(n int) int // Dimensionality returns the number of dimensions. For example, returns 1, // 2, 3 or 4 if the matrix is 1D, 2D, 3D or 4D, respectively. Dimensionality() int // Get1D (and variants) return the value at the given coordinate. This // will panic if out of bounds. Get1D(x int) T Get2D(x, y int) T Get3D(x, y, z int) T Get4D(x, y, z, w int) T // GetN returns the value at the given offsets with arbitrary dimension. // Must contain an offset for each dimension. GetN(offsets ...int) T // Set1D (and variants) writes a value at the given coordinate. This will // panic if out of bounds. Set1D(value T, x int) Set2D(value T, x, y int) Set3D(value T, x, y, z int) Set4D(value T, x, y, z, w int) // SetN writes a value at the given coordinate at the given offsets with // arbitrary dimension. Must contain one offset for each dimension. SetN(value T, offset ...int) // Clear sets all elements to zero. Clear() // Capacity returns the total number of elements in the matrix (across all // dimensions). Capacity() int }
Interface is the basic interface implemented by any matrix type. It maps coordinates (e.g. x, y, z, w) to elements of type T.
func New1D ¶
func New1D[T any](implementation Constructor[T], width int) Interface[T]
New1D creates, sizes, and returns a 1D implementation of the matrix interface with the given constructor e.g. NewGrid.
func New2D ¶
func New2D[T any](implementation Constructor[T], width, height int) Interface[T]
New2D creates, sizes, and returns a 2D implementation of the matrix interface with the given constructor e.g. NewGrid.
func New3D ¶
func New3D[T any](implementation Constructor[T], width, height, depth int) Interface[T]
New3D creates, sizes, and returns a 3D implementation of the matrix interface with the given constructor e.g. NewGrid.