template<typename T = double>
liarsdice::data_structures::SparseMatrix class

High-performance sparse matrix for game analytics.

Template parameters
T Element type (default: double)

Uses boost::numeric::ublas::compressed_matrix for efficient storage of sparse data like player interaction patterns and game statistics.

Public types

using value_type = T
using matrix_type = ublas::compressed_matrix<T>
using size_type = typename matrix_type::size_type

Constructors, destructors, conversion operators

SparseMatrix(size_type rows, size_type cols, size_type non_zeros = 0)
Construct sparse matrix with dimensions.

Public functions

void set(size_type row, size_type col, const T& value)
Set element value.
auto get(size_type row, size_type col) const -> T
Get element value.
void increment(size_type row, size_type col, const T& delta = T{1})
Increment element value.
auto get_row(size_type row) const -> std::vector<T>
Get row as dense vector.
auto get_column(size_type col) const -> std::vector<T>
Get column as dense vector.
auto row_sums() const -> std::vector<T>
Calculate row sums.
auto column_sums() const -> std::vector<T>
Calculate column sums.
template<typename Func>
void for_each_non_zero(Func func) const
Apply function to all non-zero elements.
auto find_top_n(size_type n) const -> std::vector<std::tuple<size_type, size_type, T>>
Find top N elements by value.
auto normalize_rows() const -> SparseMatrix
Normalize matrix rows to sum to 1.
auto operator*(const SparseMatrix& other) const -> SparseMatrix
Matrix multiplication.
auto rows() const -> size_type
auto cols() const -> size_type
auto non_zeros() const -> size_type
auto sparsity() const -> double
void clear()
auto data() const -> const matrix_type&
auto data() -> matrix_type&

Function documentation

template<typename T>
liarsdice::data_structures::SparseMatrix<T>::SparseMatrix(size_type rows, size_type cols, size_type non_zeros = 0)

Construct sparse matrix with dimensions.

Parameters
rows Number of rows
cols Number of columns
non_zeros Expected number of non-zero elements (hint)

template<typename T>
void liarsdice::data_structures::SparseMatrix<T>::set(size_type row, size_type col, const T& value)

Set element value.

Parameters
row Row index
col Column index
value Value to set

template<typename T>
T liarsdice::data_structures::SparseMatrix<T>::get(size_type row, size_type col) const

Get element value.

Parameters
row Row index
col Column index
Returns Element value (zero if not stored)

template<typename T>
void liarsdice::data_structures::SparseMatrix<T>::increment(size_type row, size_type col, const T& delta = T{1})

Increment element value.

Parameters
row Row index
col Column index
delta Value to add

template<typename T>
std::vector<T> liarsdice::data_structures::SparseMatrix<T>::get_row(size_type row) const

Get row as dense vector.

Parameters
row Row index
Returns Vector containing row values

template<typename T>
std::vector<T> liarsdice::data_structures::SparseMatrix<T>::get_column(size_type col) const

Get column as dense vector.

Parameters
col Column index
Returns Vector containing column values

template<typename T>
std::vector<T> liarsdice::data_structures::SparseMatrix<T>::row_sums() const

Calculate row sums.

Returns Vector of row sums

template<typename T>
std::vector<T> liarsdice::data_structures::SparseMatrix<T>::column_sums() const

Calculate column sums.

Returns Vector of column sums

template<typename T> template<typename Func>
void liarsdice::data_structures::SparseMatrix<T>::for_each_non_zero(Func func) const

Apply function to all non-zero elements.

Template parameters
Func Function type
Parameters
func Function to apply (row, col, value)

template<typename T>
std::vector<std::tuple<size_type, size_type, T>> liarsdice::data_structures::SparseMatrix<T>::find_top_n(size_type n) const

Find top N elements by value.

Parameters
n Number of elements to find
Returns Vector of (row, col, value) tuples

template<typename T>
SparseMatrix liarsdice::data_structures::SparseMatrix<T>::normalize_rows() const

Normalize matrix rows to sum to 1.

Returns New normalized matrix

template<typename T>
SparseMatrix liarsdice::data_structures::SparseMatrix<T>::operator*(const SparseMatrix& other) const

Matrix multiplication.

Parameters
other Other sparse matrix
Returns Result of matrix multiplication