mat32

package
v0.0.0-...-d860047 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNegativeDimension   = Error{"mat: negative dimension"}
	ErrIndexOutOfRange     = Error{"mat: index out of range"}
	ErrReuseNonEmpty       = Error{"mat: reuse of non-empty matrix"}
	ErrRowAccess           = Error{"mat: row index out of range"}
	ErrColAccess           = Error{"mat: column index out of range"}
	ErrVectorAccess        = Error{"mat: vector index out of range"}
	ErrZeroLength          = Error{"mat: zero length in matrix dimension"}
	ErrRowLength           = Error{"mat: row length mismatch"}
	ErrColLength           = Error{"mat: col length mismatch"}
	ErrSquare              = Error{"mat: expect square matrix"}
	ErrNormOrder           = Error{"mat: invalid norm order for matrix"}
	ErrSingular            = Error{"mat: matrix is singular"}
	ErrShape               = Error{"mat: dimension mismatch"}
	ErrIllegalStride       = Error{"mat: illegal stride"}
	ErrPivot               = Error{"mat: malformed pivot list"}
	ErrTriangle            = Error{"mat: triangular storage mismatch"}
	ErrTriangleSet         = Error{"mat: triangular set out of bounds"}
	ErrBandwidth           = Error{"mat: bandwidth out of range"}
	ErrBandSet             = Error{"mat: band set out of bounds"}
	ErrDiagSet             = Error{"mat: diagonal set out of bounds"}
	ErrSliceLengthMismatch = Error{"mat: input slice length mismatch"}
	ErrNotPSD              = Error{"mat: input not positive symmetric definite"}
	ErrFailedEigen         = Error{"mat: eigendecomposition not successful"}
)

Functions

func Equal

func Equal(a, b Matrix) bool

Equal returns whether the matrices a and b have the same size and are element-wise equal.

func Formatted

func Formatted(m Matrix, options ...FormatOption) fmt.Formatter

Formatted returns a fmt.Formatter for the matrix m using the given options.

func Max

func Max(a Matrix) float32

Max returns the largest element value of the matrix A.

Max will panic with ErrZeroLength if the matrix has zero size.

func Min

func Min(a Matrix) float32

Min returns the smallest element value of the matrix A.

Min will panic with ErrZeroLength if the matrix has zero size.

func Norm

func Norm(a Matrix, norm float32) float32

Norm returns the specified norm of the matrix A. Valid norms are:

1 - The maximum absolute column sum
2 - The Frobenius norm, the square root of the sum of the squares of the elements
Inf - The maximum absolute row sum

If a is a Normer, its Norm method will be used to calculate the norm.

Norm will panic with ErrNormOrder if an illegal norm is specified and with ErrShape if the matrix has zero size.

func Row

func Row(dst []float32, i int, a Matrix) []float32

Row copies the elements in the ith row of the matrix into the slice dst. The length of the provided slice must equal the number of columns, unless the slice is nil in which case a new slice is first allocated.

func Sum

func Sum(a Matrix) float32

Sum returns the sum of the elements of the matrix.

Sum will panic with ErrZeroLength if the matrix has zero size.

Types

type Dense

type Dense struct {
	// contains filtered or unexported fields
}

Dense is a dense matrix representation.

func DenseCopyOf

func DenseCopyOf(a Matrix) *Dense

DenseCopyOf returns a newly allocated copy of the elements of a.

func NewDense

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

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 Dense 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) Add

func (m *Dense) Add(a, b Matrix)

Add adds a and b element-wise, placing the result in the receiver. Add will panic if the two matrices do not have the same shape.

func (*Dense) Apply

func (m *Dense) Apply(fn func(i, j int, v float32) float32, a Matrix)

Apply applies the function fn to each of the elements of a, placing the resulting matrix in the receiver. The function fn takes a row/column index and element value and returns some function of that tuple.

func (*Dense) At

func (m *Dense) At(i, j int) float32

At returns the element at row i, column j.

func (*Dense) Augment

func (m *Dense) Augment(a, b Matrix)

Augment creates the augmented matrix of a and b, where b is placed in the greater indexed columns. Augment will panic if the two input matrices do not have the same number of rows or the constructed augmented matrix is not the same shape as the receiver.

func (*Dense) Caps

func (m *Dense) Caps() (r, c int)

Caps returns the number of rows and columns in the backing matrix.

func (*Dense) CloneFrom

func (m *Dense) CloneFrom(a Matrix)

CloneFrom makes a copy of a into the receiver, overwriting the previous value of the receiver. The clone from operation does not make any restriction on shape and will not cause shadowing.

See the ClonerFrom interface for more information.

func (*Dense) ColView

func (m *Dense) ColView(j int) Vector

ColView returns a Vector reflecting the column j, backed by the matrix data.

See ColViewer for more information.

func (*Dense) Copy

func (m *Dense) Copy(a Matrix) (r, c int)

Copy makes a copy of elements of a into the receiver. It is similar to the built-in copy; it copies as much as the overlap between the two matrices and returns the number of rows and columns it copied. If a aliases the receiver and is a transposed Dense or VecDense, with a non-unitary increment, Copy will panic.

See the Copier interface for more information.

func (*Dense) Dims

func (m *Dense) Dims() (r, c int)

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

func (*Dense) DivElem

func (m *Dense) DivElem(a, b Matrix)

DivElem performs element-wise division of a by b, placing the result in the receiver. DivElem will panic if the two matrices do not have the same shape.

func (*Dense) IsEmpty

func (m *Dense) IsEmpty() bool

IsEmpty returns whether the receiver is empty. Empty matrices can be the receiver for size-restricted operations. The receiver can be emptied using Reset.

func (*Dense) Mul

func (m *Dense) Mul(a, b Matrix)

Mul takes the matrix product of a and b, placing the result in the receiver. If the number of columns in a does not equal the number of rows in b, Mul will panic.

func (*Dense) MulElem

func (m *Dense) MulElem(a, b Matrix)

MulElem performs element-wise multiplication of a and b, placing the result in the receiver. MulElem will panic if the two matrices do not have the same shape.

func (*Dense) RawMatrix

func (m *Dense) RawMatrix() blas32.General

RawMatrix returns the underlying blas32.General used by the receiver. Changes to elements in the receiver following the call will be reflected in returned blas32.General.

func (*Dense) RowView

func (m *Dense) RowView(i int) Vector

RowView returns row i of the matrix data represented as a column vector, backed by the matrix data.

See RowViewer for more information.

func (*Dense) Scale

func (m *Dense) Scale(f float32, a Matrix)

Scale multiplies the elements of a by f, placing the result in the receiver.

See the Scaler interface for more information.

func (*Dense) Set

func (m *Dense) Set(i, j int, v float32)

Set sets the element at row i, column j to the value v.

func (*Dense) SetCol

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

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

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

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

func (*Dense) Slice

func (m *Dense) Slice(i, k, j, l int) Matrix

Slice returns a new Matrix that shares backing data with the receiver. The returned matrix starts at {i,j} of the receiver and extends k-i rows and l-j columns. The final row in the resulting matrix is k-1 and the final column is l-1. Slice panics with ErrIndexOutOfRange if the slice is outside the capacity of the receiver.

func (*Dense) Stack

func (m *Dense) Stack(a, b Matrix)

Stack appends the rows of b onto the rows of a, placing the result into the receiver with b placed in the greater indexed rows. Stack will panic if the two input matrices do not have the same number of columns or the constructed stacked matrix is not the same shape as the receiver.

func (*Dense) Sub

func (m *Dense) Sub(a, b Matrix)

Sub subtracts the matrix b from a, placing the result in the receiver. Sub will panic if the two matrices do not have the same shape.

func (*Dense) T

func (m *Dense) T() Matrix

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

func (*Dense) Zero

func (m *Dense) Zero()

Zero sets all of the matrix elements to zero.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents matrix handling errors. These errors can be recovered by Maybe wrappers.

func (Error) Error

func (err Error) Error() string

type FormatOption

type FormatOption func(*formatter)

FormatOption is a functional option for matrix formatting.

type Matrix

type Matrix 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) float32

	// T returns the transpose of the Matrix. Whether T returns a copy of the
	// underlying data is implementation dependent.
	// This method may be implemented using the Transpose type, which
	// provides an implicit matrix transpose.
	T() Matrix
}

Matrix is the basic matrix interface type.

type Normer

type Normer interface {
	Norm(norm float32) float32
}

A Normer can compute a norm of the matrix. Valid norms are:

1 - The maximum absolute column sum
2 - The Frobenius norm, the square root of the sum of the squares of the elements
Inf - The maximum absolute row sum

type RawMatrixer

type RawMatrixer interface {
	RawMatrix() blas32.General
}

A RawMatrixer can return a blas32.General representation of the receiver. Changes to the blas32.General.Data slice will be reflected in the original matrix, changes to the Rows, Cols and Stride fields will not.

type RawVectorer

type RawVectorer interface {
	RawVector() blas32.Vector
}

A RawVectorer can return a blas32.Vector representation of the receiver. Changes to the blas32.Vector.Data slice will be reflected in the original matrix, changes to the Inc field will not.

type Transpose

type Transpose struct {
	Matrix Matrix
}

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

func (t Transpose) At(i, j int) float32

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.

func (Transpose) Dims

func (t Transpose) 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

func (t Transpose) T() Matrix

T performs an implicit transpose by returning the Matrix field.

func (Transpose) Untranspose

func (t Transpose) Untranspose() Matrix

Untranspose returns the Matrix field.

type Untransposer

type Untransposer interface {

	// Untranspose returns the underlying Matrix stored for the implicit transpose.
	Untranspose() Matrix
}

Untransposer is a type that can undo an implicit transpose.

type VecDense

type VecDense struct {
	// contains filtered or unexported fields
}

VecDense represents a column vector.

func NewVecDense

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

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

func (v *VecDense) AddVec(a, b Vector)

AddVec adds the vectors a and b, placing the result in the receiver.

func (*VecDense) At

func (v *VecDense) At(i, j int) float32

At returns the element at row i. It panics if i is out of bounds or if j is not zero.

func (*VecDense) AtVec

func (v *VecDense) AtVec(i int) float32

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

func (*VecDense) Cap

func (v *VecDense) Cap() int

Cap returns the capacity of the vector.

func (*VecDense) CloneFromVec

func (v *VecDense) CloneFromVec(a Vector)

CloneFromVec makes a copy of a into the receiver, overwriting the previous value of the receiver.

func (*VecDense) ColViewOf

func (v *VecDense) ColViewOf(m RawMatrixer, j int)

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

func (*VecDense) CopyVec

func (v *VecDense) CopyVec(a Vector) int

CopyVec makes a copy of elements of a into the receiver. It is similar to the built-in copy; it copies as much as the overlap between the two vectors and returns the number of elements it copied.

func (*VecDense) Dims

func (v *VecDense) Dims() (r, c int)

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

func (*VecDense) DivElemVec

func (v *VecDense) DivElemVec(a, b Vector)

DivElemVec performs element-wise division of a by b, placing the result in the receiver.

func (*VecDense) IsEmpty

func (v *VecDense) IsEmpty() bool

IsEmpty returns whether the receiver is empty. Empty matrices can be the receiver for size-restricted operations. The receiver can be emptied using Reset.

func (*VecDense) Len

func (v *VecDense) Len() int

Len returns the length of the vector.

func (*VecDense) RawVector

func (v *VecDense) RawVector() blas32.Vector

RawVector returns the underlying blas32.Vector used by the receiver. Changes to elements in the receiver following the call will be reflected in returned blas32.Vector.

func (*VecDense) RowViewOf

func (v *VecDense) RowViewOf(m RawMatrixer, i int)

RowViewOf reflects the row i of the RawMatrixer 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) SetVec

func (v *VecDense) SetVec(i int, val float32)

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

func (*VecDense) SliceVec

func (v *VecDense) SliceVec(i, k int) Vector

SliceVec returns a new Vector that shares backing data with the receiver. The returned matrix starts at i of the receiver and extends k-i elements. SliceVec panics with ErrIndexOutOfRange if the slice is outside the capacity of the receiver.

func (*VecDense) SubVec

func (v *VecDense) SubVec(a, b Vector)

SubVec subtracts the vector b from a, placing the result in the receiver.

func (*VecDense) T

func (v *VecDense) T() Matrix

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

type Vector

type Vector interface {
	Matrix
	AtVec(int) float32
	Len() int
}

Vector is a vector.

Jump to

Keyboard shortcuts

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