matrix

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

matrix package

go get github.com/humilityai/matrix

Yet another matrix implementation ...

Matrix package provides a simple matrix API for float64 and bool data.

This package was designed to have yet another custom matrix-as-a-data-structure API. It currently does not support matrix operations.

Superior matrix implementations can be found at: https://godoc.org/gorgonia.org/tensor and https://godoc.org/gonum.org/v1/gonum/mat

Or, you can use the API (ToTensor() and ToGonum()) of this package to easily convert a matrix into a Gorgonia tensor or Gonum matrix.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRowSize     = fmt.Errorf("row has incorrect number of columns")
	ErrRowIndex    = fmt.Errorf("row index is out of bounds")
	ErrColumnIndex = fmt.Errorf("column index is out of bounds")
)

Functions

This section is empty.

Types

type Func

type Func func(interface{}) interface{}

Func is the basic function type for modifying the values of a matrix.

type Iterator

type Iterator struct {
	Matrix
	// contains filtered or unexported fields
}

Iterator is an object that can be used to traverse the rows of a matrix in order exactly once.

func (*Iterator) ApplyToColumns

func (i *Iterator) ApplyToColumns(f Func, columns sam.SliceInt)

ApplyToColumns can be used to apply a function to the values of one or more columns in the matrix.

func (*Iterator) ApplyToMatrix

func (i *Iterator) ApplyToMatrix(f Func)

ApplyToMatrix will apply the supplied function to all values in the matrix. This should only be called after the Iterator has been created and before the Next() method has been called

func (*Iterator) Index

func (i *Iterator) Index() int

Index returns the current index of the iterator object.

func (*Iterator) Next

func (i *Iterator) Next() bool

Next will set the iterator to return the next row. It returns false if the row is larger than the number of rows in the matrix.

func (*Iterator) Row

func (i *Iterator) Row() sam.Slice

Row will return the data of the current row for the iterator.

func (*Iterator) RowIndices

func (i *Iterator) RowIndices() sam.SliceInt

RowIndices ...

type Matrix

type Matrix interface {
	Rows() int
	Columns() int
	GetRow(i int) (sam.Slice, error)
	Type() string
}

Matrix is the minimal interface for any new matrices.

type MatrixBool

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

MatrixBool is backed by a single array.

func NewMatrixBool

func NewMatrixBool(columns int) *MatrixBool

NewMatrixBool creates a Matrix with the specified column count.

func (*MatrixBool) AddRow

func (m *MatrixBool) AddRow(row sam.SliceBool) error

AddRow will append the boolean array to the matrix as a new row. If the size of the row does not match the number of columns in the matrix then an ErrRowSize will be returned.

func (*MatrixBool) AppendColumn

func (m *MatrixBool) AppendColumn(defaultValue bool)

AppendColumn will add a column to the matrix and place the specified default value into each row's column value.

func (*MatrixBool) Columns

func (m *MatrixBool) Columns() int

Columns will return the number of columns found in the matrix.

func (*MatrixBool) Dimensions

func (m *MatrixBool) Dimensions() (int, int)

Dimensions returns the number of rows and columns in the matrix: (rows, columns).

func (*MatrixBool) GetColumnData

func (m *MatrixBool) GetColumnData(column int) (data sam.SliceBool, err error)

GetColumnData will return a float64 array that contains all the data points of the specified column. If the specified column is out of bounds then an ErrColumnIndex will be returned.

func (*MatrixBool) GetRow

func (m *MatrixBool) GetRow(row int) (sam.Slice, error)

GetRow ...

func (*MatrixBool) GetValue

func (m *MatrixBool) GetValue(row, column int) (bool, error)

GetValue will return the boolean value found at the row and column arguments provided. It will return an error if something is invalid about either the row or column argument.

func (*MatrixBool) Iterator

func (m *MatrixBool) Iterator() *Iterator

Iterator will return an object that allows row iteration of the matrix.

func (*MatrixBool) Len

func (m *MatrixBool) Len() int

Len is a standard method that satisfies many common interfaces.

func (*MatrixBool) RemoveRow

func (m *MatrixBool) RemoveRow(row int) error

RemoveRow will delete the row from the matrix.

func (*MatrixBool) Rows

func (m *MatrixBool) Rows() int

Rows will return the number of rows found in the matrix.

func (*MatrixBool) SetBackingData

func (m *MatrixBool) SetBackingData(data sam.SliceBool)

SetBackingData will replace the matrix backing array with the array provided.

func (*MatrixBool) ToTensor

func (m *MatrixBool) ToTensor() tensor.Tensor

ToTensor will create and return a new Gorgonia Tensor (dense) object from the MatrixBool.

func (*MatrixBool) Type

func (m *MatrixBool) Type() string

Type is the type of values in MatrixFloat64

func (*MatrixBool) UpdateValue

func (m *MatrixBool) UpdateValue(value bool, row, column int) error

UpdateValue will update the value found at the provided row and column arguments. If the row or column are out of bounds for the matrix then the proper error will be returned.

type MatrixFloat64

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

MatrixFloat64 is backed by a single float64 array.

func NewMatrixFloat64

func NewMatrixFloat64(columns int) *MatrixFloat64

NewMatrixFloat64 creates a Matrix with the specified column count.

func (*MatrixFloat64) AddRow

func (m *MatrixFloat64) AddRow(row []float64) error

AddRow will append the float64 array to the matrix as a new row. If the size of the row does not match the number of columns in the matrix then an ErrRowSize will be returned.

func (*MatrixFloat64) AppendColumn

func (m *MatrixFloat64) AppendColumn(defaultValue float64)

AppendColumn will add a column to the matrix and place the specified default value into each row's column value.

func (*MatrixFloat64) Columns

func (m *MatrixFloat64) Columns() int

Columns will return the number of columns found in the matrix.

func (*MatrixFloat64) Dimensions

func (m *MatrixFloat64) Dimensions() (int, int)

Dimensions returns the number of rows and columns in the matrix: (rows, columns).

func (*MatrixFloat64) GetColumnData

func (m *MatrixFloat64) GetColumnData(column int) (data sam.SliceFloat64, err error)

GetColumnData will return a float64 array that contains all the data points of the specified column. If the specified column is out of bounds then an ErrColumnIndex will be returned.

func (*MatrixFloat64) GetRow

func (m *MatrixFloat64) GetRow(row int) (sam.Slice, error)

GetRow will retrieve the data at the given row index.

func (*MatrixFloat64) GetValue

func (m *MatrixFloat64) GetValue(row, column int) (float64, error)

GetValue will return the float64 value found at the row and column arguments provided. It will return an error if something is invalid about either the row or column argument.

func (*MatrixFloat64) Iterator

func (m *MatrixFloat64) Iterator() *Iterator

Iterator will return an object that allows row iteration of the matrix.

func (*MatrixFloat64) Len

func (m *MatrixFloat64) Len() int

Len is a standard method that satisfies many common interfaces.

func (*MatrixFloat64) MaxSum

func (m *MatrixFloat64) MaxSum() sam.SliceFloat64

MaxSum will return the row with the greatest sum of its components.

func (*MatrixFloat64) MinSum

func (m *MatrixFloat64) MinSum() sam.SliceFloat64

MinSum will return the row with the smallest sum of its components.

func (*MatrixFloat64) Mode

func (m *MatrixFloat64) Mode() sam.SliceFloat64

Mode will return the "mode" of each column as a "vector" or "row".

func (*MatrixFloat64) NonZeroRows

func (m *MatrixFloat64) NonZeroRows() (*MatrixFloat64, error)

NonZeroRows will return a new matrix that contains only the non-zero rows of the original matrix.

func (*MatrixFloat64) RemoveRow

func (m *MatrixFloat64) RemoveRow(row int) error

RemoveRow will delete the row from the matrix.

func (*MatrixFloat64) Rows

func (m *MatrixFloat64) Rows() int

Rows will return the number of rows found in the matrix.

func (*MatrixFloat64) Sample

func (m *MatrixFloat64) Sample(amount int) *MatrixFloat64

Sample will grab the number of rows provided as an argument randomly. The results are returend as a new *MatrixFloat64. If the number of rows is less than zero, then zero rows will be returned. If the number of rows is equal to or greater than the number of rows already in the matrix, then a pointer to the original matrix will beb returned.

func (*MatrixFloat64) SetBackingData

func (m *MatrixFloat64) SetBackingData(data sam.SliceFloat64)

SetBackingData will replace the matrix backing array with the array provided.

func (*MatrixFloat64) ToGonum

func (m *MatrixFloat64) ToGonum() mat.Matrix

ToGonum will create and return a new Gonum Mat64 object from the MatrixFloat64

func (*MatrixFloat64) ToTensor

func (m *MatrixFloat64) ToTensor() tensor.Tensor

ToTensor will create and return a new Gorgonia Tensor (dense) object from the MatrixFloat64.

func (*MatrixFloat64) Type

func (m *MatrixFloat64) Type() string

Type is the type of values in MatrixFloat64

func (*MatrixFloat64) UpdateValue

func (m *MatrixFloat64) UpdateValue(value float64, row, column int) error

UpdateValue will update the value found at the provided row and column arguments. If the row or column are out of bounds for the matrix then the proper error will be returned.

func (*MatrixFloat64) Values

func (m *MatrixFloat64) Values(i int) []float64

Values is a wrapper for GetRow.

type Sparse

type Sparse struct {
	C    int                     `json:"columns"`
	Data map[int]map[int]float64 `json:"data"`
}

Sparse represents a sparse matrix and should only be used when the data in the matrix is expected to be mostly zero (unset).

func NewSparse

func NewSparse() *Sparse

NewSparse will return a `*Sparse` matrix.

func (*Sparse) Columns

func (s *Sparse) Columns() int

Columns will return the number of columns in the sparse matrix

func (*Sparse) Get

func (s *Sparse) Get(i, j int) float64

Get will return the value found at the provided coordinates. The value will return `0` if the coordinates do not exist in the matrix.

func (*Sparse) GetColumn

func (s *Sparse) GetColumn(i int) rowValues

GetColumn will return a set of row value {row, value} pairs for the specified column

func (*Sparse) GetRow

func (s *Sparse) GetRow(i int) (columnValues, error)

GetRow will return the list of values found at row `i`. It will return a list of {column, value} pairs.

func (*Sparse) Increment

func (s *Sparse) Increment(i, j int)

Increment will add +1 to the value found at the coordinates. If the coordinates do not exist then they will be created.

func (*Sparse) Rows

func (s *Sparse) Rows() int

Rows will return the number of rows in the sparse matrix

func (*Sparse) Set

func (s *Sparse) Set(i, j int, value float64)

Set will set a float64 value at the specified coordinates in the matrix.

func (*Sparse) Type

func (s *Sparse) Type() string

Type says the Sparse matrix is a float64 data type.

Jump to

Keyboard shortcuts

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