mat

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 7 Imported by: 3

Documentation

Overview

Package mat defines matrices and operations with them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreEqual

func AreEqual(m1, m2 ReadOnlyMatrix) bool

AreEqual returns true iff matrices have the same number of rows and columns with exactly the same values in matching positions.

func FillMatrixWithData

func FillMatrixWithData(matrix MutableMatrix, data []float64)

FillMatrixWithData fills the matrix using the given slice of float64 numbers. This function expects the matrix to have the same number of items as the slice.

func HasZeroInMainDiagonal

func HasZeroInMainDiagonal(m ReadOnlyMatrix) bool

HasZeroInMainDiagonal returns true if a zero is found in the matrix main diagonal.

func IsRowDominant

func IsRowDominant(m ReadOnlyMatrix) bool

IsRowDominant returns true if for every row in the matrix, the element in the main diagonal is greater than every other element.

func IsSquare

func IsSquare(m ReadOnlyMatrix) bool

IsSquare returns true if the given matrix has the same number of rows and columns.

func IsSymmetric

func IsSymmetric(m ReadOnlyMatrix) bool

IsSymmetric returns true if the given matrix is square and equals to it's traspose.

func MainDiagonal

func MainDiagonal(m ReadOnlyMatrix) vec.ReadOnlyVector

MainDiagonal returns a vector containing the values of the main diagonal.

func MatrixContainsData

func MatrixContainsData(matrix ReadOnlyMatrix, data []float64) bool

MatrixContainsData tests whether a given matrix has exactly the same data as the slice of float64 numbers.

The number of items in the matrix and the slice need to be the same in order for this test to return true.

func ToImage

func ToImage(m ReadOnlyMatrix, filePath string)

ToImage creates an image with as many width pixels as columns has the matrix and as many height pixels as rows.

Each pixel will be colored:

  • Gray if the value is zero
  • Red if the value is positive
  • Blue if the value is negative

Types

type DenseMat

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

A DenseMat is an implementation of a dense Matrix.

Dense matrices allocate all the memory required to store every value. Every value which hasn't been explecitly set is zero.

func MakeDense

func MakeDense(rows, cols int) *DenseMat

MakeDense creates a new dense matrix (stores zeroes) with the given rows and columns filled with zeroes.

func MakeDenseWithData

func MakeDenseWithData(rows, cols int, data []float64) *DenseMat

MakeDenseWithData creates a new matrix initialized with the given data.

func MakeSquareDense

func MakeSquareDense(size int) *DenseMat

MakeSquareDense creates a new dense matrix (strores zeroes) with the given dimension all filled with zeroes.

func (*DenseMat) AddToValue

func (m *DenseMat) AddToValue(row, col int, value float64)

AddToValue adds the given value to the existing value in the indicated row and column.

func (DenseMat) Cols

func (m DenseMat) Cols() int

Cols returns the number of columns in the matrix.

func (DenseMat) NonZeroIndicesAtRow

func (m DenseMat) NonZeroIndicesAtRow(row int) []int

NonZeroIndicesAtRow returns a slice with all non-zero elements indices for the given row.

func (DenseMat) RowTimesVector

func (m DenseMat) RowTimesVector(row int, v vec.ReadOnlyVector) float64

RowTimesVector returns the result of multiplying the row at the given index times the given vector.

func (DenseMat) Rows

func (m DenseMat) Rows() int

Rows returns the number of rows in the matrix.

func (*DenseMat) SetIdentityRow

func (m *DenseMat) SetIdentityRow(row int)

SetIdentityRow sets the given row as identity: one in the main diagonal value, and zeroes in all other positions of the row.

func (*DenseMat) SetValue

func (m *DenseMat) SetValue(row, col int, value float64)

SetValue sets a value for a given row and column.

func (*DenseMat) SetZeroCol

func (m *DenseMat) SetZeroCol(col int)

SetZeroCol sets all the values in the given column as zero.

func (DenseMat) TimesMatrix

func (m DenseMat) TimesMatrix(other ReadOnlyMatrix) ReadOnlyMatrix

TimesMatrix multiplies this matrix with other.

func (DenseMat) TimesVector

func (m DenseMat) TimesVector(v vec.ReadOnlyVector) vec.ReadOnlyVector

TimesVector creates a new vector result of multiplying this matrix and a vector.

func (DenseMat) Value

func (m DenseMat) Value(row, col int) float64

Value returns the value at a given row and column.

type MutableMatrix

type MutableMatrix interface {
	ReadOnlyMatrix

	/* Methods */
	SetValue(int, int, float64)
	AddToValue(int, int, float64)

	SetZeroCol(int)
	SetIdentityRow(int)
}

A MutableMatrix defines the contract for a matrix which implements the ReadOnlyMatrix and also provides methods that allow the mutation of its data.

type ReadOnlyMatrix

type ReadOnlyMatrix interface {
	/* Properties */
	Rows() int
	Cols() int
	NonZeroIndicesAtRow(int) []int

	/* Methods */
	Value(int, int) float64

	/* Operations */
	RowTimesVector(row int, v vec.ReadOnlyVector) float64
	TimesVector(v vec.ReadOnlyVector) vec.ReadOnlyVector
	TimesMatrix(other ReadOnlyMatrix) ReadOnlyMatrix
}

A ReadOnlyMatrix defines the contract for a matrix whose methods can't (and shouldn't) mutate the matrix.

The operations defined in a ReadOnlyMatrix should always return a new instance, never mutate the matrix.

func CholeskyDecomposition

func CholeskyDecomposition(m ReadOnlyMatrix) ReadOnlyMatrix

CholeskyDecomposition computes the Cholesky lower matrix for a square, symmetric matrix.

func IncompleteCholeskyDecomposition

func IncompleteCholeskyDecomposition(m ReadOnlyMatrix) ReadOnlyMatrix

IncompleteCholeskyDecomposition computes the Incomplete Cholesky lower matrix decomposition for the given square and symmetric matrix.

type SparseMat

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

A SparseMat is a matrix where the zeroes aren't stored.

func MakeIdentity

func MakeIdentity(size int) *SparseMat

MakeIdentity creates a new sparse matrix with all zeroes except in the main diagonal, which has ones.

func MakeSparse

func MakeSparse(rows, cols int) *SparseMat

MakeSparse creates a new sparse matrix with the given number of rows and columns.

func MakeSparseWithData

func MakeSparseWithData(rows, cols int, data []float64) *SparseMat

MakeSparseWithData creates a new sparse matrix initialized with the given data.

This method is mainly used for testing purposes as it makes no sense to create a sparse matrix with a given data slice. Most of the elements in a sparse matrix should be zero.

func MakeSquareSparse

func MakeSquareSparse(size int) *SparseMat

MakeSquareSparse creates a new square sparse matrix with the given number of rows and columns.

func (*SparseMat) AddToValue

func (m *SparseMat) AddToValue(row, col int, value float64)

AddToValue adds the given value to the existing value in the indicated row and column.

func (SparseMat) Cols

func (m SparseMat) Cols() int

Cols returns the number of columns in the matrix.

func (SparseMat) NonZeroIndicesAtRow

func (m SparseMat) NonZeroIndicesAtRow(row int) []int

NonZeroIndicesAtRow returns a slice with all non-zero elements indices for the given row.

func (SparseMat) RowTimesVector

func (m SparseMat) RowTimesVector(row int, vector vec.ReadOnlyVector) float64

RowTimesVector returns the result of multiplying the row at the given index times the given vector.

func (SparseMat) Rows

func (m SparseMat) Rows() int

Rows returns the number of rows in the matrix.

func (*SparseMat) SetIdentityRow

func (m *SparseMat) SetIdentityRow(row int)

SetIdentityRow sets the given row as identity: one in the main diagonal value, and zeroes in all other positions of the row.

func (*SparseMat) SetValue

func (m *SparseMat) SetValue(row, col int, value float64)

SetValue sets a value for a given row and column.

func (*SparseMat) SetZeroCol

func (m *SparseMat) SetZeroCol(col int)

SetZeroCol sets all the values in the given column as zero.

func (SparseMat) TimesMatrix

func (m SparseMat) TimesMatrix(other ReadOnlyMatrix) ReadOnlyMatrix

TimesMatrix multiplies this matrix times other.

func (SparseMat) TimesVector

func (m SparseMat) TimesVector(vector vec.ReadOnlyVector) vec.ReadOnlyVector

TimesVector multiplies this matrix and a vector.

func (SparseMat) Value

func (m SparseMat) Value(row, col int) float64

Value returns the value at a given row and column.

Jump to

Keyboard shortcuts

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