matrix

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package matrix provides Matrix interface and matrix implementation along with a few util functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyByElement

func ApplyByElement[T Signed | Float](M Matrix[T], f func(T) T)

ApplyByElement applies some function elementwise to the matrix. This opeartion changes the matrix, so consider using Matrix.Deepcopy() depending on your needs.

Types

type Matrix

type Matrix[T Signed | Float] interface {
	json.Marshaler
	json.Unmarshaler
	fmt.Stringer

	Size() [2]int
	RowCount() int
	ColumnCount() int
	AreSameSize(Matrix[T]) bool
	Broadcast(int, int) error

	Equals(Matrix[T]) bool

	At(int, int) (T, error)
	Set(int, int, T) error

	Add(Matrix[T]) (Matrix[T], error)
	AddScalar(T) Matrix[T]
	Multiply(Matrix[T]) (Matrix[T], error)
	MultiplyByScalar(T) Matrix[T]
	MultiplyElementwise(Matrix[T]) (Matrix[T], error)

	T() Matrix[T]

	Minor(int, int) (Matrix[T], error)
	Determinant() (T, error)
	Inverse() (Matrix[T], error)

	Copy() Matrix[T]
	DeepCopy() Matrix[T]
}

Matrix is an interface for matrix implementations.

Implementations have to implement Stringer and Marshaler/Unmarshaler.

Size returns the dimensions of the matrix in the format [2]{rows, columns}

RowCount returns the row number (i.e. Size()[0])

ColumnCount returns the column number (i.e. Size()[1])

AreSameSize compares Size() of two matrices by dimension.

Broadcast broadcasts the matrix to the desired size. This is implemented by copying the values in existing rows and columns to fit the new.

var M Matrix[int]  // let it hold 1x1 matrix with value 3.
fmt.Println(M.Broadcast(2, 3))  // [ [3, 3, 3], [3, 3, 3] ]

Equals compares to matrices by exact value. Returns false on incompatible matrices sizes. Depending on the implementation, comparisons can be exact or using some epsilon error.

At gets the value at given indices. On the counterpart to math notation, indexing starts at 0.

Set sets the value at given indices. Indexing starts at 0.

Add adds two matrices in math manner, elementwise. Returns an error if the matrices are non-conformable.

AddScalar add a scalar to each element of the matrix. Equivalent to Add(M.Broadcast(rows, columns)), where M is a 1x1 matrix with given scalar.

Multiply multiplies two matrices in the math manner. If the matrices are non-conformable (columns_1 != rows_2), returns an error.

MultiplyByScalar multiples the matrix by a scalar, i.e. multiply each element by a given scalar.

MultiplyElementwise multiplies two matrices elementwise. If matrices are not of the same size, returns an error.

T transposes the matrix by the main diagonal.

Minor returns the minor from a certain element, returning (rows-1)x(columns-1) matrix. Returns error if invalid indices.

Determinant returns the determinant of the matrix.

Inverse returns the inverse matrix for a given matrix. Only possible for square matrices. If the determinant is equal to zero, returns an error.

Copy and Deepcopy copy the matrix to a new location.

func Clip

func Clip[T Signed | Float](M Matrix[T], lower, upper T) Matrix[T]

Clip clips all the values in the matrix using lower and upper bound.

 if value > upper {
	value = upper
 } else if value < lower {
	value = lower
 }

func IdentityMatrix

func IdentityMatrix(rowCount int) Matrix[float64]

IdentityMatrix returns a sqaure identity matrix implementation.

func NewMatrix

func NewMatrix[T Signed | Float](data [][]T) (Matrix[T], error)

NewMatrix returns a matrix implementation using the slice data.

if the data is empty, or column count is incosistnent, returns an error,

func NewOnesMatrix

func NewOnesMatrix(rowCount int, columnCount int) Matrix[float64]

NewOnesMatrix returns a matrix implementation filled with ones with required size.

func NewZeroMatrix

func NewZeroMatrix[T Signed | Float](rowCount int, columnCount int) Matrix[T]

NewZeroMatrix returns a matrix implementation filled with zeros with required size.

Jump to

Keyboard shortcuts

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