mat64

package
v1.2.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package mat64 provides basic linear algebra operations for float64 matrices.

Note that in all interfaces that assign the result to the receiver, the receiver must be either the correct dimensions for the result or a zero-sized matrix. In the latter case, matrix data is allocated and stored in the receiver. If the matrix dimensions do not match the result, the method must panic.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexOutOfRange = Error{"mat64: index out of range"}
	ErrRowAccess       = Error{"mat64: row index out of range"}
	ErrColAccess       = Error{"mat64: column index out of range"}
	ErrVectorAccess    = Error{"mat64: vector index out of range"}
	ErrZeroLength      = Error{"mat64: zero length in matrix definition"}
	ErrRowLength       = Error{"mat64: row length mismatch"}
	ErrColLength       = Error{"mat64: col length mismatch"}
	ErrSquare          = Error{"mat64: expect square matrix"}
	ErrNormOrder       = Error{"mat64: invalid norm order for matrix"}
	ErrSingular        = Error{"mat64: matrix is singular"}
	ErrShape           = Error{"mat64: dimension mismatch"}
	ErrIllegalStride   = Error{"mat64: illegal stride"}
	ErrPivot           = Error{"mat64: malformed pivot list"}
	ErrTriangle        = Error{"mat64: triangular storage mismatch"}
)

Functions

func Det

func Det(a Matrix) float64

Det returns the determinant of the matrix a.

func Format

func Format(m Matrix, margin int, dot byte, fs fmt.State, c rune)

Format prints a pretty representation of m to the fs io.Writer. The format character c specifies the numerical representation of of elements; valid values are those for float64 specified in the fmt package, with their associated flags. In addition to this, a '#' for all valid verbs except 'v' indicates that zero values be represented by the dot character. The '#' associated with the 'v' verb formats the matrix with Go syntax representation. The printed range of the matrix can be limited by specifying a positive value for margin; If margin is greater than zero, only the first and last margin rows/columns of the matrix are output.

func Inner

func Inner(x *Vector, A Matrix, y *Vector) float64

Inner computes the generalized inner product

x^T A y

between vectors x and y with matrix A. This is only a true inner product if A is symmetric positive definite, though the operation works for any matrix A.

Inner panics if x.Len != m or y.Len != n when A is an m x n matrix.

func Maybe

func Maybe(fn Panicker) (err error)

Maybe will recover a panic with a type mat64.Error from fn, and return this error. Any other error is re-panicked.

func MaybeFloat

func MaybeFloat(fn FloatPanicker) (f float64, err error)

MaybeFloat will recover a panic with a type mat64.Error from fn, and return this error. Any other error is re-panicked.

Types

type Adder

type Adder interface {
	Add(a, b Matrix)
}

An Adder can add the matrices represented by a and b, placing the result in the receiver. Add will panic if the two matrices do not have the same shape.

type ApplyFunc

type ApplyFunc func(r, c int, v float64) float64

An ApplyFunc takes a row/column index and element value and returns some function of that tuple.

type Applyer

type Applyer interface {
	Apply(f ApplyFunc, a Matrix)
}

An Applyer can apply an Applyfunc f to each of the elements of the matrix represented by a, placing the resulting matrix in the receiver.

type ApproxEqualer

type ApproxEqualer interface {
	EqualsApprox(b Matrix, epsilon float64) bool
}

An ApproxEqualer can compare the matrices represented by b and the receiver, with tolerance for element-wise equailty specified by epsilon. Matrices with non-equal shapes are not equal.

type Augmenter

type Augmenter interface {
	Augment(a, b Matrix)
}

An Augmenter can create the augmented matrix of a with b, where b is placed in the greater indexed columns. The result of augmentation is placed in the receiver, overwriting the previous value of the receiver. Augment will panic if the two input matrices do not have the same number of rows.

type BandWidther

type BandWidther interface {
	BandWidth() (k1, k2 int)
}

A BandWidther represents a banded matrix and can return the left and right half-bandwidths, k1 and k2.

type Cloner

type Cloner interface {
	Clone(a Matrix)
}

A Cloner can make a copy of a into the receiver, overwriting the previous value of the receiver. The clone operation does not make any restriction on shape.

type ColViewer

type ColViewer interface {
	ColView(c int) *Vector
}

A ColViewer can return a Vector reflecting a row that is backed by the matrix data. The Vector returned will have Len() == nRows.

type Copier

type Copier interface {
	Copy(a Matrix) (r, c int)
}

A Copier can make a copy of elements of a into the receiver. The submatrix copied starts at row and column 0 and has dimensions equal to the minimum dimensions of the two matrices. The number of row and columns copied is returned.

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 Inverse

func Inverse(a Matrix) (*Dense, error)

Inverse returns the inverse or pseudoinverse of the matrix a. It returns a nil matrix and ErrSingular if a is singular.

func NewDense

func NewDense(r, c int, mat []float64) *Dense

NewDense creates a new matrix of type Dense with dimensions r and c. If the mat argument is nil, a new data slice is allocated.

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

func Solve

func Solve(a, b Matrix) (x *Dense, err error)

Solve returns a matrix x that satisfies ax = b. It returns a nil matrix and ErrSingular if a is singular.

func (*Dense) Add

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

Add adds a and b element-wise, placing the result in the receiver.

See the Adder interface for more information.

func (*Dense) Apply

func (m *Dense) Apply(f ApplyFunc, a Matrix)

Apply applies the function f to each of the elements of a, placing the resulting matrix in the receiver.

See the Applyer interface for more information.

func (*Dense) At

func (m *Dense) At(r, c int) float64

At returns the element at row r, column c.

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.

See the Augmenter interface for more information.

func (*Dense) Caps

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

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

func (*Dense) Clone

func (m *Dense) Clone(a Matrix)

Clone makes a copy of a into the receiver, overwriting the previous value of the receiver. The clone operation does not make any restriction on shape.

See the Cloner interface for more information.

func (*Dense) Col

func (m *Dense) Col(dst []float64, j int) []float64

Col copies the elements in the jth column of the matrix into the slice dst. If the provided slice is nil, a new slice is first allocated.

See the Vectorer interface for more information.

func (*Dense) ColView

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

ColView returns a Vector reflecting col 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.

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.

See the ElemDiver interface for more information.

func (*Dense) Dot

func (m *Dense) Dot(b Matrix) float64

Dot returns the sum of the element-wise products of the elements of the receiver and b.

See the Dotter interface for more information.

func (*Dense) Equals

func (m *Dense) Equals(b Matrix) bool

Equals returns true if b and the receiver have the same size and contain all equal elements.

See the Equaler interface for more information.

func (*Dense) EqualsApprox

func (m *Dense) EqualsApprox(b Matrix, epsilon float64) bool

EqualsApprox compares the matrices represented by b and the receiver, with tolerance for element-wise equality specified by epsilon.

See the ApproxEqualer interface for more information.

func (*Dense) Exp

func (m *Dense) Exp(a Matrix)

Exp calculates the exponential of the matrix a, e^a, placing the result in the receiver.

See the Exper interface for more information.

Exp uses the scaling and squaring method described in section 3 of http://www.cs.cornell.edu/cv/researchpdf/19ways+.pdf.

func (*Dense) Grow

func (m *Dense) Grow(r, c int) Matrix

Grow returns an expanded copy of the receiver. The copy is expanded by r rows and c columns. If the dimensions of the new copy are outside the caps of the receiver a new allocation is made, otherwise not.

func (*Dense) L

func (m *Dense) L(a Matrix)

L places the lower triangular matrix of a in the receiver.

See the Ler interface for more information.

func (Dense) MarshalBinary

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

MarshalBinary encodes the receiver into a binary form and returns the result.

Dense is little-endian encoded as follows:

 0 -  8  number of rows    (int64)
 8 - 16  number of columns (int64)
16 - ..  matrix data elements (float64)
         [0,0] [0,1] ... [0,ncols-1]
         [1,0] [1,1] ... [1,ncols-1]
         ...
         [nrows-1,0] ... [nrows-1,ncols-1]

func (*Dense) Max

func (m *Dense) Max() float64

Max returns the largest element value of the receiver.

func (*Dense) Min

func (m *Dense) Min() float64

Min returns the smallest element value of the receiver.

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.

See the Muler interface for more information.

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.

See the ElemMuler interface for more information.

func (*Dense) MulTrans

func (m *Dense) MulTrans(a Matrix, aTrans bool, b Matrix, bTrans bool)

MulTrans takes the matrix product of a and b, optionally transposing each, and placing the result in the receiver.

See the MulTranser interface for more information.

func (*Dense) Norm

func (m *Dense) Norm(ord float64) float64

Norm returns the specified matrix p-norm of the receiver.

See the Normer interface for more information.

func (*Dense) Outer

func (m *Dense) Outer(x, y *Vector)

Outer calculates the outer product of x and y, and stores the result in the receiver. In order to update to an existing matrix, see RankOne.

m = x * y'

func (*Dense) Pow

func (m *Dense) Pow(a Matrix, n int)

Pow calculates the integral power of the matrix a to n, placing the result in the receiver.

See the Power interface for more information.

func (*Dense) RankOne

func (m *Dense) RankOne(a Matrix, alpha float64, x, y *Vector)

RankOne performs a rank-one update to the matrix a and stores the result in the receiver. If a is zero, see Outer.

m = a + alpha * x * y'

func (*Dense) RawMatrix

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

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

func (*Dense) RawRowView

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

RawRowView returns a slice backed by the same array as backing the receiver.

func (*Dense) Reset

func (m *Dense) Reset()

Reset zeros the dimensions of the matrix so that it can be reused as the receiver of a dimensionally restricted operation.

See the Reseter interface for more information.

func (*Dense) Row

func (m *Dense) Row(dst []float64, i int) []float64

Row copies the elements in the ith row of the matrix into the slice dst. If the provided slice is nil, a new slice is first allocated.

See the Vectorer interface for more information.

func (*Dense) RowView

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

RowView returns a Vector reflecting row i, backed by the matrix data.

See RowViewer for more information.

func (*Dense) Scale

func (m *Dense) Scale(f float64, 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(r, c int, v float64)

Set sets the element at row r, column c to the value v.

func (*Dense) SetCol

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

SetCol sets the elements of the matrix in the specified column to the values of src.

See the VectorSetter interface for more information.

func (*Dense) SetRawMatrix

func (m *Dense) SetRawMatrix(b blas64.General)

SetRawMatrix sets the underlying blas64.General used by the receiver. Changes to elements in the receiver following the call will be reflected in b.

func (*Dense) SetRow

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

SetRow sets the elements of the matrix in the specified row to the values of src.

See the VectorSetter interface for more information.

func (*Dense) SolveCholesky

func (m *Dense) SolveCholesky(t Triangular, b Matrix)

SolveCholesky finds the matrix m that solves A * m = b where A = L * L^T or A = U^T * U, and U or L are represented by t, placing the result in the receiver.

func (*Dense) SolveTri

func (m *Dense) SolveTri(a Triangular, trans bool, b Matrix)

SolveTri finds the matrix x that solves op(A) * X = B where A is a triangular matrix and op is specified by trans.

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.

See the Stacker interface for more information.

func (*Dense) Sub

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

Sub subtracts the matrix b from a, placing the result in the receiver.

See the Suber interface for more information.

func (*Dense) Sum

func (m *Dense) Sum() float64

Sum returns the sum of the elements of the matrix.

See the Sumer interface for more information.

func (*Dense) TCopy

func (m *Dense) TCopy(a Matrix)

TCopy makes a copy of the transpose the matrix represented by a, placing the result into the receiver.

See the TransposeCopier interface for more information.

func (*Dense) Trace

func (m *Dense) Trace() float64

Trace returns the trace of the matrix.

See the Tracer interface for more information.

func (*Dense) U

func (m *Dense) U(a Matrix)

U places the upper triangular matrix of a in the receiver.

See the Uer interface for more information.

func (*Dense) UnmarshalBinary

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

UnmarshalBinary decodes the binary form into the receiver. It panics if the receiver is a non-zero Dense matrix.

See MarshalBinary for the on-disk layout.

func (*Dense) View

func (m *Dense) View(i, j, r, c int) Matrix

View returns a new Matrix that shares backing data with the receiver. The new matrix is located from row i, column j extending r rows and c columns.

type Deter

type Deter interface {
	Det() float64
}

A Deter can return the determinant of the represented matrix.

type Dotter

type Dotter interface {
	Dot(b Matrix) float64
}

A Dotter can determine the sum of the element-wise products of the elements of the receiver and b. If the shapes of the two matrices differ, Dot will panic.

type EigenFactors

type EigenFactors struct {
	V *Dense
	// contains filtered or unexported fields
}

func Eigen

func Eigen(a *Dense, epsilon float64) EigenFactors

Eigen returns the Eigenvalues and eigenvectors of a square real matrix. The matrix a is overwritten during the decomposition. If a is symmetric, then a = v*D*v' where the eigenvalue matrix D is diagonal and the eigenvector matrix v is orthogonal.

If a is not symmetric, then the eigenvalue matrix D is block diagonal with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues, lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The columns of v represent the eigenvectors in the sense that a*v = v*D, i.e. a.v equals v.D. The matrix v may be badly conditioned, or even singular, so the validity of the equation a = v*D*inverse(v) depends upon the 2-norm condition number of v.

func (EigenFactors) D

func (f EigenFactors) D() *Dense

D returns the block diagonal eigenvalue matrix from the real and imaginary components d and e.

type ElemDiver

type ElemDiver interface {
	DivElem(a, b Matrix)
}

An ElemDiver can perform element-wise division a / b of the matrices represented by a and b, placing the result in the receiver. DivElem will panic if the two matrices do not have the same shape.

type ElemMuler

type ElemMuler interface {
	MulElem(a, b Matrix)
}

An ElemMuler can perform element-wise multiplication of the matrices represented by a and b, placing the result in the receiver. MulEmen will panic if the two matrices do not have the same shape.

type Equaler

type Equaler interface {
	Equals(b Matrix) bool
}

An Equaler can compare the matrices represented by b and the receiver. Matrices with non-equal shapes are not equal.

type Error

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

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

func (Error) Error

func (err Error) Error() string

type Exper

type Exper interface {
	Exp(a Matrix)
}

An Exper can perform a matrix exponentiation of the square matrix a. Exp will panic with ErrShape if a is not square.

type FloatPanicker

type FloatPanicker func() float64

A FloatPanicker is a function that returns a float64 and may panic.

type Grower

type Grower interface {
	Caps() (r, c int)
	Grow(r, c int) Matrix
}

A Grower can grow the size of the represented matrix by the given number of rows and columns. Growing beyond the size given by the Caps method will result in the allocation of a new matrix and copying of the elements. If Grow is called with negative increments it will panic with ErrIndexOutOfRange.

type Inver

type Inver interface {
	Inv(a Matrix) error
}

An Inver can calculate the inverse of the matrix represented by a and stored in the receiver. ErrSingular is returned if there is no inverse of the matrix.

type LQFactor

type LQFactor struct {
	LQ *Dense
	// contains filtered or unexported fields
}

func LQ

func LQ(a *Dense) LQFactor

LQ computes an LQ Decomposition for an m-by-n matrix a with m <= n by Householder reflections. The LQ decomposition is an m-by-n orthogonal matrix q and an m-by-m lower triangular matrix l so that a = l.q. LQ will panic with ErrShape if m > n.

The LQ decomposition always exists, even if the matrix does not have full rank, so LQ will never fail unless m > n. The primary use of the LQ decomposition is in the least squares solution of non-square systems of simultaneous linear equations. This will fail if LQIsFullRank() returns false. The matrix a is overwritten by the decomposition.

func (LQFactor) IsFullRank

func (f LQFactor) IsFullRank() bool

IsFullRank returns whether the L matrix and hence a has full rank.

func (LQFactor) L

func (f LQFactor) L() *Dense

L returns the lower triangular factor for the LQ decomposition.

func (LQFactor) Solve

func (f LQFactor) Solve(b *Dense) (x *Dense)

Solve computes minimum norm least squares solution of a.x = b where b has as many rows as a. A matrix x is returned that minimizes the two norm of Q*R*X-B. Solve will panic if a is not full rank.

type LUFactors

type LUFactors struct {
	LU    *Dense
	Pivot []int
	Sign  int
}

func LU

func LU(a *Dense) LUFactors

LU performs an LU Decomposition for an m-by-n matrix a.

If m >= n, the LU decomposition is an m-by-n unit lower triangular matrix L, an n-by-n upper triangular matrix U, and a permutation vector piv of length m so that A(piv,:) = L*U.

If m < n, then L is m-by-m and U is m-by-n.

The LU decompostion with pivoting always exists, even if the matrix is singular, so LU will never fail. The primary use of the LU decomposition is in the solution of square systems of simultaneous linear equations. This will fail if IsSingular() returns true.

func (LUFactors) Det

func (f LUFactors) Det() float64

Det returns the determinant of matrix a decomposed into lu. The matrix a must have been square.

func (LUFactors) IsSingular

func (f LUFactors) IsSingular() bool

IsSingular returns whether the the upper triangular factor and hence a is singular.

func (LUFactors) L

func (f LUFactors) L() *Dense

L returns the lower triangular factor of the LU decomposition.

func (LUFactors) Solve

func (f LUFactors) Solve(b *Dense) (x *Dense)

Solve computes a solution of a.x = b where b has as many rows as a. A matrix x is returned that minimizes the two norm of L*U*X - B(piv,:). Solve will panic if a is singular. The matrix b is overwritten during the call.

func (LUFactors) U

func (f LUFactors) U() *Dense

U returns the upper triangular factor of the LU decomposition.

type Ler

type Ler interface {
	L(a Matrix)
}

An Ler can return the lower triangular matrix of the matrix represented by a, placing the result in the receiver. If the concrete value of a is the receiver, the upper residue is zeroed in place.

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 (r, c). It will panic if r or c are
	// out of bounds for the matrix.
	At(r, c int) float64
}

Matrix is the basic matrix interface type.

type MulTranser

type MulTranser interface {
	MulTrans(a Matrix, aTrans bool, b Matrix, bTrans bool)
}

A MulTranser can determine the matrix product of a and b, optionally taking the transpose of either a, b, or both, placing the result in the receiver. It performs OpA(a) * OpB(b), where OpA is transpose(a) when aTrans is true, and does nothing when aTrans == blas.NoTrans. The same logic applies to OpB. If the number of columns in OpA(a) does not equal the number of rows in OpB(b), MulTrans will panic.

type Muler

type Muler interface {
	Mul(a, b Matrix)
}

A Muler can determine 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.

type Mutable

type Mutable interface {
	// Set alters the matrix element at (r, c) to v. It will panic if r or c are out of
	// bounds for the matrix.
	Set(r, c int, v float64)

	Matrix
}

Mutable is a matrix interface type that allows elements to be altered.

type Normer

type Normer interface {
	Norm(o float64) float64
}

A Normer can return the specified matrix norm, o of the matrix represented by the receiver.

Valid order values are:

   1 - max of the sum of the absolute values of columns
  -1 - min of the sum of the absolute values of columns
 Inf - max of the sum of the absolute values of rows
-Inf - min of the sum of the absolute values of rows
   0 - Frobenius norm

Norm will panic with ErrNormOrder if an illegal norm order is specified.

type Panicker

type Panicker func()

A Panicker is a function that may panic.

type Power

type Power interface {
	Pow(a Matrix, n int)
}

A Power can raise a square matrix, a to a positive integral power, n. Pow will panic if n is negative or if a is not square.

type QRFactor

type QRFactor struct {
	QR *Dense
	// contains filtered or unexported fields
}

func QR

func QR(a *Dense) QRFactor

QR computes a QR Decomposition for an m-by-n matrix a with m >= n by Householder reflections, the QR decomposition is an m-by-n orthogonal matrix q and an n-by-n upper triangular matrix r so that a = q.r. QR will panic with ErrShape if m < n.

The QR decomposition always exists, even if the matrix does not have full rank, so QR will never fail unless m < n. The primary use of the QR decomposition is in the least squares solution of non-square systems of simultaneous linear equations. This will fail if QRIsFullRank() returns false. The matrix a is overwritten by the decomposition.

func (QRFactor) H

func (f QRFactor) H() *Dense

H returns the Householder vectors in a lower trapezoidal matrix whose columns define the reflections.

func (QRFactor) IsFullRank

func (f QRFactor) IsFullRank() bool

IsFullRank returns whether the R matrix and hence a has full rank.

func (QRFactor) Q

func (f QRFactor) Q() *Dense

Q generates and returns the (economy-sized) orthogonal factor.

func (QRFactor) R

func (f QRFactor) R() *Dense

R returns the upper triangular factor for the QR decomposition.

func (QRFactor) Solve

func (f QRFactor) Solve(b *Dense) (x *Dense)

Solve computes a least squares solution of a.x = b where b has as many rows as a. A matrix x is returned that minimizes the two norm of Q*R*X-B. Solve will panic if a is not full rank. The matrix b is overwritten during the call.

type RawColViewer

type RawColViewer interface {
	RawColView(c int) *Vector
}

A RawColViewer can return a slice of float64 reflecting a column that is backed by the matrix data.

type RawMatrixSetter

type RawMatrixSetter interface {
	SetRawMatrix(a blas64.General)
}

A RawMatrixSetter can set the underlying blas64.General used by the receiver. There is no restriction on the shape of the receiver. Changes to the receiver's elements will be reflected in the blas64.General.Data.

type RawMatrixer

type RawMatrixer interface {
	RawMatrix() blas64.General
}

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

type RawRowViewer

type RawRowViewer interface {
	RawRowView(r int) []float64
}

A RawRowViewer can return a slice of float64 reflecting a row that is backed by the matrix data.

type RawSymmetricer

type RawSymmetricer interface {
	RawSymmetric() blas64.Symmetric
}

A RawSymmetricer can return a view of itself as a BLAS Symmetric matrix.

type RawTriangular

type RawTriangular interface {
	RawTriangular() blas64.Triangular
}

type RawVectorer

type RawVectorer interface {
	RawVector() blas64.Vector
}

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

type Reseter

type Reseter interface {
	Reset()
}

A Reseter can reset the matrix so that it can be reused as the receiver of a dimensionally restricted operation. This is commonly used when the matrix is being used a a workspace or temporary matrix.

If the matrix is a view, using the reset matrix may result in data corruption in elements outside the view.

type RowViewer

type RowViewer interface {
	RowView(r int) *Vector
}

A RowViewer can return a Vector reflecting a row that is backed by the matrix data. The Vector returned will have Len() == nCols.

type SVDFactors

type SVDFactors struct {
	U     *Dense
	Sigma []float64
	V     *Dense
	// contains filtered or unexported fields
}

func SVD

func SVD(a *Dense, epsilon, small float64, wantu, wantv bool) SVDFactors

SVD performs singular value decomposition for an m-by-n matrix a. The singular value decomposition is an m-by-n orthogonal matrix u, an n-by-n diagonal matrix s, and an n-by-n orthogonal matrix v so that a = u*s*v'. If a is a wide matrix a copy of its transpose is allocated, otherwise a is overwritten during the decomposition. Matrices u and v are only created when wantu and wantv are true respectively.

The singular values, sigma[k] = s[k][k], are ordered so that

sigma[0] >= sigma[1] >= ... >= sigma[n-1].

The matrix condition number and the effective numerical rank can be computed from this decomposition.

func (SVDFactors) Cond

func (f SVDFactors) Cond() float64

Cond returns the 2-norm condition number for the S matrix.

func (SVDFactors) Rank

func (f SVDFactors) Rank(epsilon float64) int

Rank returns the number of non-negligible singular values in the sigma held by the factorisation with the given epsilon.

func (SVDFactors) S

func (f SVDFactors) S() *Dense

S returns a newly allocated S matrix from the sigma values held by the factorisation.

type Scaler

type Scaler interface {
	Scale(c float64, a Matrix)
}

A Scaler can perform scalar multiplication of the matrix represented by a with c, placing the result in the receiver.

type Stacker

type Stacker interface {
	Stack(a, b Matrix)
}

A Stacker can create the stacked matrix of a with b, where b is placed in the greater indexed rows. The result of stacking is placed in the receiver, overwriting the previous value of the receiver. Stack will panic if the two input matrices do not have the same number of columns.

type Suber

type Suber interface {
	Sub(a, b Matrix)
}

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

type Sumer

type Sumer interface {
	Sum() float64
}

A Sumer can return the sum of elements of the matrix represented by the receiver.

type SymDense

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

SymDense is a symmetric matrix that uses Dense storage.

func NewSymDense

func NewSymDense(n int, mat []float64) *SymDense

NewSymDense constructs an n x n symmetric matrix. If len(mat) == n * n, mat will be used to hold the underlying data, or if mat == nil, new data will be allocated. The underlying data representation is the same as a Dense matrix, except the values of the entries in the lower triangular portion are completely ignored.

func (*SymDense) AddSym

func (s *SymDense) AddSym(a, b Symmetric)

func (*SymDense) At

func (s *SymDense) At(r, c int) float64

At returns the element at row r and column c.

func (*SymDense) CopySym

func (s *SymDense) CopySym(a Symmetric) int

func (*SymDense) Dims

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

func (*SymDense) RankTwo

func (s *SymDense) RankTwo(a Symmetric, alpha float64, x, y []float64)

RankTwo performs a symmmetric rank-two update to the matrix a and stores the result in the receiver

m = a + alpha * (x * y' + y * x')

func (*SymDense) RawSymmetric

func (s *SymDense) RawSymmetric() blas64.Symmetric

RawSymmetric returns the matrix as a blas64.Symmetric. The returned value must be stored in upper triangular format.

func (*SymDense) SetSym

func (s *SymDense) SetSym(r, c int, v float64)

SetSym sets the elements at (r,c) and (c,r) to the value v.

func (*SymDense) SymRankOne

func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x []float64)

SymRankOne performs a symetric rank-one update to the matrix a and stores the result in the receiver

s = a + alpha * x * x'

func (*SymDense) Symmetric

func (s *SymDense) Symmetric() int

type Symmetric

type Symmetric interface {
	Matrix
	// Symmetric returns the number of rows/columns in the matrix.
	Symmetric() int
}

Symmetric represents a symmetric matrix (where the element at {i, j} equals the element at {j, i}). Symmetric matrices are always square.

type Tracer

type Tracer interface {
	Trace() float64
}

A Tracer can return the trace of the matrix represented by the receiver. Trace will panic if the matrix is not square.

type TransposeCopier

type TransposeCopier interface {
	TCopy(a Matrix)
}

A TransposeCopier can make a copy of the transpose the matrix represented by a, placing the elements into the receiver.

type Transposer

type Transposer interface {
	T() Matrix
}

A Transposer can create a transposed view matrix from the matrix represented by the receiver. Changes made to the returned Matrix may be reflected in the original.

type TriDense

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

TriDense represents an upper or lower triangular matrix in dense storage format.

func NewTriDense

func NewTriDense(n int, upper bool, mat []float64) *TriDense

NewTriangular constructs an n x n triangular matrix. The constructed matrix is upper triangular if upper == true and lower triangular otherwise. If len(mat) == n * n, mat will be used to hold the underlying data, if mat == nil, new data will be allocated, and will panic if neither of these cases is true. The underlying data representation is the same as that of a Dense matrix, except the values of the entries in the opposite half are completely ignored.

func (*TriDense) At

func (t *TriDense) At(r, c int) float64

At returns the element at row r, column c.

func (*TriDense) Cholesky

func (t *TriDense) Cholesky(a Symmetric, upper bool) (ok bool)

Cholesky calculates the Cholesky decomposition of the matrix A and returns whether the matrix is positive definite. The returned matrix is either a lower triangular matrix such that A = L * L^T or an upper triangular matrix such that A = U^T * U depending on the upper parameter.

func (*TriDense) Dims

func (t *TriDense) Dims() (r, c int)

func (*TriDense) RawTriangular

func (t *TriDense) RawTriangular() blas64.Triangular

func (*TriDense) Reset

func (t *TriDense) Reset()

Reset zeros the dimensions of the matrix so that it can be reused as the receiver of a dimensionally restricted operation.

See the Reseter interface for more information.

func (*TriDense) SetTri

func (t *TriDense) SetTri(r, c int, v float64)

SetTri sets the element at row r, column c to the value v. It panics if the location is outside the appropriate half of the matrix.

func (*TriDense) Triangle

func (t *TriDense) Triangle() (n int, upper bool)

type Triangular

type Triangular interface {
	Matrix
	// Triangular returns the number of rows/columns in the matrix and if it is
	// an upper triangular matrix.
	Triangle() (n int, upper bool)
}

type Uer

type Uer interface {
	U(a Matrix)
}

A Uer can return the upper triangular matrix of the matrix represented by a, placing the result in the receiver. If the concrete value of a is the receiver, the lower residue is zeroed in place.

type Vector

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

Vector represents a column vector.

func NewVector

func NewVector(n int, data []float64) *Vector

NewVector creates a new Vector of length n. If len(data) == n, data is used as the backing data slice. If data == nil, a new slice is allocated. If neither of these is true, NewVector will panic.

func (*Vector) AddVec

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

AddVec adds a and b element-wise, placing the result in the receiver.

func (*Vector) At

func (v *Vector) At(r, c int) float64

At returns the element at row r, column c. It panics if c is not zero.

func (*Vector) CopyVec

func (v *Vector) CopyVec(a *Vector) (n 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 matrices and returns the number of rows and columns it copied.

func (*Vector) Dims

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

func (*Vector) DivElemVec

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

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

func (*Vector) EqualsApproxVec

func (v *Vector) EqualsApproxVec(b *Vector, epsilon float64) bool

EqualsApproxVec compares the vectors represented by b and the receiver, with tolerance for element-wise equality specified by epsilon.

func (*Vector) EqualsVec

func (v *Vector) EqualsVec(b *Vector) bool

Equals compares the vectors represented by b and the receiver and returns true if the vectors are element-wise equal.

func (*Vector) Len

func (v *Vector) Len() int

Len returns the length of the vector.

func (*Vector) MulElemVec

func (v *Vector) MulElemVec(a, b *Vector)

MulElemVec performs element-wise multiplication of a and b, placing the result in the receiver.

func (*Vector) MulVec

func (v *Vector) MulVec(a Matrix, trans bool, b *Vector)

MulVec computes a * b if trans == false and a^T * b if trans == true. The result is stored into the receiver. MulVec panics if the number of columns in a does not equal the number of rows in b.

func (*Vector) RawVector

func (v *Vector) RawVector() blas64.Vector

func (*Vector) Reset

func (v *Vector) Reset()

func (*Vector) SetVec

func (v *Vector) SetVec(i int, val float64)

Set sets the element at row r to the value val. It panics if r is less than zero or greater than the length.

func (*Vector) SolveCholeskyVec

func (v *Vector) SolveCholeskyVec(t Triangular, b *Vector)

SolveCholeskyVec finds the vector v that solves A * v = b where A = L * L^T or A = U^T * U, and U or L are represented by t, placing the result in the receiver.

func (*Vector) SubVec

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

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

func (*Vector) ViewVec

func (v *Vector) ViewVec(i, n int) *Vector

ViewVec returns a sub-vector view of the receiver starting at element i and extending n columns. If i is out of range, or if n is zero or extend beyond the bounds of the Vector ViewVec will panic with ErrIndexOutOfRange. The returned Vector retains reference to the underlying vector.

type VectorSetter

type VectorSetter interface {
	// SetRow sets the values of the specified row to the values held in a slice of float64.
	// It will panic if the index is out of bounds. The number of elements copied is
	// returned and will be the minimum of the length of the slice and the number of columns
	// in the matrix.
	SetRow(i int, src []float64) int

	// SetCol sets the values of the specified column to the values held in a slice of float64.
	// It will panic if the index is out of bounds. The number of elements copied is
	// returned and will be the minimum of the length of the slice and the number of rows
	// in the matrix.
	SetCol(i int, src []float64) int
}

A VectorSetter can set rows and columns in the represented matrix.

type Vectorer

type Vectorer interface {
	// Row returns a slice of float64 for the row specified. It will panic if the index
	// is out of bounds. If the call requires a copy and dst is not nil it will be used and
	// returned, if it is not nil the number of elements copied will be the minimum of the
	// length of the slice and the number of columns in the matrix.
	Row(dst []float64, i int) []float64

	// Col returns a slice of float64 for the column specified. It will panic if the index
	// is out of bounds. If the call requires a copy and dst is not nil it will be used and
	// returned, if it is not nil the number of elements copied will be the minimum of the
	// length of the slice and the number of rows in the matrix.
	Col(dst []float64, j int) []float64
}

A Vectorer can return rows and columns of the represented matrix.

type Viewer

type Viewer interface {
	View(i, j, r, c int) Matrix
}

A Viewer returns a submatrix view of the Matrix parameter, starting at row i, column j and extending r rows and c columns. If i or j are out of range, or r or c are zero or extend beyond the bounds of the matrix View will panic with ErrIndexOutOfRange. The returned matrix must retain the receiver's reference to the original matrix such that changes in the elements of the submatrix are reflected in the original and vice versa.

Jump to

Keyboard shortcuts

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