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
 
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  CompressionMethod {
  kAutomaticMethod = 1, kSpeechFeature = 2, kTwoByteAuto = 3, kTwoByteSignedInteger = 4,
  kOneByteAuto = 5, kOneByteUnsignedInteger = 6, kOneByteZeroOne = 7
}
 
enum  DataFormat { kOneByteWithColHeaders = 1, kTwoByte = 2, kOneByte = 3 }
 
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...
 
void ExtractRowRangeWithPadding (const GeneralMatrix &in, int32 row_offset, int32 num_rows, GeneralMatrix *out)
 This function extracts a row-range of a GeneralMatrix and writes as a GeneralMatrix containing the same type of underlying matrix. More...
 

Detailed Description

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

Enumeration Type Documentation

enum CompressionMethod
Enumerator
kAutomaticMethod 
kSpeechFeature 
kTwoByteAuto 
kTwoByteSignedInteger 
kOneByteAuto 
kOneByteUnsignedInteger 
kOneByteZeroOne 

Definition at line 75 of file compressed-matrix.h.

enum DataFormat
private
Enumerator
kOneByteWithColHeaders 
kTwoByte 
kOneByte 

Definition at line 201 of file compressed-matrix.h.

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

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

This function extracts a row-range of a GeneralMatrix and writes as a GeneralMatrix containing the same type of underlying matrix.

If the row-range is partly outside the row-range of 'in' (i.e. if row_offset < 0 or row_offset + num_rows > in.NumRows()) then it will pad with copies of the first and last row as needed. This is more efficient than un-compressing and re-compressing the underlying CompressedMatrix, and causes less accuracy loss due to re-compression (no loss in most cases).

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

References VectorBase< Real >::CopyFromVec(), GeneralMatrix::GetCompressedMatrix(), GeneralMatrix::GetFullMatrix(), GeneralMatrix::GetSparseMatrix(), KALDI_ASSERT, KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kSparseMatrix, kaldi::kUndefined, MatrixBase< Real >::NumCols(), SparseMatrix< Real >::NumCols(), CompressedMatrix::NumCols(), MatrixBase< Real >::NumRows(), SparseMatrix< Real >::NumRows(), SparseMatrix< Real >::Row(), SparseMatrix< Real >::SetRow(), GeneralMatrix::SwapCompressedMatrix(), GeneralMatrix::SwapFullMatrix(), GeneralMatrix::SwapSparseMatrix(), and GeneralMatrix::Type().

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

1141  {
1142  // make sure 'out' is empty to start with.
1143  Matrix<BaseFloat> empty_mat;
1144  *out = empty_mat;
1145  if (num_rows == 0) return;
1146  switch (in.Type()) {
1147  case kFullMatrix: {
1148  const Matrix<BaseFloat> &mat_in = in.GetFullMatrix();
1149  int32 num_rows_in = mat_in.NumRows(), num_cols = mat_in.NumCols();
1150  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1151  // matrix.
1152  Matrix<BaseFloat> mat_out(num_rows, num_cols, kUndefined);
1153  for (int32 row = 0; row < num_rows; row++) {
1154  int32 row_in = row + row_offset;
1155  if (row_in < 0) row_in = 0;
1156  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1157  SubVector<BaseFloat> vec_in(mat_in, row_in),
1158  vec_out(mat_out, row);
1159  vec_out.CopyFromVec(vec_in);
1160  }
1161  out->SwapFullMatrix(&mat_out);
1162  break;
1163  }
1164  case kSparseMatrix: {
1165  const SparseMatrix<BaseFloat> &smat_in = in.GetSparseMatrix();
1166  int32 num_rows_in = smat_in.NumRows(),
1167  num_cols = smat_in.NumCols();
1168  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1169  // matrix.
1170  SparseMatrix<BaseFloat> smat_out(num_rows, num_cols);
1171  for (int32 row = 0; row < num_rows; row++) {
1172  int32 row_in = row + row_offset;
1173  if (row_in < 0) row_in = 0;
1174  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1175  smat_out.SetRow(row, smat_in.Row(row_in));
1176  }
1177  out->SwapSparseMatrix(&smat_out);
1178  break;
1179  }
1180  case kCompressedMatrix: {
1181  const CompressedMatrix &cmat_in = in.GetCompressedMatrix();
1182  bool allow_padding = true;
1183  CompressedMatrix cmat_out(cmat_in, row_offset, num_rows,
1184  0, cmat_in.NumCols(), allow_padding);
1185  out->SwapCompressedMatrix(&cmat_out);
1186  break;
1187  }
1188  default:
1189  KALDI_ERR << "Bad matrix type.";
1190  }
1191 }
#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 897 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().

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

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

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

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