GeneralMatrix Class Reference

This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<BaseFloat>, or a CompressedMatrix, or a SparseMatrix<BaseFloat>. More...

#include <sparse-matrix.h>

Collaboration diagram for GeneralMatrix:

Public Member Functions

GeneralMatrixType Type () const
 Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix. More...
 
void Compress ()
 
void Uncompress ()
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &is, bool binary)
 Note: if you write a compressed matrix in text form, it will be read as a regular full matrix. More...
 
const SparseMatrix< BaseFloat > & GetSparseMatrix () const
 Returns the contents as a SparseMatrix. More...
 
void SwapSparseMatrix (SparseMatrix< BaseFloat > *smat)
 Swaps the with the given SparseMatrix. More...
 
const CompressedMatrixGetCompressedMatrix () const
 Returns the contents as a compressed matrix. More...
 
void SwapCompressedMatrix (CompressedMatrix *cmat)
 Swaps the with the given CompressedMatrix. More...
 
const Matrix< BaseFloat > & GetFullMatrix () const
 Returns the contents as a Matrix<BaseFloat>. More...
 
void GetMatrix (Matrix< BaseFloat > *mat) const
 Outputs the contents as a matrix. More...
 
void SwapFullMatrix (Matrix< BaseFloat > *mat)
 Swaps the with the given Matrix. More...
 
void CopyToMat (MatrixBase< BaseFloat > *mat, MatrixTransposeType trans=kNoTrans) const
 Copies contents, regardless of type, to "mat", which must be correctly sized. More...
 
void CopyToMat (CuMatrixBase< BaseFloat > *cu_mat, MatrixTransposeType trans=kNoTrans) const
 Copies contents, regardless of type, to "cu_mat", which must be correctly sized. More...
 
void AddToMat (BaseFloat alpha, MatrixBase< BaseFloat > *mat, MatrixTransposeType trans=kNoTrans) const
 Adds alpha times *this to mat. More...
 
void AddToMat (BaseFloat alpha, CuMatrixBase< BaseFloat > *cu_mat, MatrixTransposeType trans=kNoTrans) const
 Adds alpha times *this to cu_mat. More...
 
void Scale (BaseFloat alpha)
 Scale each element of matrix by alpha. More...
 
GeneralMatrixoperator= (const MatrixBase< BaseFloat > &mat)
 Assignment from regular matrix. More...
 
GeneralMatrixoperator= (const CompressedMatrix &mat)
 Assignment from compressed matrix. More...
 
GeneralMatrixoperator= (const SparseMatrix< BaseFloat > &smat)
 Assignment from SparseMatrix<BaseFloat> More...
 
MatrixIndexT NumRows () const
 
MatrixIndexT NumCols () const
 
 GeneralMatrix (const MatrixBase< BaseFloat > &mat)
 
 GeneralMatrix (const CompressedMatrix &cmat)
 
 GeneralMatrix (const SparseMatrix< BaseFloat > &smat)
 
 GeneralMatrix ()
 
GeneralMatrixoperator= (const GeneralMatrix &other)
 
 GeneralMatrix (const GeneralMatrix &other)
 
void Clear ()
 
void Swap (GeneralMatrix *other)
 

Private Attributes

Matrix< BaseFloatmat_
 
CompressedMatrix cmat_
 
SparseMatrix< BaseFloatsmat_
 

Detailed Description

This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<BaseFloat>, or a CompressedMatrix, or a SparseMatrix<BaseFloat>.

It handles the I/O for you, i.e. you read and write a single object type. It is useful for neural-net training targets which might be sparse or not, and might be compressed or not.

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

Constructor & Destructor Documentation

◆ GeneralMatrix() [1/5]

GeneralMatrix ( const MatrixBase< BaseFloat > &  mat)
inlineexplicit

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

365 { *this = mat; }

◆ GeneralMatrix() [2/5]

GeneralMatrix ( const CompressedMatrix cmat)
inlineexplicit

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

367 { *this = cmat; }

◆ GeneralMatrix() [3/5]

GeneralMatrix ( const SparseMatrix< BaseFloat > &  smat)
inlineexplicit

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

369 { *this = smat; }

◆ GeneralMatrix() [4/5]

GeneralMatrix ( )
inline

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

References SparseVector< Real >::operator=().

371 { }

◆ GeneralMatrix() [5/5]

GeneralMatrix ( const GeneralMatrix other)
inline

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

References SparseVector< Real >::Swap().

375 { *this = other; }

Member Function Documentation

◆ AddToMat() [1/2]

void AddToMat ( BaseFloat  alpha,
MatrixBase< BaseFloat > *  mat,
MatrixTransposeType  trans = kNoTrans 
) const

Adds alpha times *this to mat.

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

References MatrixBase< Real >::AddMat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, and kaldi::kSparseMatrix.

1146  {
1147  switch (this->Type()) {
1148  case kFullMatrix: {
1149  mat->AddMat(alpha, mat_, trans);
1150  break;
1151  }
1152  case kSparseMatrix: {
1153  smat_.AddToMat(alpha, mat, trans);
1154  break;
1155  }
1156  case kCompressedMatrix: {
1157  Matrix<BaseFloat> temp_mat(cmat_);
1158  mat->AddMat(alpha, temp_mat, trans);
1159  break;
1160  }
1161  default:
1162  KALDI_ERR << "Invalid general-matrix type.";
1163  }
1164 }
CompressedMatrix cmat_
GeneralMatrixType Type() const
Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix.
#define KALDI_ERR
Definition: kaldi-error.h:147
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ AddToMat() [2/2]

void AddToMat ( BaseFloat  alpha,
CuMatrixBase< BaseFloat > *  cu_mat,
MatrixTransposeType  trans = kNoTrans 
) const

Adds alpha times *this to cu_mat.

Implemented in ../cudamatrix/cu-sparse-matrix.cc

Definition at line 679 of file cu-sparse-matrix.cc.

References CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::AddSmat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kSparseMatrix, and CuMatrixBase< Real >::Mat().

681  {
682  switch (Type()) {
683  case kFullMatrix: {
684 #if HAVE_CUDA == 1
685  if (CuDevice::Instantiate().Enabled()) {
686  CuMatrix<BaseFloat> cu_copy(mat_);
687  cu_mat->AddMat(alpha, cu_copy);
688  break;
689  }
690 #endif
691  cu_mat->Mat().AddMat(alpha, mat_);
692  break;
693  }
694  case kSparseMatrix: {
695 #if HAVE_CUDA == 1
696  if (CuDevice::Instantiate().Enabled()) {
697  CuSparseMatrix<BaseFloat> cu_smat(smat_);
698  cu_mat->AddSmat(alpha, cu_smat, trans);
699  break;
700  }
701 #endif
702  cu_mat->Mat().AddSmat(alpha, smat_, trans);
703  break;
704  }
705  case kCompressedMatrix: {
706  Matrix<BaseFloat> mat(cmat_);
707 #if HAVE_CUDA == 1
708  if (CuDevice::Instantiate().Enabled()) {
709  CuMatrix<BaseFloat> cu_mat_copy(mat);
710  cu_mat->AddMat(alpha, cu_mat_copy, trans);
711  break;
712  }
713 #endif
714  cu_mat->Mat().AddMat(alpha, mat, trans);
715  break;
716  }
717  default:
718  KALDI_ERR << "Invalid GeneralMatrix type.";
719  }
720 }
CompressedMatrix cmat_
GeneralMatrixType Type() const
Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix.
#define KALDI_ERR
Definition: kaldi-error.h:147
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Clear()

void Clear ( )

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

Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractObjectRange(), kaldi::FilterGeneralMatrixRows(), and kaldi::UnitTestGeneralMatrix().

740  {
741  mat_.Resize(0, 0);
742  cmat_.Clear();
743  smat_.Resize(0, 0);
744 }
CompressedMatrix cmat_
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Compress()

void Compress ( )

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

Referenced by kaldi::nnet3::GenerateSimpleNnetTrainingExample(), and kaldi::UnitTestGeneralMatrix().

802  {
803  if (mat_.NumRows() != 0) {
805  mat_.Resize(0, 0);
806  }
807 }
CompressedMatrix cmat_
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< BaseFloat > mat_
void CopyFromMat(const MatrixBase< Real > &mat, CompressionMethod method=kAutomaticMethod)
This will resize *this and copy the contents of mat to *this.

◆ CopyToMat() [1/2]

void CopyToMat ( MatrixBase< BaseFloat > *  mat,
MatrixTransposeType  trans = kNoTrans 
) const

Copies contents, regardless of type, to "mat", which must be correctly sized.

See also GetMatrix(), which will size its output for you.

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

References MatrixBase< Real >::CopyFromMat(), KALDI_ASSERT, and MatrixBase< Real >::NumRows().

Referenced by kaldi::AppendGeneralMatrixRows().

832  {
833  if (mat_.NumRows() !=0) {
834  mat->CopyFromMat(mat_, trans);
835  } else if (cmat_.NumRows() != 0) {
836  cmat_.CopyToMat(mat, trans);
837  } else if (smat_.NumRows() != 0) {
838  smat_.CopyToMat(mat, trans);
839  } else {
840  KALDI_ASSERT(mat->NumRows() == 0);
841  }
842 }
CompressedMatrix cmat_
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_
void CopyToMat(MatrixBase< Real > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents to matrix.

◆ CopyToMat() [2/2]

void CopyToMat ( CuMatrixBase< BaseFloat > *  cu_mat,
MatrixTransposeType  trans = kNoTrans 
) const

Copies contents, regardless of type, to "cu_mat", which must be correctly sized.

Implemented in ../cudamatrix/cu-sparse-matrix.cc

Definition at line 582 of file cu-sparse-matrix.cc.

References CuMatrixBase< Real >::CopyFromMat(), CuSparseMatrix< Real >::CopyToMat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kNoTrans, kaldi::kSparseMatrix, kaldi::kTrans, CuMatrixBase< Real >::Mat(), and CuMatrix< Real >::Swap().

583  {
584 #if HAVE_CUDA == 1
585  if (CuDevice::Instantiate().Enabled()) {
586  switch (Type()) {
587  case kFullMatrix: {
588  cu_mat->CopyFromMat(mat_);
589  break;
590  }
591  case kSparseMatrix: {
592  CuSparseMatrix<BaseFloat> smat(smat_);
593  smat.CopyToMat(cu_mat, trans);
594  break;
595  }
596  case kCompressedMatrix: {
597  Matrix<BaseFloat> mat(cmat_);
598  if (trans == kNoTrans) {
599  cu_mat->CopyFromMat(mat);
600  break;
601  } else {
602  CuMatrix<BaseFloat> temp_cu;
603  temp_cu.Swap(&mat);
604  cu_mat->CopyFromMat(temp_cu, kTrans);
605  break;
606  }
607  }
608  default:
609  KALDI_ERR << "Invalid GeneralMatrix type.";
610  }
611  return;
612  } else
613 #endif
614  {
615  CopyToMat(&(cu_mat->Mat()), trans);
616  }
617 }
CompressedMatrix cmat_
GeneralMatrixType Type() const
Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix.
#define KALDI_ERR
Definition: kaldi-error.h:147
void CopyToMat(MatrixBase< BaseFloat > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents, regardless of type, to "mat", which must be correctly sized.
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ GetCompressedMatrix()

const CompressedMatrix & GetCompressedMatrix ( ) const

Returns the contents as a compressed matrix.

This will only work if Type() returns kCompressedMatrix, or NumRows() == 0; otherwise it will crash.

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

References KALDI_ERR.

Referenced by kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().

872  {
873  if (mat_.NumRows() != 0 || smat_.NumRows() != 0)
874  KALDI_ERR << "GetCompressedMatrix called on GeneralMatrix of wrong type.";
875  return cmat_;
876 }
CompressedMatrix cmat_
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ GetFullMatrix()

const Matrix< BaseFloat > & GetFullMatrix ( ) const

Returns the contents as a Matrix<BaseFloat>.

This will only work if Type() returns kFullMatrix, or NumRows() == 0; otherwise it will crash.

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

References KALDI_ERR.

Referenced by kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().

878  {
879  if (smat_.NumRows() != 0 || cmat_.NumRows() != 0)
880  KALDI_ERR << "GetFullMatrix called on GeneralMatrix of wrong type.";
881  return mat_;
882 }
CompressedMatrix cmat_
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ GetMatrix()

void GetMatrix ( Matrix< BaseFloat > *  mat) const

Outputs the contents as a matrix.

This will work regardless of Type(). Sizes its output, unlike CopyToMat().

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

References kaldi::kUndefined, and Matrix< Real >::Resize().

Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::nnet3::ExampleApproxEqual(), kaldi::ExtractObjectRange(), NnetIo::operator==(), kaldi::nnet3::PerturbImageInNnetExample(), and kaldi::UnitTestGeneralMatrix().

817  {
818  if (mat_.NumRows() !=0) {
819  *mat = mat_;
820  } else if (cmat_.NumRows() != 0) {
822  cmat_.CopyToMat(mat);
823  } else if (smat_.NumRows() != 0) {
824  mat->Resize(smat_.NumRows(), smat_.NumCols(), kUndefined);
825  smat_.CopyToMat(mat);
826  } else {
827  mat->Resize(0, 0);
828  }
829 }
CompressedMatrix cmat_
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
void CopyToMat(MatrixBase< Real > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents to matrix.

◆ GetSparseMatrix()

const SparseMatrix< BaseFloat > & GetSparseMatrix ( ) const

Returns the contents as a SparseMatrix.

This will only work if Type() returns kSparseMatrix, or NumRows() == 0; otherwise it will crash.

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

References KALDI_ERR.

Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().

854  {
855  if (mat_.NumRows() != 0 || cmat_.NumRows() != 0)
856  KALDI_ERR << "GetSparseMatrix called on GeneralMatrix of wrong type.";
857  return smat_;
858 }
CompressedMatrix cmat_
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ NumCols()

MatrixIndexT NumCols ( ) const

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

Referenced by NnetComputer::AcceptInputs(), NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::AppendGeneralMatrixRows(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), kaldi::nnet3::GetIoSizes(), NnetIoStructureCompare::operator()(), NnetIo::operator==(), NnetComputeProb::ProcessOutputs(), and kaldi::UnitTestGeneralMatrix().

791  {
792  MatrixIndexT r = smat_.NumCols();
793  if (r != 0)
794  return r;
795  r = cmat_.NumCols();
796  if (r != 0)
797  return r;
798  return mat_.NumCols();
799 }
CompressedMatrix cmat_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int32 MatrixIndexT
Definition: matrix-common.h:98
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).

◆ NumRows()

MatrixIndexT NumRows ( ) const

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

Referenced by NnetComputer::AcceptInputs(), NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::AppendGeneralMatrixRows(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), kaldi::nnet3::FilterExample(), kaldi::FilterGeneralMatrixRows(), kaldi::nnet3::GetIoSizes(), main(), NnetIo::NnetIo(), NnetIoStructureCompare::operator()(), NnetIo::operator==(), kaldi::nnet3::ProcessFile(), kaldi::UnitTestGeneralMatrix(), and NnetIo::Write().

781  {
782  MatrixIndexT r = smat_.NumRows();
783  if (r != 0)
784  return r;
785  r = cmat_.NumRows();
786  if (r != 0)
787  return r;
788  return mat_.NumRows();
789 }
CompressedMatrix cmat_
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ operator=() [1/4]

GeneralMatrix & operator= ( const MatrixBase< BaseFloat > &  mat)

Assignment from regular matrix.

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

746  {
747  Clear();
748  mat_ = mat;
749  return *this;
750 }
Matrix< BaseFloat > mat_

◆ operator=() [2/4]

GeneralMatrix & operator= ( const CompressedMatrix mat)

Assignment from compressed matrix.

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

752  {
753  Clear();
754  cmat_ = cmat;
755  return *this;
756 }
CompressedMatrix cmat_

◆ operator=() [3/4]

GeneralMatrix & operator= ( const SparseMatrix< BaseFloat > &  smat)

Assignment from SparseMatrix<BaseFloat>

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

758  {
759  Clear();
760  smat_ = smat;
761  return *this;
762 }
SparseMatrix< BaseFloat > smat_

◆ operator=() [4/4]

GeneralMatrix & operator= ( const GeneralMatrix other)

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

References GeneralMatrix::cmat_, GeneralMatrix::mat_, and GeneralMatrix::smat_.

764  {
765  mat_ = gmat.mat_;
766  smat_ = gmat.smat_;
767  cmat_ = gmat.cmat_;
768  return *this;
769 }
CompressedMatrix cmat_
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Read()

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

Note: if you write a compressed matrix in text form, it will be read as a regular full matrix.

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

Referenced by NnetIo::Read(), and kaldi::UnitTestGeneralMatrix().

901  {
902  Clear();
903  if (binary) {
904  int peekval = is.peek();
905  if (peekval == 'C') {
906  // Token CM for compressed matrix
907  cmat_.Read(is, binary);
908  } else if (peekval == 'S') {
909  // Token SM for sparse matrix
910  smat_.Read(is, binary);
911  } else {
912  mat_.Read(is, binary);
913  }
914  } else {
915  // note: in text mode we will only ever read regular
916  // or sparse matrices, because the compressed-matrix format just
917  // gets written as a regular matrix in text mode.
918  is >> std::ws; // Eat up white space.
919  int peekval = is.peek();
920  if (peekval == 'r') { // sparse format starts rows=[int].
921  smat_.Read(is, binary);
922  } else {
923  mat_.Read(is, binary);
924  }
925  }
926 }
CompressedMatrix cmat_
void Read(std::istream &is, bool binary)
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Scale()

void Scale ( BaseFloat  alpha)

Scale each element of matrix by alpha.

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

844  {
845  if (mat_.NumRows() != 0) {
846  mat_.Scale(alpha);
847  } else if (cmat_.NumRows() != 0) {
848  cmat_.Scale(alpha);
849  } else if (smat_.NumRows() != 0) {
850  smat_.Scale(alpha);
851  }
852 
853 }
CompressedMatrix cmat_
void Scale(Real alpha)
Multiply each element with a scalar value.
void Scale(float alpha)
scales all elements of matrix by alpha.
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Swap()

void Swap ( GeneralMatrix other)

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

References GeneralMatrix::cmat_, GeneralMatrix::mat_, and GeneralMatrix::smat_.

Referenced by NnetIo::Swap().

1226  {
1227  mat_.Swap(&(other->mat_));
1228  cmat_.Swap(&(other->cmat_));
1229  smat_.Swap(&(other->smat_));
1230 }
CompressedMatrix cmat_
void Swap(CompressedMatrix *other)
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ SwapCompressedMatrix()

void SwapCompressedMatrix ( CompressedMatrix cmat)

Swaps the with the given CompressedMatrix.

This will only work if Type() returns kCompressedMatrix, or NumRows() == 0.

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

References KALDI_ERR, and CompressedMatrix::Swap().

Referenced by kaldi::ExtractRowRangeWithPadding().

866  {
867  if (mat_.NumRows() != 0 || smat_.NumRows() != 0)
868  KALDI_ERR << "GetSparseMatrix called on GeneralMatrix of wrong type.";
869  cmat->Swap(&cmat_);
870 }
CompressedMatrix cmat_
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ SwapFullMatrix()

void SwapFullMatrix ( Matrix< BaseFloat > *  mat)

Swaps the with the given Matrix.

This will only work if Type() returns kFullMatrix, or NumRows() == 0.

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

References KALDI_ERR, and Matrix< Real >::Swap().

Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().

885  {
886  if (cmat_.NumRows() != 0 || smat_.NumRows() != 0)
887  KALDI_ERR << "SwapMatrix called on GeneralMatrix of wrong type.";
888  mat->Swap(&mat_);
889 }
CompressedMatrix cmat_
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ SwapSparseMatrix()

void SwapSparseMatrix ( SparseMatrix< BaseFloat > *  smat)

Swaps the with the given SparseMatrix.

This will only work if Type() returns kSparseMatrix, or NumRows() == 0.

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

References KALDI_ERR, and SparseMatrix< Real >::Swap().

Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterGeneralMatrixRows(), and kaldi::UnitTestGeneralMatrix().

860  {
861  if (mat_.NumRows() != 0 || cmat_.NumRows() != 0)
862  KALDI_ERR << "GetSparseMatrix called on GeneralMatrix of wrong type.";
863  smat->Swap(&smat_);
864 }
CompressedMatrix cmat_
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

◆ Type()

GeneralMatrixType Type ( ) const

Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix.

If this matrix is empty, returns kFullMatrix.

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

References kaldi::kCompressedMatrix, kaldi::kFullMatrix, and kaldi::kSparseMatrix.

Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().

772  {
773  if (smat_.NumRows() != 0)
774  return kSparseMatrix;
775  else if (cmat_.NumRows() != 0)
776  return kCompressedMatrix;
777  else
778  return kFullMatrix;
779 }
CompressedMatrix cmat_
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
SparseMatrix< BaseFloat > smat_

◆ Uncompress()

void Uncompress ( )

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

References kaldi::kUndefined.

809  {
810  if (cmat_.NumRows() != 0) {
812  cmat_.CopyToMat(&mat_);
813  cmat_.Clear();
814  }
815 }
CompressedMatrix cmat_
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< BaseFloat > mat_
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
void CopyToMat(MatrixBase< Real > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents to matrix.

◆ Write()

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

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

Referenced by kaldi::UnitTestGeneralMatrix(), and NnetIo::Write().

891  {
892  if (smat_.NumRows() != 0) {
893  smat_.Write(os, binary);
894  } else if (cmat_.NumRows() != 0) {
895  cmat_.Write(os, binary);
896  } else {
897  mat_.Write(os, binary);
898  }
899 }
void Write(std::ostream &out, bool binary) const
write to stream.
CompressedMatrix cmat_
void Write(std::ostream &os, bool binary) const
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Matrix< BaseFloat > mat_
SparseMatrix< BaseFloat > smat_

Member Data Documentation

◆ cmat_

CompressedMatrix cmat_
private

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

Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().

◆ mat_

Matrix<BaseFloat> mat_
private

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

Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().

◆ smat_

SparseMatrix<BaseFloat> smat_
private

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

Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().


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