#include <matrix-common.h>
Public Member Functions | |
MatrixIndexT | NumRows () const |
MatrixIndexT | NumCols () const |
MatrixIndexT | NumElements () const |
Real | Sum () const |
Real | FrobeniusNorm () const |
SparseMatrix (const MatrixBase< Real > &mat) | |
This constructor creates a SparseMatrix that just contains the nonzero elements of 'mat'. More... | |
template<class OtherReal > | |
void | CopyToMat (MatrixBase< OtherReal > *other, MatrixTransposeType t=kNoTrans) const |
Copy to matrix. It must already have the correct size. More... | |
void | CopyElementsToVec (VectorBase< Real > *other) const |
Copies the values of all the elements in SparseMatrix into a VectorBase object. More... | |
template<class OtherReal > | |
void | CopyFromSmat (const SparseMatrix< OtherReal > &other, MatrixTransposeType trans=kNoTrans) |
Copies data from another sparse matrix. More... | |
void | AddToMat (BaseFloat alpha, MatrixBase< Real > *other, MatrixTransposeType t=kNoTrans) const |
Does *other = *other + alpha * *this. More... | |
SparseMatrix< Real > & | operator= (const SparseMatrix< Real > &other) |
SparseMatrix (const SparseMatrix< Real > &other, MatrixTransposeType trans=kNoTrans) | |
void | Swap (SparseMatrix< Real > *other) |
SparseVector< Real > * | Data () |
const SparseVector< Real > * | Data () const |
SparseMatrix (int32 dim, const std::vector< std::vector< std::pair< MatrixIndexT, Real > > > &pairs) | |
void | SetRandn (BaseFloat zero_prob) |
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed- mostly for purposes of testing. More... | |
void | Write (std::ostream &os, bool binary) const |
void | Read (std::istream &os, bool binary) |
const SparseVector< Real > & | Row (MatrixIndexT r) const |
void | SetRow (int32 r, const SparseVector< Real > &vec) |
Sets row r to "vec"; makes sure it has the correct dimension. More... | |
void | SelectRows (const std::vector< int32 > &row_indexes, const SparseMatrix< Real > &smat_other) |
Select a subset of the rows of a SparseMatrix. More... | |
void | AppendSparseMatrixRows (std::vector< SparseMatrix< Real > > *inputs) |
Sets *this to all the rows of *inputs appended together; this function is destructive of the inputs. More... | |
SparseMatrix () | |
SparseMatrix (int32 num_rows, int32 num_cols) | |
SparseMatrix (const std::vector< int32 > &indexes, int32 dim, MatrixTransposeType trans=kNoTrans) | |
Constructor from an array of indexes. More... | |
SparseMatrix (const std::vector< int32 > &indexes, const VectorBase< Real > &weights, int32 dim, MatrixTransposeType trans=kNoTrans) | |
Constructor from an array of indexes and an array of weights; requires indexes.Dim() == weights.Dim(). More... | |
void | Resize (MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero) |
Resizes the matrix; analogous to Matrix::Resize(). More... | |
void | Scale (Real alpha) |
Scale all elements in sparse matrix. More... | |
Private Attributes | |
std::vector< SparseVector< Real > > | rows_ |
Definition at line 65 of file matrix-common.h.
|
explicit |
This constructor creates a SparseMatrix that just contains the nonzero elements of 'mat'.
Definition at line 694 of file sparse-matrix.cc.
|
inline |
Definition at line 166 of file sparse-matrix.h.
SparseMatrix | ( | int32 | dim, |
const std::vector< std::vector< std::pair< MatrixIndexT, Real > > > & | pairs | ||
) |
Definition at line 618 of file sparse-matrix.cc.
|
inline |
Definition at line 215 of file sparse-matrix.h.
|
inline |
Definition at line 217 of file sparse-matrix.h.
SparseMatrix | ( | const std::vector< int32 > & | indexes, |
int32 | dim, | ||
MatrixTransposeType | trans = kNoTrans |
||
) |
Constructor from an array of indexes.
If trans == kNoTrans, construct a sparse matrix with num-rows == indexes.Dim() and num-cols = 'dim'. 'indexes' is expected to contain elements in the range [0, dim - 1]. Each row 'i' of *this after calling the constructor will contain a single element at column-index indexes[i] with value 1.0.
If trans == kTrans, the result will be the transpose of the sparse matrix described above.
Definition at line 566 of file sparse-matrix.cc.
SparseMatrix | ( | const std::vector< int32 > & | indexes, |
const VectorBase< Real > & | weights, | ||
int32 | dim, | ||
MatrixTransposeType | trans = kNoTrans |
||
) |
Constructor from an array of indexes and an array of weights; requires indexes.Dim() == weights.Dim().
If trans == kNoTrans, construct a sparse matrix with num-rows == indexes.Dim() and num-cols = 'dim'. 'indexes' is expected to contain elements in the range [0, dim - 1]. Each row 'i' of *this after calling the constructor will contain a single element at column-index indexes[i] with value weights[i]. If trans == kTrans, the result will be the transpose of the sparse matrix described above.
Definition at line 585 of file sparse-matrix.cc.
void AddToMat | ( | BaseFloat | alpha, |
MatrixBase< Real > * | other, | ||
MatrixTransposeType | t = kNoTrans |
||
) | const |
Does *other = *other + alpha * *this.
Definition at line 496 of file sparse-matrix.cc.
Referenced by kaldi::UnitTestSparseMatrixAddToMat().
void AppendSparseMatrixRows | ( | std::vector< SparseMatrix< Real > > * | inputs | ) |
Sets *this to all the rows of *inputs appended together; this function is destructive of the inputs.
Requires, obviously, that the inputs all have the same dimension (although some may be empty).
Definition at line 656 of file sparse-matrix.cc.
Referenced by kaldi::AppendGeneralMatrixRows().
void CopyElementsToVec | ( | VectorBase< Real > * | other | ) | const |
Copies the values of all the elements in SparseMatrix into a VectorBase object.
Definition at line 391 of file sparse-matrix.cc.
template void CopyFromSmat | ( | const SparseMatrix< OtherReal > & | other, |
MatrixTransposeType | trans = kNoTrans |
||
) |
Copies data from another sparse matrix.
Definition at line 406 of file sparse-matrix.cc.
Referenced by SparseMatrix< float >::CopyFromSmat(), and CuSparseMatrix< Real >::CopyToSmat().
template void CopyToMat | ( | MatrixBase< OtherReal > * | other, |
MatrixTransposeType | t = kNoTrans |
||
) | const |
Copy to matrix. It must already have the correct size.
Definition at line 352 of file sparse-matrix.cc.
Referenced by CuMatrixBase< float >::CopyFromGeneralMat(), SparseMatrix< float >::CopyToMat(), kaldi::UnitTestCuSparseMatrixConstructFromIndexes(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestSparseMatrixAddToMat(), kaldi::UnitTestSparseMatrixConstructor(), kaldi::UnitTestSparseMatrixFrobeniusNorm(), kaldi::UnitTestSparseMatrixSum(), and kaldi::UnitTestSparseMatrixTraceMatSmat().
SparseVector< Real > * Data | ( | ) |
Definition at line 314 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::CopyFromSmat().
const SparseVector< Real > * Data | ( | ) | const |
Definition at line 322 of file sparse-matrix.cc.
Real FrobeniusNorm | ( | ) | const |
Definition at line 339 of file sparse-matrix.cc.
Referenced by kaldi::UnitTestSparseMatrixFrobeniusNorm().
MatrixIndexT NumCols | ( | ) | const |
Definition at line 297 of file sparse-matrix.cc.
Referenced by MatrixBase< float >::AddMatSmat(), MatrixBase< float >::AddSmat(), MatrixBase< float >::AddSmatMat(), CuSparseMatrix< Real >::CopyFromSmat(), SparseMatrix< float >::CopyFromSmat(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterSparseMatrixRows(), SparseMatrix< float >::SelectRows(), kaldi::TraceMatSmat(), kaldi::UnitTestCuSparseMatrixConstructFromIndexes(), and kaldi::UnitTestCuSparseMatrixSwap().
MatrixIndexT NumElements | ( | ) | const |
Definition at line 305 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::CopyFromSmat().
MatrixIndexT NumRows | ( | ) | const |
Definition at line 292 of file sparse-matrix.cc.
Referenced by MatrixBase< float >::AddMatSmat(), MatrixBase< float >::AddSmat(), MatrixBase< float >::AddSmatMat(), CuSparseMatrix< Real >::CopyFromSmat(), SparseMatrix< float >::CopyFromSmat(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterSparseMatrixRows(), kaldi::TraceMatSmat(), kaldi::UnitTestCuSparseMatrixConstructFromIndexes(), and kaldi::UnitTestCuSparseMatrixSwap().
SparseMatrix< Real > & operator= | ( | const SparseMatrix< Real > & | other | ) |
Definition at line 606 of file sparse-matrix.cc.
void Read | ( | std::istream & | os, |
bool | binary | ||
) |
Definition at line 467 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::Read().
void Resize | ( | MatrixIndexT | rows, |
MatrixIndexT | cols, | ||
MatrixResizeType | resize_type = kSetZero |
||
) |
Resizes the matrix; analogous to Matrix::Resize().
resize_type == kUndefined behaves the same as kSetZero.
Definition at line 637 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::CopyToSmat(), and kaldi::FilterSparseMatrixRows().
const SparseVector< Real > & Row | ( | MatrixIndexT | r | ) | const |
Definition at line 543 of file sparse-matrix.cc.
Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), MatrixBase< float >::AddMatSmat(), MatrixBase< float >::AddSmat(), MatrixBase< float >::AddSmatMat(), kaldi::nnet3::ComputeAccuracy(), SparseMatrix< float >::CopyFromSmat(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterSparseMatrixRows(), SparseMatrix< float >::SelectRows(), and kaldi::TraceMatSmat().
void Scale | ( | Real | alpha | ) |
Scale all elements in sparse matrix.
Definition at line 687 of file sparse-matrix.cc.
void SelectRows | ( | const std::vector< int32 > & | row_indexes, |
const SparseMatrix< Real > & | smat_other | ||
) |
Select a subset of the rows of a SparseMatrix.
Sets *this to only the rows of 'smat_other' that are listed in 'row_indexes'. 'row_indexes' must satisfy 0 <= row_indexes[i] < smat_other.NumRows().
Definition at line 557 of file sparse-matrix.cc.
Referenced by kaldi::UnitTestCuSparseMatrixSelectRowsAndTranspose().
void SetRandn | ( | BaseFloat | zero_prob | ) |
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed- mostly for purposes of testing.
Definition at line 630 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::SetRandn(), kaldi::UnitTestCuSparseMatrixCopyToSmat(), kaldi::UnitTestCuSparseMatrixFrobeniusNorm(), kaldi::UnitTestCuSparseMatrixSelectRowsAndTranspose(), kaldi::UnitTestCuSparseMatrixSum(), kaldi::UnitTestCuSparseMatrixSwap(), kaldi::UnitTestCuSparseMatrixTraceMatSmat(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestMatrixAddMatSmat(), kaldi::UnitTestMatrixAddSmatMat(), kaldi::UnitTestSparseMatrixAddToMat(), kaldi::UnitTestSparseMatrixFrobeniusNorm(), kaldi::UnitTestSparseMatrixSum(), kaldi::UnitTestSparseMatrixTraceMatSmat(), kaldi::UnitTextCuMatrixAddMatSmat(), kaldi::UnitTextCuMatrixAddSmat(), and kaldi::UnitTextCuMatrixAddSmatMat().
void SetRow | ( | int32 | r, |
const SparseVector< Real > & | vec | ||
) |
Sets row r to "vec"; makes sure it has the correct dimension.
Definition at line 549 of file sparse-matrix.cc.
Referenced by kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterSparseMatrixRows().
Real Sum | ( | ) | const |
void Swap | ( | SparseMatrix< Real > * | other | ) |
Definition at line 613 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::CopyToSmat(), and GeneralMatrix::SwapSparseMatrix().
void Write | ( | std::ostream & | os, |
bool | binary | ||
) | const |
Definition at line 443 of file sparse-matrix.cc.
Referenced by CuSparseMatrix< Real >::Write().
|
private |
Definition at line 261 of file sparse-matrix.h.
Referenced by SparseMatrix< float >::operator=(), and SparseMatrix< float >::Swap().