SparseMatrix< Real > Class Template Reference

#include <matrix-common.h>

Collaboration diagram for SparseMatrix< Real >:

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_
 

Detailed Description

template<typename Real>
class kaldi::SparseMatrix< Real >

Definition at line 65 of file matrix-common.h.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/7]

SparseMatrix ( const MatrixBase< Real > &  mat)
explicit

This constructor creates a SparseMatrix that just contains the nonzero elements of 'mat'.

Definition at line 694 of file sparse-matrix.cc.

694  {
695  MatrixIndexT num_rows = mat.NumRows();
696  rows_.resize(num_rows);
697  for (int32 row = 0; row < num_rows; row++) {
698  SparseVector<Real> this_row(mat.Row(row));
699  rows_[row].Swap(&this_row);
700  }
701 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
int32 MatrixIndexT
Definition: matrix-common.h:98

◆ SparseMatrix() [2/7]

SparseMatrix ( const SparseMatrix< Real > &  other,
MatrixTransposeType  trans = kNoTrans 
)
inline

Definition at line 166 of file sparse-matrix.h.

167  {
168  this->CopyFromSmat(other, trans);
169  }
void CopyFromSmat(const SparseMatrix< OtherReal > &other, MatrixTransposeType trans=kNoTrans)
Copies data from another sparse matrix.

◆ SparseMatrix() [3/7]

SparseMatrix ( int32  dim,
const std::vector< std::vector< std::pair< MatrixIndexT, Real > > > &  pairs 
)

Definition at line 618 of file sparse-matrix.cc.

620  :
621  rows_(pairs.size()) {
622  MatrixIndexT num_rows = pairs.size();
623  for (MatrixIndexT row = 0; row < num_rows; row++) {
624  SparseVector<Real> svec(dim, pairs[row]);
625  rows_[row].Swap(&svec);
626  }
627 }
std::vector< SparseVector< Real > > rows_
int32 MatrixIndexT
Definition: matrix-common.h:98

◆ SparseMatrix() [4/7]

SparseMatrix ( )
inline

Definition at line 215 of file sparse-matrix.h.

215 { }

◆ SparseMatrix() [5/7]

SparseMatrix ( int32  num_rows,
int32  num_cols 
)
inline

Definition at line 217 of file sparse-matrix.h.

217 { Resize(num_rows, num_cols); }
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
Resizes the matrix; analogous to Matrix::Resize().

◆ SparseMatrix() [6/7]

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.

567  {
568  const std::vector<int32>& idx = indexes;
569  std::vector<std::vector<std::pair<MatrixIndexT, Real> > > pair(idx.size());
570  for (int i = 0; i < idx.size(); ++i) {
571  if (idx[i] >= 0) {
572  pair[i].push_back( { idx[i], Real(1) });
573  }
574  }
575  SparseMatrix<Real> smat_cpu(dim, pair);
576  if (trans == kNoTrans) {
577  this->Swap(&smat_cpu);
578  } else {
579  SparseMatrix<Real> tmp(smat_cpu, kTrans);
580  this->Swap(&tmp);
581  }
582 }
void Swap(SparseMatrix< Real > *other)

◆ SparseMatrix() [7/7]

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.

587  {
588  const std::vector<int32>& idx = indexes;
589  const VectorBase<Real>& w = weights;
590  std::vector<std::vector<std::pair<MatrixIndexT, Real> > > pair(idx.size());
591  for (int i = 0; i < idx.size(); ++i) {
592  if (idx[i] >= 0) {
593  pair[i].push_back( { idx[i], w(i) });
594  }
595  }
596  SparseMatrix<Real> smat_cpu(dim, pair);
597  if (trans == kNoTrans) {
598  this->Swap(&smat_cpu);
599  } else {
600  SparseMatrix<Real> tmp(smat_cpu, kTrans);
601  this->Swap(&tmp);
602  }
603 }
void Swap(SparseMatrix< Real > *other)

Member Function Documentation

◆ AddToMat()

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().

498  {
499  if (trans == kNoTrans) {
500  MatrixIndexT num_rows = rows_.size();
501  KALDI_ASSERT(other->NumRows() == num_rows);
502  for (MatrixIndexT i = 0; i < num_rows; i++) {
503  SubVector<Real> vec(*other, i);
504  rows_[i].AddToVec(alpha, &vec);
505  }
506  } else {
507  Real *other_col_data = other->Data();
508  MatrixIndexT other_stride = other->Stride(),
509  num_rows = NumRows(), num_cols = NumCols();
510  KALDI_ASSERT(num_rows == other->NumCols() && num_cols == other->NumRows());
511  for (MatrixIndexT row = 0; row < num_rows; row++, other_col_data++) {
512  const SparseVector<Real> &svec = rows_[row];
513  MatrixIndexT num_elems = svec.NumElements();
514  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
515  for (MatrixIndexT e = 0; e < num_elems; e++)
516  other_col_data[sdata[e].first * other_stride] +=
517  alpha * sdata[e].second;
518  }
519  }
520 }
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const

◆ AppendSparseMatrixRows()

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().

657  {
658  rows_.clear();
659  size_t num_rows = 0;
660  typename std::vector<SparseMatrix<Real> >::iterator
661  input_iter = inputs->begin(),
662  input_end = inputs->end();
663  for (; input_iter != input_end; ++input_iter)
664  num_rows += input_iter->rows_.size();
665  rows_.resize(num_rows);
666  typename std::vector<SparseVector<Real> >::iterator
667  row_iter = rows_.begin(),
668  row_end = rows_.end();
669  for (input_iter = inputs->begin(); input_iter != input_end; ++input_iter) {
670  typename std::vector<SparseVector<Real> >::iterator
671  input_row_iter = input_iter->rows_.begin(),
672  input_row_end = input_iter->rows_.end();
673  for (; input_row_iter != input_row_end; ++input_row_iter, ++row_iter)
674  row_iter->Swap(&(*input_row_iter));
675  }
676  KALDI_ASSERT(row_iter == row_end);
677  int32 num_cols = NumCols();
678  for (row_iter = rows_.begin(); row_iter != row_end; ++row_iter) {
679  if (row_iter->Dim() != num_cols)
680  KALDI_ERR << "Appending rows with inconsistent dimensions, "
681  << row_iter->Dim() << " vs. " << num_cols;
682  }
683  inputs->clear();
684 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyElementsToVec()

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.

391  {
392  KALDI_ASSERT(other->Dim() == NumElements());
393  Real *dst_data = other->Data();
394  int32 dst_index = 0;
395  for (int32 i = 0; i < rows_.size(); ++i) {
396  for (int32 j = 0; j < rows_[i].NumElements(); ++j) {
397  dst_data[dst_index] =
398  static_cast<Real>(rows_[i].GetElement(j).second);
399  dst_index++;
400  }
401  }
402 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumElements() const

◆ CopyFromSmat()

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().

407  {
408  if (trans == kNoTrans) {
409  rows_.resize(other.NumRows());
410  if (rows_.size() == 0)
411  return;
412  for (int32 r = 0; r < rows_.size(); ++r) {
413  rows_[r].CopyFromSvec(other.Row(r));
414  }
415  } else {
416  std::vector<std::vector<std::pair<MatrixIndexT, Real> > > pairs(
417  other.NumCols());
418  for (MatrixIndexT i = 0; i < other.NumRows(); ++i) {
419  for (int id = 0; id < other.Row(i).NumElements(); ++id) {
420  MatrixIndexT j = other.Row(i).GetElement(id).first;
421  Real v = static_cast<Real>(other.Row(i).GetElement(id).second);
422  pairs[j].push_back( { i, v });
423  }
424  }
425  SparseMatrix<Real> temp(other.NumRows(), pairs);
426  Swap(&temp);
427  }
428 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
int32 MatrixIndexT
Definition: matrix-common.h:98
void Swap(SparseMatrix< Real > *other)

◆ CopyToMat()

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().

353  {
354  if (trans == kNoTrans) {
355  MatrixIndexT num_rows = rows_.size();
356  KALDI_ASSERT(other->NumRows() == num_rows);
357  for (MatrixIndexT i = 0; i < num_rows; i++) {
358  SubVector<OtherReal> vec(*other, i);
359  rows_[i].CopyElementsToVec(&vec);
360  }
361  } else {
362  OtherReal *other_col_data = other->Data();
363  MatrixIndexT other_stride = other->Stride(),
364  num_rows = NumRows(), num_cols = NumCols();
365  KALDI_ASSERT(num_rows == other->NumCols() && num_cols == other->NumRows());
366  other->SetZero();
367  for (MatrixIndexT row = 0; row < num_rows; row++, other_col_data++) {
368  const SparseVector<Real> &svec = rows_[row];
369  MatrixIndexT num_elems = svec.NumElements();
370  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
371  for (MatrixIndexT e = 0; e < num_elems; e++)
372  other_col_data[sdata[e].first * other_stride] = sdata[e].second;
373  }
374  }
375 }
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const

◆ Data() [1/2]

SparseVector< Real > * Data ( )

Definition at line 314 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::CopyFromSmat().

314  {
315  if (rows_.empty())
316  return NULL;
317  else
318  return rows_.data();
319 }
std::vector< SparseVector< Real > > rows_

◆ Data() [2/2]

const SparseVector< Real > * Data ( ) const

Definition at line 322 of file sparse-matrix.cc.

322  {
323  if (rows_.empty())
324  return NULL;
325  else
326  return rows_.data();
327 }
std::vector< SparseVector< Real > > rows_

◆ FrobeniusNorm()

Real FrobeniusNorm ( ) const

Definition at line 339 of file sparse-matrix.cc.

Referenced by kaldi::UnitTestSparseMatrixFrobeniusNorm().

339  {
340  Real squared_sum = 0;
341  for (int32 i = 0; i < rows_.size(); ++i) {
342  const std::pair<MatrixIndexT, Real> *row_data = rows_[i].Data();
343  for (int32 j = 0; j < rows_[i].NumElements(); ++j) {
344  squared_sum += row_data[j].second * row_data[j].second;
345  }
346  }
347  return std::sqrt(squared_sum);
348 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_

◆ NumCols()

◆ NumElements()

MatrixIndexT NumElements ( ) const

Definition at line 305 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::CopyFromSmat().

305  {
306  int32 num_elements = 0;
307  for (int32 i = 0; i < rows_.size(); ++i) {
308  num_elements += rows_[i].NumElements();
309  }
310  return num_elements;
311 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_

◆ NumRows()

◆ operator=()

SparseMatrix< Real > & operator= ( const SparseMatrix< Real > &  other)

Definition at line 606 of file sparse-matrix.cc.

607  {
608  rows_ = other.rows_;
609  return *this;
610 }
std::vector< SparseVector< Real > > rows_

◆ Read()

void Read ( std::istream &  os,
bool  binary 
)

Definition at line 467 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::Read().

467  {
468  if (binary) {
469  ExpectToken(is, binary, "SM");
470  int32 num_rows;
471  ReadBasicType(is, binary, &num_rows);
472  KALDI_ASSERT(num_rows >= 0 && num_rows < 10000000);
473  rows_.resize(num_rows);
474  for (int32 row = 0; row < num_rows; row++)
475  rows_[row].Read(is, binary);
476  } else {
477  std::string str;
478  is >> str;
479  if (str.substr(0, 5) != "rows=")
480  KALDI_ERR << "Reading sparse matrix, expected 'rows=xxx', got " << str;
481  std::string rows_str = str.substr(5, std::string::npos);
482  std::istringstream rows_istr(rows_str);
483  int32 num_rows = -1;
484  rows_istr >> num_rows;
485  if (num_rows < 0 || rows_istr.fail()) {
486  KALDI_ERR << "Reading sparse vector, expected 'rows=[int]', got " << str;
487  }
488  rows_.resize(num_rows);
489  for (int32 row = 0; row < num_rows; row++)
490  rows_[row].Read(is, binary);
491  }
492 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
void Read(std::istream &os, bool binary)
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Resize()

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().

639  {
640  KALDI_ASSERT(num_rows >= 0 && num_cols >= 0);
641  if (resize_type == kSetZero || resize_type == kUndefined) {
642  rows_.clear();
643  Resize(num_rows, num_cols, kCopyData);
644  } else {
645  // Assume resize_type == kCopyData from here.
646  int32 old_num_rows = rows_.size(), old_num_cols = NumCols();
647  SparseVector<Real> initializer(num_cols);
648  rows_.resize(num_rows, initializer);
649  if (num_cols != old_num_cols)
650  for (int32 row = 0; row < old_num_rows; row++)
651  rows_[row].Resize(num_cols, kCopyData);
652  }
653 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
Resizes the matrix; analogous to Matrix::Resize().

◆ Row()

◆ Scale()

void Scale ( Real  alpha)

Scale all elements in sparse matrix.

Definition at line 687 of file sparse-matrix.cc.

687  {
688  MatrixIndexT num_rows = rows_.size();
689  for (MatrixIndexT row = 0; row < num_rows; row++)
690  rows_[row].Scale(alpha);
691 }
std::vector< SparseVector< Real > > rows_
void Scale(Real alpha)
Scale all elements in sparse matrix.
int32 MatrixIndexT
Definition: matrix-common.h:98

◆ SelectRows()

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().

558  {
559  Resize(row_indexes.size(), smat_other.NumCols());
560  for (int i = 0; i < row_indexes.size(); ++i) {
561  SetRow(i, smat_other.Row(row_indexes[i]));
562  }
563 }
void SetRow(int32 r, const SparseVector< Real > &vec)
Sets row r to "vec"; makes sure it has the correct dimension.
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
Resizes the matrix; analogous to Matrix::Resize().

◆ SetRandn()

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().

630  {
631  MatrixIndexT num_rows = rows_.size();
632  for (MatrixIndexT row = 0; row < num_rows; row++)
633  rows_[row].SetRandn(zero_prob);
634 }
std::vector< SparseVector< Real > > rows_
int32 MatrixIndexT
Definition: matrix-common.h:98
void SetRandn(BaseFloat zero_prob)
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob an...

◆ SetRow()

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().

549  {
550  KALDI_ASSERT(static_cast<size_t>(r) < rows_.size() &&
551  vec.Dim() == rows_[0].Dim());
552  rows_[r] = vec;
553 }
std::vector< SparseVector< Real > > rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Sum()

Real Sum ( ) const

Definition at line 330 of file sparse-matrix.cc.

Referenced by kaldi::UnitTestSparseMatrixSum().

330  {
331  Real sum = 0;
332  for (int32 i = 0; i < rows_.size(); ++i) {
333  sum += rows_[i].Sum();
334  }
335  return sum;
336 }
kaldi::int32 int32
std::vector< SparseVector< Real > > rows_

◆ Swap()

void Swap ( SparseMatrix< Real > *  other)

Definition at line 613 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::CopyToSmat(), and GeneralMatrix::SwapSparseMatrix().

613  {
614  rows_.swap(other->rows_);
615 }
std::vector< SparseVector< Real > > rows_

◆ Write()

void Write ( std::ostream &  os,
bool  binary 
) const

Definition at line 443 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::Write().

443  {
444  if (binary) {
445  // Note: we can use the same marker for float and double SparseMatrix,
446  // because internally we use WriteBasicType and ReadBasicType to read the
447  // floats and doubles, and this will automatically take care of type
448  // conversion.
449  WriteToken(os, binary, "SM");
450  int32 num_rows = rows_.size();
451  WriteBasicType(os, binary, num_rows);
452  for (int32 row = 0; row < num_rows; row++)
453  rows_[row].Write(os, binary);
454  } else {
455  // The format is "rows=10 dim=20 [ 1 0.4 9 1.2 ] dim=20 [ 3 1.7 19 0.6 ] ..
456  // not 100% efficient, but easy to work with, and we can re-use the
457  // read/write code from SparseVector.
458  int32 num_rows = rows_.size();
459  os << "rows=" << num_rows << " ";
460  for (int32 row = 0; row < num_rows; row++)
461  rows_[row].Write(os, binary);
462  os << "\n"; // Might make it a little more readable.
463  }
464 }
kaldi::int32 int32
void Write(std::ostream &os, bool binary) const
std::vector< SparseVector< Real > > rows_
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Member Data Documentation

◆ rows_

std::vector<SparseVector<Real> > rows_
private

The documentation for this class was generated from the following files: