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

842  {
843  mat->Clear();
844  int32 size = src.size();
845  if (size == 0)
846  return;
847  bool all_sparse = true;
848  for (int32 i = 0; i < size; i++) {
849  if (src[i]->Type() != kSparseMatrix && src[i]->NumRows() != 0) {
850  all_sparse = false;
851  break;
852  }
853  }
854  if (all_sparse) {
855  std::vector<SparseMatrix<BaseFloat> > sparse_mats(size);
856  for (int32 i = 0; i < size; i++)
857  sparse_mats[i] = src[i]->GetSparseMatrix();
858  SparseMatrix<BaseFloat> appended_mat;
859  appended_mat.AppendSparseMatrixRows(&sparse_mats);
860  mat->SwapSparseMatrix(&appended_mat);
861  } else {
862  int32 tot_rows = 0, num_cols = -1;
863  for (int32 i = 0; i < size; i++) {
864  const GeneralMatrix &src_mat = *(src[i]);
865  int32 src_rows = src_mat.NumRows(), src_cols = src_mat.NumCols();
866  if (src_rows != 0) {
867  tot_rows += src_rows;
868  if (num_cols == -1) num_cols = src_cols;
869  else if (num_cols != src_cols)
870  KALDI_ERR << "Appending rows of matrices with inconsistent num-cols: "
871  << num_cols << " vs. " << src_cols;
872  }
873  }
874  Matrix<BaseFloat> appended_mat(tot_rows, num_cols, kUndefined);
875  int32 row_offset = 0;
876  for (int32 i = 0; i < size; i++) {
877  const GeneralMatrix &src_mat = *(src[i]);
878  int32 src_rows = src_mat.NumRows();
879  if (src_rows != 0) {
880  SubMatrix<BaseFloat> dest_submat(appended_mat, row_offset, src_rows,
881  0, num_cols);
882  src_mat.CopyToMat(&dest_submat);
883  row_offset += src_rows;
884  }
885  }
886  KALDI_ASSERT(row_offset == tot_rows);
887  mat->SwapFullMatrix(&appended_mat);
888  }
889 }
#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 891 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().

893  {
894  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
895  int32 num_kept_rows = 0;
896  std::vector<bool>::const_iterator iter = keep_rows.begin(),
897  end = keep_rows.end();
898  for (; iter != end; ++iter)
899  if (*iter)
900  num_kept_rows++;
901  if (num_kept_rows == 0)
902  KALDI_ERR << "No kept rows";
903  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
904  out->Resize(in.NumRows(), in.NumCols(), kUndefined);
905  in.CopyToMat(out);
906  return;
907  }
908  const BaseFloat heuristic = 0.33;
909  // should be > 0 and < 1.0. represents the performance hit we get from
910  // iterating row-wise versus column-wise in compressed-matrix uncompression.
911 
912  if (num_kept_rows > heuristic * in.NumRows()) {
913  // if quite a few of the the rows are kept, it may be more efficient
914  // to uncompress the entire compressed matrix, since per-column operation
915  // is more efficient.
916  Matrix<BaseFloat> full_mat(in);
917  FilterMatrixRows(full_mat, keep_rows, out);
918  } else {
919  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
920 
921  iter = keep_rows.begin();
922  int32 out_row = 0;
923  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
924  if (*iter) {
925  SubVector<BaseFloat> dest(*out, out_row);
926  in.CopyRowToVec(in_row, &dest);
927  out_row++;
928  }
929  }
930  KALDI_ASSERT(out_row == num_kept_rows);
931  }
932 }
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 1013 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().

1015  {
1016  out->Clear();
1017  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1018  int32 num_kept_rows = 0;
1019  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1020  end = keep_rows.end();
1021  for (; iter != end; ++iter)
1022  if (*iter)
1023  num_kept_rows++;
1024  if (num_kept_rows == 0)
1025  KALDI_ERR << "No kept rows";
1026  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1027  *out = in;
1028  return;
1029  }
1030  switch (in.Type()) {
1031  case kCompressedMatrix: {
1032  const CompressedMatrix &cmat = in.GetCompressedMatrix();
1033  Matrix<BaseFloat> full_mat;
1034  FilterCompressedMatrixRows(cmat, keep_rows, &full_mat);
1035  out->SwapFullMatrix(&full_mat);
1036  return;
1037  }
1038  case kSparseMatrix: {
1039  const SparseMatrix<BaseFloat> &smat = in.GetSparseMatrix();
1040  SparseMatrix<BaseFloat> smat_out;
1041  FilterSparseMatrixRows(smat, keep_rows, &smat_out);
1042  out->SwapSparseMatrix(&smat_out);
1043  return;
1044  }
1045  case kFullMatrix: {
1046  const Matrix<BaseFloat> &full_mat = in.GetFullMatrix();
1047  Matrix<BaseFloat> full_mat_out;
1048  FilterMatrixRows(full_mat, keep_rows, &full_mat_out);
1049  out->SwapFullMatrix(&full_mat_out);
1050  return;
1051  }
1052  default:
1053  KALDI_ERR << "Invalid general-matrix type.";
1054  }
1055 }
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 935 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().

937  {
938  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
939  int32 num_kept_rows = 0;
940  std::vector<bool>::const_iterator iter = keep_rows.begin(),
941  end = keep_rows.end();
942  for (; iter != end; ++iter)
943  if (*iter)
944  num_kept_rows++;
945  if (num_kept_rows == 0)
946  KALDI_ERR << "No kept rows";
947  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
948  *out = in;
949  return;
950  }
951  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
952  iter = keep_rows.begin();
953  int32 out_row = 0;
954  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
955  if (*iter) {
956  SubVector<Real> src(in, in_row);
957  SubVector<Real> dest(*out, out_row);
958  dest.CopyFromVec(src);
959  out_row++;
960  }
961  }
962  KALDI_ASSERT(out_row == num_kept_rows);
963 }
#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 975 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().

977  {
978  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
979  int32 num_kept_rows = 0;
980  std::vector<bool>::const_iterator iter = keep_rows.begin(),
981  end = keep_rows.end();
982  for (; iter != end; ++iter)
983  if (*iter)
984  num_kept_rows++;
985  if (num_kept_rows == 0)
986  KALDI_ERR << "No kept rows";
987  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
988  *out = in;
989  return;
990  }
991  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
992  iter = keep_rows.begin();
993  int32 out_row = 0;
994  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
995  if (*iter) {
996  out->SetRow(out_row, in.Row(in_row));
997  out_row++;
998  }
999  }
1000  KALDI_ASSERT(out_row == num_kept_rows);
1001 }
#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 623 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().

625  {
626  Real sum = 0.0;
627  if (trans == kTrans) {
628  MatrixIndexT num_rows = A.NumRows();
629  KALDI_ASSERT(B.NumRows() == num_rows);
630  for (MatrixIndexT r = 0; r < num_rows; r++)
631  sum += VecSvec(A.Row(r), B.Row(r));
632  } else {
633  const Real *A_col_data = A.Data();
634  MatrixIndexT Astride = A.Stride(), Acols = A.NumCols(), Arows = A.NumRows();
635  KALDI_ASSERT(Arows == B.NumCols() && Acols == B.NumRows());
636  sum = 0.0;
637  for (MatrixIndexT i = 0; i < Acols; i++, A_col_data++) {
638  Real col_sum = 0.0;
639  const SparseVector<Real> &svec = B.Row(i);
640  MatrixIndexT num_elems = svec.NumElements();
641  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
642  for (MatrixIndexT e = 0; e < num_elems; e++)
643  col_sum += A_col_data[Astride * sdata[e].first] * sdata[e].second;
644  sum += col_sum;
645  }
646  }
647  return sum;
648 }
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 502 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().

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