matrix

package
v0.0.0-...-5bd4ecb Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package matrix provides a generic implementation of matrix structure.

This is similar to https://github.com/gonum/gonum with minimal API, new methods and generic implementation.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrZeroLength        = errors.New("matrix: zero length in matrix dimension")
	ErrNegativeDimension = errors.New("matrix: negative dimension")
	ErrShape             = errors.New("matrix: dimension mismatch")
	ErrRowAccess         = errors.New("matrix: row index out of range")
	ErrColAccess         = errors.New("matrix: column index out of range")
	ErrRowLength         = errors.New("matrix: row length mismatch")
	ErrColLength         = errors.New("matrix: column length mismatch")
	ErrVectorLength      = errors.New("matrix: vector length mismatch")
)

Functions

This section is empty.

Types

type Dense

type Dense[T any] struct {
	// Rows and Cols are the total number of rows and columns in the matrix.
	Rows int
	Cols int

	// Stride is the number of elements between beginnings of successive
	// array elements. In other words, it tells us how many elements to skip
	// to move to the next position along a certain axis.
	Stride int

	// Data is an array of elements contained in the matrix. The order is from
	// top to bottom, left to right.
	Data []T
}

Dense is a generic dense matrix representation.

func NewDense

func NewDense[T any](r, c int, data []T) *Dense[T]

NewDense creates a new Dense matrix with r rows and c columns. If data == nil, a new slice is allocated for the backing slice. If len(data) == r*c, data is used as the backing slice, and changes to the elements of the returned Matrix will be reflected in data. If neither of these is true, NewDense will panic. NewDense will panic if either r or c is zero.

The data must be arranged in row-major order, i.e. the (i*c + j)-th element in the data slice is the {i, j}-th element in the matrix.

func (*Dense[T]) AppendRow

func (m *Dense[T]) AppendRow(src []T)

AppendRow appends a new row at the end of the matrix with the values in src. If the receiver is empty, as determined by the `IsEmpty()` method, then it will be initialized as per the given src. It will panic if len(src) is not equal to the number of columns in a non-empty receiver.

func (*Dense[T]) At

func (m *Dense[T]) At(i, j int) T

At returns the value of a matrix element at row i, column j. It will panic if i or j are out of bounds for the matrix.

func (*Dense[T]) ColView

func (m *Dense[T]) ColView(j int) *VecDense[T]

ColView returns a Vector reflecting the column j, backed by the matrix data. It will panic if j is out of bounds for the matrix.

func (*Dense[T]) Copy

func (m *Dense[T]) Copy() *Dense[T]

Copy returns a copy of the receiver matrix.

func (*Dense[T]) Dims

func (m *Dense[T]) Dims() (r, c int)

Dims returns the number of rows and columns in the matrix.

func (*Dense[T]) IsEmpty

func (m *Dense[T]) IsEmpty() bool

IsEmpty returns whether the receiver is empty.

func (*Dense[T]) RawRowView

func (m *Dense[T]) RawRowView(i int) []T

RawRowView returns a slice for the specified row backed by the same array as backing the receiver. It will panic if i is out of bounds for the matrix.

func (*Dense[T]) RowView

func (m *Dense[T]) RowView(i int) *VecDense[T]

RowView returns row i of the matrix data represented as a column vector, backed by the matrix data. It will panic if i is out of bounds for the matrix.

func (*Dense[T]) Set

func (m *Dense[T]) Set(i, j int, v T)

Set sets the element at row i, column j to the value v. It will panic if i or j are out of bounds for the matrix.

func (*Dense[T]) SetCol

func (m *Dense[T]) SetCol(j int, src []T)

SetCol sets the values in the specified column of the matrix to the values in src. len(src) must equal the number of rows in the receiver.

func (*Dense[T]) SetRow

func (m *Dense[T]) SetRow(i int, src []T)

SetRow sets the values in the specified row i of the matrix to the values in src. len(src) must equal the number of columns in the receiver. It will panic if i is out of bounds for the matrix. Use `AppendRow` to append a new row to the matrix.

func (*Dense[T]) SliceRow

func (m *Dense[T]) SliceRow(r, start, stop int) []T

SliceRow returns a slice of the specified row `r` from `start` (inclusive) upto `stop` (exclusive). The same rule applies for the slice parameters as governed by the language except this requires both the start and stop index.

It will panic if `r`, `start`, `stop` is out of bounds with an exception that `stop` can be equal to the number of columns.

func (*Dense[T]) T

func (m *Dense[T]) T() Matrix[T]

T performs an implicit transpose by returning the receiver inside a Transpose.

type Matrix

type Matrix[T any] interface {
	// Dims returns the dimensions of a Matrix.
	Dims() (r, c int)

	// At returns the value of a matrix element at row i, column j.
	// It will panic if i or j are out of bounds for the matrix.
	At(i, j int) T

	// Set alters the matrix element at row i, column j to v.
	// It will panic if i or j are out of bounds for the matrix.
	Set(i, j int, v T)

	// T returns the transpose of the Matrix.
	T() Matrix[T]
}

Matrix is the basic matrix interface type.

type Transpose

type Transpose[T any] struct {
	Matrix Matrix[T]
}

Transpose is a type for performing an implicit matrix transpose. It implements the Matrix interface, returning values from the transpose of the matrix within.

func (Transpose[T]) At

func (t Transpose[T]) At(i, j int) T

At returns the value of the element at row i and column j of the transposed matrix, that is, row j and column i of the Matrix field. It will panic if i or j are out of bounds for the matrix.

func (Transpose[T]) Dims

func (t Transpose[T]) Dims() (r, c int)

Dims returns the dimensions of the transposed matrix. The number of rows returned is the number of columns in the Matrix field, and the number of columns is the number of rows in the Matrix field.

func (Transpose[T]) Set

func (t Transpose[T]) Set(i, j int, v T)

Set sets the element at row i, column j of the transposed matrix to the value v, that is, row j and column i of the Matrix field. It will panic if i or j are out of bounds for the matrix.

func (Transpose[T]) T

func (t Transpose[T]) T() Matrix[T]

T performs an implicit transpose by returning the Matrix field.

type VecDense

type VecDense[T any] struct {
	N    int
	Inc  int
	Data []T
}

VecDense represents a column vector.

func NewVecDense

func NewVecDense[T any](n int, data []T) *VecDense[T]

NewVecDense creates a new VecDense of length n. If data == nil, a new slice is allocated for the backing slice. If len(data) == n, data is used as the backing slice, and changes to the elements of the returned VecDense will be reflected in data. If neither of these is true, NewVecDense will panic. NewVecDense will panic if n is zero.

func (*VecDense[T]) At

func (v *VecDense[T]) At(i int) T

At returns the element at row i. It panics if i is out of bounds.

func (*VecDense[T]) ColViewOf

func (v *VecDense[T]) ColViewOf(m *Dense[T], j int)

ColViewOf reflects the column j of the Matrix m, into the receiver backed by the same underlying data. The receiver must either be empty or have length equal to the number of rows of m.

func (*VecDense[T]) Dims

func (v *VecDense[T]) Dims() (r, c int)

Dims returns the number of rows and columns in the matrix. Columns is always 1 for the vector.

func (*VecDense[T]) IsEmpty

func (v *VecDense[T]) IsEmpty() bool

IsEmpty returns whether the receiver is empty.

func (*VecDense[T]) Len

func (v *VecDense[T]) Len() int

Len returns the length of the vector.

func (*VecDense[T]) RowViewOf

func (v *VecDense[T]) RowViewOf(m *Dense[T], i int)

RowViewOf reflects the row i of the Matrix m, into the receiver backed by the same underlying data. The receiver must either be empty or have length equal to the number of columns of m.

func (*VecDense[T]) Set

func (v *VecDense[T]) Set(i int, val T)

Set sets the element at row i to the value val. It panics if i is out of bounds.

Jump to

Keyboard shortcuts

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