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
 
class  MatrixBase< Real >
 Base class which provides matrix operations not involving resizing or allocation. More...
 
class  Matrix< Real >
 A class for storing matrices. More...
 
class  VectorBase< Real >
 Provides a vector abstraction class. More...
 
class  Vector< Real >
 A class representing a vector. More...
 
class  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  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

◆ CompressionMethod

Enumerator
kAutomaticMethod 
kSpeechFeature 
kTwoByteAuto 
kTwoByteSignedInteger 
kOneByteAuto 
kOneByteUnsignedInteger 
kOneByteZeroOne 

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

◆ GeneralMatrixType

Enumerator
kFullMatrix 
kCompressedMatrix 
kSparseMatrix 

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

Function Documentation

◆ AppendGeneralMatrixRows()

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

930  {
931  mat->Clear();
932  int32 size = src.size();
933  if (size == 0)
934  return;
935  bool all_sparse = true;
936  for (int32 i = 0; i < size; i++) {
937  if (src[i]->Type() != kSparseMatrix && src[i]->NumRows() != 0) {
938  all_sparse = false;
939  break;
940  }
941  }
942  if (all_sparse) {
943  std::vector<SparseMatrix<BaseFloat> > sparse_mats(size);
944  for (int32 i = 0; i < size; i++)
945  sparse_mats[i] = src[i]->GetSparseMatrix();
946  SparseMatrix<BaseFloat> appended_mat;
947  appended_mat.AppendSparseMatrixRows(&sparse_mats);
948  mat->SwapSparseMatrix(&appended_mat);
949  } else {
950  int32 tot_rows = 0, num_cols = -1;
951  for (int32 i = 0; i < size; i++) {
952  const GeneralMatrix &src_mat = *(src[i]);
953  int32 src_rows = src_mat.NumRows(), src_cols = src_mat.NumCols();
954  if (src_rows != 0) {
955  tot_rows += src_rows;
956  if (num_cols == -1) num_cols = src_cols;
957  else if (num_cols != src_cols)
958  KALDI_ERR << "Appending rows of matrices with inconsistent num-cols: "
959  << num_cols << " vs. " << src_cols;
960  }
961  }
962  Matrix<BaseFloat> appended_mat(tot_rows, num_cols, kUndefined);
963  int32 row_offset = 0;
964  for (int32 i = 0; i < size; i++) {
965  const GeneralMatrix &src_mat = *(src[i]);
966  int32 src_rows = src_mat.NumRows();
967  if (src_rows != 0) {
968  SubMatrix<BaseFloat> dest_submat(appended_mat, row_offset, src_rows,
969  0, num_cols);
970  src_mat.CopyToMat(&dest_submat);
971  row_offset += src_rows;
972  }
973  }
974  KALDI_ASSERT(row_offset == tot_rows);
975  mat->SwapFullMatrix(&appended_mat);
976  }
977 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ExtractRowRangeWithPadding()

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

1237  {
1238  // make sure 'out' is empty to start with.
1239  Matrix<BaseFloat> empty_mat;
1240  *out = empty_mat;
1241  if (num_rows == 0) return;
1242  switch (in.Type()) {
1243  case kFullMatrix: {
1244  const Matrix<BaseFloat> &mat_in = in.GetFullMatrix();
1245  int32 num_rows_in = mat_in.NumRows(), num_cols = mat_in.NumCols();
1246  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1247  // matrix.
1248  Matrix<BaseFloat> mat_out(num_rows, num_cols, kUndefined);
1249  for (int32 row = 0; row < num_rows; row++) {
1250  int32 row_in = row + row_offset;
1251  if (row_in < 0) row_in = 0;
1252  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1253  SubVector<BaseFloat> vec_in(mat_in, row_in),
1254  vec_out(mat_out, row);
1255  vec_out.CopyFromVec(vec_in);
1256  }
1257  out->SwapFullMatrix(&mat_out);
1258  break;
1259  }
1260  case kSparseMatrix: {
1261  const SparseMatrix<BaseFloat> &smat_in = in.GetSparseMatrix();
1262  int32 num_rows_in = smat_in.NumRows(),
1263  num_cols = smat_in.NumCols();
1264  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1265  // matrix.
1266  SparseMatrix<BaseFloat> smat_out(num_rows, num_cols);
1267  for (int32 row = 0; row < num_rows; row++) {
1268  int32 row_in = row + row_offset;
1269  if (row_in < 0) row_in = 0;
1270  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1271  smat_out.SetRow(row, smat_in.Row(row_in));
1272  }
1273  out->SwapSparseMatrix(&smat_out);
1274  break;
1275  }
1276  case kCompressedMatrix: {
1277  const CompressedMatrix &cmat_in = in.GetCompressedMatrix();
1278  bool allow_padding = true;
1279  CompressedMatrix cmat_out(cmat_in, row_offset, num_rows,
1280  0, cmat_in.NumCols(), allow_padding);
1281  out->SwapCompressedMatrix(&cmat_out);
1282  break;
1283  }
1284  default:
1285  KALDI_ERR << "Bad matrix type.";
1286  }
1287 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FilterCompressedMatrixRows()

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

981  {
982  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
983  int32 num_kept_rows = 0;
984  std::vector<bool>::const_iterator iter = keep_rows.begin(),
985  end = keep_rows.end();
986  for (; iter != end; ++iter)
987  if (*iter)
988  num_kept_rows++;
989  if (num_kept_rows == 0)
990  KALDI_ERR << "No kept rows";
991  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
992  out->Resize(in.NumRows(), in.NumCols(), kUndefined);
993  in.CopyToMat(out);
994  return;
995  }
996  const BaseFloat heuristic = 0.33;
997  // should be > 0 and < 1.0. represents the performance hit we get from
998  // iterating row-wise versus column-wise in compressed-matrix uncompression.
999 
1000  if (num_kept_rows > heuristic * in.NumRows()) {
1001  // if quite a few of the the rows are kept, it may be more efficient
1002  // to uncompress the entire compressed matrix, since per-column operation
1003  // is more efficient.
1004  Matrix<BaseFloat> full_mat(in);
1005  FilterMatrixRows(full_mat, keep_rows, out);
1006  } else {
1007  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
1008 
1009  iter = keep_rows.begin();
1010  int32 out_row = 0;
1011  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
1012  if (*iter) {
1013  SubVector<BaseFloat> dest(*out, out_row);
1014  in.CopyRowToVec(in_row, &dest);
1015  out_row++;
1016  }
1017  }
1018  KALDI_ASSERT(out_row == num_kept_rows);
1019  }
1020 }
kaldi::int32 int32
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:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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).

◆ FilterGeneralMatrixRows()

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

1103  {
1104  out->Clear();
1105  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1106  int32 num_kept_rows = 0;
1107  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1108  end = keep_rows.end();
1109  for (; iter != end; ++iter)
1110  if (*iter)
1111  num_kept_rows++;
1112  if (num_kept_rows == 0)
1113  KALDI_ERR << "No kept rows";
1114  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1115  *out = in;
1116  return;
1117  }
1118  switch (in.Type()) {
1119  case kCompressedMatrix: {
1120  const CompressedMatrix &cmat = in.GetCompressedMatrix();
1121  Matrix<BaseFloat> full_mat;
1122  FilterCompressedMatrixRows(cmat, keep_rows, &full_mat);
1123  out->SwapFullMatrix(&full_mat);
1124  return;
1125  }
1126  case kSparseMatrix: {
1127  const SparseMatrix<BaseFloat> &smat = in.GetSparseMatrix();
1128  SparseMatrix<BaseFloat> smat_out;
1129  FilterSparseMatrixRows(smat, keep_rows, &smat_out);
1130  out->SwapSparseMatrix(&smat_out);
1131  return;
1132  }
1133  case kFullMatrix: {
1134  const Matrix<BaseFloat> &full_mat = in.GetFullMatrix();
1135  Matrix<BaseFloat> full_mat_out;
1136  FilterMatrixRows(full_mat, keep_rows, &full_mat_out);
1137  out->SwapFullMatrix(&full_mat_out);
1138  return;
1139  }
1140  default:
1141  KALDI_ERR << "Invalid general-matrix type.";
1142  }
1143 }
kaldi::int32 int32
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<Real> containing only the rows r of "in" such that keep_rows[r] == true...
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FilterMatrixRows()

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 1023 of file sparse-matrix.cc.

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

1025  {
1026  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1027  int32 num_kept_rows = 0;
1028  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1029  end = keep_rows.end();
1030  for (; iter != end; ++iter)
1031  if (*iter)
1032  num_kept_rows++;
1033  if (num_kept_rows == 0)
1034  KALDI_ERR << "No kept rows";
1035  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1036  *out = in;
1037  return;
1038  }
1039  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
1040  iter = keep_rows.begin();
1041  int32 out_row = 0;
1042  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
1043  if (*iter) {
1044  SubVector<Real> src(in, in_row);
1045  SubVector<Real> dest(*out, out_row);
1046  dest.CopyFromVec(src);
1047  out_row++;
1048  }
1049  }
1050  KALDI_ASSERT(out_row == num_kept_rows);
1051 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FilterSparseMatrixRows()

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 1063 of file sparse-matrix.cc.

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

1065  {
1066  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1067  int32 num_kept_rows = 0;
1068  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1069  end = keep_rows.end();
1070  for (; iter != end; ++iter)
1071  if (*iter)
1072  num_kept_rows++;
1073  if (num_kept_rows == 0)
1074  KALDI_ERR << "No kept rows";
1075  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1076  *out = in;
1077  return;
1078  }
1079  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
1080  iter = keep_rows.begin();
1081  int32 out_row = 0;
1082  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
1083  if (*iter) {
1084  out->SetRow(out_row, in.Row(in_row));
1085  out_row++;
1086  }
1087  }
1088  KALDI_ASSERT(out_row == num_kept_rows);
1089 }
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ TraceMatSmat()

Real TraceMatSmat ( const MatrixBase< Real > &  A,
const SparseMatrix< Real > &  B,
MatrixTransposeType  trans 
)

Definition at line 704 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(), MatrixBase< Real >::Row(), SparseMatrix< Real >::Row(), MatrixBase< Real >::Stride(), kaldi::TraceMatSmat(), and kaldi::VecSvec().

Referenced by kaldi::nnet3::ComputeObjectiveFunction(), kaldi::TestCuSparseMatrixTraceMatSmat(), kaldi::UnitTestCuSparseMatrixTraceMatSmat(), kaldi::UnitTestMatrixAddMatSmat(), kaldi::UnitTestMatrixAddSmatMat(), and kaldi::UnitTestSparseMatrixTraceMatSmat().

706  {
707  Real sum = 0.0;
708  if (trans == kTrans) {
709  MatrixIndexT num_rows = A.NumRows();
710  KALDI_ASSERT(B.NumRows() == num_rows);
711  for (MatrixIndexT r = 0; r < num_rows; r++)
712  sum += VecSvec(A.Row(r), B.Row(r));
713  } else {
714  const Real *A_col_data = A.Data();
715  MatrixIndexT Astride = A.Stride(), Acols = A.NumCols(), Arows = A.NumRows();
716  KALDI_ASSERT(Arows == B.NumCols() && Acols == B.NumRows());
717  sum = 0.0;
718  for (MatrixIndexT i = 0; i < Acols; i++, A_col_data++) {
719  Real col_sum = 0.0;
720  const SparseVector<Real> &svec = B.Row(i);
721  MatrixIndexT num_elems = svec.NumElements();
722  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
723  for (MatrixIndexT e = 0; e < num_elems; e++)
724  col_sum += A_col_data[Astride * sdata[e].first] * sdata[e].second;
725  sum += col_sum;
726  }
727  }
728  return sum;
729 }
template double VecSvec(const VectorBase< double > &vec, const SparseVector< double > &svec)
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ VecSvec()

Real VecSvec ( const VectorBase< Real > &  vec,
const SparseVector< Real > &  svec 
)

Definition at line 523 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::UnitTestSparseVectorVecSvec().

524  {
525  KALDI_ASSERT(vec.Dim() == svec.Dim());
526  MatrixIndexT n = svec.NumElements();
527  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
528  const Real *data = vec.Data();
529  Real ans = 0.0;
530  for (MatrixIndexT i = 0; i < n; i++)
531  ans += data[sdata[i].first] * sdata[i].second;
532  return ans;
533 }
int32 MatrixIndexT
Definition: matrix-common.h:98
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185