matrix

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: BSD-3-Clause Imports: 9 Imported by: 1

README

matrix: linear algebra with tensors

This package provides interfaces for Tensor types to the gonum functions for linear algebra, defined on the 2D mat.Matrix interface.

See the Cogent Lab Docs for full documentation.

TODO

Add following functions here:

  • eye
  • identity
  • diag
  • diagonal

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallOut1

func CallOut1(fun func(a tensor.Tensor, out *tensor.Float64) error, a tensor.Tensor) *tensor.Float64

CallOut1 calls an Out function with 1 input arg. All matrix functions require *tensor.Float64 outputs.

func CallOut2

func CallOut2(fun func(a, b tensor.Tensor, out *tensor.Float64) error, a, b tensor.Tensor) *tensor.Float64

CallOut2 calls an Out function with 2 input args. All matrix functions require *tensor.Float64 outputs.

func CopyFromDense

func CopyFromDense(to tensor.Values, dm *mat.Dense)

CopyFromDense copies a gonum mat.Dense matrix into given Tensor using standard Float64 interface

func Det

func Det(a tensor.Tensor) *tensor.Float64

Det returns the determinant of the given tensor. For a 2D matrix [[a, b], [c, d]] it this is ad - bc. See also LogDet for a version that is more numerically stable for large matricies.

func Diagonal

func Diagonal(tsr tensor.Tensor, offset ...int) *tensor.Indexed

Diagonal returns an [Indexed] view of the given tensor for the diagonal values, as a 1D list. An error is logged if the tensor is not 2D. Use the optional offset parameter to get values above (positive) or below (negative) the diagonal.

func DiagonalIndices

func DiagonalIndices(size int, offset_cols ...int) *tensor.Int

DiagonalIndices returns a list of indices for the diagonal elements of a 2D matrix of given row and column size. The result is a 2D list of indices, where the outer (row) dimension is the number of indices, and the inner dimension is 2 for the r, c coords.

  • If one additional parameter is passed, it is the offset, to set values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func DiagonalN

func DiagonalN(size int, offset_cols ...int) int

DiagonalN returns the number of elements in the along the diagonal of a 2D matrix of given row and column size.

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func Eig

func Eig(a tensor.Tensor) (vecs, vals *tensor.Float64)

Eig performs the eigen decomposition of the given square matrix, which is not symmetric. See EigSym for a symmetric square matrix. In this non-symmetric case, the results are typically complex valued, so the outputs are complex tensors. TODO: need complex support! The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *lowest* to *highest* across the columns, i.e., maximum vector is the last column. The values are the size of one row, ordered *lowest* to *highest*. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func EigOut

func EigOut(a tensor.Tensor, vecs, vals *tensor.Float64) error

EigOut performs the eigen decomposition of the given square matrix, which is not symmetric. See EigSym for a symmetric square matrix. In this non-symmetric case, the results are typically complex valued, so the outputs are complex tensors. TODO: need complex support! The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *lowest* to *highest* across the columns, i.e., maximum vector is the last column. The values are the size of one row, ordered *lowest* to *highest*. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func EigSym

func EigSym(a tensor.Tensor) (vecs, vals *tensor.Float64)

EigSym performs the eigen decomposition of the given symmetric square matrix, which produces real-valued results. When input is the [metric.CovarianceMatrix], this is known as Principal Components Analysis (PCA). The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *lowest* to *highest* across the columns, i.e., maximum vector is the last column. The values are the size of one row, ordered *lowest* to *highest*. Note that Eig produces results in the *opposite* order of SVD (which is much faster). If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func EigSymOut

func EigSymOut(a tensor.Tensor, vecs, vals *tensor.Float64) error

EigSymOut performs the eigen decomposition of the given symmetric square matrix, which produces real-valued results. When input is the [metric.CovarianceMatrix], this is known as Principal Components Analysis (PCA). The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *lowest* to *highest* across the columns, i.e., maximum vector is the last column. The values are the size of one row, ordered *lowest* to *highest*. Note that Eig produces results in the *opposite* order of SVD (which is much faster). If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func Identity

func Identity(size int, offset_cols ...int) *tensor.Float64

Identity returns a new 2D Float64 tensor with 1s along the diagonal and 0s elsewhere, with the given row and column size.

  • If one additional parameter is passed, it is the offset, to set values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func Inverse

func Inverse(a tensor.Tensor) *tensor.Float64

Inverse performs matrix inversion of a square matrix, logging an error for non-invertable cases. See InverseOut for a version that returns an error. If the input tensor is > 2D, it is treated as a list of 2D matricies which are each inverted.

func InverseOut

func InverseOut(a tensor.Tensor, out *tensor.Float64) error

InverseOut performs matrix inversion of a square matrix, returning an error for non-invertable cases. If the input tensor is > 2D, it is treated as a list of 2D matricies which are each inverted.

func LogDet

func LogDet(a tensor.Tensor) *tensor.Float64

LogDet returns the determinant of the given tensor, as the log and sign of the value, which is more numerically stable. The return is a 1D vector of length 2, with the first value being the log, and the second the sign.

func Mul

func Mul(a, b tensor.Tensor) *tensor.Float64

Mul performs matrix multiplication, using the following rules based on the shapes of the relevant tensors. If the tensor shapes are not suitable, an error is logged (see MulOut for a version returning the error). N > 2 dimensional cases use parallel threading where beneficial.

  • If both arguments are 2-D they are multiplied like conventional matrices.
  • If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
  • If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
  • If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

func MulOut

func MulOut(a, b tensor.Tensor, out *tensor.Float64) error

MulOut performs matrix multiplication, into the given output tensor, using the following rules based on the shapes of the relevant tensors. If the tensor shapes are not suitable, a [gonum] mat.ErrShape error is returned. N > 2 dimensional cases use parallel threading where beneficial.

  • If both arguments are 2-D they are multiplied like conventional matrices. The result has shape a.Rows, b.Columns.
  • If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly. Both cannot be > 2 dimensional, unless their outer dimension size is 1 or the same.
  • If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
  • If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

func NewDense

func NewDense(tsr *tensor.Float64) (*mat.Dense, error)

NewDense returns given tensor.Float64 as a [gonum] mat.Dense Matrix, on which many of the matrix operations are defined. It functions similar to the tensor.Values type, as the output of matrix operations. The Dense type serves as a view onto the tensor's data, so operations directly modify it.

func ProjectOnMatrixColumn

func ProjectOnMatrixColumn(mtx, vec, colindex tensor.Tensor) tensor.Values

ProjectOnMatrixColumn is a convenience function for projecting given vector of values along a specific column (2nd dimension) of the given 2D matrix, specified by the scalar colindex, putting results into out. If the vec is more than 1 dimensional, then it is treated as rows x cells, and each row of cells is projected through the matrix column, producing a 1D output with the number of rows. Otherwise a single number is produced. This is typically done with results from SVD or EigSym (PCA).

func ProjectOnMatrixColumnOut

func ProjectOnMatrixColumnOut(mtx, vec, colindex tensor.Tensor, out tensor.Values) error

ProjectOnMatrixColumnOut is a convenience function for projecting given vector of values along a specific column (2nd dimension) of the given 2D matrix, specified by the scalar colindex, putting results into out. If the vec is more than 1 dimensional, then it is treated as rows x cells, and each row of cells is projected through the matrix column, producing a 1D output with the number of rows. Otherwise a single number is produced. This is typically done with results from SVD or EigSym (PCA).

func SVD

func SVD(a tensor.Tensor) (vecs, vals *tensor.Float64)

SVD performs the singular value decomposition of the given symmetric square matrix, which produces real-valued results, and is generally much faster than EigSym, while producing the same results. The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *highest* to *lowest* across the columns, i.e., maximum vector is the first column. The values are the size of one row ordered in alignment with the vectors. Note that SVD produces results in the *opposite* order of EigSym. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func SVDOut

func SVDOut(a tensor.Tensor, vecs, vals *tensor.Float64) error

SVDOut performs the singular value decomposition of the given symmetric square matrix, which produces real-valued results, and is generally much faster than EigSym, while producing the same results. The vectors are same size as the input. Each vector is a column in this 2D square matrix, ordered *highest* to *lowest* across the columns, i.e., maximum vector is the first column. The values are the size of one row ordered in alignment with the vectors. Note that SVD produces results in the *opposite* order of EigSym. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func SVDValues

func SVDValues(a tensor.Tensor) *tensor.Float64

SVDValues performs the singular value decomposition of the given symmetric square matrix, which produces real-valued results, and is generally much faster than EigSym, while producing the same results. This version only generates eigenvalues, not vectors: see SVD. The values are the size of one row ordered highest to lowest, which is the opposite of EigSym. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func SVDValuesOut

func SVDValuesOut(a tensor.Tensor, vals *tensor.Float64) error

SVDValuesOut performs the singular value decomposition of the given symmetric square matrix, which produces real-valued results, and is generally much faster than EigSym, while producing the same results. This version only generates eigenvalues, not vectors: see SVDOut. The values are the size of one row ordered highest to lowest, which is the opposite of EigSym. If the input tensor is > 2D, it is treated as a list of 2D matricies, and parallel threading is used where beneficial.

func StringCheck

func StringCheck(tsr tensor.Tensor) error

func Trace

func Trace(tsr tensor.Tensor, offset ...int) tensor.Values

Trace returns the sum of the Diagonal elements of the given tensor, as a tensor scalar. An error is logged if the tensor is not 2D. Use the optional offset parameter to get values above (positive) or below (negative) the diagonal.

func Tri

func Tri(size int, offset_cols ...int) *tensor.Float64

Tri returns a new 2D Float64 tensor with 1s along the diagonal and below it, and 0s elsewhere (i.e., a filled lower triangle).

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func TriL

func TriL(tsr tensor.Tensor, offset ...int) tensor.Tensor

TriL returns a copy of the given tensor containing the lower triangular region of values (including the diagonal), with the lower triangular region zeroed. An error is logged if the tensor is not 2D. Use the optional offset parameter to include values above (positive) or below (negative) the diagonal.

func TriLIndicies

func TriLIndicies(size int, offset_cols ...int) *tensor.Int

TriLIndicies returns the list of r, c indexes for the lower triangular portion of a square matrix of size n, including the diagonal. The result is a 2D list of indices, where the outer (row) dimension is the number of indices, and the inner dimension is 2 for the r, c coords.

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix.

func TriLNum

func TriLNum(size int, offset_cols ...int) int

TriLNum returns the number of elements in the lower triangular region of a 2D matrix of given row and column size, where the triangle includes the elements along the diagonal.

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func TriLView

func TriLView(tsr tensor.Tensor, offset ...int) *tensor.Indexed

TriLView returns an [Indexed] view of the given tensor for the lower triangular region of values, as a 1D list. An error is logged if the tensor is not 2D. Use the optional offset parameter to get values above (positive) or below (negative) the diagonal.

func TriU

func TriU(tsr tensor.Tensor, offset ...int) tensor.Tensor

TriU returns a copy of the given tensor containing the upper triangular region of values (including the diagonal), with the lower triangular region zeroed. An error is logged if the tensor is not 2D. Use the optional offset parameter to include values above (positive) or below (negative) the diagonal.

func TriUIndicies

func TriUIndicies(size int, offset_cols ...int) *tensor.Int

TriUIndicies returns the list of r, c indexes for the upper triangular portion of a square matrix of size n, including the diagonal. If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal. If a second additional parameter is passed, it is the number of columns for a non-square matrix. The result is a 2D list of indices, where the outer (row) dimension is the number of indices, and the inner dimension is 2 for the r, c coords.

func TriUNum

func TriUNum(size int, offset_cols ...int) int

TriUNum returns the number of elements in the upper triangular region of a 2D matrix of given row and column size, where the triangle includes the elements along the diagonal.

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

func TriUView

func TriUView(tsr tensor.Tensor, offset ...int) *tensor.Indexed

TriUView returns an [Indexed] view of the given tensor for the upper triangular region of values, as a 1D list. An error is logged if the tensor is not 2D. Use the optional offset parameter to get values above (positive) or below (negative) the diagonal.

func TriUpper

func TriUpper(size int, offset_cols ...int) *tensor.Float64

TriUpper returns a new 2D Float64 tensor with 1s along the diagonal and above it, and 0s elsewhere (i.e., a filled upper triangle).

  • If one additional parameter is passed, it is the offset, to include values above (positive) or below (negative) the diagonal.
  • If a second additional parameter is passed, it is the number of columns for a non-square matrix (first size parameter = number of rows).

Types

type Matrix

type Matrix struct {
	Tensor tensor.Tensor
}

Matrix provides a view of the given tensor.Tensor as a [gonum] mat.Matrix interface type.

func NewMatrix

func NewMatrix(tsr tensor.Tensor) (*Matrix, error)

NewMatrix returns given tensor.Tensor as a [gonum] mat.Matrix. It returns an error if the tensor is not 2D.

func (*Matrix) At

func (mx *Matrix) At(i, j int) float64

At is the gonum/mat.Matrix interface method for returning 2D matrix element at given row, column index. Assumes Row-major ordering.

func (*Matrix) Dims

func (mx *Matrix) Dims() (r, c int)

Dims is the gonum/mat.Matrix interface method for returning the dimension sizes of the 2D Matrix. Assumes Row-major ordering.

func (*Matrix) T

func (mx *Matrix) T() mat.Matrix

T is the gonum/mat.Matrix transpose method. It performs an implicit transpose by returning the receiver inside a Transpose.

type Symmetric

type Symmetric struct {
	Matrix
}

Symmetric provides a view of the given tensor.Tensor as a [gonum] mat.Symmetric matrix interface type.

func NewSymmetric

func NewSymmetric(tsr tensor.Tensor) (*Symmetric, error)

NewSymmetric returns given tensor.Tensor as a [gonum] mat.Symmetric matrix. It returns an error if the tensor is not 2D or not symmetric.

func (*Symmetric) SymmetricDim

func (sy *Symmetric) SymmetricDim() (r int)

SymmetricDim is the gonum/mat.Matrix interface method for returning the dimensionality of a symmetric 2D Matrix.

Jump to

Keyboard shortcuts

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