sparse

package
v0.0.0-...-c04549c Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDimensionMismatch = errors.New("dimension mismatch")

ErrDimensionMismatch signals a dimension mismatch between related data structures, ex: a local trust matrix and a pre-trust vector.

View Source
var ErrZeroSum = errors.New("zero sum")

ErrZeroSum signals that an input vector's components sum to zero.

Functions

func Filter

func Filter[T any](slice []T, pred func(T) bool) []T

Filter returns the slice elements for which the predicate evaluates true.

func NilIfEmpty

func NilIfEmpty[T any](slice []T) []T

NilIfEmpty returns the given slice, except if empty, it returns nil.

func VecDot

func VecDot(v1, v2 *Vector) float64

VecDot computes the dot product of the two given sparse vectors.

Types

type CSCEntriesSort

type CSCEntriesSort []CooEntry

CSCEntriesSort sorts CooEntry objects by (column, row) key.

func (CSCEntriesSort) Len

func (a CSCEntriesSort) Len() int

func (CSCEntriesSort) Less

func (a CSCEntriesSort) Less(i, j int) bool

func (CSCEntriesSort) Swap

func (a CSCEntriesSort) Swap(i, j int)

type CSCMatrix

type CSCMatrix struct {
	CSMatrix
}

CSCMatrix is a compressed sparse column matrix.

func (*CSCMatrix) ColumnVector

func (m *CSCMatrix) ColumnVector(index int) *Vector

ColumnVector returns the given row as a sparse vector. The returned vector shares the same entry objects.

func (*CSCMatrix) Dims

func (m *CSCMatrix) Dims() (rows, cols int)

Dims returns the numbers of rows/columns.

func (*CSCMatrix) SetDim

func (m *CSCMatrix) SetDim(rows, cols int)

SetDim grows/shrinks the receiver in-place, so it contains the specified number of rows/columns.

func (*CSCMatrix) Transpose

func (m *CSCMatrix) Transpose(ctx context.Context) (*CSCMatrix, error)

Transpose transposes the matrix.

func (*CSCMatrix) TransposeToCSR

func (m *CSCMatrix) TransposeToCSR() *CSRMatrix

TransposeToCSR transposes the matrix. The returned matrix shares the same entry objects.

type CSMatrix

type CSMatrix struct {
	MajorDim, MinorDim int
	Entries            [][]Entry
	// contains filtered or unexported fields
}

CSMatrix is a compressed sparse matrix. Used as the base of CSRMatrix and CSCMatrix.

(Shallow-)copying CSMatrix is lightweight.

func (*CSMatrix) Dim

func (m *CSMatrix) Dim() (int, error)

Dim asserts the receiver is a square matrix and returns the dimension.

func (*CSMatrix) Merge

func (m *CSMatrix) Merge(m2 *CSMatrix)

Merge merges the given matrix (m2) into the receiver.

If both m and m2 contain an entry at the same location, m2's entry wins.

m2 is reset after merge.

func (*CSMatrix) Mmap

func (m *CSMatrix) Mmap(ctx context.Context) error

Mmap swaps out contents onto a temp file and mmap-s it, freeing core memory.

If the receiver (m) is Mmap()-ed, future operations on it that replaces its major spans will not automatically map the replaced major spans. If needed, Mmap() can be called again on the same receiver in order to refresh the mapping and ensure that the entirety of the receiver's contents is swapped out. (In this case, Munmap() need not be called first.)

func (*CSMatrix) Munmap

func (m *CSMatrix) Munmap() error

func (*CSMatrix) NNZ

func (m *CSMatrix) NNZ() (nnz int)

NNZ counts nonzero entries.

func (*CSMatrix) Reset

func (m *CSMatrix) Reset()

Reset resets the receiver to be empty (0x0).

func (*CSMatrix) SetMajorDim

func (m *CSMatrix) SetMajorDim(dim int)

SetMajorDim grows/shrinks the receiver in-place, so it matches the given major dimension.

func (*CSMatrix) SetMinorDim

func (m *CSMatrix) SetMinorDim(dim int)

SetMinorDim grows/shrinks the receiver in-place, so it matches the given minor dimension.

func (*CSMatrix) Transpose

func (m *CSMatrix) Transpose(ctx context.Context) (*CSMatrix, error)

Transpose transposes the sparse matrix.

type CSREntriesSort

type CSREntriesSort []CooEntry

CSREntriesSort sorts CooEntry objects by (row, column) key.

func (CSREntriesSort) Len

func (a CSREntriesSort) Len() int

func (CSREntriesSort) Less

func (a CSREntriesSort) Less(i, j int) bool

func (CSREntriesSort) Swap

func (a CSREntriesSort) Swap(i, j int)

type CSRMatrix

type CSRMatrix struct {
	CSMatrix
}

CSRMatrix is a compressed sparse row matrix.

func NewCSRMatrix

func NewCSRMatrix(
	rows, cols int,
	entries []CooEntry,
) *CSRMatrix

NewCSRMatrix creates a new compressed sparse row matrix with the given dimension and entries.

The given entries are sorted in row-column order.

func (*CSRMatrix) Dims

func (m *CSRMatrix) Dims() (rows, cols int)

Dims returns the numbers of rows/columns.

func (*CSRMatrix) RowVector

func (m *CSRMatrix) RowVector(index int) *Vector

RowVector returns the given row as a sparse vector. The returned vector shares the same slice of entry objects.

func (*CSRMatrix) SetDim

func (m *CSRMatrix) SetDim(rows, cols int)

SetDim grows/shrinks the receiver in-place, so it contains the specified number of rows/columns.

func (*CSRMatrix) SetRowVector

func (m *CSRMatrix) SetRowVector(index int, vector *Vector)

SetRowVector replaces the given row. The receiver shares the same slice of entry objects.

func (*CSRMatrix) Transpose

func (m *CSRMatrix) Transpose(ctx context.Context) (*CSRMatrix, error)

Transpose transposes the matrix.

func (*CSRMatrix) TransposeToCSC

func (m *CSRMatrix) TransposeToCSC() *CSCMatrix

TransposeToCSC transposes the matrix. The returned matrix shares the same entry objects.

type CooEntry

type CooEntry struct {
	Row, Column int
	Value       float64
}

CooEntry is a sparse matrix coordinate-format ("Coo") entry. Used as an input to a sparse matrix builder.

type EntriesByIndex

type EntriesByIndex []Entry

func (EntriesByIndex) Len

func (a EntriesByIndex) Len() int

func (EntriesByIndex) Less

func (a EntriesByIndex) Less(i, j int) bool

func (EntriesByIndex) Swap

func (a EntriesByIndex) Swap(i, j int)

type EntriesByValue

type EntriesByValue []Entry

func (EntriesByValue) Len

func (a EntriesByValue) Len() int

func (EntriesByValue) Less

func (a EntriesByValue) Less(i, j int) bool

func (EntriesByValue) Swap

func (a EntriesByValue) Swap(i, j int)

type Entry

type Entry struct {
	// Index is the index of the entry.
	// Context decides the meaning: For example, it is a column index when used
	// as a compressed sparse row (CSR) matrix.
	Index int

	// Value is the entry value.  For sparse use, it should be nonzero.
	Value float64
}

Entry is an entry in a sparse vector or matrix.

func SortEntriesByIndex

func SortEntriesByIndex(entries []Entry) []Entry

func SortEntriesByValue

func SortEntriesByValue(entries []Entry) []Entry

type KBNSummer

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

KBNSummer is the Kahan-Babushka-Neumaier compensated summation algorithm.

func (*KBNSummer) Add

func (s *KBNSummer) Add(value float64)

func (*KBNSummer) Sum

func (s *KBNSummer) Sum() float64

type Matrix

type Matrix = CSRMatrix

Matrix is just an alias of CSRMatrix, which is the more popular of the two compressed sparse variants.

type Vector

type Vector struct {
	// Dim is the dimension of the vector.
	Dim int

	// Entries contain sparse entries, sorted by their Entry.Index.
	// For each Entry in Entries, 0 <= Entry.Index < Dim holds.
	Entries []Entry
}

Vector is a sparse vector.

func NewVector

func NewVector(dim int, entries []Entry) *Vector

NewVector creates and returns a new sparse vector with given entries.

func (*Vector) AddVec

func (v *Vector) AddVec(v1, v2 *Vector) error

AddVec stores v1 + v2 into the receiver.

func (*Vector) Assign

func (v *Vector) Assign(v1 *Vector)

Assign clones (copies) the given vector into the receiver.

func (*Vector) Clone

func (v *Vector) Clone() *Vector

Clone returns a copy (clone) of the receiver.

func (*Vector) MulVec

func (v *Vector) MulVec(
	ctx context.Context, m *Matrix, v1 *Vector,
) error

MulVec stores m multiplied by v1 into the receiver.

func (*Vector) NNZ

func (v *Vector) NNZ() int

NNZ returns the number of non-zero entries.

func (*Vector) Norm2

func (v *Vector) Norm2() float64

Norm2 returns the Frobenius norm (sqrt of sum of elements).

func (*Vector) ScaleVec

func (v *Vector) ScaleVec(a float64, v1 *Vector)

ScaleVec scales the vector and stores the result into the receiver.

func (*Vector) SetDim

func (v *Vector) SetDim(dim int)

SetDim grows/shrinks the receiver in-place to match the given dimension.

func (*Vector) SubVec

func (v *Vector) SubVec(v1, v2 *Vector) error

SubVec stores v1 - v2 into the receiver.

func (*Vector) Sum

func (v *Vector) Sum() float64

Sum computes the sum of all vector elements.

Jump to

Keyboard shortcuts

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