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 262 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 918 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().

919  {
920  mat->Clear();
921  int32 size = src.size();
922  if (size == 0)
923  return;
924  bool all_sparse = true;
925  for (int32 i = 0; i < size; i++) {
926  if (src[i]->Type() != kSparseMatrix && src[i]->NumRows() != 0) {
927  all_sparse = false;
928  break;
929  }
930  }
931  if (all_sparse) {
932  std::vector<SparseMatrix<BaseFloat> > sparse_mats(size);
933  for (int32 i = 0; i < size; i++)
934  sparse_mats[i] = src[i]->GetSparseMatrix();
935  SparseMatrix<BaseFloat> appended_mat;
936  appended_mat.AppendSparseMatrixRows(&sparse_mats);
937  mat->SwapSparseMatrix(&appended_mat);
938  } else {
939  int32 tot_rows = 0, num_cols = -1;
940  for (int32 i = 0; i < size; i++) {
941  const GeneralMatrix &src_mat = *(src[i]);
942  int32 src_rows = src_mat.NumRows(), src_cols = src_mat.NumCols();
943  if (src_rows != 0) {
944  tot_rows += src_rows;
945  if (num_cols == -1) num_cols = src_cols;
946  else if (num_cols != src_cols)
947  KALDI_ERR << "Appending rows of matrices with inconsistent num-cols: "
948  << num_cols << " vs. " << src_cols;
949  }
950  }
951  Matrix<BaseFloat> appended_mat(tot_rows, num_cols, kUndefined);
952  int32 row_offset = 0;
953  for (int32 i = 0; i < size; i++) {
954  const GeneralMatrix &src_mat = *(src[i]);
955  int32 src_rows = src_mat.NumRows();
956  if (src_rows != 0) {
957  SubMatrix<BaseFloat> dest_submat(appended_mat, row_offset, src_rows,
958  0, num_cols);
959  src_mat.CopyToMat(&dest_submat);
960  row_offset += src_rows;
961  }
962  }
963  KALDI_ASSERT(row_offset == tot_rows);
964  mat->SwapFullMatrix(&appended_mat);
965  }
966 }
#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 1208 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().

1212  {
1213  // make sure 'out' is empty to start with.
1214  Matrix<BaseFloat> empty_mat;
1215  *out = empty_mat;
1216  if (num_rows == 0) return;
1217  switch (in.Type()) {
1218  case kFullMatrix: {
1219  const Matrix<BaseFloat> &mat_in = in.GetFullMatrix();
1220  int32 num_rows_in = mat_in.NumRows(), num_cols = mat_in.NumCols();
1221  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1222  // matrix.
1223  Matrix<BaseFloat> mat_out(num_rows, num_cols, kUndefined);
1224  for (int32 row = 0; row < num_rows; row++) {
1225  int32 row_in = row + row_offset;
1226  if (row_in < 0) row_in = 0;
1227  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1228  SubVector<BaseFloat> vec_in(mat_in, row_in),
1229  vec_out(mat_out, row);
1230  vec_out.CopyFromVec(vec_in);
1231  }
1232  out->SwapFullMatrix(&mat_out);
1233  break;
1234  }
1235  case kSparseMatrix: {
1236  const SparseMatrix<BaseFloat> &smat_in = in.GetSparseMatrix();
1237  int32 num_rows_in = smat_in.NumRows(),
1238  num_cols = smat_in.NumCols();
1239  KALDI_ASSERT(num_rows_in > 0); // we can't extract >0 rows from an empty
1240  // matrix.
1241  SparseMatrix<BaseFloat> smat_out(num_rows, num_cols);
1242  for (int32 row = 0; row < num_rows; row++) {
1243  int32 row_in = row + row_offset;
1244  if (row_in < 0) row_in = 0;
1245  else if (row_in >= num_rows_in) row_in = num_rows_in - 1;
1246  smat_out.SetRow(row, smat_in.Row(row_in));
1247  }
1248  out->SwapSparseMatrix(&smat_out);
1249  break;
1250  }
1251  case kCompressedMatrix: {
1252  const CompressedMatrix &cmat_in = in.GetCompressedMatrix();
1253  bool allow_padding = true;
1254  CompressedMatrix cmat_out(cmat_in, row_offset, num_rows,
1255  0, cmat_in.NumCols(), allow_padding);
1256  out->SwapCompressedMatrix(&cmat_out);
1257  break;
1258  }
1259  default:
1260  KALDI_ERR << "Bad matrix type.";
1261  }
1262 }
#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 968 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().

970  {
971  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
972  int32 num_kept_rows = 0;
973  std::vector<bool>::const_iterator iter = keep_rows.begin(),
974  end = keep_rows.end();
975  for (; iter != end; ++iter)
976  if (*iter)
977  num_kept_rows++;
978  if (num_kept_rows == 0)
979  KALDI_ERR << "No kept rows";
980  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
981  out->Resize(in.NumRows(), in.NumCols(), kUndefined);
982  in.CopyToMat(out);
983  return;
984  }
985  const BaseFloat heuristic = 0.33;
986  // should be > 0 and < 1.0. represents the performance hit we get from
987  // iterating row-wise versus column-wise in compressed-matrix uncompression.
988 
989  if (num_kept_rows > heuristic * in.NumRows()) {
990  // if quite a few of the the rows are kept, it may be more efficient
991  // to uncompress the entire compressed matrix, since per-column operation
992  // is more efficient.
993  Matrix<BaseFloat> full_mat(in);
994  FilterMatrixRows(full_mat, keep_rows, out);
995  } else {
996  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
997 
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  SubVector<BaseFloat> dest(*out, out_row);
1003  in.CopyRowToVec(in_row, &dest);
1004  out_row++;
1005  }
1006  }
1007  KALDI_ASSERT(out_row == num_kept_rows);
1008  }
1009 }
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 1090 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().

1092  {
1093  out->Clear();
1094  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1095  int32 num_kept_rows = 0;
1096  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1097  end = keep_rows.end();
1098  for (; iter != end; ++iter)
1099  if (*iter)
1100  num_kept_rows++;
1101  if (num_kept_rows == 0)
1102  KALDI_ERR << "No kept rows";
1103  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1104  *out = in;
1105  return;
1106  }
1107  switch (in.Type()) {
1108  case kCompressedMatrix: {
1109  const CompressedMatrix &cmat = in.GetCompressedMatrix();
1110  Matrix<BaseFloat> full_mat;
1111  FilterCompressedMatrixRows(cmat, keep_rows, &full_mat);
1112  out->SwapFullMatrix(&full_mat);
1113  return;
1114  }
1115  case kSparseMatrix: {
1116  const SparseMatrix<BaseFloat> &smat = in.GetSparseMatrix();
1117  SparseMatrix<BaseFloat> smat_out;
1118  FilterSparseMatrixRows(smat, keep_rows, &smat_out);
1119  out->SwapSparseMatrix(&smat_out);
1120  return;
1121  }
1122  case kFullMatrix: {
1123  const Matrix<BaseFloat> &full_mat = in.GetFullMatrix();
1124  Matrix<BaseFloat> full_mat_out;
1125  FilterMatrixRows(full_mat, keep_rows, &full_mat_out);
1126  out->SwapFullMatrix(&full_mat_out);
1127  return;
1128  }
1129  default:
1130  KALDI_ERR << "Invalid general-matrix type.";
1131  }
1132 }
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 1012 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().

1014  {
1015  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1016  int32 num_kept_rows = 0;
1017  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1018  end = keep_rows.end();
1019  for (; iter != end; ++iter)
1020  if (*iter)
1021  num_kept_rows++;
1022  if (num_kept_rows == 0)
1023  KALDI_ERR << "No kept rows";
1024  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1025  *out = in;
1026  return;
1027  }
1028  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
1029  iter = keep_rows.begin();
1030  int32 out_row = 0;
1031  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
1032  if (*iter) {
1033  SubVector<Real> src(in, in_row);
1034  SubVector<Real> dest(*out, out_row);
1035  dest.CopyFromVec(src);
1036  out_row++;
1037  }
1038  }
1039  KALDI_ASSERT(out_row == num_kept_rows);
1040 }
#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 1052 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().

1054  {
1055  KALDI_ASSERT(keep_rows.size() == static_cast<size_t>(in.NumRows()));
1056  int32 num_kept_rows = 0;
1057  std::vector<bool>::const_iterator iter = keep_rows.begin(),
1058  end = keep_rows.end();
1059  for (; iter != end; ++iter)
1060  if (*iter)
1061  num_kept_rows++;
1062  if (num_kept_rows == 0)
1063  KALDI_ERR << "No kept rows";
1064  if (num_kept_rows == static_cast<int32>(keep_rows.size())) {
1065  *out = in;
1066  return;
1067  }
1068  out->Resize(num_kept_rows, in.NumCols(), kUndefined);
1069  iter = keep_rows.begin();
1070  int32 out_row = 0;
1071  for (int32 in_row = 0; iter != end; ++iter, ++in_row) {
1072  if (*iter) {
1073  out->SetRow(out_row, in.Row(in_row));
1074  out_row++;
1075  }
1076  }
1077  KALDI_ASSERT(out_row == num_kept_rows);
1078 }
#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 694 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(), and kaldi::VecSvec().

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

696  {
697  Real sum = 0.0;
698  if (trans == kTrans) {
699  MatrixIndexT num_rows = A.NumRows();
700  KALDI_ASSERT(B.NumRows() == num_rows);
701  for (MatrixIndexT r = 0; r < num_rows; r++)
702  sum += VecSvec(A.Row(r), B.Row(r));
703  } else {
704  const Real *A_col_data = A.Data();
705  MatrixIndexT Astride = A.Stride(), Acols = A.NumCols(), Arows = A.NumRows();
706  KALDI_ASSERT(Arows == B.NumCols() && Acols == B.NumRows());
707  sum = 0.0;
708  for (MatrixIndexT i = 0; i < Acols; i++, A_col_data++) {
709  Real col_sum = 0.0;
710  const SparseVector<Real> &svec = B.Row(i);
711  MatrixIndexT num_elems = svec.NumElements();
712  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
713  for (MatrixIndexT e = 0; e < num_elems; e++)
714  col_sum += A_col_data[Astride * sdata[e].first] * sdata[e].second;
715  sum += col_sum;
716  }
717  }
718  return sum;
719 }
template double VecSvec(const VectorBase< double > &vec, const SparseVector< double > &svec)
int32 MatrixIndexT
Definition: matrix-common.h:98
KALDI_ASSERT & A
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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::TraceMatSmat(), and 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:169