mat32

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: BSD-2-Clause Imports: 10 Imported by: 0

README

Matrix

In spaGo, all mathematical operations are performed on 2D matrices (vectors and scalars as a subset of). The Matrix interface defines setter and getter methods to access its elements plus a few methods to perform linear algebra operations with other matrices, such as element-wise addition, subtraction, product, and matrix-matrix multiplication. Other convenient methods are the usual max(), min(), abs(), and not much else. Dense and Sparse are the two Matrix implementations values that work with dense and sparse values, respectively.

Note

The performance of linear algebra operations is a major bottleneck in a machine learning library. The higher the speed on this basic module, the higher the throughput of high-level tasks. This is why after several attempts to write efficient code in Go, I decided to rely on Gonum assembly code, importing their _ internal_ package directly into spaGO.

By the way, some colleagues asked me why I didn't use Gonum library as a whole instead of reimplementing it all from scratch. First, I wanted to get my hands dirty and understand what structures and math operations are needed to build deep learning models from the ground up: that's what spaGO is all about! Second, Gonum uses distinct types for matrices, vectors, symmetric and triangular matrices, transposed, and so on, often forcing you to manage numerous "switch" in your code. It is just too sophisticated for what I think I need at the moment. Giving power to the Matrix by adding many functions is a common approach. Nevertheless, in spaGO, I wanted to reduce the responsibility of this part, keeping the implementation really to a minimum.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInf

func IsInf(f Float, sign int) bool

IsInf reports whether f is an infinity, according to sign.

func MarshalBinaryMatrix added in v0.4.0

func MarshalBinaryMatrix(m Matrix, w io.Writer) error

MarshalBinaryMatrix encodes a Matrix into binary form.

func Print

func Print(a Matrix)

Print performs a simple print of the matrix.

func ReleaseDense

func ReleaseDense(w *Dense)

ReleaseDense replaces a used *Dense into the appropriate size workspace pool. ReleaseDense must not be called with a matrix where references to the underlying data slice have been kept.

func SameDims

func SameDims(a, b Matrix) bool

SameDims returns whether the two matrices have the same number of rows and columns (so also of the same size).

func SameSize

func SameSize(a, b Matrix) bool

SameSize returns whether the two matrices have the same size (number of elements).

func VectorsOfSameSize

func VectorsOfSameSize(a, b Matrix) bool

VectorsOfSameSize returns whether the two matrices are vector of the same size.

Types

type Coordinate

type Coordinate struct {
	I, J int
}

Coordinate represents the row I and column J of a Sparse matrix.

type Dense

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

Dense is a Matrix implementation that uses Float as data type.

func ConcatH

func ConcatH(ms ...Matrix) *Dense

ConcatH returns a new Matrix created concatenating the input matrices horizontally.

func GetDenseWorkspace

func GetDenseWorkspace(r, c int) *Dense

GetDenseWorkspace returns a *Dense of size r×c and a data slice with a cap that is less than 2*r*c. Warning, the values may not be at zero. If you need a ready-to-use matrix you can call GetEmptyDenseWorkspace().

func GetEmptyDenseWorkspace

func GetEmptyDenseWorkspace(r, c int) *Dense

GetEmptyDenseWorkspace returns a *Dense of size r×c and a data slice with a cap that is less than 2*r*c. The returned matrix is ready-to-use (with all the values set to zeros).

func I

func I(size int) *Dense

I a.k.a identity returns square matrix with ones on the diagonal and zeros elsewhere.

func NewDense

func NewDense(rows, cols int, elements []Float) *Dense

NewDense returns a new rows x cols dense matrix populated with a copy of the elements. The elements cannot be nil, panic otherwise. Use NewEmptyDense to initialize an empty matrix.

func NewEmptyDense

func NewEmptyDense(rows, cols int) *Dense

NewEmptyDense returns a new rows x cols matrix initialized to zeros.

func NewEmptyVecDense

func NewEmptyVecDense(size int) *Dense

NewEmptyVecDense returns a new vector of the given size, initialized to zeros.

func NewInitDense

func NewInitDense(rows, cols int, val Float) *Dense

NewInitDense returns a new rows x cols dense matrix initialized with a constant value.

func NewInitVecDense

func NewInitVecDense(size int, val Float) *Dense

NewInitVecDense returns a new size x 1 dense matrix initialized with a constant value.

func NewScalar

func NewScalar(n Float) *Dense

NewScalar returns a new 1x1 matrix containing the input value.

func NewVecDense

func NewVecDense(elements []Float) *Dense

NewVecDense returns a new column vector populated with a copy of the elements. The elements cannot be nil, panic otherwise. Use NewEmptyVecDense to initialize an empty matrix.

func OneHotVecDense

func OneHotVecDense(size int, oneAt int) *Dense

OneHotVecDense returns a new one-hot vector of the given size.

func Stack

func Stack(vs ...*Dense) *Dense

Stack returns a new Matrix created concatenating the input vectors horizontally.

func (*Dense) Abs

func (d *Dense) Abs() Matrix

Abs returns a new matrix applying the absolute value function to all elements.

func (*Dense) Add

func (d *Dense) Add(other Matrix) Matrix

Add returns the addition between the receiver and another matrix.

func (*Dense) AddInPlace

func (d *Dense) AddInPlace(other Matrix) Matrix

AddInPlace performs the in-place addition with the other matrix.

func (*Dense) AddScalar

func (d *Dense) AddScalar(n Float) Matrix

AddScalar performs the addition between the matrix and the given value.

func (*Dense) AddScalarInPlace

func (d *Dense) AddScalarInPlace(n Float) Matrix

AddScalarInPlace adds the scalar to all values of the matrix.

func (*Dense) Apply

func (d *Dense) Apply(fn func(i, j int, v Float) Float, a Matrix)

Apply executes the unary function fn.

func (*Dense) ApplyWithAlpha

func (d *Dense) ApplyWithAlpha(fn func(i, j int, v Float, alpha ...Float) Float, a Matrix, alpha ...Float)

ApplyWithAlpha executes the unary function fn, taking additional parameters alpha.

func (*Dense) At

func (d *Dense) At(i int, j int) Float

At returns the value at row i and column j. It panics if the given indices are out of range.

func (*Dense) AtVec

func (d *Dense) AtVec(i int) Float

AtVec returns the value at position i of a vector. It panics if the receiver is not a vector.

func (*Dense) Augment

func (d *Dense) Augment() Matrix

Augment places the identity matrix at the end of the original matrix

func (*Dense) ClipInPlace

func (d *Dense) ClipInPlace(min, max Float) Matrix

ClipInPlace clips in place each value of the matrix.

func (*Dense) Clone

func (d *Dense) Clone() Matrix

Clone returns a new Dense matrix, copying all its values from the receiver.

func (*Dense) Columns

func (d *Dense) Columns() int

Columns returns the number of columns of the matrix.

func (*Dense) Copy

func (d *Dense) Copy(other Matrix)

Copy copies the data from the other matrix to the receiver. It panics if the matrices have different dimensions, or if the other matrix is not Dense.

func (*Dense) Data

func (d *Dense) Data() []Float

Data returns the underlying data of the matrix, as a raw one-dimensional slice of values.

func (*Dense) Dims

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

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

func (*Dense) Div

func (d *Dense) Div(other Matrix) Matrix

Div returns the result of the element-wise division of the receiver by the other matrix.

func (*Dense) DivInPlace

func (d *Dense) DivInPlace(other Matrix) Matrix

DivInPlace performs the in-place element-wise division of the receiver by the other matrix.

func (*Dense) DotUnitary

func (d *Dense) DotUnitary(other Matrix) Float

DotUnitary returns the dot product of two vectors.

func (*Dense) ExtractColumn

func (d *Dense) ExtractColumn(i int) Matrix

ExtractColumn returns a copy of the i-th column of the matrix.

func (*Dense) ExtractRow

func (d *Dense) ExtractRow(i int) Matrix

ExtractRow returns a copy of the i-th row of the matrix.

func (*Dense) Format

func (d *Dense) Format(f fmt.State, c rune)

Format implements custom formatting for represeinting a Dense matrix. Thanks to this method, a Dense matrix implements the fmt.Formatter interface.

func (Dense) Inverse

func (d Dense) Inverse() Matrix

Inverse returns the inverse of the matrix.

func (*Dense) IsScalar

func (d *Dense) IsScalar() bool

IsScalar returns whether the matrix contains exactly one scalar value.

func (*Dense) IsVector

func (d *Dense) IsVector() bool

IsVector returns whether the matrix is either a row or column vector.

func (*Dense) LU

func (d *Dense) LU() (l, u, p *Dense)

LU performs lower–upper (LU) decomposition of a square matrix D such as PLU = D, L is lower diagonal and U is upper diagonal, p are pivots.

func (*Dense) LastIndex

func (d *Dense) LastIndex() int

LastIndex returns the last element's index, in respect of linear indexing. It returns -1 if the matrix is empty.

func (Dense) MarshalBinary added in v0.4.0

func (d Dense) MarshalBinary() ([]byte, error)

MarshalBinary marshals a Dense matrix into binary form.

func (*Dense) Max

func (d *Dense) Max() Float

Max returns the maximum value of the matrix.

func (*Dense) Maximum

func (d *Dense) Maximum(other Matrix) *Dense

Maximum returns a new matrix containing the element-wise maxima.

func (*Dense) Min

func (d *Dense) Min() Float

Min returns the minimum value of the matrix.

func (*Dense) Minimum

func (d *Dense) Minimum(other Matrix) *Dense

Minimum returns a new matrix containing the element-wise minima.

func (*Dense) Mul

func (d *Dense) Mul(other Matrix) Matrix

Mul performs the multiplication row by column. If A is an i×j Matrix, and B is j×k, then the resulting Matrix C = AB will be i×k.

func (*Dense) MulT

func (d *Dense) MulT(other Matrix) Matrix

MulT performs the matrix multiplication row by column. ATB = C, where AT is the transpose of B if A is an r x c Matrix, and B is j x k, r = j the resulting Matrix C will be c x k

func (*Dense) Norm

func (d *Dense) Norm(pow Float) Float

Norm returns the vector's norm. Use pow = 2.0 to compute the Euclidean norm.

func (*Dense) Normalize2

func (d *Dense) Normalize2() *Dense

Normalize2 normalizes an array with the Euclidean norm.

func (*Dense) OnesLike

func (d *Dense) OnesLike() Matrix

OnesLike returns a new Dense matrix with the same dimensions of the receiver, initialized with ones.

func (*Dense) Pivoting

func (d *Dense) Pivoting(row int) (Matrix, bool, []int)

Pivoting returns the partial pivots of a square matrix to reorder rows. Considerate square sub-matrix from element (offset, offset).

func (*Dense) Pow

func (d *Dense) Pow(power Float) Matrix

Pow returns a new matrix, applying the power function with given exponent to all elements of the matrix.

func (*Dense) Prod

func (d *Dense) Prod(other Matrix) Matrix

Prod performs the element-wise product between the receiver and the other matrix.

func (*Dense) ProdInPlace

func (d *Dense) ProdInPlace(other Matrix) Matrix

ProdInPlace performs the in-place element-wise product with the other matrix.

func (*Dense) ProdMatrixScalarInPlace

func (d *Dense) ProdMatrixScalarInPlace(m Matrix, n Float) Matrix

ProdMatrixScalarInPlace multiplies the given matrix with the value, storing the result in the receiver.

func (*Dense) ProdScalar

func (d *Dense) ProdScalar(n Float) Matrix

ProdScalar returns the multiplication between the matrix and the given value.

func (*Dense) ProdScalarInPlace

func (d *Dense) ProdScalarInPlace(n Float) Matrix

ProdScalarInPlace performs the in-place multiplication between the matrix and the given value.

func (*Dense) Range

func (d *Dense) Range(start, end int) Matrix

Range extracts data from the the Matrix from elements start (inclusive) and end (exclusive).

func (*Dense) Reshape

func (d *Dense) Reshape(r, c int) Matrix

Reshape returns a copy of the matrix. It panics if the dimensions are incompatible.

func (*Dense) Rows

func (d *Dense) Rows() int

Rows returns the number of rows of the matrix.

func (*Dense) Scalar

func (d *Dense) Scalar() Float

Scalar returns the scalar value. It panics if the matrix does not contain exactly one element.

func (*Dense) Set

func (d *Dense) Set(i int, j int, v Float)

Set sets the value v at row i and column j. It panics if the given indices are out of range.

func (*Dense) SetData

func (d *Dense) SetData(data []Float)

SetData sets the values of the matrix, given a raw one-dimensional slice data representation.

func (*Dense) SetVec

func (d *Dense) SetVec(i int, v Float)

SetVec sets the value v at position i of a vector. It panics if the receiver is not a vector.

func (*Dense) Size

func (d *Dense) Size() int

Size returns the size of the matrix (rows × columns).

func (*Dense) SplitV

func (d *Dense) SplitV(sizes ...int) []Matrix

SplitV extract N vectors from the matrix d. N[i] has size sizes[i].

func (*Dense) Sqrt

func (d *Dense) Sqrt() Matrix

Sqrt returns a new matrix applying the square root function to all elements.

func (*Dense) String

func (d *Dense) String() string

String returns a string representation of the matrix data.

func (*Dense) Sub

func (d *Dense) Sub(other Matrix) Matrix

Sub returns the subtraction of the other matrix from the receiver.

func (*Dense) SubInPlace

func (d *Dense) SubInPlace(other Matrix) Matrix

SubInPlace performs the in-place subtraction with the other matrix.

func (*Dense) SubScalar

func (d *Dense) SubScalar(n Float) Matrix

SubScalar performs a subtraction between the matrix and the given value.

func (*Dense) SubScalarInPlace

func (d *Dense) SubScalarInPlace(n Float) Matrix

SubScalarInPlace subtracts the scalar from the receiver's values.

func (*Dense) Sum

func (d *Dense) Sum() Float

Sum returns the sum of all values of the matrix.

func (Dense) SwapInPlace

func (d Dense) SwapInPlace(r1, r2 int)

SwapInPlace swaps two rows of the matrix in place

func (*Dense) T

func (d *Dense) T() Matrix

T returns the transpose of the matrix.

func (*Dense) UnmarshalBinary added in v0.4.0

func (d *Dense) UnmarshalBinary(data []byte) error

UnmarshalBinary unmarshals a binary representation of a Dense matrix.

func (*Dense) View

func (d *Dense) View(rows, cols int) *Dense

View returns a new Matrix sharing the same underlying data.

func (*Dense) Zeros

func (d *Dense) Zeros()

Zeros sets all the values of the matrix to zero.

func (*Dense) ZerosLike

func (d *Dense) ZerosLike() Matrix

ZerosLike returns a new Dense matrix with the same dimensions of the receiver, initialized with zeroes.

type Float

type Float = float32

Float is the main float type for the mat32 package. It is an alias for float32.

const (
	// SmallestNonzeroFloat corresponds to math.SmallestNonzeroFloat32.
	SmallestNonzeroFloat Float = math.SmallestNonzeroFloat32
	// Pi mathematical constant.
	Pi Float = math.Pi
)

func Abs

func Abs(x Float) Float

Abs returns the absolute value of x.

func Ceil

func Ceil(x Float) Float

Ceil returns the least integer value greater than or equal to x.

func Cos

func Cos(x Float) Float

Cos returns the cosine of the radian argument x.

func Cosh

func Cosh(x Float) Float

Cosh returns the hyperbolic cosine of x.

func Cosine

func Cosine(x, y Matrix) Float

Cosine returns the cosine similarity between two not normalized vectors.

func Exp

func Exp(x Float) Float

Exp returns e**x, the base-e exponential of x.

func Floor

func Floor(x Float) Float

Floor returns the greatest integer value less than or equal to x.

func Inf

func Inf(sign int) Float

Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

func Log

func Log(x Float) Float

Log returns the natural logarithm of x.

func Max

func Max(x, y Float) Float

Max returns the larger of x or y.

func NaN

func NaN() Float

NaN returns an IEEE 754 “not-a-number” value.

func Pow

func Pow(x, y Float) Float

Pow returns x**y, the base-x exponential of y.

func Round

func Round(x Float) Float

Round returns the nearest integer, rounding half away from zero.

func Sin

func Sin(x Float) Float

Sin returns the sine of the radian argument x.

func Sinh

func Sinh(x Float) Float

Sinh returns the hyperbolic sine of x.

func Sqrt

func Sqrt(x Float) Float

Sqrt returns the square root of x.

func Tan

func Tan(x Float) Float

Tan returns the tangent of the radian argument x.

func Tanh

func Tanh(x Float) Float

Tanh returns the hyperbolic tangent of x.

type Matrix

type Matrix interface {
	// ZerosLike returns a new matrix with the same dimensions of the receiver,
	// initialized with zeroes.
	ZerosLike() Matrix
	// OnesLike returns a new matrix with the same dimensions of the receiver,
	// initialized with ones.
	OnesLike() Matrix
	// Clone returns a new matrix, copying all its values from the receiver.
	Clone() Matrix
	// Copy copies the data from the other matrix to the receiver.
	Copy(other Matrix)
	// Zeros sets all the values of the matrix to zero.
	Zeros()
	// Dims returns the number of rows and columns of the matrix.
	Dims() (r, c int)
	// Rows returns the number of rows of the matrix.
	Rows() int
	// Columns returns the number of columns of the matrix.
	Columns() int
	// Size returns the size of the matrix (rows × columns).
	Size() int
	// LastIndex returns the last element's index, in respect of linear indexing.
	// It returns -1 if the matrix is empty.
	LastIndex() int
	// Data returns the underlying data of the matrix, as a raw one-dimensional slice of values.
	Data() []Float
	// IsVector returns whether the matrix is either a row or column vector.
	IsVector() bool
	// IsScalar returns whether the matrix contains exactly one scalar value.
	IsScalar() bool
	// Scalar returns the scalar value.
	// It panics if the matrix does not contain exactly one element.
	Scalar() Float
	// Set sets the value v at row i and column j.
	Set(i int, j int, v Float)
	// At returns the value at row i and column j.
	At(i int, j int) Float
	// SetVec sets the value v at position i of a vector.
	// It panics if the receiver is not a vector.
	SetVec(i int, v Float)
	// AtVec returns the value at position i of a vector.
	// It panics if the receiver is not a vector.
	AtVec(i int) Float
	// T returns the transpose of the matrix.
	T() Matrix
	// Reshape returns a copy of the matrix.
	// It panics if the dimensions are incompatible.
	Reshape(r, c int) Matrix
	// Apply executes the unary function fn.
	Apply(fn func(i, j int, v Float) Float, a Matrix)
	// ApplyWithAlpha executes the unary function fn, taking additional parameters alpha.
	ApplyWithAlpha(fn func(i, j int, v Float, alpha ...Float) Float, a Matrix, alpha ...Float)
	// AddScalar performs the addition between the matrix and the given value.
	AddScalar(n Float) Matrix
	// AddScalarInPlace adds the scalar to all values of the matrix.
	AddScalarInPlace(n Float) Matrix
	// SubScalar performs a subtraction between the matrix and the given value.
	SubScalar(n Float) Matrix
	// SubScalarInPlace subtracts the scalar from the receiver's values.
	SubScalarInPlace(n Float) Matrix
	// ProdScalar returns the multiplication between the matrix and the given value.
	ProdScalar(n Float) Matrix
	// ProdScalarInPlace performs the in-place multiplication between the matrix and
	// the given value.
	ProdScalarInPlace(n Float) Matrix
	// ProdMatrixScalarInPlace multiplies the given matrix with the value, storing the
	// result in the receiver.
	ProdMatrixScalarInPlace(m Matrix, n Float) Matrix
	// Add returns the addition between the receiver and another matrix.
	Add(other Matrix) Matrix
	// AddInPlace performs the in-place addition with the other matrix.
	AddInPlace(other Matrix) Matrix
	// Sub returns the subtraction of the other matrix from the receiver.
	Sub(other Matrix) Matrix
	// SubInPlace performs the in-place subtraction with the other matrix.
	SubInPlace(other Matrix) Matrix
	// Prod performs the element-wise product between the receiver and the other matrix.
	Prod(other Matrix) Matrix
	// ProdInPlace performs the in-place element-wise product with the other matrix.
	ProdInPlace(other Matrix) Matrix
	// Div returns the result of the element-wise division of the receiver by the other matrix.
	Div(other Matrix) Matrix
	// DivInPlace performs the in-place element-wise division of the receiver by the other matrix.
	DivInPlace(other Matrix) Matrix
	// Mul performs the multiplication row by column.
	// If A is an i×j Matrix, and B is j×k, then the resulting Matrix C = AB will be i×k.
	Mul(other Matrix) Matrix
	// DotUnitary returns the dot product of two vectors.
	DotUnitary(other Matrix) Float
	// Pow returns a new matrix, applying the power function with given exponent to all elements
	// of the matrix.
	Pow(power Float) Matrix
	// Norm returns the vector's norm. Use pow = 2.0 to compute the Euclidean norm.
	Norm(pow Float) Float
	// Sqrt returns a new matrix applying the square root function to all elements.
	Sqrt() Matrix
	// ClipInPlace clips in place each value of the matrix.
	ClipInPlace(min, max Float) Matrix
	// Abs returns a new matrix applying the absolute value function to all elements.
	Abs() Matrix
	// Sum returns the sum of all values of the matrix.
	Sum() Float
	// Max returns the maximum value of the matrix.
	Max() Float
	// Min returns the minimum value of the matrix.
	Min() Float
	// String returns a string representation of the matrix data.
	String() string
	// SetData sets the values of the matrix, given a raw one-dimensional slice
	// data representation.
	SetData(data []Float)
}

The Matrix interface defines set and get methods to access its elements plus a few variants to perform linear algebra operations with other matrices, such as element-wise addition, subtraction, product and matrix-matrix multiplication.

func ConcatV

func ConcatV(vs ...Matrix) Matrix

ConcatV returns a new Matrix created concatenating the input matrices vertically.

func SqrtMatrix

func SqrtMatrix(m Matrix) Matrix

SqrtMatrix returns a new matrix filled with the sqrt of the values of the input matrix.

func UnmarshalBinaryMatrix added in v0.4.0

func UnmarshalBinaryMatrix(r io.Reader) (Matrix, error)

UnmarshalBinaryMatrix decodes a Matrix from binary form.

type Sparse

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

Sparse is the implementation of a sparse matrix that uses Float as data type.

func NewEmptySparse

func NewEmptySparse(rows, cols int) *Sparse

NewEmptySparse returns a new rows x cols Sparse matrix.

func NewEmptyVecSparse

func NewEmptyVecSparse(size int) *Sparse

NewEmptyVecSparse returns a new sparse vector of the given size.

func NewSparse

func NewSparse(rows, cols int, elements []Float) *Sparse

NewSparse returns a new rows x cols sparse matrix populated with a copy of the non-zero elements. The elements cannot be nil, panic otherwise. Use NewEmptySparse to initialize an empty matrix.

func NewSparseFromMap

func NewSparseFromMap(rows, cols int, elements map[Coordinate]Float) *Sparse

NewSparseFromMap creates a new Sparse matrix from a raw map of values.

func NewVecSparse

func NewVecSparse(elements []Float) *Sparse

NewVecSparse returns a new column sparse vector populated with the non-zero elements. The elements cannot be nil, panic otherwise. Use NewEmptyVecSparse to initialize an empty matrix.

func OneHotSparse

func OneHotSparse(size int, oneAt int) *Sparse

OneHotSparse creates a new one-hot Sparse vector. It panics if oneAt is an invalid index.

func (*Sparse) Abs

func (s *Sparse) Abs() Matrix

Abs returns a new matrix applying the absolute value function to all elements.

func (*Sparse) Add

func (s *Sparse) Add(other Matrix) Matrix

Add returns the addition between the receiver and another matrix. It returns the same type of matrix of other, that is, a Dense matrix if other is Dense, or a Sparse matrix otherwise.

func (*Sparse) AddInPlace

func (s *Sparse) AddInPlace(other Matrix) Matrix

AddInPlace performs the in-place addition with the other matrix. It panics if other is not a Sparse matrix.

func (*Sparse) AddScalar

func (s *Sparse) AddScalar(n Float) Matrix

AddScalar performs the addition between the matrix and the given value, returning a new Dense matrix.

func (*Sparse) AddScalarInPlace

func (s *Sparse) AddScalarInPlace(n Float) Matrix

AddScalarInPlace is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) Apply

func (s *Sparse) Apply(fn func(i, j int, v Float) Float, a Matrix)

Apply executes the unary function fn. Important: apply to Functions such that f(0) = 0 (i.e. Sin, Tan)

func (*Sparse) ApplyWithAlpha

func (s *Sparse) ApplyWithAlpha(fn func(i, j int, v Float, alpha ...Float) Float, a Matrix, alpha ...Float)

ApplyWithAlpha is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) At

func (s *Sparse) At(i int, j int) Float

At returns the value at row i and column j. It panics if the given indices are out of range.

func (*Sparse) AtVec

func (s *Sparse) AtVec(i int) Float

AtVec returns the value at position i of a vector. It panics if the receiver is not a vector.

func (*Sparse) ClipInPlace

func (s *Sparse) ClipInPlace(min, max Float) Matrix

ClipInPlace clips in place each value of the matrix.

func (*Sparse) Clone

func (s *Sparse) Clone() Matrix

Clone returns a new Sparse matrix, copying all its values from the receiver.

func (*Sparse) Columns

func (s *Sparse) Columns() int

Columns returns the number of columns of the matrix.

func (*Sparse) Copy

func (s *Sparse) Copy(other Matrix)

Copy copies the data from the other matrix to the receiver. It panics if the matrices have different dimensions, or if the other matrix is not Sparse.

func (*Sparse) Data

func (s *Sparse) Data() []Float

Data returns the underlying data of the matrix, as a raw one-dimensional slice of values.

func (*Sparse) Dims

func (s *Sparse) Dims() (r, c int)

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

func (*Sparse) Div

func (s *Sparse) Div(other Matrix) Matrix

Div returns the result of the element-wise division of the receiver by the other matrix, returning a new Sparse matrix. It panics if other is a Sparse matrix.

func (*Sparse) DivInPlace

func (s *Sparse) DivInPlace(other Matrix) Matrix

DivInPlace is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) DoNonZero

func (s *Sparse) DoNonZero(fn func(i, j int, v Float))

DoNonZero calls a function for each non-zero element of the matrix. The parameters of the function are the element indices and its value.

func (*Sparse) DotUnitary

func (s *Sparse) DotUnitary(other Matrix) Float

DotUnitary returns the dot product of two vectors.

func (*Sparse) IsScalar

func (s *Sparse) IsScalar() bool

IsScalar returns whether the matrix contains exactly one scalar value.

func (*Sparse) IsVector

func (s *Sparse) IsVector() bool

IsVector returns whether the matrix is either a row or column vector.

func (*Sparse) LastIndex

func (s *Sparse) LastIndex() int

LastIndex returns the last element's index, in respect of linear indexing. It returns -1 if the matrix is empty.

func (Sparse) MarshalBinary added in v0.4.0

func (s Sparse) MarshalBinary() ([]byte, error)

MarshalBinary marshals a Sparse matrix into binary form.

func (*Sparse) Max

func (s *Sparse) Max() Float

Max returns the maximum value of the matrix.

func (*Sparse) Maximum

func (s *Sparse) Maximum(other Matrix) Matrix

Maximum returns a new Sparse matrix initialized with the element-wise maximum value between the receiver and the other matrix.

func (*Sparse) Min

func (s *Sparse) Min() Float

Min returns the minimum value of the matrix.

func (*Sparse) Minimum

func (s *Sparse) Minimum(other Matrix) Matrix

Minimum returns a new Sparse matrix initialized with the element-wise minimum value between the receiver and the other matrix.

func (*Sparse) Mul

func (s *Sparse) Mul(other Matrix) Matrix

Mul performs the multiplication row by column, returning a Dense matrix. If A is an i×j Matrix, and B is j×k, then the resulting Matrix C = AB will be i×k.

func (*Sparse) Norm

func (s *Sparse) Norm(pow Float) Float

Norm returns the vector's norm. Use pow = 2.0 to compute the Euclidean norm.

func (*Sparse) OnesLike

func (s *Sparse) OnesLike() Matrix

OnesLike is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) Pow

func (s *Sparse) Pow(power Float) Matrix

Pow returns a new matrix, applying the power function with given exponent to all elements of the matrix.

func (*Sparse) Prod

func (s *Sparse) Prod(other Matrix) Matrix

Prod performs the element-wise product between the receiver and the other matrix, returning a new Sparse matrix.

func (*Sparse) ProdInPlace

func (s *Sparse) ProdInPlace(other Matrix) Matrix

ProdInPlace performs the in-place element-wise product with the other matrix. It panics if other is not a Sparse matrix.

func (*Sparse) ProdMatrixScalarInPlace

func (s *Sparse) ProdMatrixScalarInPlace(m Matrix, n Float) Matrix

ProdMatrixScalarInPlace multiplies the given matrix with the value, storing the result in the receiver, and returning the same receiver Sparse matrix.

func (*Sparse) ProdScalar

func (s *Sparse) ProdScalar(n Float) Matrix

ProdScalar returns the multiplication between the matrix and the given value, returning a new Sparse matrix.

func (*Sparse) ProdScalarInPlace

func (s *Sparse) ProdScalarInPlace(n Float) Matrix

ProdScalarInPlace performs the in-place multiplication between the matrix and the given value, returning the same receiver Sparse matrix.

func (*Sparse) Reshape

func (s *Sparse) Reshape(r, c int) Matrix

Reshape is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) Rows

func (s *Sparse) Rows() int

Rows returns the number of rows of the matrix.

func (*Sparse) Scalar

func (s *Sparse) Scalar() Float

Scalar returns the scalar value. It panics if the matrix does not contain exactly one element.

func (*Sparse) Set

func (s *Sparse) Set(i int, j int, v Float)

Set sets the value v at row i and column j. It panics if the given indices are out of range.

func (*Sparse) SetData

func (s *Sparse) SetData(data []Float)

SetData is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) SetVec

func (s *Sparse) SetVec(i int, v Float)

SetVec is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) Size

func (s *Sparse) Size() int

Size returns the size of the matrix (rows × columns).

func (*Sparse) Sparsity

func (s *Sparse) Sparsity() Float

Sparsity returns the sparsity of the Sparse matrix.

func (*Sparse) Sqrt

func (s *Sparse) Sqrt() Matrix

Sqrt returns a new matrix applying the square root function to all elements.

func (*Sparse) String

func (s *Sparse) String() string

String returns a string representation of the matrix data.

func (*Sparse) Sub

func (s *Sparse) Sub(other Matrix) Matrix

Sub returns the subtraction of the other matrix from the receiver. It returns a Dense matrix if other is Dense, or a Sparse matrix otherwise.

func (*Sparse) SubInPlace

func (s *Sparse) SubInPlace(other Matrix) Matrix

SubInPlace performs the in-place subtraction with the other matrix. It panics if other is not a Sparse matrix.

func (*Sparse) SubScalar

func (s *Sparse) SubScalar(n Float) Matrix

SubScalar performs a subtraction between the matrix and the given value, returning a new Dense matrix.

func (*Sparse) SubScalarInPlace

func (s *Sparse) SubScalarInPlace(n Float) Matrix

SubScalarInPlace is currently not implemented for a Sparse matrix (it always panics).

func (*Sparse) Sum

func (s *Sparse) Sum() Float

Sum returns the sum of all values of the matrix.

func (*Sparse) T

func (s *Sparse) T() Matrix

T returns the transpose of the matrix.

func (*Sparse) ToDense

func (s *Sparse) ToDense() *Dense

ToDense transforms a Sparse matrix into a new Dense matrix.

func (*Sparse) UnmarshalBinary added in v0.4.0

func (s *Sparse) UnmarshalBinary(data []byte) error

UnmarshalBinary unmarshals a binary representation of a Sparse matrix.

func (*Sparse) Zeros

func (s *Sparse) Zeros()

Zeros sets all the values of the matrix to zero.

func (*Sparse) ZerosLike

func (s *Sparse) ZerosLike() Matrix

ZerosLike returns a new Sparse matrix with the same dimensions of the receiver, initialized with zeroes.

Directories

Path Synopsis
asm/f32
Package f32 provides float32 vector primitives.
Package f32 provides float32 vector primitives.

Jump to

Keyboard shortcuts

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