All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Matrix and vector classes

For an overview of the matrix library, see The Kaldi Matrix library. More...

Collaboration diagram for Matrix and vector classes:

Modules

 Matrix-vector functions returning scalars
 
 Miscellaneous matrix/vector library functions and classes
 

Classes

class  CompressedMatrix
 This class does lossy compression of a matrix. More...
 
struct  CompressedMatrix::GlobalHeader
 
struct  CompressedMatrix::PerColHeader
 
singleton  MatrixBase< Real >
 Base class which provides matrix operations not involving resizing or allocation. More...
 
singleton  Matrix< Real >
 A class for storing matrices. More...
 
singleton  VectorBase< Real >
 Provides a vector abstraction class. More...
 
singleton  Vector< Real >
 A class representing a vector. More...
 
singleton  SubVector< Real >
 Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vector [or as the row of a matrix]. More...
 
class  PackedMatrix< Real >
 Packed matrix: base class for triangular and symmetric matrices. More...
 
class  SpMatrix< Real >
 Packed symetric matrix class. More...
 
class  SparseVector< Real >
 
class  SparseMatrix< Real >
 
class  GeneralMatrix
 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...
 
class  TpMatrix< Real >
 Packed symetric matrix class. More...
 

Enumerations

enum  GeneralMatrixType { kFullMatrix, kCompressedMatrix, kSparseMatrix }
 

Functions

template<typename Real >
Real VecSvec (const VectorBase< Real > &vec, const SparseVector< Real > &svec)
 
template<typename Real >
Real TraceMatSmat (const MatrixBase< Real > &A, const SparseMatrix< Real > &B, MatrixTransposeType trans)
 
void AppendGeneralMatrixRows (const std::vector< const GeneralMatrix * > &src, GeneralMatrix *mat)
 Appends all the matrix rows of a list of GeneralMatrixes, to get a single GeneralMatrix. More...
 
template<typename Real >
void FilterSparseMatrixRows (const SparseMatrix< Real > &in, const std::vector< bool > &keep_rows, SparseMatrix< Real > *out)
 Outputs a SparseMatrix<Real> containing only the rows r of "in" such that keep_rows[r] == true. More...
 
template<typename Real >
void FilterMatrixRows (const Matrix< Real > &in, const std::vector< bool > &keep_rows, Matrix< Real > *out)
 Outputs a Matrix<Real> containing only the rows r of "in" such that keep_keep_rows[r] == true. More...
 
void FilterCompressedMatrixRows (const CompressedMatrix &in, const std::vector< bool > &keep_rows, Matrix< BaseFloat > *out)
 Outputs a Matrix<Real> containing only the rows r of "in" such that keep_rows[r] == true. More...
 
void FilterGeneralMatrixRows (const GeneralMatrix &in, const std::vector< bool > &keep_rows, GeneralMatrix *out)
 Outputs a GeneralMatrix containing only the rows r of "in" such that keep_rows[r] == true. More...
 

Detailed Description

For an overview of the matrix library, see The Kaldi Matrix library.

Enumeration Type Documentation

enum GeneralMatrixType
Enumerator
kFullMatrix 
kCompressedMatrix 
kSparseMatrix 

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

Function Documentation

void AppendGeneralMatrixRows ( const std::vector< const GeneralMatrix * > &  src,
GeneralMatrix *  mat 
)

Appends all the matrix rows of a list of GeneralMatrixes, to get a single GeneralMatrix.

Preserves sparsity if all inputs were sparse (or empty). Does not preserve compression, if inputs were compressed; you have to re-compress manually, if that's what you need.

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

References SparseMatrix< Real >::AppendSparseMatrixRows(), GeneralMatrix::Clear(), GeneralMatrix::CopyToMat(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, kaldi::kSparseMatrix, kaldi::kUndefined, GeneralMatrix::NumCols(), GeneralMatrix::NumRows(), GeneralMatrix::SwapFullMatrix(), and GeneralMatrix::SwapSparseMatrix().

Referenced by kaldi::nnet3::MergeIo().

819  {
820  mat->Clear();
821  int32 size = src.size();
822  if (size == 0)
823  return;
824  bool all_sparse = true;
825  for (int32 i = 0; i < size; i++) {
826  if (src[i]->Type() != kSparseMatrix && src[i]->NumRows() != 0) {
827  all_sparse = false;
828  break;
829  }
830  }
831  if (all_sparse) {
832  std::vector<SparseMatrix<BaseFloat> > sparse_mats(size);
833  for (int32 i = 0; i < size; i++)
834  sparse_mats[i] = src[i]->GetSparseMatrix();
835  SparseMatrix<BaseFloat> appended_mat;
836  appended_mat.AppendSparseMatrixRows(&sparse_mats);
837  mat->SwapSparseMatrix(&appended_mat);
838  } else {
839  int32 tot_rows = 0, num_cols = -1;
840  for (int32 i = 0; i < size; i++) {
841  const GeneralMatrix &src_mat = *(src[i]);
842  int32 src_rows = src_mat.NumRows(), src_cols = src_mat.NumCols();
843  if (src_rows != 0) {
844  tot_rows += src_rows;
845  if (num_cols == -1) num_cols = src_cols;
846  else if (num_cols != src_cols)
847  KALDI_ERR << "Appending rows of matrices with inconsistent num-cols: "
848  << num_cols << " vs. " << src_cols;
849  }
850  }
851  Matrix<BaseFloat> appended_mat(tot_rows, num_cols, kUndefined);
852  int32 row_offset = 0;
853  for (int32 i = 0; i < size; i++) {
854  const GeneralMatrix &src_mat = *(src[i]);
855  int32 src_rows = src_mat.NumRows();
856  if (src_rows != 0) {
857  SubMatrix<BaseFloat> dest_submat(appended_mat, row_offset, src_rows,
858  0, num_cols);
859  src_mat.CopyToMat(&dest_submat);
860  row_offset += src_rows;
861  }
862  }
863  KALDI_ASSERT(row_offset == tot_rows);
864  mat->SwapFullMatrix(&appended_mat);
865  }
866 }
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void FilterCompressedMatrixRows ( const CompressedMatrix &  in,
const std::vector< bool > &  keep_rows,
Matrix< BaseFloat > *  out 
)

Outputs a Matrix<Real> containing only the rows r of "in" such that keep_rows[r] == true.

keep_rows.size() must equal in.NumRows(), and rows must contain at least one "true" element.

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

References CompressedMatrix::CopyRowToVec(), CompressedMatrix::CopyToMat(), kaldi::FilterMatrixRows(), KALDI_ASSERT, KALDI_ERR, kaldi::kUndefined, CompressedMatrix::NumCols(), CompressedMatrix::NumRows(), and Matrix< Real >::Resize().

Referenced by kaldi::FilterGeneralMatrixRows().

870  {
871  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
872  int32 num_kept_rows = 0;
873  std::vector<bool>::const_iterator iter = keep_rows.begin(),
874  end = keep_rows.end();
875  for (; iter != end; ++iter)
876  if (*iter)
877  num_kept_rows++;
878  if (num_kept_rows == 0)
879  KALDI_ERR << "No kept rows";
880  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
881  out->Resize(in.NumRows(), in.NumCols(), kUndefined);
882  in.CopyToMat(out);
883  return;
884  }
885  const BaseFloat heuristic = 0.33;
886  // should be > 0 and < 1.0. represents the performance hit we get from
887  // iterating row-wise versus column-wise in compressed-matrix uncompression.
888 
889  if (num_kept_rows > heuristic * in.NumRows()) {
890  // if quite a few of the the rows are kept, it may be more efficient
891  // to uncompress the entire compressed matrix, since per-column operation
892  // is more efficient.
893  Matrix<BaseFloat> full_mat(in);
894  FilterMatrixRows(full_mat, keep_rows, out);
895  } else {
896  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
897 
898  iter = keep_rows.begin();
899  int32 out_row = 0;
900  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
901  if (*iter) {
902  SubVector<BaseFloat> dest(*out, out_row);
903  in.CopyRowToVec(in_row, &dest);
904  out_row++;
905  }
906  }
907  KALDI_ASSERT(out_row == num_kept_rows);
908  }
909 }
template void FilterMatrixRows(const Matrix< double > &in, const std::vector< bool > &keep_rows, Matrix< double > *out)
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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).
void FilterGeneralMatrixRows ( const GeneralMatrix &  in,
const std::vector< bool > &  keep_rows,
GeneralMatrix *  out 
)

Outputs a GeneralMatrix containing only the rows r of "in" such that keep_rows[r] == true.

keep_rows.size() must equal in.NumRows(), and keep_rows must contain at least one "true" element. If in.Type() is kCompressedMatrix, the result will not be compressed; otherwise, the type is preserved.

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

References GeneralMatrix::Clear(), kaldi::FilterCompressedMatrixRows(), kaldi::FilterMatrixRows(), kaldi::FilterSparseMatrixRows(), GeneralMatrix::GetCompressedMatrix(), GeneralMatrix::GetFullMatrix(), GeneralMatrix::GetSparseMatrix(), KALDI_ASSERT, KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kSparseMatrix, GeneralMatrix::NumRows(), GeneralMatrix::SwapFullMatrix(), GeneralMatrix::SwapSparseMatrix(), and GeneralMatrix::Type().

Referenced by kaldi::nnet3::FilterExample().

992  {
993  out->Clear();
994  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
995  int32 num_kept_rows = 0;
996  std::vector<bool>::const_iterator iter = keep_rows.begin(),
997  end = keep_rows.end();
998  for (; iter != end; ++iter)
999  if (*iter)
1000  num_kept_rows++;
1001  if (num_kept_rows == 0)
1002  KALDI_ERR << "No kept rows";
1003  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1004  *out = in;
1005  return;
1006  }
1007  switch (in.Type()) {
1008  case kCompressedMatrix: {
1009  const CompressedMatrix &cmat = in.GetCompressedMatrix();
1010  Matrix<BaseFloat> full_mat;
1011  FilterCompressedMatrixRows(cmat, keep_rows, &full_mat);
1012  out->SwapFullMatrix(&full_mat);
1013  return;
1014  }
1015  case kSparseMatrix: {
1016  const SparseMatrix<BaseFloat> &smat = in.GetSparseMatrix();
1017  SparseMatrix<BaseFloat> smat_out;
1018  FilterSparseMatrixRows(smat, keep_rows, &smat_out);
1019  out->SwapSparseMatrix(&smat_out);
1020  return;
1021  }
1022  case kFullMatrix: {
1023  const Matrix<BaseFloat> &full_mat = in.GetFullMatrix();
1024  Matrix<BaseFloat> full_mat_out;
1025  FilterMatrixRows(full_mat, keep_rows, &full_mat_out);
1026  out->SwapFullMatrix(&full_mat_out);
1027  return;
1028  }
1029  default:
1030  KALDI_ERR << "Invalid general-matrix type.";
1031  }
1032 }
template void FilterMatrixRows(const Matrix< double > &in, const std::vector< bool > &keep_rows, Matrix< double > *out)
template void FilterSparseMatrixRows(const SparseMatrix< double > &in, const std::vector< bool > &keep_rows, SparseMatrix< double > *out)
void FilterCompressedMatrixRows(const CompressedMatrix &in, const std::vector< bool > &keep_rows, Matrix< BaseFloat > *out)
Outputs a Matrix containing only the rows r of "in" such that keep_rows[r] == true...
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void FilterMatrixRows ( const Matrix< Real > &  in,
const std::vector< bool > &  keep_rows,
Matrix< Real > *  out 
)

Outputs a Matrix<Real> containing only the rows r of "in" such that keep_keep_rows[r] == true.

keep_rows.size() must equal in.NumRows(), and keep_rows must contain at least one "true" element.

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

References VectorBase< Real >::CopyFromVec(), KALDI_ASSERT, KALDI_ERR, kaldi::kUndefined, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and Matrix< Real >::Resize().

Referenced by kaldi::FilterCompressedMatrixRows(), and kaldi::FilterGeneralMatrixRows().

914  {
915  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
916  int32 num_kept_rows = 0;
917  std::vector<bool>::const_iterator iter = keep_rows.begin(),
918  end = keep_rows.end();
919  for (; iter != end; ++iter)
920  if (*iter)
921  num_kept_rows++;
922  if (num_kept_rows == 0)
923  KALDI_ERR << "No kept rows";
924  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
925  *out = in;
926  return;
927  }
928  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
929  iter = keep_rows.begin();
930  int32 out_row = 0;
931  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
932  if (*iter) {
933  SubVector<Real> src(in, in_row);
934  SubVector<Real> dest(*out, out_row);
935  dest.CopyFromVec(src);
936  out_row++;
937  }
938  }
939  KALDI_ASSERT(out_row == num_kept_rows);
940 }
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void FilterSparseMatrixRows ( const SparseMatrix< Real > &  in,
const std::vector< bool > &  keep_rows,
SparseMatrix< Real > *  out 
)

Outputs a SparseMatrix<Real> containing only the rows r of "in" such that keep_rows[r] == true.

keep_rows.size() must equal in.NumRows(), and rows must contain at least one "true" element.

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

References KALDI_ASSERT, KALDI_ERR, kaldi::kUndefined, SparseMatrix< Real >::NumCols(), SparseMatrix< Real >::NumRows(), SparseMatrix< Real >::Resize(), SparseMatrix< Real >::Row(), and SparseMatrix< Real >::SetRow().

Referenced by kaldi::FilterGeneralMatrixRows().

954  {
955  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
956  int32 num_kept_rows = 0;
957  std::vector<bool>::const_iterator iter = keep_rows.begin(),
958  end = keep_rows.end();
959  for (; iter != end; ++iter)
960  if (*iter)
961  num_kept_rows++;
962  if (num_kept_rows == 0)
963  KALDI_ERR << "No kept rows";
964  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
965  *out = in;
966  return;
967  }
968  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
969  iter = keep_rows.begin();
970  int32 out_row = 0;
971  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
972  if (*iter) {
973  out->SetRow(out_row, in.Row(in_row));
974  out_row++;
975  }
976  }
977  KALDI_ASSERT(out_row == num_kept_rows);
978 }
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real TraceMatSmat ( const MatrixBase< Real > &  A,
const SparseMatrix< Real > &  B,
MatrixTransposeType  trans 
)

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

References MatrixBase< Real >::Data(), SparseVector< Real >::Data(), rnnlm::i, KALDI_ASSERT, kaldi::kTrans, MatrixBase< Real >::NumCols(), SparseMatrix< Real >::NumCols(), SparseVector< Real >::NumElements(), MatrixBase< Real >::NumRows(), SparseMatrix< Real >::NumRows(), SparseMatrix< Real >::Row(), MatrixBase< Real >::Row(), MatrixBase< Real >::Stride(), and kaldi::VecSvec().

Referenced by kaldi::nnet3::ComputeObjectiveFunction(), kaldi::TraceMatSmat(), kaldi::UnitTestCuSparseMatrixTraceMatSmat(), and kaldi::UnitTestSparseMatrixTraceMatSmat().

612  {
613  Real sum = 0.0;
614  if (trans == kTrans) {
615  MatrixIndexT num_rows = A.NumRows();
616  KALDI_ASSERT(B.NumRows() == num_rows);
617  for (MatrixIndexT r = 0; r < num_rows; r++)
618  sum += VecSvec(A.Row(r), B.Row(r));
619  } else {
620  const Real *A_col_data = A.Data();
621  MatrixIndexT Astride = A.Stride(), Acols = A.NumCols(), Arows = A.NumRows();
622  KALDI_ASSERT(Arows == B.NumCols() && Acols == B.NumRows());
623  sum = 0.0;
624  for (MatrixIndexT i = 0; i < Acols; i++, A_col_data++) {
625  Real col_sum = 0.0;
626  const SparseVector<Real> &svec = B.Row(i);
627  MatrixIndexT num_elems = svec.NumElements();
628  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
629  for (MatrixIndexT e = 0; e < num_elems; e++)
630  col_sum += A_col_data[Astride * sdata[e].first] * sdata[e].second;
631  sum += col_sum;
632  }
633  }
634  return sum;
635 }
template double VecSvec(const VectorBase< double > &vec, const SparseVector< double > &svec)
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real VecSvec ( const VectorBase< Real > &  vec,
const SparseVector< Real > &  svec 
)

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

References VectorBase< Real >::Data(), SparseVector< Real >::Data(), SparseVector< Real >::Dim(), VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, rnnlm::n, and SparseVector< Real >::NumElements().

Referenced by kaldi::TraceMatSmat(), and kaldi::UnitTestSparseVectorVecSvec().

497  {
498  KALDI_ASSERT(vec.Dim() == svec.Dim());
499  MatrixIndexT n = svec.NumElements();
500  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
501  const Real *data = vec.Data();
502  Real ans = 0.0;
503  for (MatrixIndexT i = 0; i < n; i++)
504  ans += data[sdata[i].first] * sdata[i].second;
505  return ans;
506 }
int32 MatrixIndexT
Definition: matrix-common.h:96
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169