matrix

package module
v0.0.0-...-8c2e006 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2020 License: BSD-3-Clause Imports: 7 Imported by: 1

README

go.matrix

linear algebra for go

Documentation

Overview

Linear algebra.

Index

Constants

This section is empty.

Variables

View Source
var (
	WhichParMethod  = 2
	WhichSyncMethod = 1
)
View Source
var (
	//The matrix returned was nil.
	ErrorNilMatrix error_ = error_(errorNilMatrix)
	//The dimensions of the inputs do not make sense for this operation.
	ErrorDimensionMismatch error_ = error_(errorDimensionMismatch)
	//The indices provided are out of bounds.
	ErrorIllegalIndex error_ = error_(errorIllegalIndex)
	//The matrix provided has a singularity.
	ExceptionSingular error_ = error_(exceptionSingular)
	//The matrix provided is not positive semi-definite.
	ExceptionNotSPD error_ = error_(exceptionNotSPD)
)

Functions

func ApproxEquals

func ApproxEquals(A, B MatrixRO, ε float64) bool

Tests to see if the difference between two matrices, element-wise, exceeds ε.

func Equals

func Equals(A, B MatrixRO) bool

Tests the element-wise equality of the two matrices.

func String

func String(A MatrixRO) string

Types

type DenseMatrix

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

A matrix backed by a flat array of all elements.

func Diagonal

func Diagonal(d []float64) *DenseMatrix

func Difference

func Difference(A, B MatrixRO) (C *DenseMatrix)

Finds the difference between two matrices.

func Eye

func Eye(span int) *DenseMatrix

Create an identity matrix with span rows and span columns.

func Kronecker

func Kronecker(A, B MatrixRO) (C *DenseMatrix)

The Kronecker product. (http://en.wikipedia.org/wiki/Kronecker_product)

func MakeDenseCopy

func MakeDenseCopy(A MatrixRO) *DenseMatrix

func MakeDenseMatrix

func MakeDenseMatrix(elements []float64, rows, cols int) *DenseMatrix

func MakeDenseMatrixStacked

func MakeDenseMatrixStacked(data [][]float64) *DenseMatrix

func Normals

func Normals(rows, cols int) *DenseMatrix

func Numbers

func Numbers(rows, cols int, num float64) *DenseMatrix

func Ones

func Ones(rows, cols int) *DenseMatrix

func ParallelProduct

func ParallelProduct(A, B MatrixRO) (C *DenseMatrix)

Uses a number of goroutines to do the dot products necessary for the matrix multiplication in parallel.

func ParseMatlab

func ParseMatlab(txt string) (A *DenseMatrix, err error)

Take a matlab-style matrix representation

eg [a b c; d e f]

func Product

func Product(A MatrixRO, Bs ...MatrixRO) (C *DenseMatrix)

Finds the Product of two matrices.

func Scaled

func Scaled(A MatrixRO, f float64) (B *DenseMatrix)

Scales a matrix by a scalar.

func Sum

func Sum(A MatrixRO, Bs ...MatrixRO) (C *DenseMatrix)

Finds the sum of two matrices.

func Unvectorize

func Unvectorize(V MatrixRO, rows, cols int) (A *DenseMatrix)

func Vectorize

func Vectorize(Am MatrixRO) (V *DenseMatrix)

func Zeros

func Zeros(rows, cols int) *DenseMatrix

func (*DenseMatrix) Add

func (A *DenseMatrix) Add(B MatrixRO) error

func (*DenseMatrix) AddDense

func (A *DenseMatrix) AddDense(B *DenseMatrix) error

func (*DenseMatrix) Array

func (A *DenseMatrix) Array() []float64

Returns the contents of this matrix stored into a flat array (row-major).

func (*DenseMatrix) Arrays

func (A *DenseMatrix) Arrays() [][]float64

Returns an array of slices referencing the matrix data. Changes to the slices effect changes to the matrix.

func (*DenseMatrix) Augment

func (A *DenseMatrix) Augment(B *DenseMatrix) (C *DenseMatrix, err error)

Get a new matrix [A B].

func (*DenseMatrix) AugmentFill

func (A *DenseMatrix) AugmentFill(B, C *DenseMatrix) (err error)

func (*DenseMatrix) BufferCol

func (A *DenseMatrix) BufferCol(j int, buf []float64)

func (*DenseMatrix) BufferDiagonal

func (A *DenseMatrix) BufferDiagonal(buf []float64)

func (*DenseMatrix) BufferRow

func (A *DenseMatrix) BufferRow(i int, buf []float64)

func (*DenseMatrix) Cholesky

func (A *DenseMatrix) Cholesky() (L *DenseMatrix, err error)

Returns the cholesky decomposition C of A, st CC'=A.

func (*DenseMatrix) ColCopy

func (A *DenseMatrix) ColCopy(j int) []float64

returns a copy of the column (not a slice)

func (*DenseMatrix) Cols

func (A *DenseMatrix) Cols() int

func (*DenseMatrix) Copy

func (A *DenseMatrix) Copy() *DenseMatrix

func (*DenseMatrix) DenseMatrix

func (A *DenseMatrix) DenseMatrix() *DenseMatrix

func (*DenseMatrix) Det

func (A *DenseMatrix) Det() float64

func (*DenseMatrix) DiagonalCopy

func (A *DenseMatrix) DiagonalCopy() []float64

returns a copy of the diagonal (not a slice)

func (*DenseMatrix) Eigen

func (A *DenseMatrix) Eigen() (V, D *DenseMatrix, err error)

Returns V,D st V*D*inv(V) = A and D is diagonal (or block diagonal).

func (*DenseMatrix) ElementMult

func (A *DenseMatrix) ElementMult(B MatrixRO) (Matrix, error)

func (*DenseMatrix) ElementMultDense

func (A *DenseMatrix) ElementMultDense(B *DenseMatrix) (*DenseMatrix, error)

func (*DenseMatrix) FillCol

func (A *DenseMatrix) FillCol(j int, buf []float64)

func (*DenseMatrix) FillDiagonal

func (A *DenseMatrix) FillDiagonal(buf []float64)

func (*DenseMatrix) FillRow

func (A *DenseMatrix) FillRow(i int, buf []float64)

func (*DenseMatrix) Get

func (A *DenseMatrix) Get(i int, j int) (v float64)

Get the element in the ith row and jth column.

func (*DenseMatrix) GetColVector

func (A *DenseMatrix) GetColVector(j int) *DenseMatrix

func (*DenseMatrix) GetMatrix

func (A *DenseMatrix) GetMatrix(i, j, rows, cols int) *DenseMatrix

Get a submatrix starting at i,j with rows rows and cols columns. Changes to the returned matrix show up in the original.

func (*DenseMatrix) GetRowVector

func (A *DenseMatrix) GetRowVector(i int) *DenseMatrix

func (*DenseMatrix) GetSize

func (A *DenseMatrix) GetSize() (rows, cols int)

func (*DenseMatrix) InfinityNorm

func (A *DenseMatrix) InfinityNorm() (ε float64)

func (*DenseMatrix) Inverse

func (A *DenseMatrix) Inverse() (*DenseMatrix, error)

func (*DenseMatrix) L

func (A *DenseMatrix) L() *DenseMatrix

Get a copy of this matrix with 0s above the diagonal.

func (*DenseMatrix) LU

func (A *DenseMatrix) LU() (L, U *DenseMatrix, P *PivotMatrix)

return L,U,P, st PLU=A.

func (*DenseMatrix) LUInPlace

func (A *DenseMatrix) LUInPlace() (P *PivotMatrix)

Overwrites A with [L\U] and returns P, st PLU=A. L is considered to have 1s in the diagonal.

func (*DenseMatrix) Minus

func (A *DenseMatrix) Minus(B MatrixRO) (Matrix, error)

func (*DenseMatrix) MinusDense

func (A *DenseMatrix) MinusDense(B *DenseMatrix) (*DenseMatrix, error)

func (*DenseMatrix) Nil

func (A *DenseMatrix) Nil() bool

func (*DenseMatrix) NumElements

func (A *DenseMatrix) NumElements() int

func (*DenseMatrix) OneNorm

func (A *DenseMatrix) OneNorm() (ε float64)

func (*DenseMatrix) Plus

func (A *DenseMatrix) Plus(B MatrixRO) (Matrix, error)

func (*DenseMatrix) PlusDense

func (A *DenseMatrix) PlusDense(B *DenseMatrix) (*DenseMatrix, error)

func (*DenseMatrix) QR

func (A *DenseMatrix) QR() (Q, R *DenseMatrix)

func (*DenseMatrix) RowCopy

func (A *DenseMatrix) RowCopy(i int) []float64

returns a copy of the row (not a slice)

func (*DenseMatrix) Rows

func (A *DenseMatrix) Rows() int

func (*DenseMatrix) SVD

func (Arg *DenseMatrix) SVD() (theU, Σ, theV *DenseMatrix, err error)

Returns U, Σ, V st Σ is diagonal (or block diagonal) and UΣV'=Arg

func (*DenseMatrix) Scale

func (A *DenseMatrix) Scale(f float64)

func (*DenseMatrix) ScaleAddRow

func (m *DenseMatrix) ScaleAddRow(rd int, rs int, f float64)

func (*DenseMatrix) ScaleMatrix

func (A *DenseMatrix) ScaleMatrix(B MatrixRO) error

func (*DenseMatrix) ScaleMatrixDense

func (A *DenseMatrix) ScaleMatrixDense(B *DenseMatrix) error

func (*DenseMatrix) ScaleRow

func (m *DenseMatrix) ScaleRow(r int, f float64)

func (*DenseMatrix) Set

func (A *DenseMatrix) Set(i int, j int, v float64)

Set the element in the ith row and jth column to v.

func (*DenseMatrix) SetMatrix

func (A *DenseMatrix) SetMatrix(i, j int, B *DenseMatrix)

Copy B into A, with B's 0, 0 aligning with A's i, j

func (*DenseMatrix) Solve

func (A *DenseMatrix) Solve(b MatrixRO) (*DenseMatrix, error)

func (*DenseMatrix) SolveDense

func (A *DenseMatrix) SolveDense(b *DenseMatrix) (*DenseMatrix, error)

func (*DenseMatrix) SparseMatrix

func (A *DenseMatrix) SparseMatrix() *SparseMatrix

Create a sparse matrix copy.

func (*DenseMatrix) Stack

func (A *DenseMatrix) Stack(B *DenseMatrix) (C *DenseMatrix, err error)

Get a new matrix [A; B], with A above B.

func (*DenseMatrix) StackFill

func (A *DenseMatrix) StackFill(B, C *DenseMatrix) (err error)

func (*DenseMatrix) String

func (A *DenseMatrix) String() string

func (*DenseMatrix) Subtract

func (A *DenseMatrix) Subtract(B MatrixRO) error

func (*DenseMatrix) SubtractDense

func (A *DenseMatrix) SubtractDense(B *DenseMatrix) error

func (*DenseMatrix) SwapRows

func (m *DenseMatrix) SwapRows(r1 int, r2 int)

func (*DenseMatrix) Symmetric

func (A *DenseMatrix) Symmetric() bool

func (*DenseMatrix) Times

func (A *DenseMatrix) Times(B MatrixRO) (Matrix, error)

func (*DenseMatrix) TimesDense

func (A *DenseMatrix) TimesDense(B *DenseMatrix) (C *DenseMatrix, err error)

func (*DenseMatrix) TimesDenseFill

func (A *DenseMatrix) TimesDenseFill(B, C *DenseMatrix) (err error)

func (*DenseMatrix) Trace

func (A *DenseMatrix) Trace() float64

func (*DenseMatrix) Transpose

func (A *DenseMatrix) Transpose() *DenseMatrix

func (*DenseMatrix) TransposeInPlace

func (A *DenseMatrix) TransposeInPlace() (err error)

func (*DenseMatrix) TwoNorm

func (A *DenseMatrix) TwoNorm() float64

func (*DenseMatrix) U

func (A *DenseMatrix) U() *DenseMatrix

Get a copy of this matrix with 0s below the diagonal.

type Matrix

type Matrix interface {
	MatrixRO

	//Set the element at the ith row and jth column to v.
	Set(i int, j int, v float64)

	Add(MatrixRO) error
	Subtract(MatrixRO) error
	Scale(float64)
}

A mutable matrix.

func Inverse

func Inverse(A MatrixRO) (B Matrix)

func Transpose

func Transpose(A MatrixRO) (B Matrix)

type MatrixRO

type MatrixRO interface {
	//Returns true if the underlying object is nil.
	Nil() bool

	//The number of rows in this matrix.
	Rows() int
	//The number of columns in this matrix.
	Cols() int

	//The number of elements in this matrix.
	NumElements() int
	//The size pair, (Rows(), Cols())
	GetSize() (int, int)

	//The element in the ith row and jth column.
	Get(i, j int) float64

	Plus(MatrixRO) (Matrix, error)
	Minus(MatrixRO) (Matrix, error)
	Times(MatrixRO) (Matrix, error)

	//The determinant of this matrix.
	Det() float64
	//The trace of this matrix.
	Trace() float64

	//A pretty-print string.
	String() string

	DenseMatrix() *DenseMatrix
	SparseMatrix() *SparseMatrix
}

The MatrixRO interface defines matrix operations that do not change the underlying data, such as information requests or the creation of transforms

Read-only matrix types (at the moment, PivotMatrix).

type PivotMatrix

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

A space-optimized structure for pivot matrices, ie a matrix with exactly one 1 in each row and each column.

func MakePivotMatrix

func MakePivotMatrix(pivots []int, pivotSign float64) *PivotMatrix

func (*PivotMatrix) ColPivotDense

func (P *PivotMatrix) ColPivotDense(A *DenseMatrix) (*DenseMatrix, error)

Equivalent to AxP, but streamlined to take advantage of the datastructures.

func (*PivotMatrix) ColPivotSparse

func (P *PivotMatrix) ColPivotSparse(A *SparseMatrix) (*SparseMatrix, error)

Equivalent to AxP, but streamlined to take advantage of the datastructures.

func (*PivotMatrix) Cols

func (A *PivotMatrix) Cols() int

func (*PivotMatrix) Copy

func (P *PivotMatrix) Copy() *PivotMatrix

Make a copy of this PivotMatrix.

func (*PivotMatrix) DenseMatrix

func (P *PivotMatrix) DenseMatrix() *DenseMatrix

Convert this PivotMatrix into a DenseMatrix.

func (*PivotMatrix) Det

func (P *PivotMatrix) Det() float64

func (*PivotMatrix) Get

func (P *PivotMatrix) Get(i, j int) float64

func (*PivotMatrix) GetSize

func (A *PivotMatrix) GetSize() (rows, cols int)

func (*PivotMatrix) InfinityNorm

func (A *PivotMatrix) InfinityNorm() float64

func (*PivotMatrix) Inverse

func (A *PivotMatrix) Inverse() *PivotMatrix

func (*PivotMatrix) Minus

func (P *PivotMatrix) Minus(A MatrixRO) (Matrix, error)

func (*PivotMatrix) Nil

func (A *PivotMatrix) Nil() bool

func (*PivotMatrix) NumElements

func (A *PivotMatrix) NumElements() int

func (*PivotMatrix) OneNorm

func (A *PivotMatrix) OneNorm() float64

func (*PivotMatrix) Plus

func (P *PivotMatrix) Plus(A MatrixRO) (Matrix, error)

func (*PivotMatrix) RowPivotDense

func (P *PivotMatrix) RowPivotDense(A *DenseMatrix) (*DenseMatrix, error)

Equivalent to PxA, but streamlined to take advantage of the datastructures.

func (*PivotMatrix) RowPivotSparse

func (P *PivotMatrix) RowPivotSparse(A *SparseMatrix) (*SparseMatrix, error)

Equivalent to PxA, but streamlined to take advantage of the datastructures.

func (*PivotMatrix) Rows

func (A *PivotMatrix) Rows() int

func (*PivotMatrix) Solve

func (P *PivotMatrix) Solve(b MatrixRO) (Matrix, error)

Returns x such that Px=b.

func (*PivotMatrix) SparseMatrix

func (P *PivotMatrix) SparseMatrix() *SparseMatrix

Convert this PivotMatrix into a SparseMatrix.

func (*PivotMatrix) String

func (A *PivotMatrix) String() string

func (*PivotMatrix) SwapRows

func (P *PivotMatrix) SwapRows(r1, r2 int) error

Swap two rows in this PivotMatrix.

func (*PivotMatrix) Symmetric

func (P *PivotMatrix) Symmetric() bool

func (*PivotMatrix) Times

func (P *PivotMatrix) Times(A MatrixRO) (Matrix, error)

Multiply this pivot matrix by another.

func (*PivotMatrix) TimesPivot

func (P *PivotMatrix) TimesPivot(A *PivotMatrix) (*PivotMatrix, error)

Multiplication optimized for when two pivots are the operands.

func (*PivotMatrix) Trace

func (P *PivotMatrix) Trace() (r float64)

func (*PivotMatrix) Transpose

func (P *PivotMatrix) Transpose() *PivotMatrix

func (*PivotMatrix) TwoNorm

func (A *PivotMatrix) TwoNorm() float64

type SparseMatrix

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

A sparse matrix based on go's map datastructure.

func MakeSparseMatrix

func MakeSparseMatrix(elements map[int]float64, rows int, cols int) *SparseMatrix

Create a sparse matrix using the provided map as its backing.

func NormalsSparse

func NormalsSparse(rows int, cols int, n int) *SparseMatrix

Creates a matrix and puts a standard normal in n random elements, with replacement.

func ZerosSparse

func ZerosSparse(rows int, cols int) *SparseMatrix

func (*SparseMatrix) Add

func (A *SparseMatrix) Add(B MatrixRO) error

Add another matrix to this one in place.

func (*SparseMatrix) AddSparse

func (A *SparseMatrix) AddSparse(B *SparseMatrix) error

Add another matrix to this one in place, optimized for sparsity.

func (*SparseMatrix) Augment

func (A *SparseMatrix) Augment(B *SparseMatrix) (*SparseMatrix, error)

Creates a new matrix [A B].

func (*SparseMatrix) Cols

func (A *SparseMatrix) Cols() int

func (*SparseMatrix) Copy

func (A *SparseMatrix) Copy() *SparseMatrix

func (*SparseMatrix) DenseMatrix

func (A *SparseMatrix) DenseMatrix() *DenseMatrix

Convert this sparse matrix into a dense matrix.

func (*SparseMatrix) Det

func (A *SparseMatrix) Det() float64

func (*SparseMatrix) ElementMult

func (A *SparseMatrix) ElementMult(B MatrixRO) (*SparseMatrix, error)

Get the element-wise product of this matrix and another.

func (*SparseMatrix) ElementMultSparse

func (A *SparseMatrix) ElementMultSparse(B *SparseMatrix) (*SparseMatrix, error)

Get the element-wise product of this matrix and another, optimized for sparsity.

func (*SparseMatrix) Get

func (A *SparseMatrix) Get(i, j int) float64

func (*SparseMatrix) GetColIndex

func (A *SparseMatrix) GetColIndex(index int) (j int)

Turn an element index into a column number.

func (*SparseMatrix) GetColVector

func (A *SparseMatrix) GetColVector(j int) *SparseMatrix

Gets a reference to a column vector.

func (*SparseMatrix) GetIndex

func (A *SparseMatrix) GetIndex(index int) float64

Looks up an element given its element index.

func (*SparseMatrix) GetMatrix

func (A *SparseMatrix) GetMatrix(i, j, rows, cols int) (subMatrix *SparseMatrix)

Get a matrix representing a subportion of A. Changes to the new matrix will be reflected in A.

func (*SparseMatrix) GetRowColIndex

func (A *SparseMatrix) GetRowColIndex(index int) (i int, j int)

Turn an element index into a row and column number.

func (*SparseMatrix) GetRowIndex

func (A *SparseMatrix) GetRowIndex(index int) (i int)

Turn an element index into a row number.

func (*SparseMatrix) GetRowVector

func (A *SparseMatrix) GetRowVector(i int) *SparseMatrix

Gets a reference to a row vector.

func (*SparseMatrix) GetSize

func (A *SparseMatrix) GetSize() (rows, cols int)

func (*SparseMatrix) Indices

func (A *SparseMatrix) Indices() (out chan int)

A channel that will carry the indices of non-zero elements.

func (*SparseMatrix) InfinityNorm

func (A *SparseMatrix) InfinityNorm() (res float64)

func (*SparseMatrix) L

func (A *SparseMatrix) L() *SparseMatrix

Returns a copy with all zeros above the diagonal.

func (*SparseMatrix) Minus

func (A *SparseMatrix) Minus(B MatrixRO) (Matrix, error)

The difference between this matrix and another.

func (*SparseMatrix) MinusSparse

func (A *SparseMatrix) MinusSparse(B *SparseMatrix) (*SparseMatrix, error)

The difference between this matrix and another sparse matrix, optimized for sparsity.

func (*SparseMatrix) Nil

func (A *SparseMatrix) Nil() bool

func (*SparseMatrix) NumElements

func (A *SparseMatrix) NumElements() int

func (*SparseMatrix) OneNorm

func (A *SparseMatrix) OneNorm() (res float64)

func (*SparseMatrix) Plus

func (A *SparseMatrix) Plus(B MatrixRO) (Matrix, error)

The sum of this matrix and another.

func (*SparseMatrix) PlusSparse

func (A *SparseMatrix) PlusSparse(B *SparseMatrix) (*SparseMatrix, error)

The sum of this matrix and another sparse matrix, optimized for sparsity.

func (*SparseMatrix) Rows

func (A *SparseMatrix) Rows() int

func (*SparseMatrix) Scale

func (A *SparseMatrix) Scale(f float64)

Scale this matrix by f.

func (*SparseMatrix) ScaleAddRow

func (A *SparseMatrix) ScaleAddRow(rd, rs int, f float64)

Add a multiple of row rs to row rd.

func (*SparseMatrix) ScaleMatrix

func (A *SparseMatrix) ScaleMatrix(B MatrixRO) error

Scale this matrix by another, element-wise.

func (*SparseMatrix) ScaleMatrixSparse

func (A *SparseMatrix) ScaleMatrixSparse(B *SparseMatrix) error

Scale this matrix by another sparse matrix, element-wise. Optimized for sparsity.

func (*SparseMatrix) ScaleRow

func (A *SparseMatrix) ScaleRow(r int, f float64)

Scale a row by a scalar.

func (*SparseMatrix) Set

func (A *SparseMatrix) Set(i int, j int, v float64)

func (*SparseMatrix) SetIndex

func (A *SparseMatrix) SetIndex(index int, v float64)

Sets an element given its index.

func (*SparseMatrix) SparseMatrix

func (A *SparseMatrix) SparseMatrix() *SparseMatrix

func (*SparseMatrix) Stack

func (A *SparseMatrix) Stack(B *SparseMatrix) (*SparseMatrix, error)

Creates a new matrix [A;B], where A is above B.

func (*SparseMatrix) String

func (A *SparseMatrix) String() string

func (*SparseMatrix) Subtract

func (A *SparseMatrix) Subtract(B MatrixRO) error

Subtract another matrix from this one in place.

func (*SparseMatrix) SubtractSparse

func (A *SparseMatrix) SubtractSparse(B *SparseMatrix) error

Subtract another matrix from this one in place, optimized for sparsity.

func (*SparseMatrix) SwapRows

func (A *SparseMatrix) SwapRows(r1, r2 int)

Swap two rows in this matrix.

func (*SparseMatrix) Symmetric

func (A *SparseMatrix) Symmetric() bool

func (*SparseMatrix) Times

func (A *SparseMatrix) Times(B MatrixRO) (Matrix, error)

Get the product of this matrix and another.

func (*SparseMatrix) TimesSparse

func (A *SparseMatrix) TimesSparse(B *SparseMatrix) (*SparseMatrix, error)

Get the product of this matrix and another, optimized for sparsity.

func (*SparseMatrix) Trace

func (A *SparseMatrix) Trace() (res float64)

func (*SparseMatrix) Transpose

func (A *SparseMatrix) Transpose() *SparseMatrix

func (*SparseMatrix) TwoNorm

func (A *SparseMatrix) TwoNorm() float64

func (*SparseMatrix) U

func (A *SparseMatrix) U() *SparseMatrix

Returns a copy with all zeros below the diagonal.

Jump to

Keyboard shortcuts

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