matrix

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 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 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) Capacity

func (m Bit) Capacity() int

func (*Bit) Clear

func (m *Bit) Clear()

func (Bit) Depth

func (m Bit) Depth() int

func (Bit) DimensionN

func (m Bit) DimensionN(n int) int

func (Bit) Dimensionality

func (m Bit) Dimensionality() int

func (Bit) Dimensions1D

func (m Bit) Dimensions1D() int

func (Bit) Dimensions2D

func (m Bit) Dimensions2D() (int, int)

func (Bit) Dimensions3D

func (m Bit) Dimensions3D() (int, int, int)

func (Bit) Dimensions4D

func (m Bit) Dimensions4D() (int, int, int, int)

func (Bit) Extent

func (m Bit) Extent() int

func (Bit) Get1D

func (m Bit) Get1D(x int) int

func (Bit) Get2D

func (m Bit) Get2D(x, y int) int

func (Bit) Get3D

func (m Bit) Get3D(x, y, z int) int

func (Bit) Get4D

func (m Bit) Get4D(x, y, z, w int) int

func (Bit) GetN

func (m Bit) GetN(offsets ...int) int

func (Bit) Height

func (m Bit) Height() int

func (*Bit) Resize1D

func (m *Bit) Resize1D(w int)

func (*Bit) Resize2D

func (m *Bit) Resize2D(w, h int)

func (*Bit) Resize3D

func (m *Bit) Resize3D(w, h, d int)

func (*Bit) Resize4D

func (m *Bit) Resize4D(w, h, d, x int)

func (*Bit) ResizeN

func (m *Bit) ResizeN(lengths ...int)

func (*Bit) Set1D

func (m *Bit) Set1D(value int, x int)

func (*Bit) Set2D

func (m *Bit) Set2D(value int, x, y int)

func (*Bit) Set3D

func (m *Bit) Set3D(value int, x, y, z int)

func (*Bit) Set4D

func (m *Bit) Set4D(value int, x, y, z, w int)

func (*Bit) SetN

func (m *Bit) SetN(value int, offsets ...int)

func (Bit) Width

func (m Bit) Width() int

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) Capacity

func (m Bool) Capacity() int

func (*Bool) Clear

func (m *Bool) Clear()

func (Bool) Depth

func (m Bool) Depth() int

func (Bool) DimensionN

func (m Bool) DimensionN(n int) int

func (Bool) Dimensionality

func (m Bool) Dimensionality() int

func (Bool) Dimensions1D

func (m Bool) Dimensions1D() int

func (Bool) Dimensions2D

func (m Bool) Dimensions2D() (int, int)

func (Bool) Dimensions3D

func (m Bool) Dimensions3D() (int, int, int)

func (Bool) Dimensions4D

func (m Bool) Dimensions4D() (int, int, int, int)

func (Bool) Extent

func (m Bool) Extent() int

func (Bool) Get1D

func (m Bool) Get1D(x int) bool

func (Bool) Get2D

func (m Bool) Get2D(x, y int) bool

func (Bool) Get3D

func (m Bool) Get3D(x, y, z int) bool

func (Bool) Get4D

func (m Bool) Get4D(x, y, z, w int) bool

func (Bool) GetN

func (m Bool) GetN(offsets ...int) bool

func (Bool) Height

func (m Bool) Height() int

func (*Bool) Resize1D

func (m *Bool) Resize1D(w int)

func (*Bool) Resize2D

func (m *Bool) Resize2D(w, h int)

func (*Bool) Resize3D

func (m *Bool) Resize3D(w, h, d int)

func (*Bool) Resize4D

func (m *Bool) Resize4D(w, h, d, x int)

func (*Bool) ResizeN

func (m *Bool) ResizeN(lengths ...int)

func (*Bool) Set1D

func (m *Bool) Set1D(value bool, x int)

func (*Bool) Set2D

func (m *Bool) Set2D(value bool, x, y int)

func (*Bool) Set3D

func (m *Bool) Set3D(value bool, x, y, z int)

func (*Bool) Set4D

func (m *Bool) Set4D(value bool, x, y, z, w int)

func (*Bool) SetN

func (m *Bool) SetN(value bool, offsets ...int)

func (Bool) Width

func (m Bool) Width() int

type Constructor

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

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) Capacity

func (m Diagonal) Capacity() int

func (*Diagonal[T]) Clear

func (m *Diagonal[T]) Clear()

func (Diagonal) Depth

func (m Diagonal) Depth() int

func (Diagonal) DimensionN

func (m Diagonal) DimensionN(n int) int

func (Diagonal) Dimensionality

func (m Diagonal) Dimensionality() int

func (Diagonal) Dimensions1D

func (m Diagonal) Dimensions1D() int

func (Diagonal) Dimensions2D

func (m Diagonal) Dimensions2D() (int, int)

func (Diagonal) Dimensions3D

func (m Diagonal) Dimensions3D() (int, int, int)

func (Diagonal) Dimensions4D

func (m Diagonal) Dimensions4D() (int, int, int, int)

func (Diagonal) Extent

func (m Diagonal) Extent() int

func (Diagonal[T]) Get1D

func (m Diagonal[T]) Get1D(x int) T

func (Diagonal[T]) Get2D

func (m Diagonal[T]) Get2D(x, y int) T

func (Diagonal[T]) Get3D

func (m Diagonal[T]) Get3D(x, y, z int) T

func (Diagonal[T]) Get4D

func (m Diagonal[T]) Get4D(x, y, z, w int) T

func (Diagonal[T]) GetN

func (m Diagonal[T]) GetN(offset ...int) T

func (Diagonal) Height

func (m Diagonal) Height() int

func (*Diagonal[T]) Resize1D

func (m *Diagonal[T]) Resize1D(w int)

func (*Diagonal[T]) Resize2D

func (m *Diagonal[T]) Resize2D(w, h int)

func (*Diagonal[T]) Resize3D

func (m *Diagonal[T]) Resize3D(w, h, d int)

func (*Diagonal[T]) Resize4D

func (m *Diagonal[T]) Resize4D(w, h, d, x int)

func (*Diagonal[T]) ResizeN

func (m *Diagonal[T]) ResizeN(lengths ...int)

func (*Diagonal[T]) Set1D

func (m *Diagonal[T]) Set1D(value T, x int)

func (*Diagonal[T]) Set2D

func (m *Diagonal[T]) Set2D(value T, x, y int)

func (*Diagonal[T]) Set3D

func (m *Diagonal[T]) Set3D(value T, x, y, z int)

func (*Diagonal[T]) Set4D

func (m *Diagonal[T]) Set4D(value T, x, y, z, w int)

func (*Diagonal[T]) SetN

func (m *Diagonal[T]) SetN(value T, offsets ...int)

func (Diagonal) Width

func (m Diagonal) Width() int

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) Capacity

func (m Grid) Capacity() int

func (*Grid[T]) Clear

func (m *Grid[T]) Clear()

func (Grid) Depth

func (m Grid) Depth() int

func (Grid) DimensionN

func (m Grid) DimensionN(n int) int

func (Grid) Dimensionality

func (m Grid) Dimensionality() int

func (Grid) Dimensions1D

func (m Grid) Dimensions1D() int

func (Grid) Dimensions2D

func (m Grid) Dimensions2D() (int, int)

func (Grid) Dimensions3D

func (m Grid) Dimensions3D() (int, int, int)

func (Grid) Dimensions4D

func (m Grid) Dimensions4D() (int, int, int, int)

func (Grid) Extent

func (m Grid) Extent() int

func (Grid[T]) Get1D

func (m Grid[T]) Get1D(x int) T

func (Grid[T]) Get2D

func (m Grid[T]) Get2D(x, y int) T

func (Grid[T]) Get3D

func (m Grid[T]) Get3D(x, y, z int) T

func (Grid[T]) Get4D

func (m Grid[T]) Get4D(x, y, z, w int) T

func (Grid[T]) GetN

func (m Grid[T]) GetN(offsets ...int) T

func (Grid) Height

func (m Grid) Height() int

func (*Grid[T]) Resize1D

func (m *Grid[T]) Resize1D(w int)

func (*Grid[T]) Resize2D

func (m *Grid[T]) Resize2D(w, h int)

func (*Grid[T]) Resize3D

func (m *Grid[T]) Resize3D(w, h, d int)

func (*Grid[T]) Resize4D

func (m *Grid[T]) Resize4D(w, h, d, x int)

func (*Grid[T]) ResizeN

func (m *Grid[T]) ResizeN(lengths ...int)

func (*Grid[T]) Set1D

func (m *Grid[T]) Set1D(value T, x int)

func (*Grid[T]) Set2D

func (m *Grid[T]) Set2D(value T, x, y int)

func (*Grid[T]) Set3D

func (m *Grid[T]) Set3D(value T, x, y, z int)

func (*Grid[T]) Set4D

func (m *Grid[T]) Set4D(value T, x, y, z, w int)

func (Grid[T]) SetN

func (m Grid[T]) SetN(value T, offsets ...int)

func (Grid) Width

func (m Grid) Width() int

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) Capacity

func (m Hash) Capacity() int

func (*Hash[T]) Clear

func (m *Hash[T]) Clear()

func (Hash) Depth

func (m Hash) Depth() int

func (Hash) DimensionN

func (m Hash) DimensionN(n int) int

func (Hash) Dimensionality

func (m Hash) Dimensionality() int

func (Hash) Dimensions1D

func (m Hash) Dimensions1D() int

func (Hash) Dimensions2D

func (m Hash) Dimensions2D() (int, int)

func (Hash) Dimensions3D

func (m Hash) Dimensions3D() (int, int, int)

func (Hash) Dimensions4D

func (m Hash) Dimensions4D() (int, int, int, int)

func (Hash) Extent

func (m Hash) Extent() int

func (Hash[T]) Get1D

func (m Hash[T]) Get1D(x int) T

func (Hash[T]) Get2D

func (m Hash[T]) Get2D(x, y int) T

func (Hash[T]) Get3D

func (m Hash[T]) Get3D(x, y, z int) T

func (Hash[T]) Get4D

func (m Hash[T]) Get4D(x, y, z, w int) T

func (*Hash[T]) GetN

func (m *Hash[T]) GetN(offsets ...int) T

func (Hash) Height

func (m Hash) Height() int

func (Hash[T]) Reduce

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

func (*Hash[T]) Resize1D

func (m *Hash[T]) Resize1D(width int)

func (*Hash[T]) Resize2D

func (m *Hash[T]) Resize2D(width, height int)

func (*Hash[T]) Resize3D

func (m *Hash[T]) Resize3D(width, height, depth int)

func (*Hash[T]) Resize4D

func (m *Hash[T]) Resize4D(width, height, depth, extent int)

func (*Hash[T]) ResizeN

func (m *Hash[T]) ResizeN(lengths ...int)

func (*Hash[T]) Set1D

func (m *Hash[T]) Set1D(value T, x int)

func (*Hash[T]) Set2D

func (m *Hash[T]) Set2D(value T, x, y int)

func (*Hash[T]) Set3D

func (m *Hash[T]) Set3D(value T, x, y, z int)

func (*Hash[T]) Set4D

func (m *Hash[T]) Set4D(value T, x, y, z, w int)

func (*Hash[T]) SetN

func (m *Hash[T]) SetN(value T, offsets ...int)

func (Hash) Width

func (m Hash) Width() int

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.

func NewBool

func NewBool() Interface[bool]

NewBool is a matrix Constructor.

func NewDiagonal

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

NewDiagonal is a matrix Constructor.

func NewGrid

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

NewGrid is a matrix Constructor.

func NewHash

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

NewHash is a matrix Constructor.

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