matrix

package
v2.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func Reduce[T any](m Interface[T], identity T, sum func(a, b T) T) T

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 Constructor

type Constructor[T any] func() Interface[T]

Constructor is any function that returns a new matrix - e.g. NewGrid.

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 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.

func New4D

func New4D[T any](implementation Constructor[T], width, height, depth, extent int) Interface[T]

New4D creates, sizes, and returns a 4D implementation of the matrix interface with the given constructor e.g. NewGrid.

func NewBit

func NewBit() Interface[int]

NewBit is a matrix Constructor for a matrix implemented as a contiguous sequence of bits with integer value 1 or 0. This type is more efficient than NewGrid for storing single-bit values.

When setting a value in this matrix, any non-zero integer is treated as positive one.

func NewBool

func NewBool() Interface[bool]

NewBool is a matrix Constructor for a matrix implemented as a contiguous sequence of true or false bits. This type is more efficient than NewGrid for storing single-bit values.

func NewDiagonal

func NewDiagonal[T any]() Interface[T]

NewDiagonal is a matrix Constructor that implements a matrix 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 NewGrid

func NewGrid[T any]() Interface[T]

NewGrid is a matrix Constructor for a matrix implemented as a contiguous slice of values. This type is more efficient for representing dense matrices (i.e. one where few values are zero).

func NewHash

func NewHash[T any]() Interface[T]

NewHash is a matrix Constructor implemented 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 has a non-zero value.

This type only implements 1- to 4-dimensional matrices.

func NewN

func NewN[T any](implementation Constructor[T], lengths ...int) Interface[T]

NewN creates, sizes, and returns an implementation of the matrix interface with the given constructor, e.g. NewGrid, for a matrix of arbitrary dimension.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL