sparsemat

package module
v0.0.0-...-7dea17e Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 8 Imported by: 7

README

sparsemat

A simple sparse matrix for values over binary Galois Field - GF(2).

Currently implemented:

Future implementations:

Usage

Importing:

import mat "github.com/nathanhack/sparsemat"

Creating a matrix:

m1 := mat.CSRIdentity(3)
m2 := mat.DOKMat(3,3, 1,0,0,0,1,0,0,0,1)

Multiplying: Mul

r := mat.CSRMat(3,3)
r.Mul(m1,m2) // multiplies m1xm2 and stores into r

Accessors: At and Set

fmt.Printf("value at (%v,%v) is %v\n",1,1,r.At(1,1))
r.Set(1,1,0)
fmt.Printf("value at (%v,%v) is now %v\n",1,1,r.At(1,1))

Slices: Slice

m := mat.DOKMat(1,4, 1,1,1,1) // creates matrix [1 1 1 1]
s := m.Slice(0,1,1,2) // creates a slice (new matrix) of the two middle 1's [1 1]

Transposes: T

t := m.T() // note this creates a new allocated matrix

Deps

Golang

Version 1.14+

Libs

github.com/olekukonko/tablewriter

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSRMatrix

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

func (*CSRMatrix) Add

func (mat *CSRMatrix) Add(a, b SparseMat) SparseMat

Add stores the addition of a and b in this matrix.

func (*CSRMatrix) AddRows

func (mat *CSRMatrix) AddRows(i1, i2, dest int) SparseMat

AddRows is fast row operation to add two rows and put the result in a destination row.

func (*CSRMatrix) And

func (mat *CSRMatrix) And(a, b SparseMat) SparseMat

And executes a piecewise logical AND on the two matrices and stores the values in this matrix.

func (*CSRMatrix) At

func (mat *CSRMatrix) At(i, j int) int

At returns the value at row index i and column index j.

func (*CSRMatrix) Column

func (mat *CSRMatrix) Column(j int) SparseVector

Column returns a map containing the non zero row indices as the keys and it's associated values.

func (*CSRMatrix) Dims

func (mat *CSRMatrix) Dims() (int, int)

Dims returns the dimensions of the matrix.

func (*CSRMatrix) Equals

func (mat *CSRMatrix) Equals(m SparseMat) bool

Equals return true if the m matrix has the same shape and values as this matrix.

func (*CSRMatrix) MarshalJSON

func (mat *CSRMatrix) MarshalJSON() ([]byte, error)

func (*CSRMatrix) Mul

func (mat *CSRMatrix) Mul(a, b SparseMat) SparseMat

Mul multiplies two matrices and stores the values in this matrix.

func (*CSRMatrix) Negate

func (mat *CSRMatrix) Negate() SparseMat

Negate performs a piecewise logical negation.

func (*CSRMatrix) Or

func (mat *CSRMatrix) Or(a, b SparseMat) SparseMat

Or executes a piecewise logical OR on the two matrices and stores the values in this matrix.

func (*CSRMatrix) Row

func (mat *CSRMatrix) Row(i int) SparseVector

Row returns a map containing the non zero column indices as the keys and it's associated values.

func (*CSRMatrix) Set

func (mat *CSRMatrix) Set(i, j, value int) SparseMat

Set sets the value at row index i and column index j to value.

func (*CSRMatrix) SetColumn

func (mat *CSRMatrix) SetColumn(j int, vec SparseVector) SparseMat

SetColumn sets the values in column j. The values' keys are expected to be row indices.

func (*CSRMatrix) SetMatrix

func (mat *CSRMatrix) SetMatrix(a SparseMat, iOffset, jOffset int) SparseMat

SetMatrix replaces the values of this matrix with the values of from matrix a. The shape of 'a' must be less than or equal mat. If the 'a' shape is less then iOffset and jOffset can be used to place 'a' matrix in a specific location.

func (*CSRMatrix) SetRow

func (mat *CSRMatrix) SetRow(i int, vec SparseVector) SparseMat

SetRow sets the values in row i. The values' keys are expected to be column indices.

func (*CSRMatrix) Slice

func (mat *CSRMatrix) Slice(i, j, rows, cols int) SparseMat

Slice creates a new matrix containing the slice of data.

func (CSRMatrix) String

func (mat CSRMatrix) String() string

String returns a string representation of this matrix.

func (*CSRMatrix) SwapColumns

func (mat *CSRMatrix) SwapColumns(j1, j2 int) SparseMat

func (*CSRMatrix) SwapRows

func (mat *CSRMatrix) SwapRows(i1, i2 int) SparseMat

func (*CSRMatrix) T

func (mat *CSRMatrix) T() SparseMat

T returns a new matrix that is the transpose of the underlying matrix.

func (*CSRMatrix) UnmarshalJSON

func (mat *CSRMatrix) UnmarshalJSON(bytes []byte) error

func (*CSRMatrix) XOr

func (mat *CSRMatrix) XOr(a, b SparseMat) SparseMat

XOr executes a piecewise logical XOR on the two matrices and stores the values in this matrix.

func (*CSRMatrix) Zeroize

func (mat *CSRMatrix) Zeroize() SparseMat

Zeroize take the current matrix sets all values to 0.

func (*CSRMatrix) ZeroizeRange

func (mat *CSRMatrix) ZeroizeRange(i, j, rows, cols int) SparseMat

ZeroizeRange take the current matrix sets values inside the range to zero.

type CSRVector

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

func (*CSRVector) Add

func (vec *CSRVector) Add(a, b SparseVector) SparseVector

func (*CSRVector) And

func (vec *CSRVector) And(a, b SparseVector) SparseVector

func (*CSRVector) At

func (vec *CSRVector) At(i int) int

At returns the value at index i.

func (*CSRVector) Dot

func (vec *CSRVector) Dot(a SparseVector) int

func (*CSRVector) Equals

func (vec *CSRVector) Equals(v SparseVector) bool

func (*CSRVector) HammingDistance

func (vec *CSRVector) HammingDistance(a SparseVector) int

func (*CSRVector) HammingWeight

func (vec *CSRVector) HammingWeight() int

func (*CSRVector) IsZero

func (vec *CSRVector) IsZero() bool

func (*CSRVector) Len

func (vec *CSRVector) Len() int

func (*CSRVector) MarshalJSON

func (vec *CSRVector) MarshalJSON() ([]byte, error)

func (*CSRVector) MatMul

func (vec *CSRVector) MatMul(mat SparseMat, vec2 SparseVector) SparseVector

func (*CSRVector) MulMat

func (vec *CSRVector) MulMat(vec2 SparseVector, mat SparseMat) SparseVector

func (*CSRVector) Negate

func (vec *CSRVector) Negate() SparseVector

func (*CSRVector) NextSet

func (vec *CSRVector) NextSet(startingIndex int) (index int, has bool)

NextSet returns the next bit which is set starting from startingIndex, so if the startingIndex is set it will be returned, if not it will be the next bit. If no bits are found has bool will be set to false.

func (*CSRVector) NonzeroArray

func (vec *CSRVector) NonzeroArray() (indices []int)

func (*CSRVector) NonzeroMap

func (vec *CSRVector) NonzeroMap() (indexToValues map[int]int)

func (*CSRVector) Or

func (vec *CSRVector) Or(a, b SparseVector) SparseVector

func (*CSRVector) Set

func (vec *CSRVector) Set(i, value int) SparseVector

Set sets the value at row index i and column index j to value.

func (*CSRVector) SetVec

func (vec *CSRVector) SetVec(a SparseVector, i int) SparseVector

SetVec replaces the values of this vector with the values of from vector a.

func (*CSRVector) Slice

func (vec *CSRVector) Slice(i, length int) SparseVector

Slice creates a slice of the Vector. The slice will be connected to the original Vector, changes to one causes changes in the other.

func (*CSRVector) String

func (vec *CSRVector) String() string

String returns a string representation of this vector.

func (*CSRVector) UnmarshalJSON

func (vec *CSRVector) UnmarshalJSON(bytes []byte) error

func (*CSRVector) XOr

func (vec *CSRVector) XOr(a, b SparseVector) SparseVector

type DOKMatrix

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

func (*DOKMatrix) Add

func (mat *DOKMatrix) Add(a, b SparseMat) SparseMat

Add stores the addition of a and b in this matrix.

func (*DOKMatrix) AddCols

func (mat *DOKMatrix) AddCols(j1, j2, dest int) SparseMat

AddRows is fast row operation to add two rows and put the result in a destination row.

func (*DOKMatrix) AddRows

func (mat *DOKMatrix) AddRows(i1, i2, dest int) SparseMat

AddRows is fast row operation to add two rows and put the result in a destination row.

func (*DOKMatrix) And

func (mat *DOKMatrix) And(a, b SparseMat) SparseMat

And executes a piecewise logical AND on the two matrices and stores the values in this matrix.

func (*DOKMatrix) At

func (mat *DOKMatrix) At(i, j int) int

At returns the value at row index i and column index j.

func (*DOKMatrix) Column

func (mat *DOKMatrix) Column(j int) SparseVector

Column returns a map containing the non zero row indices as the keys and it's associated values.

func (*DOKMatrix) Dims

func (mat *DOKMatrix) Dims() (int, int)

Dims returns the dimensions of the matrix.

func (*DOKMatrix) Equals

func (mat *DOKMatrix) Equals(m SparseMat) bool

Equals return true if the m matrix has the same shape and values as this matrix.

func (*DOKMatrix) MarshalJSON

func (mat *DOKMatrix) MarshalJSON() ([]byte, error)

func (*DOKMatrix) Mul

func (mat *DOKMatrix) Mul(a, b SparseMat) SparseMat

Mul multiplies two matrices and stores the values in this matrix.

func (*DOKMatrix) Negate

func (mat *DOKMatrix) Negate() SparseMat

Negate performs a piecewise logical negation.

func (*DOKMatrix) Or

func (mat *DOKMatrix) Or(a, b SparseMat) SparseMat

Or executes a piecewise logical OR on the two matrices and stores the values in this matrix.

func (*DOKMatrix) Row

func (mat *DOKMatrix) Row(i int) SparseVector

Row returns a map containing the non zero column indices as the keys and it's associated values.

func (*DOKMatrix) Set

func (mat *DOKMatrix) Set(i, j, value int) SparseMat

Set sets the value at row index i and column index j to value.

func (*DOKMatrix) SetColumn

func (mat *DOKMatrix) SetColumn(j int, vec SparseVector) SparseMat

SetColumn sets the values in column j. The values' keys are expected to be row indices.

func (*DOKMatrix) SetMatrix

func (mat *DOKMatrix) SetMatrix(a SparseMat, iOffset, jOffset int) SparseMat

SetMatrix replaces the values of this matrix with the values of from matrix a. The shape of 'a' must be less than or equal mat. If the 'a' shape is less then iOffset and jOffset can be used to place 'a' matrix in a specific location.

func (*DOKMatrix) SetRow

func (mat *DOKMatrix) SetRow(i int, vec SparseVector) SparseMat

SetRow sets the values in row i. The values' keys are expected to be column indices.

func (*DOKMatrix) Slice

func (mat *DOKMatrix) Slice(i, j, rows, cols int) SparseMat

Slice creates a slice of the matrix. The slice will be connected to the original matrix, changes to one causes changes in the other.

func (*DOKMatrix) String

func (mat *DOKMatrix) String() string

String returns a string representation of this matrix.

func (*DOKMatrix) SwapColumns

func (mat *DOKMatrix) SwapColumns(j1, j2 int) SparseMat

func (*DOKMatrix) SwapRows

func (mat *DOKMatrix) SwapRows(i1, i2 int) SparseMat

func (*DOKMatrix) T

func (mat *DOKMatrix) T() SparseMat

T returns a matrix that is the transpose of the underlying matrix. Note the transpose is connected to matrix it is a transpose of, and changes made to one affect the other.

func (*DOKMatrix) UnmarshalJSON

func (mat *DOKMatrix) UnmarshalJSON(bytes []byte) error

func (*DOKMatrix) XOr

func (mat *DOKMatrix) XOr(a, b SparseMat) SparseMat

XOr executes a piecewise logical XOR on the two matrices and stores the values in this matrix.

func (*DOKMatrix) Zeroize

func (mat *DOKMatrix) Zeroize() SparseMat

Zeroize take the current matrix sets all values to 0.

func (*DOKMatrix) ZeroizeRange

func (mat *DOKMatrix) ZeroizeRange(i, j, rows, cols int) SparseMat

ZeroizeRange take the current matrix sets values inside the range to zero.

type DOKVector

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

func (*DOKVector) Add

func (vec *DOKVector) Add(a, b SparseVector) SparseVector

func (*DOKVector) And

func (vec *DOKVector) And(a, b SparseVector) SparseVector

func (*DOKVector) At

func (vec *DOKVector) At(i int) int

At returns the value at index i.

func (*DOKVector) Dot

func (vec *DOKVector) Dot(a SparseVector) int

func (*DOKVector) Equals

func (vec *DOKVector) Equals(v SparseVector) bool

func (*DOKVector) HammingDistance

func (vec *DOKVector) HammingDistance(a SparseVector) int

func (*DOKVector) HammingWeight

func (vec *DOKVector) HammingWeight() int

func (*DOKVector) IsZero

func (vec *DOKVector) IsZero() bool

func (*DOKVector) Len

func (vec *DOKVector) Len() int

func (*DOKVector) MarshalJSON

func (vec *DOKVector) MarshalJSON() ([]byte, error)

func (*DOKVector) MatMul

func (vec *DOKVector) MatMul(mat SparseMat, vec2 SparseVector) SparseVector

func (*DOKVector) MulMat

func (vec *DOKVector) MulMat(vec2 SparseVector, mat SparseMat) SparseVector

func (*DOKVector) Negate

func (vec *DOKVector) Negate() SparseVector

func (*DOKVector) NextSet

func (vec *DOKVector) NextSet(startingIndex int) (index int, has bool)

NextSet returns the next bit which is set starting from startingIndex, so if the startingIndex is set it will be returned, if not it will be the next bit. If no bits are found has bool will be set to false.

func (*DOKVector) NonzeroArray

func (vec *DOKVector) NonzeroArray() (indices []int)

func (*DOKVector) NonzeroMap

func (vec *DOKVector) NonzeroMap() (indexToValues map[int]int)

func (*DOKVector) Or

func (vec *DOKVector) Or(a, b SparseVector) SparseVector

func (*DOKVector) Set

func (vec *DOKVector) Set(i, value int) SparseVector

Set sets the value at row index i and column index j to value.

func (*DOKVector) SetVec

func (vec *DOKVector) SetVec(a SparseVector, i int) SparseVector

SetVec replaces the values of this vector with the values of from vector a.

func (*DOKVector) Slice

func (vec *DOKVector) Slice(i, length int) SparseVector

Slice creates a slice of the Vector. The slice will be connected to the original Vector, changes to one causes changes in the other.

func (*DOKVector) String

func (vec *DOKVector) String() string

String returns a string representation of this vector.

func (*DOKVector) UnmarshalJSON

func (vec *DOKVector) UnmarshalJSON(bytes []byte) error

func (*DOKVector) XOr

func (vec *DOKVector) XOr(a, b SparseVector) SparseVector

type SparseMat

type SparseMat interface {
	Add(a, b SparseMat) SparseMat
	AddRows(i1, i2, dest int) SparseMat
	And(a, b SparseMat) SparseMat
	At(i, j int) int
	Column(j int) SparseVector
	Dims() (int, int)
	Equals(m SparseMat) bool
	MarshalJSON() ([]byte, error)
	Mul(a, b SparseMat) SparseMat
	Negate() SparseMat
	Or(a, b SparseMat) SparseMat
	Row(i int) SparseVector
	Set(i, j, value int) SparseMat
	SetColumn(j int, vec SparseVector) SparseMat
	SetMatrix(a SparseMat, iOffset, jOffset int) SparseMat
	SetRow(i int, vec SparseVector) SparseMat
	Slice(i, j, rows, cols int) SparseMat
	String() string
	SwapRows(i1, i2 int) SparseMat
	SwapColumns(j1, j2 int) SparseMat
	T() SparseMat
	UnmarshalJSON(bytes []byte) error
	XOr(a, b SparseMat) SparseMat
	Zeroize() SparseMat
	ZeroizeRange(i, j, rows, cols int) SparseMat
}

func CSRIdentity

func CSRIdentity(size int) SparseMat

Identity create an identity matrix (one's on the diagonal).

func CSRMat

func CSRMat(rows, cols int, values ...int) SparseMat

CSRMat creates a new matrix with the specified number of rows and cols. If values is empty, the matrix will be zeroized. If values are not empty it must have rows*cols items. The values are expected to be 0's or 1's anything else may have unexpected behavior matrix's methods.

func CSRMatCopy

func CSRMatCopy(m SparseMat) SparseMat

Copy will create a NEW matrix that will have all the same values as m.

func CSRMatFromVec

func CSRMatFromVec(vec SparseVector) SparseMat

func CSRMatRandom

func CSRMatRandom(rows, cols int) SparseMat

CSRRandom creates a new matrix with random values

func DOKIdentity

func DOKIdentity(size int) SparseMat

Identity create an identity matrix (one's on the diagonal).

func DOKMat

func DOKMat(rows, cols int, values ...int) SparseMat

NewMat creates a new matrix with the specified number of rows and cols. If values is empty, the matrix will be zeroized. If values are not empty it must have rows*cols items. The values are expected to be 0's or 1's anything else may have unexpected behavior matrix's methods.

func DOKMatCopy

func DOKMatCopy(m SparseMat) SparseMat

Copy will create a NEW matrix that will have all the same values as m.

func DOKMatFromVec

func DOKMatFromVec(vec SparseVector) SparseMat

func DOKMatRandom

func DOKMatRandom(rows, cols int) SparseMat

DOKRandom creates a new matrix with random values

type SparseVector

type SparseVector interface {
	Add(a, b SparseVector) SparseVector
	And(a, b SparseVector) SparseVector
	At(i int) int
	Dot(a SparseVector) int
	Equals(v SparseVector) bool
	HammingDistance(a SparseVector) int
	HammingWeight() int
	IsZero() bool
	Len() int
	MarshalJSON() ([]byte, error)
	MatMul(mat SparseMat, vec SparseVector) SparseVector
	MulMat(vec SparseVector, mat SparseMat) SparseVector
	Negate() SparseVector
	NonzeroMap() (indicesToValues map[int]int)
	NonzeroArray() (indices []int)
	NextSet(startingIndex int) (index int, has bool)
	Or(a, b SparseVector) SparseVector
	Set(i, value int) SparseVector
	SetVec(a SparseVector, i int) SparseVector
	Slice(i, length int) SparseVector
	String() string
	XOr(a, b SparseVector) SparseVector
	UnmarshalJSON(bytes []byte) error
}

func CSRVec

func CSRVec(length int, values ...int) SparseVector

func CSRVecCopy

func CSRVecCopy(a SparseVector) SparseVector

func DOKVec

func DOKVec(length int, values ...int) SparseVector

func DOKVecCopy

func DOKVecCopy(a SparseVector) SparseVector

Jump to

Keyboard shortcuts

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