All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MatrixBase< Real > Singleton Reference

Base class which provides matrix operations not involving resizing or allocation. More...

#include <kaldi-matrix.h>

Inheritance diagram for MatrixBase< Real >:
Collaboration diagram for MatrixBase< Real >:

Public Member Functions

MatrixIndexT NumRows () const
 Returns number of rows (or zero for emtpy matrix). More...
 
MatrixIndexT NumCols () const
 Returns number of columns (or zero for emtpy matrix). More...
 
MatrixIndexT Stride () const
 Stride (distance in memory between each row). Will be >= NumCols. More...
 
size_t SizeInBytes () const
 Returns size in bytes of the data held by the matrix. More...
 
const Real * Data () const
 Gives pointer to raw data (const). More...
 
Real * Data ()
 Gives pointer to raw data (non-const). More...
 
Real * RowData (MatrixIndexT i)
 Returns pointer to data for one row (non-const) More...
 
const Real * RowData (MatrixIndexT i) const
 Returns pointer to data for one row (const) More...
 
Real & operator() (MatrixIndexT r, MatrixIndexT c)
 Indexing operator, non-const (only checks sizes if compiled with -DKALDI_PARANOID) More...
 
Real & Index (MatrixIndexT r, MatrixIndexT c)
 Indexing operator, provided for ease of debugging (gdb doesn't work with parenthesis operator). More...
 
const Real operator() (MatrixIndexT r, MatrixIndexT c) const
 Indexing operator, const (only checks sizes if compiled with -DKALDI_PARANOID) More...
 
void SetZero ()
 Sets matrix to zero. More...
 
void Set (Real)
 Sets all elements to a specific value. More...
 
void SetUnit ()
 Sets to zero, except ones along diagonal [for non-square matrices too]. More...
 
void SetRandn ()
 Sets to random values of a normal distribution. More...
 
void SetRandUniform ()
 Sets to numbers uniformly distributed on (0, 1) More...
 
template<typename OtherReal >
void CopyFromMat (const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
 Copy given matrix. (no resize is done). More...
 
void CopyFromMat (const CompressedMatrix &M)
 Copy from compressed matrix. More...
 
template<typename OtherReal >
void CopyFromSp (const SpMatrix< OtherReal > &M)
 Copy given spmatrix. (no resize is done). More...
 
template<typename OtherReal >
void CopyFromTp (const TpMatrix< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
 Copy given tpmatrix. (no resize is done). More...
 
template<typename OtherReal >
void CopyFromMat (const CuMatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
 Copy from CUDA matrix. Implemented in ../cudamatrix/cu-matrix.h. More...
 
void CopyRowsFromVec (const VectorBase< Real > &v)
 This function has two modes of operation. More...
 
void CopyRowsFromVec (const CuVectorBase< Real > &v)
 This version of CopyRowsFromVec is implemented in ../cudamatrix/cu-vector.cc. More...
 
template<typename OtherReal >
void CopyRowsFromVec (const VectorBase< OtherReal > &v)
 
void CopyColsFromVec (const VectorBase< Real > &v)
 Copies vector into matrix, column-by-column. More...
 
void CopyColFromVec (const VectorBase< Real > &v, const MatrixIndexT col)
 Copy vector into specific column of matrix. More...
 
void CopyRowFromVec (const VectorBase< Real > &v, const MatrixIndexT row)
 Copy vector into specific row of matrix. More...
 
void CopyDiagFromVec (const VectorBase< Real > &v)
 Copy vector into diagonal of matrix. More...
 
const SubVector< Real > Row (MatrixIndexT i) const
 Return specific row of matrix [const]. More...
 
SubVector< Real > Row (MatrixIndexT i)
 Return specific row of matrix. More...
 
SubMatrix< Real > Range (const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
 Return a sub-part of matrix. More...
 
SubMatrix< Real > RowRange (const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
 
SubMatrix< Real > ColRange (const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
 
Real Sum () const
 Returns sum of all elements in matrix. More...
 
Real Trace (bool check_square=true) const
 Returns trace of matrix. More...
 
Real Max () const
 Returns maximum element of matrix. More...
 
Real Min () const
 Returns minimum element of matrix. More...
 
void MulElements (const MatrixBase< Real > &A)
 Element by element multiplication with a given matrix. More...
 
void DivElements (const MatrixBase< Real > &A)
 Divide each element by the corresponding element of a given matrix. More...
 
void Scale (Real alpha)
 Multiply each element with a scalar value. More...
 
void Max (const MatrixBase< Real > &A)
 Set, element-by-element, *this = max(*this, A) More...
 
void Min (const MatrixBase< Real > &A)
 Set, element-by-element, *this = min(*this, A) More...
 
void MulColsVec (const VectorBase< Real > &scale)
 Equivalent to (*this) = (*this) * diag(scale). More...
 
void MulRowsVec (const VectorBase< Real > &scale)
 Equivalent to (*this) = diag(scale) * (*this). More...
 
void MulRowsGroupMat (const MatrixBase< Real > &src)
 Divide each row into src.NumCols() equal groups, and then scale i'th row's j'th group of elements by src(i, j). More...
 
Real LogDet (Real *det_sign=NULL) const
 Returns logdet of matrix. More...
 
void Invert (Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
 matrix inverse. More...
 
void InvertDouble (Real *LogDet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
 matrix inverse [double]. More...
 
void InvertElements ()
 Inverts all the elements of the matrix. More...
 
void Transpose ()
 Transpose the matrix. More...
 
void CopyCols (const MatrixBase< Real > &src, const MatrixIndexT *indices)
 Copies column r from column indices[r] of src. More...
 
void CopyRows (const MatrixBase< Real > &src, const MatrixIndexT *indices)
 Copies row r from row indices[r] of src (does nothing As a special case, if indexes[i] == -1, sets row i to zero. More...
 
void AddCols (const MatrixBase< Real > &src, const MatrixIndexT *indices)
 Add column indices[r] of src to column r. More...
 
void CopyRows (const Real *const *src)
 Copies row r of this matrix from an array of floats at the location given by src[r]. More...
 
void CopyToRows (Real *const *dst) const
 Copies row r of this matrix to the array of floats at the location given by dst[r]. More...
 
void AddRows (Real alpha, const MatrixBase< Real > &src, const MatrixIndexT *indexes)
 Does for each row r, this.Row(r) += alpha * src.row(indexes[r]). More...
 
void AddRows (Real alpha, const Real *const *src)
 Does for each row r, this.Row(r) += alpha * src[r], treating src[r] as the beginning of a region of memory representing a vector of floats, of the same length as this.NumCols(). More...
 
void AddToRows (Real alpha, Real *const *dst) const
 For each row r of this matrix, adds it (times alpha) to the array of floats at the location given by dst[r]. More...
 
void ApplyFloor (Real floor_val)
 Applies floor to all matrix elements. More...
 
void ApplyCeiling (Real ceiling_val)
 Applies floor to all matrix elements. More...
 
void ApplyLog ()
 Calculates log of all the matrix elemnts. More...
 
void ApplyExp ()
 Exponentiate each of the elements. More...
 
void ApplyPow (Real power)
 Applies power to all matrix elements. More...
 
void ApplyPowAbs (Real power, bool include_sign=false)
 Apply power to the absolute value of each element. More...
 
void ApplyHeaviside ()
 Applies the Heaviside step function (x > 0 ? 1 : 0) to all matrix elements Note: in general you can make different choices for x = 0, but for now please leave it as it (i.e. More...
 
void Eig (MatrixBase< Real > *P, VectorBase< Real > *eigs_real, VectorBase< Real > *eigs_imag) const
 Eigenvalue Decomposition of a square NxN matrix into the form (*this) = P D P^{-1}. More...
 
bool Power (Real pow)
 The Power method attempts to take the matrix to a power using a method that works in general for fractional and negative powers. More...
 
void DestructiveSvd (VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
 Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= NumCols), and we return the "skinny" Svd, i.e. More...
 
void Svd (VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
 Compute SVD (*this) = U diag(s) Vt. More...
 
void Svd (VectorBase< Real > *s) const
 Compute SVD but only retain the singular values. More...
 
Real MinSingularValue () const
 Returns smallest singular value. More...
 
void TestUninitialized () const
 
Real Cond () const
 Returns condition number by computing Svd. More...
 
bool IsSymmetric (Real cutoff=1.0e-05) const
 Returns true if matrix is Symmetric. More...
 
bool IsDiagonal (Real cutoff=1.0e-05) const
 Returns true if matrix is Diagonal. More...
 
bool IsUnit (Real cutoff=1.0e-05) const
 Returns true if the matrix is all zeros, except for ones on diagonal. More...
 
bool IsZero (Real cutoff=1.0e-05) const
 Returns true if matrix is all zeros. More...
 
Real FrobeniusNorm () const
 Frobenius norm, which is the sqrt of sum of square elements. More...
 
bool ApproxEqual (const MatrixBase< Real > &other, float tol=0.01) const
 Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). More...
 
bool Equal (const MatrixBase< Real > &other) const
 Tests for exact equality. It's usually preferable to use ApproxEqual. More...
 
Real LargestAbsElem () const
 largest absolute value. More...
 
Real LogSumExp (Real prune=-1.0) const
 Returns log(sum(exp())) without exp overflow If prune > 0.0, it uses a pruning beam, discarding terms less than (max - prune). More...
 
Real ApplySoftMax ()
 Apply soft-max to the collection of all elements of the matrix and return normalizer (log sum of exponentials). More...
 
void Sigmoid (const MatrixBase< Real > &src)
 Set each element to the sigmoid of the corresponding element of "src". More...
 
void Heaviside (const MatrixBase< Real > &src)
 Sets each element to the Heaviside step function (x > 0 ? 1 : 0) of the corresponding element in "src". More...
 
void SoftHinge (const MatrixBase< Real > &src)
 Set each element to y = log(1 + exp(x)) More...
 
void GroupPnorm (const MatrixBase< Real > &src, Real power)
 Apply the function y(i) = (sum_{j = i*G}^{(i+1)*G-1} x_j^(power))^(1 / p). More...
 
void GroupPnormDeriv (const MatrixBase< Real > &input, const MatrixBase< Real > &output, Real power)
 Calculate derivatives for the GroupPnorm function above... More...
 
void GroupMax (const MatrixBase< Real > &src)
 Apply the function y(i) = (max_{j = i*G}^{(i+1)*G-1} x_j Requires src.NumRows() == this->NumRows() and src.NumCols() % this->NumCols() == 0. More...
 
void GroupMaxDeriv (const MatrixBase< Real > &input, const MatrixBase< Real > &output)
 Calculate derivatives for the GroupMax function above, where "input" is the input to the GroupMax function above (i.e. More...
 
void Tanh (const MatrixBase< Real > &src)
 Set each element to the tanh of the corresponding element of "src". More...
 
void DiffSigmoid (const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
 
void DiffTanh (const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
 
void SymPosSemiDefEig (VectorBase< Real > *s, MatrixBase< Real > *P, Real check_thresh=0.001)
 Uses Svd to compute the eigenvalue decomposition of a symmetric positive semi-definite matrix: (*this) = rP * diag(rS) * rP^T, with rP an orthogonal matrix so rP^{-1} = rP^T. More...
 
void Add (const Real alpha)
 Add a scalar to each element. More...
 
void AddToDiag (const Real alpha)
 Add a scalar to each diagonal element. More...
 
template<typename OtherReal >
void AddVecVec (const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
 *this += alpha * a * b^T More...
 
template<typename OtherReal >
void AddVecToRows (const Real alpha, const VectorBase< OtherReal > &v)
 [each row of *this] += alpha * v More...
 
template<typename OtherReal >
void AddVecToCols (const Real alpha, const VectorBase< OtherReal > &v)
 [each col of *this] += alpha * v More...
 
void AddMat (const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
 *this += alpha * M [or M^T] More...
 
void SymAddMat2 (const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA, Real beta)
 *this = beta * *this + alpha * M M^T, for symmetric matrices. More...
 
void AddDiagVecMat (const Real alpha, const VectorBase< Real > &v, const MatrixBase< Real > &M, MatrixTransposeType transM, Real beta=1.0)
 *this = beta * *this + alpha * diag(v) * M [or M^T]. More...
 
void AddMatDiagVec (const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, VectorBase< Real > &v, Real beta=1.0)
 *this = beta * *this + alpha * M [or M^T] * diag(v) The same as adding M but scaling each column M_j by v(j). More...
 
void AddMatMatElements (const Real alpha, const MatrixBase< Real > &A, const MatrixBase< Real > &B, const Real beta)
 *this = beta * *this + alpha * A .* B (.* element by element multiplication) More...
 
template<typename OtherReal >
void AddSp (const Real alpha, const SpMatrix< OtherReal > &S)
 *this += alpha * S More...
 
void AddMatMat (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
 
void SetMatMatDivMat (const MatrixBase< Real > &A, const MatrixBase< Real > &B, const MatrixBase< Real > &C)
 *this = a * b / c (by element; when c = 0, *this = a) More...
 
void AddMatSmat (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
 A version of AddMatMat specialized for when the second argument contains a lot of zeroes. More...
 
void AddSmatMat (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
 A version of AddMatMat specialized for when the first argument contains a lot of zeroes. More...
 
void AddMatMatMat (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const Real beta)
 this <– beta*this + alpha*A*B*C. More...
 
void AddSpMat (const Real alpha, const SpMatrix< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
 this <– beta*this + alpha*SpA*B. More...
 
void AddTpMat (const Real alpha, const TpMatrix< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
 this <– beta*this + alpha*A*B. More...
 
void AddMatSp (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const Real beta)
 this <– beta*this + alpha*A*B. More...
 
void AddSpMatSp (const Real alpha, const SpMatrix< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType transB, const SpMatrix< Real > &C, const Real beta)
 this <– beta*this + alpha*A*B*C. More...
 
void AddMatTp (const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const TpMatrix< Real > &B, MatrixTransposeType transB, const Real beta)
 this <– beta*this + alpha*A*B. More...
 
void AddTpTp (const Real alpha, const TpMatrix< Real > &A, MatrixTransposeType transA, const TpMatrix< Real > &B, MatrixTransposeType transB, const Real beta)
 this <– beta*this + alpha*A*B. More...
 
void AddSpSp (const Real alpha, const SpMatrix< Real > &A, const SpMatrix< Real > &B, const Real beta)
 this <– beta*this + alpha*A*B. More...
 
void CopyLowerToUpper ()
 Copy lower triangle to upper triangle (symmetrize) More...
 
void CopyUpperToLower ()
 Copy upper triangle to lower triangle (symmetrize) More...
 
void OrthogonalizeRows ()
 This function orthogonalizes the rows of a matrix using the Gram-Schmidt process. More...
 
void Read (std::istream &in, bool binary, bool add=false)
 stream read. More...
 
void Write (std::ostream &out, bool binary) const
 write to stream. More...
 
void LapackGesvd (VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
 
template<>
void AddVecVec (const float alpha, const VectorBase< float > &ra, const VectorBase< float > &rb)
 
template<>
void AddVecVec (const double alpha, const VectorBase< double > &ra, const VectorBase< double > &rb)
 
template<>
void AddVecVec (const float alpha, const VectorBase< float > &a, const VectorBase< float > &rb)
 
template<>
void AddVecVec (const double alpha, const VectorBase< double > &a, const VectorBase< double > &rb)
 
template<>
void CopyFromSp (const SpMatrix< float > &M)
 
template<>
void CopyFromSp (const SpMatrix< double > &M)
 

Protected Member Functions

 MatrixBase (Real *data, MatrixIndexT cols, MatrixIndexT rows, MatrixIndexT stride)
 Initializer, callable only from child. More...
 
 MatrixBase ()
 Initializer, callable only from child. More...
 
 ~MatrixBase ()
 
Real * Data_workaround () const
 A workaround that allows SubMatrix to get a pointer to non-const data for const Matrix. More...
 

Protected Attributes

Real * data_
 data memory area More...
 
MatrixIndexT num_cols_
 these atributes store the real matrix size as it is stored in memory including memalignment More...
 
MatrixIndexT num_rows_
 < Number of columns More...
 
MatrixIndexT stride_
 < Number of rows More...
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (MatrixBase)
 

Friends

class Matrix< Real >
 
class CuMatrixBase< Real >
 
class CuMatrix< Real >
 
class CuSubMatrix< Real >
 
class CuPackedMatrix< Real >
 
class PackedMatrix< Real >
 
class SubMatrix< Real >
 
Real kaldi::TraceMatMat (const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
 

Detailed Description

template<typename Real>
singleton kaldi::MatrixBase< Real >

Base class which provides matrix operations not involving resizing or allocation.

Classes Matrix and SubMatrix inherit from it and take care of allocation and resizing.

Definition at line 45 of file kaldi-matrix.h.

Constructor & Destructor Documentation

MatrixBase ( Real *  data,
MatrixIndexT  cols,
MatrixIndexT  rows,
MatrixIndexT  stride 
)
inlineexplicitprotected

Initializer, callable only from child.

Definition at line 704 of file kaldi-matrix.h.

704  :
705  data_(data), num_cols_(cols), num_rows_(rows), stride_(stride) {
707  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:133
MatrixBase ( )
inlineexplicitprotected

Initializer, callable only from child.

Empty initializer, for un-initialized matrix.

Definition at line 711 of file kaldi-matrix.h.

711  : data_(NULL) {
713  }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:133
~MatrixBase ( )
inlineprotected

Definition at line 716 of file kaldi-matrix.h.

716 { }

Member Function Documentation

void Add ( const Real  alpha)

Add a scalar to each element.

Definition at line 1523 of file kaldi-matrix.cc.

References data_.

Referenced by AmSgmm2::ComponentPosteriors(), AmSgmm2::LogLikelihood(), kaldi::UnitTestCompressedMatrix(), kaldi::UnitTestCuMatrixAdd(), kaldi::UnitTestCuMatrixAdd2(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestSetRandUniform(), and kaldi::UnitTestSimpleForMat().

1523  {
1524  Real *data = data_;
1525  MatrixIndexT stride = stride_;
1526  for (MatrixIndexT r = 0; r < num_rows_; r++)
1527  for (MatrixIndexT c = 0; c < num_cols_; c++)
1528  data[c + stride*r] += alpha;
1529 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void AddCols ( const MatrixBase< Real > &  src,
const MatrixIndexT indices 
)

Add column indices[r] of src to column r.

As a special case, if indexes[i] == -1, skip column i indices.size() must equal this->NumCols(), all elements of "reorder" must be in [-1, src.NumCols()-1], and src.NumRows() must equal this.NumRows()

Definition at line 2640 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::stride_.

2641  {
2642  KALDI_ASSERT(NumRows() == src.NumRows());
2643  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2644  this_stride = stride_, src_stride = src.stride_;
2645  Real *this_data = this->data_;
2646  const Real *src_data = src.data_;
2647 #ifdef KALDI_PARANOID
2648  MatrixIndexT src_cols = src.NumCols();
2649  for (MatrixIndexT i = 0; i < num_cols; i++)
2650  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2651 #endif
2652 
2653  // For the sake of memory locality we do this row by row, rather
2654  // than doing it column-wise using cublas_Xcopy
2655  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2656  const MatrixIndexT *index_ptr = &(indices[0]);
2657  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2658  if (*index_ptr >= 0)
2659  this_data[c] += src_data[*index_ptr];
2660  }
2661  }
2662 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddDiagVecMat ( const Real  alpha,
const VectorBase< Real > &  v,
const MatrixBase< Real > &  M,
MatrixTransposeType  transM,
Real  beta = 1.0 
)

*this = beta * *this + alpha * diag(v) * M [or M^T].

The same as adding M but scaling each row M_i by v(i).

Definition at line 426 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), kaldi::SameDim(), MatrixBase< Real >::Stride(), and kaldi::swap().

Referenced by kaldi::UnitTestCuDiffNormalizePerRow(), and kaldi::UnitTestCuDiffSoftmax().

430  {
431  if (beta != 1.0) this->Scale(beta);
432 
433  if (transM == kNoTrans) {
434  KALDI_ASSERT(SameDim(*this, M));
435  } else {
436  KALDI_ASSERT(M.NumRows() == NumCols() && M.NumCols() == NumRows());
437  }
438  KALDI_ASSERT(v.Dim() == this->NumRows());
439 
440  MatrixIndexT M_row_stride = M.Stride(), M_col_stride = 1, stride = stride_,
441  num_rows = num_rows_, num_cols = num_cols_;
442  if (transM == kTrans) std::swap(M_row_stride, M_col_stride);
443  Real *data = data_;
444  const Real *Mdata = M.Data(), *vdata = v.Data();
445  if (num_rows_ == 0) return;
446  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride, Mdata += M_row_stride, vdata++)
447  cblas_Xaxpy(num_cols, alpha * *vdata, Mdata, M_col_stride, data, 1);
448 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:96
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMat ( const Real  alpha,
const MatrixBase< Real > &  M,
MatrixTransposeType  transA = kNoTrans 
)

*this += alpha * M [or M^T]

Definition at line 349 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, and MatrixBase< Real >::stride_.

Referenced by Fmpe::AccStats(), BasisFmllrAccus::AccuGradientScatter(), AffineXformStats::Add(), GaussClusterable::Add(), AccumDiagGmm::Add(), IvectorExtractorStats::Add(), CuMatrixBase< Real >::AddMatBlocks(), GeneralMatrix::AddToMat(), MatrixBase< Real >::ApproxEqual(), MatrixExponential< Real >::BackpropTaylor(), kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), MleAmSgmm2Updater::ComputeMPrior(), MleAmSgmm2Updater::ComputeSMeans(), BasisFmllrEstimate::ComputeTransform(), kaldi::FmllrAuxfGradient(), FmllrSgmm2Accs::FmllrObjGradient(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), MleAmSgmm2Updater::MapUpdateM(), DiagGmm::Perturb(), AffineXformStats::Read(), AccumFullGmm::Read(), LdaEstimate::Read(), OnlineCmvn::SmoothOnlineCmvnStats(), AccumDiagGmm::SmoothWithModel(), kaldi::SolveQuadraticMatrixProblem(), GaussClusterable::Sub(), MatrixBase< Real >::SymPosSemiDefEig(), test_io(), kaldi::TestFmpe(), kaldi::nnet2::TransformTrainingExample(), kaldi::TypeOneUsage(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddMatMatElements(), kaldi::UnitTestAddMatSelf(), kaldi::UnitTestAddOuterProductPlusMinus(), kaldi::UnitTestAxpy(), kaldi::UnitTestCompressedMatrix(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuMatrixAddMat(), kaldi::UnitTestDeterminant(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestMatrixExponential(), kaldi::UnitTestMatrixExponentialBackprop(), kaldi::UnitTestSparseMatrixAddToMat(), FmllrSgmm2Accs::Update(), EbwAmSgmm2Updater::UpdateM(), EbwAmSgmm2Updater::UpdateN(), IvectorExtractorStats::UpdateVariances(), MleAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateWGetStats(), and UpdateWClass::~UpdateWClass().

350  {
351  if (&A == this) {
352  if (transA == kNoTrans) {
353  Scale(alpha + 1.0);
354  } else {
355  KALDI_ASSERT(num_rows_ == num_cols_ && "AddMat: adding to self (transposed): not symmetric.");
356  Real *data = data_;
357  if (alpha == 1.0) { // common case-- handle separately.
358  for (MatrixIndexT row = 0; row < num_rows_; row++) {
359  for (MatrixIndexT col = 0; col < row; col++) {
360  Real *lower = data + (row * stride_) + col, *upper = data + (col
361  * stride_) + row;
362  Real sum = *lower + *upper;
363  *lower = *upper = sum;
364  }
365  *(data + (row * stride_) + row) *= 2.0; // diagonal.
366  }
367  } else {
368  for (MatrixIndexT row = 0; row < num_rows_; row++) {
369  for (MatrixIndexT col = 0; col < row; col++) {
370  Real *lower = data + (row * stride_) + col, *upper = data + (col
371  * stride_) + row;
372  Real lower_tmp = *lower;
373  *lower += alpha * *upper;
374  *upper += alpha * lower_tmp;
375  }
376  *(data + (row * stride_) + row) *= (1.0 + alpha); // diagonal.
377  }
378  }
379  }
380  } else {
381  int aStride = (int) A.stride_, stride = stride_;
382  Real *adata = A.data_, *data = data_;
383  if (transA == kNoTrans) {
384  KALDI_ASSERT(A.num_rows_ == num_rows_ && A.num_cols_ == num_cols_);
385  if (num_rows_ == 0) return;
386  for (MatrixIndexT row = 0; row < num_rows_; row++, adata += aStride,
387  data += stride) {
388  cblas_Xaxpy(num_cols_, alpha, adata, 1, data, 1);
389  }
390  } else {
391  KALDI_ASSERT(A.num_cols_ == num_rows_ && A.num_rows_ == num_cols_);
392  if (num_rows_ == 0) return;
393  for (MatrixIndexT row = 0; row < num_rows_; row++, adata++, data += stride)
394  cblas_Xaxpy(num_cols_, alpha, adata, aStride, data, 1);
395  }
396  }
397 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatDiagVec ( const Real  alpha,
const MatrixBase< Real > &  M,
MatrixTransposeType  transM,
VectorBase< Real > &  v,
Real  beta = 1.0 
)

*this = beta * *this + alpha * M [or M^T] * diag(v) The same as adding M but scaling each column M_j by v(j).

Definition at line 451 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), kaldi::SameDim(), MatrixBase< Real >::Stride(), and kaldi::swap().

455  {
456 
457  if (beta != 1.0) this->Scale(beta);
458 
459  if (transM == kNoTrans) {
460  KALDI_ASSERT(SameDim(*this, M));
461  } else {
462  KALDI_ASSERT(M.NumRows() == NumCols() && M.NumCols() == NumRows());
463  }
464  KALDI_ASSERT(v.Dim() == this->NumCols());
465 
466  MatrixIndexT M_row_stride = M.Stride(),
467  M_col_stride = 1,
468  stride = stride_,
469  num_rows = num_rows_,
470  num_cols = num_cols_;
471 
472  if (transM == kTrans)
473  std::swap(M_row_stride, M_col_stride);
474 
475  Real *data = data_;
476  const Real *Mdata = M.Data(), *vdata = v.Data();
477  if (num_rows_ == 0) return;
478  for (MatrixIndexT i = 0; i < num_rows; i++){
479  for(MatrixIndexT j = 0; j < num_cols; j ++ ){
480  data[i*stride + j] += alpha * vdata[j] * Mdata[i*M_row_stride + j*M_col_stride];
481  }
482  }
483 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:96
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatMat ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)

Definition at line 164 of file kaldi-matrix.cc.

References kaldi::cblas_Xgemm(), data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, and MatrixBase< Real >::stride_.

Referenced by MatrixBase< Real >::AddMatMatMat(), MatrixBase< BaseFloat >::AddMatSp(), MatrixBase< BaseFloat >::AddMatTp(), MatrixBase< BaseFloat >::AddSpMat(), MatrixBase< BaseFloat >::AddTpMat(), MatrixBase< BaseFloat >::AddTpTp(), kaldi::ApplyFeatureTransformToStats(), kaldi::ApplyInvPreXformToChange(), kaldi::ApplyPreXformToGradient(), Sgmm2Project::ApplyProjection(), Fmpe::ApplyProjection(), MatrixExponential< Real >::Backprop(), MatrixExponential< Real >::BackpropTaylor(), kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::CholeskyUnitTestTr(), kaldi::ComposeTransforms(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), kaldi::ComputeLdaTransform(), AmSgmm2::ComputeNormalizersInternal(), kaldi::ComputePca(), MleAmSgmm2Updater::ComputeSMeans(), MatrixExponential< Real >::ComputeTaylor(), LogisticRegression::DoStep(), LdaEstimate::Estimate(), FeatureTransformEstimate::EstimateInternal(), FeatureTransformEstimateMulti::EstimateTransformPart(), IvectorExtractorStats::FlushCache(), kaldi::generate_features(), LogisticRegression::GetLogPosteriors(), PldaEstimator::GetOutput(), DiagGmm::LogLikelihoods(), main(), kaldi::nnet3::PerturbImage(), MatrixBase< Real >::Power(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), kaldi::SlowMatMul(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), TestSgmm2PreXform(), SpMatrix< Real >::TopEigs(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSpMat(), IvectorExtractor::TransformIvectors(), kaldi::UnitInvert(), kaldi::UnitTestAddDiagMat2(), kaldi::UnitTestAddDiagMatMat(), kaldi::UnitTestAddMat2Sp(), kaldi::UnitTestAddMatMatNans(), kaldi::UnitTestAddMatMatSpeed(), kaldi::UnitTestCholesky(), kaldi::UnitTestCuMatrixAddMatMat(), kaldi::UnitTestDct(), kaldi::UnitTestDeterminant(), kaldi::UnitTestDeterminantSign(), kaldi::UnitTestEig(), kaldi::UnitTestEigSymmetric(), UnitTestEstimateFullGmm(), kaldi::UnitTestInverse(), kaldi::UnitTestInvert(), kaldi::UnitTestLimitCondInvert(), kaldi::UnitTestMatrixExponential(), kaldi::UnitTestMmul(), kaldi::UnitTestNonsymmetricPower(), kaldi::UnitTestOrthogonalizeRows(), kaldi::UnitTestPca(), kaldi::UnitTestPldaEstimation(), kaldi::UnitTestPower(), kaldi::UnitTestRankNUpdate(), kaldi::UnitTestSpInvert(), kaldi::UnitTestSvdNodestroy(), kaldi::UnitTestSymAddMat2(), kaldi::UnitTestTopEigs(), kaldi::UnitTestTpInvert(), kaldi::UnitTestTrace(), kaldi::UnitTestTrain(), kaldi::UnitTestTransposeScatter(), PldaUnsupervisedAdaptor::UpdatePlda(), IvectorExtractorStats::UpdatePrior(), IvectorExtractorStats::UpdateVariances(), MleAmSgmm2Updater::UpdateW(), and MleAmSgmm2Updater::UpdateWGetStats().

169  {
170  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
171  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
172  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
173  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
174  KALDI_ASSERT(&A != this && &B != this);
175  if (num_rows_ == 0) return;
176  cblas_Xgemm(alpha, transA, A.data_, A.num_rows_, A.num_cols_, A.stride_,
177  transB, B.data_, B.stride_, beta, data_, num_rows_, num_cols_, stride_);
178 
179 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void cblas_Xgemm(const float alpha, MatrixTransposeType transA, const float *Adata, MatrixIndexT a_num_rows, MatrixIndexT a_num_cols, MatrixIndexT a_stride, MatrixTransposeType transB, const float *Bdata, MatrixIndexT b_stride, const float beta, float *Mdata, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT stride)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatMatElements ( const Real  alpha,
const MatrixBase< Real > &  A,
const MatrixBase< Real > &  B,
const Real  beta 
)

*this = beta * *this + alpha * A .* B (.* element by element multiplication)

Definition at line 486 of file kaldi-matrix.cc.

References MatrixBase< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

489  {
490  KALDI_ASSERT(A.NumRows() == B.NumRows() && A.NumCols() == B.NumCols());
491  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
492  Real *data = data_;
493  const Real *dataA = A.Data();
494  const Real *dataB = B.Data();
495 
496  for (MatrixIndexT i = 0; i < num_rows_; i++) {
497  for (MatrixIndexT j = 0; j < num_cols_; j++) {
498  data[j] = beta*data[j] + alpha*dataA[j]*dataB[j];
499  }
500  data += Stride();
501  dataA += A.Stride();
502  dataB += B.Stride();
503  }
504 }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:64
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatMatMat ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const MatrixBase< Real > &  C,
MatrixTransposeType  transC,
const Real  beta 
)

this <– beta*this + alpha*A*B*C.

Definition at line 1587 of file kaldi-matrix.cc.

References MatrixBase< Real >::AddMatMat(), kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, and kaldi::swap().

Referenced by MatrixBase< BaseFloat >::AddSpMatSp(), kaldi::nnet3::PerturbImage(), UnitTestEstimateLda(), kaldi::UnitTestSvd(), and kaldi::UnitTestTrace().

1591  {
1592  // Note on time taken with different orders of computation. Assume not transposed in this /
1593  // discussion. Firstly, normalize expressions using A.NumCols == B.NumRows and B.NumCols == C.NumRows, prefer
1594  // rows where there is a choice.
1595  // time taken for (AB) is: A.NumRows*B.NumRows*C.Rows
1596  // time taken for (AB)C is A.NumRows*C.NumRows*C.Cols
1597  // so this order is A.NumRows*B.NumRows*C.NumRows + A.NumRows*C.NumRows*C.NumCols.
1598 
1599  // time taken for (BC) is: B.NumRows*C.NumRows*C.Cols
1600  // time taken for A(BC) is: A.NumRows*B.NumRows*C.Cols
1601  // so this order is B.NumRows*C.NumRows*C.NumCols + A.NumRows*B.NumRows*C.Cols
1602 
1603  MatrixIndexT ARows = A.num_rows_, ACols = A.num_cols_, BRows = B.num_rows_, BCols = B.num_cols_,
1604  CRows = C.num_rows_, CCols = C.num_cols_;
1605  if (transA == kTrans) std::swap(ARows, ACols);
1606  if (transB == kTrans) std::swap(BRows, BCols);
1607  if (transC == kTrans) std::swap(CRows, CCols);
1608 
1609  MatrixIndexT AB_C_time = ARows*BRows*CRows + ARows*CRows*CCols;
1610  MatrixIndexT A_BC_time = BRows*CRows*CCols + ARows*BRows*CCols;
1611 
1612  if (AB_C_time < A_BC_time) {
1613  Matrix<Real> AB(ARows, BCols);
1614  AB.AddMatMat(1.0, A, transA, B, transB, 0.0); // AB = A * B.
1615  (*this).AddMatMat(alpha, AB, kNoTrans, C, transC, beta);
1616  } else {
1617  Matrix<Real> BC(BRows, CCols);
1618  BC.AddMatMat(1.0, B, transB, C, transC, 0.0); // BC = B * C.
1619  (*this).AddMatMat(alpha, A, transA, BC, kNoTrans, beta);
1620  }
1621 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
int32 MatrixIndexT
Definition: matrix-common.h:96
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddMatSmat ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)

A version of AddMatMat specialized for when the second argument contains a lot of zeroes.

Definition at line 260 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, MatrixBase< Real >::stride_, and kaldi::Xgemv_sparsevec().

265  {
266  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
267  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
268  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
269  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
270  KALDI_ASSERT(&A != this && &B != this);
271 
272  // We iterate over the columns of B.
273 
274  MatrixIndexT Astride = A.stride_, Bstride = B.stride_, stride = this->stride_,
275  Arows = A.num_rows_, Acols = A.num_cols_;
276  Real *data = this->data_, *Adata = A.data_, *Bdata = B.data_;
277  MatrixIndexT num_cols = this->num_cols_;
278  if (transB == kNoTrans) {
279  // Iterate over the columns of *this and of B.
280  for (MatrixIndexT c = 0; c < num_cols; c++) {
281  // for each column of *this, do
282  // [this column] = [alpha * A * this column of B] + [beta * this column]
283  Xgemv_sparsevec(transA, Arows, Acols, alpha, Adata, Astride,
284  Bdata + c, Bstride, beta, data + c, stride);
285  }
286  } else {
287  // Iterate over the columns of *this and the rows of B.
288  for (MatrixIndexT c = 0; c < num_cols; c++) {
289  // for each column of *this, do
290  // [this column] = [alpha * A * this row of B] + [beta * this column]
291  Xgemv_sparsevec(transA, Arows, Acols, alpha, Adata, Astride,
292  Bdata + (c * Bstride), 1, beta, data + c, stride);
293  }
294  }
295 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void Xgemv_sparsevec(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, Real alpha, const Real *Mdata, MatrixIndexT stride, const Real *xdata, MatrixIndexT incX, Real beta, Real *ydata, MatrixIndexT incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatSp ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const SpMatrix< Real > &  B,
const Real  beta 
)
inline

this <– beta*this + alpha*A*B.

Definition at line 628 of file kaldi-matrix.h.

Referenced by AmSgmm2::ComputeNormalizersInternal(), MleAmSgmm2Updater::MapUpdateM(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), kaldi::TraceMatSpMatSp(), kaldi::UnitTestAddMat2Sp(), kaldi::UnitTestSolve(), EbwAmSgmm2Updater::UpdateM(), and EbwAmSgmm2Updater::UpdateN().

631  {
632  Matrix<Real> M(B);
633  return AddMatMat(alpha, A, transA, M, kNoTrans, beta);
634  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddMatTp ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const TpMatrix< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)
inline

this <– beta*this + alpha*A*B.

Definition at line 645 of file kaldi-matrix.h.

Referenced by IvectorExtractorStats::CommitStatsForW(), Sgmm2Project::ComputeLdaTransform(), kaldi::UnitTestCuMatrixAddMatTp(), and PldaUnsupervisedAdaptor::UpdatePlda().

648  {
649  Matrix<Real> M(B);
650  return AddMatMat(alpha, A, transA, M, transB, beta);
651  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddRows ( Real  alpha,
const MatrixBase< Real > &  src,
const MatrixIndexT indexes 
)

Does for each row r, this.Row(r) += alpha * src.row(indexes[r]).

If indexes[r] < 0, does not add anything. all elements of "indexes" must be in [-1, src.NumRows()-1], and src.NumCols() must equal this.NumCols().

Definition at line 2706 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), data_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

2708  {
2709  KALDI_ASSERT(NumCols() == src.NumCols());
2710  MatrixIndexT num_rows = num_rows_,
2711  num_cols = num_cols_, this_stride = stride_;
2712  Real *this_data = this->data_;
2713 
2714  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2715  MatrixIndexT index = indexes[r];
2716  KALDI_ASSERT(index >= -1 && index < src.NumRows());
2717  if (index != -1)
2718  cblas_Xaxpy(num_cols, alpha, src.RowData(index), 1, this_data, 1);
2719  }
2720 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddRows ( Real  alpha,
const Real *const *  src 
)

Does for each row r, this.Row(r) += alpha * src[r], treating src[r] as the beginning of a region of memory representing a vector of floats, of the same length as this.NumCols().

If src[r] is NULL, does not add anything.

Definition at line 2723 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), and data_.

2723  {
2724  MatrixIndexT num_rows = num_rows_,
2725  num_cols = num_cols_, this_stride = stride_;
2726  Real *this_data = this->data_;
2727 
2728  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2729  const Real *const src_data = src[r];
2730  if (src_data != NULL)
2731  cblas_Xaxpy(num_cols, alpha, src_data, 1, this_data, 1);
2732  }
2733 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
void AddSmatMat ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)

A version of AddMatMat specialized for when the first argument contains a lot of zeroes.

Definition at line 298 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, MatrixBase< Real >::stride_, and kaldi::Xgemv_sparsevec().

Referenced by SpMatrix< Real >::AddSmat2Sp(), and kaldi::UnitTestAddMatSmat().

303  {
304  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
305  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
306  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
307  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
308  KALDI_ASSERT(&A != this && &B != this);
309 
310  MatrixIndexT Astride = A.stride_, Bstride = B.stride_, stride = this->stride_,
311  Brows = B.num_rows_, Bcols = B.num_cols_;
312  MatrixTransposeType invTransB = (transB == kTrans ? kNoTrans : kTrans);
313  Real *data = this->data_, *Adata = A.data_, *Bdata = B.data_;
314  MatrixIndexT num_rows = this->num_rows_;
315  if (transA == kNoTrans) {
316  // Iterate over the rows of *this and of A.
317  for (MatrixIndexT r = 0; r < num_rows; r++) {
318  // for each row of *this, do
319  // [this row] = [alpha * (this row of A) * B^T] + [beta * this row]
320  Xgemv_sparsevec(invTransB, Brows, Bcols, alpha, Bdata, Bstride,
321  Adata + (r * Astride), 1, beta, data + (r * stride), 1);
322  }
323  } else {
324  // Iterate over the rows of *this and the columns of A.
325  for (MatrixIndexT r = 0; r < num_rows; r++) {
326  // for each row of *this, do
327  // [this row] = [alpha * (this column of A) * B^T] + [beta * this row]
328  Xgemv_sparsevec(invTransB, Brows, Bcols, alpha, Bdata, Bstride,
329  Adata + r, Astride, beta, data + (r * stride), 1);
330  }
331  }
332 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void Xgemv_sparsevec(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, Real alpha, const Real *Mdata, MatrixIndexT stride, const Real *xdata, MatrixIndexT incX, Real beta, Real *ydata, MatrixIndexT incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixTransposeType
Definition: matrix-common.h:30
template void AddSp ( const Real  alpha,
const SpMatrix< OtherReal > &  S 
)

*this += alpha * S

Definition at line 401 of file kaldi-matrix.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by kaldi::UnitTestAddSp().

401  {
402  KALDI_ASSERT(S.NumRows() == NumRows() && S.NumRows() == NumCols());
403  Real *data = data_; const OtherReal *sdata = S.Data();
404  MatrixIndexT num_rows = NumRows(), stride = Stride();
405  for (MatrixIndexT i = 0; i < num_rows; i++) {
406  for (MatrixIndexT j = 0; j < i; j++, sdata++) {
407  data[i*stride + j] += alpha * *sdata;
408  data[j*stride + i] += alpha * *sdata;
409  }
410  data[i*stride + i] += alpha * *sdata++;
411  }
412 }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:64
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddSpMat ( const Real  alpha,
const SpMatrix< Real > &  A,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)
inline

this <– beta*this + alpha*SpA*B.

Definition at line 612 of file kaldi-matrix.h.

Referenced by FmllrSgmm2Accs::FmllrObjGradient(), MleAmSgmm2Updater::MapUpdateM(), and kaldi::UnitTestTopEigs().

615  {
616  Matrix<Real> M(A);
617  return AddMatMat(alpha, M, kNoTrans, B, transB, beta);
618  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddSpMatSp ( const Real  alpha,
const SpMatrix< Real > &  A,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const SpMatrix< Real > &  C,
const Real  beta 
)
inline

this <– beta*this + alpha*A*B*C.

Definition at line 636 of file kaldi-matrix.h.

640  {
641  Matrix<Real> M(A), N(C);
642  return AddMatMatMat(alpha, M, kNoTrans, B, transB, N, kNoTrans, beta);
643  }
void AddMatMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const Real beta)
this <– beta*this + alpha*A*B*C.
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddSpSp ( const Real  alpha,
const SpMatrix< Real > &  A,
const SpMatrix< Real > &  B,
const Real  beta 
)

this <– beta*this + alpha*A*B.

Definition at line 335 of file kaldi-matrix.cc.

References kaldi::cblas_Xsymm(), data_, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by kaldi::UnitTestMmulSym().

336  {
337  MatrixIndexT sz = num_rows_;
338  KALDI_ASSERT(sz == num_cols_ && sz == A_in.NumRows() && sz == B_in.NumRows());
339 
340  Matrix<Real> A(A_in), B(B_in);
341  // CblasLower or CblasUpper would work below as symmetric matrix is copied
342  // fully (to save work, we used the matrix constructor from SpMatrix).
343  // CblasLeft means A is on the left: C <-- alpha A B + beta C
344  if (sz == 0) return;
345  cblas_Xsymm(alpha, sz, A.data_, A.stride_, B.data_, B.stride_, beta, data_, stride_);
346 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
void cblas_Xsymm(const float alpha, MatrixIndexT sz, const float *Adata, MatrixIndexT a_stride, const float *Bdata, MatrixIndexT b_stride, const float beta, float *Mdata, MatrixIndexT stride)
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddToDiag ( const Real  alpha)

Add a scalar to each diagonal element.

Definition at line 1532 of file kaldi-matrix.cc.

References data_.

Referenced by kaldi::UnitTestCuMatrixAddToDiag().

1532  {
1533  Real *data = data_;
1534  MatrixIndexT this_stride = stride_ + 1,
1535  num_to_add = std::min(num_rows_, num_cols_);
1536  for (MatrixIndexT r = 0; r < num_to_add; r++)
1537  data[r * this_stride] += alpha;
1538 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void AddToRows ( Real  alpha,
Real *const *  dst 
) const

For each row r of this matrix, adds it (times alpha) to the array of floats at the location given by dst[r].

If dst[r] is NULL, does not do anything for that row. Requires that none of the memory regions pointed to by the pointers in "dst" overlap (e.g. none of the pointers should be the same).

Definition at line 2736 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), and data_.

Referenced by kaldi::UnitTestAddToRows().

2736  {
2737  MatrixIndexT num_rows = num_rows_,
2738  num_cols = num_cols_, this_stride = stride_;
2739  const Real *this_data = this->data_;
2740 
2741  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2742  Real *const dst_data = dst[r];
2743  if (dst_data != NULL)
2744  cblas_Xaxpy(num_cols, alpha, this_data, 1, dst_data, 1);
2745  }
2746 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
void AddTpMat ( const Real  alpha,
const TpMatrix< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)
inline

this <– beta*this + alpha*A*B.

Definition at line 620 of file kaldi-matrix.h.

Referenced by kaldi::UnitTestCuMatrixAddTpMat().

623  {
624  Matrix<Real> M(A);
625  return AddMatMat(alpha, M, transA, B, transB, beta);
626  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void AddTpTp ( const Real  alpha,
const TpMatrix< Real > &  A,
MatrixTransposeType  transA,
const TpMatrix< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)
inline

this <– beta*this + alpha*A*B.

Definition at line 654 of file kaldi-matrix.h.

657  {
658  Matrix<Real> M(A), N(B);
659  return AddMatMat(alpha, M, transA, N, transB, beta);
660  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
template void AddVecToCols ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

[each col of *this] += alpha * v

Definition at line 2832 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, KALDI_ASSERT, and VectorBase< Real >::Set().

Referenced by kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToColsSpeed(), and kaldi::UnitTestCuMatrixAddVecToCols().

2832  {
2833  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2834  stride = stride_;
2835  KALDI_ASSERT(v.Dim() == num_rows);
2836 
2837  if (num_rows <= 64) {
2838  Real *data = data_;
2839  const OtherReal *vdata = v.Data();
2840  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
2841  Real to_add = alpha * vdata[i];
2842  for (MatrixIndexT j = 0; j < num_cols; j++)
2843  data[j] += to_add;
2844  }
2845 
2846  } else {
2847  Vector<OtherReal> ones(num_cols);
2848  ones.Set(1.0);
2849  this->AddVecVec(alpha, v, ones);
2850  }
2851 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
template void AddVecToRows ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

[each row of *this] += alpha * v

Definition at line 2801 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, KALDI_ASSERT, and VectorBase< Real >::Set().

Referenced by kaldi::ApplyCmvn(), kaldi::ApplyCmvnReverse(), AmSgmm2::ComponentLogLikes(), kaldi::UnitTestAddVecToRows(), kaldi::UnitTestAddVecToRowsSpeed(), kaldi::UnitTestCuMatrixAddVecToRows(), and kaldi::UnitTestPldaEstimation().

2801  {
2802  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2803  stride = stride_;
2804  KALDI_ASSERT(v.Dim() == num_cols);
2805  if(num_cols <= 64) {
2806  Real *data = data_;
2807  const OtherReal *vdata = v.Data();
2808  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
2809  for (MatrixIndexT j = 0; j < num_cols; j++)
2810  data[j] += alpha * vdata[j];
2811  }
2812 
2813  } else {
2814  Vector<OtherReal> ones(num_rows);
2815  ones.Set(1.0);
2816  this->AddVecVec(alpha, ones, v);
2817  }
2818 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
void AddVecVec ( const float  alpha,
const VectorBase< float > &  ra,
const VectorBase< float > &  rb 
)
void AddVecVec ( const double  alpha,
const VectorBase< double > &  ra,
const VectorBase< double > &  rb 
)
void AddVecVec ( const float  alpha,
const VectorBase< float > &  a,
const VectorBase< float > &  rb 
)

Definition at line 112 of file kaldi-matrix.cc.

References kaldi::cblas_Xger(), VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), and KALDI_ASSERT.

114  {
115  KALDI_ASSERT(a.Dim() == num_rows_ && rb.Dim() == num_cols_);
116  cblas_Xger(a.Dim(), rb.Dim(), alpha, a.Data(), 1, rb.Data(),
117  1, data_, stride_);
118 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)
void AddVecVec ( const double  alpha,
const VectorBase< double > &  a,
const VectorBase< double > &  rb 
)

Definition at line 154 of file kaldi-matrix.cc.

References kaldi::cblas_Xger(), VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), and KALDI_ASSERT.

156  {
157  KALDI_ASSERT(a.Dim() == num_rows_ && rb.Dim() == num_cols_);
158  if (num_rows_ == 0) return;
159  cblas_Xger(a.Dim(), rb.Dim(), alpha, a.Data(), 1, rb.Data(),
160  1, data_, stride_);
161 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)
void AddVecVec ( const Real  alpha,
const VectorBase< OtherReal > &  a,
const VectorBase< OtherReal > &  b 
)

*this += alpha * a * b^T

Definition at line 122 of file kaldi-matrix.cc.

References kaldi::cblas_Xger(), VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by AccumFullGmm::AccumulateFromPosteriors(), AccumDiagGmm::AccumulateFromPosteriors(), FmllrSgmm2Accs::AccumulateFromPosteriors(), FmllrRawAccs::CommitSingleFrameStats(), FmllrDiagGmmAccs::CommitSingleFrameStats(), MleAmSgmm2Accs::CommitStatsForSpk(), IvectorExtractorStats::CommitStatsForWPoint(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), IvectorExtractor::GetAcousticAuxfWeight(), main(), kaldi::UnitInvert(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToRows(), kaldi::UnitTestAddVecVec(), kaldi::UnitTestCholesky(), kaldi::UnitTestCuMatrixAddVecVec(), UnitTestEstimateFullGmm(), kaldi::UnitTestInvert(), kaldi::nnet3::UnitTestPreconditionDirectionsOnline(), kaldi::nnet2::UnitTestPreconditionDirectionsOnline(), kaldi::UnitTestSger(), kaldi::UnitTestSpAddVecVec(), and IvectorExtractorStats::UpdatePrior().

124  {
125  KALDI_ASSERT(a.Dim() == num_rows_ && b.Dim() == num_cols_);
126  if (num_rows_ * num_cols_ > 100) { // It's probably worth it to allocate
127  // temporary vectors of the right type and use BLAS.
128  Vector<Real> temp_a(a), temp_b(b);
129  cblas_Xger(num_rows_, num_cols_, alpha, temp_a.Data(), 1,
130  temp_b.Data(), 1, data_, stride_);
131  } else {
132  const OtherReal *a_data = a.Data(), *b_data = b.Data();
133  Real *row_data = data_;
134  for (MatrixIndexT i = 0; i < num_rows_; i++, row_data += stride_) {
135  BaseFloat alpha_ai = alpha * a_data[i];
136  for (MatrixIndexT j = 0; j < num_cols_; j++)
137  row_data[j] += alpha_ai * b_data[j];
138  }
139  }
140 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)
void ApplyCeiling ( Real  ceiling_val)

Applies floor to all matrix elements.

Definition at line 1958 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::UnitTestCuMatrixApplyCeiling().

1958  {
1959  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
1960  for (MatrixIndexT i = 0; i < num_rows; i++) {
1961  Real *data = this->RowData(i);
1962  for (MatrixIndexT j = 0; j < num_cols; j++)
1963  data[j] = (data[j] > ceiling_val ? ceiling_val : data[j]);
1964  }
1965 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void ApplyExp ( )

Exponentiate each of the elements.

Definition at line 1975 of file kaldi-matrix.cc.

References rnnlm::i.

Referenced by AmSgmm2::ComponentPosteriors(), ComputeScores(), AmSgmm2::LogLikelihood(), main(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuMatrixApplyExp(), and kaldi::UnitTestSimple().

1975  {
1976  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1977  Row(i).ApplyExp();
1978  }
1979 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
void ApplyFloor ( Real  floor_val)

Applies floor to all matrix elements.

Definition at line 1948 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by main(), KlHmm::PropagateFnc(), kaldi::UnitTestCuMatrixApplyFloor(), kaldi::UnitTestCuMatrixDiffGroupPnorm(), kaldi::UnitTestCuMatrixGroupMax(), kaldi::UnitTestCuMatrixGroupMaxDeriv(), and kaldi::UnitTestCuMatrixGroupPnorm().

1948  {
1949  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
1950  for (MatrixIndexT i = 0; i < num_rows; i++) {
1951  Real *data = this->RowData(i);
1952  for (MatrixIndexT j = 0; j < num_cols; j++)
1953  data[j] = (data[j] < floor_val ? floor_val : data[j]);
1954  }
1955 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void ApplyHeaviside ( )

Applies the Heaviside step function (x > 0 ? 1 : 0) to all matrix elements Note: in general you can make different choices for x = 0, but for now please leave it as it (i.e.

returning zero) because it affects the RectifiedLinearComponent in the neural net code.

Definition at line 1996 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::UnitTestCuMatrixApplyHeaviside(), kaldi::UnitTestCuMatrixHeaviside(), and kaldi::UnitTestHeaviside().

1996  {
1997  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
1998  for (MatrixIndexT i = 0; i < num_rows; i++) {
1999  Real *data = this->RowData(i);
2000  for (MatrixIndexT j = 0; j < num_cols; j++)
2001  data[j] = (data[j] > 0 ? 1.0 : 0.0);
2002  }
2003 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void ApplyLog ( )

Calculates log of all the matrix elemnts.

Definition at line 1968 of file kaldi-matrix.cc.

References rnnlm::i.

Referenced by main(), KlHmm::PropagateFnc(), and kaldi::UnitTestCuMatrixApplyLog().

1968  {
1969  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1970  Row(i).ApplyLog();
1971  }
1972 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
void ApplyPow ( Real  power)

Applies power to all matrix elements.

Definition at line 1982 of file kaldi-matrix.cc.

References rnnlm::i.

Referenced by Fmpe::ComputeStddevs(), DiagGmm::LogLikelihoods(), kaldi::UnitTestCuMatrixApplyPow(), kaldi::UnitTestSetRandn(), and kaldi::UnitTestSetRandUniform().

1982  {
1983  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1984  Row(i).ApplyPow(power);
1985  }
1986 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
void ApplyPowAbs ( Real  power,
bool  include_sign = false 
)

Apply power to the absolute value of each element.

Include the sign of the input element if include_sign == true. If the power is negative and the input to the power is zero, The output will be set zero.

Definition at line 1989 of file kaldi-matrix.cc.

References rnnlm::i.

Referenced by kaldi::UnitTestCuMatrixApplyPowAbs().

1989  {
1990  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1991  Row(i).ApplyPowAbs(power, include_sign);
1992  }
1993 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
Real ApplySoftMax ( )

Apply soft-max to the collection of all elements of the matrix and return normalizer (log sum of exponentials).

Definition at line 2539 of file kaldi-matrix.cc.

References kaldi::Exp(), rnnlm::i, rnnlm::j, and kaldi::Log().

Referenced by kaldi::UnitTestSimpleForMat(), and kaldi::UnitTestSimpleForVec().

2539  {
2540  Real max = this->Max(), sum = 0.0;
2541  // the 'max' helps to get in good numeric range.
2542  for (MatrixIndexT i = 0; i < num_rows_; i++)
2543  for (MatrixIndexT j = 0; j < num_cols_; j++)
2544  sum += ((*this)(i, j) = Exp((*this)(i, j) - max));
2545  this->Scale(1.0 / sum);
2546  return max + Log(sum);
2547 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns maximum element of matrix.
int32 MatrixIndexT
Definition: matrix-common.h:96
double Log(double x)
Definition: kaldi-math.h:100
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
bool ApproxEqual ( const MatrixBase< Real > &  other,
float  tol = 0.01 
) const

Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm().

Definition at line 1761 of file kaldi-matrix.cc.

References MatrixBase< Real >::AddMat(), MatrixBase< Real >::FrobeniusNorm(), KALDI_ERR, MatrixBase< Real >::num_cols_, and MatrixBase< Real >::num_rows_.

Referenced by kaldi::ApproxEqual(), AccumDiagGmm::AssertEqual(), kaldi::AssertEqual(), kaldi::TestOnlineDeltaFeature(), kaldi::TestOnlineSpliceFrames(), kaldi::UnitTestExtractCompressedMatrix(), kaldi::UnitTestNorm(), kaldi::UnitTestPosteriors(), and kaldi::UnitTestTableRandomBothDoubleMatrix().

1761  {
1762  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1763  KALDI_ERR << "ApproxEqual: size mismatch.";
1764  Matrix<Real> tmp(*this);
1765  tmp.AddMat(-1.0, other);
1766  return (tmp.FrobeniusNorm() <= static_cast<Real>(tol) *
1767  this->FrobeniusNorm());
1768 }
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
SubMatrix<Real> ColRange ( const MatrixIndexT  col_offset,
const MatrixIndexT  num_cols 
) const
inline

Definition at line 207 of file kaldi-matrix.h.

Referenced by kaldi::AppendPostToFeats(), and kaldi::AppendVectorToFeats().

208  {
209  return SubMatrix<Real>(*this, 0, num_rows_, col_offset, num_cols);
210  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:524
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
Real Cond ( ) const

Returns condition number by computing Svd.

Works even if cols > rows. Returns infinity if all singular values are zero.

Definition at line 1542 of file kaldi-matrix.cc.

References VectorBase< Real >::Dim(), rnnlm::i, and KALDI_ASSERT.

Referenced by SpMatrix< BaseFloat >::Cond(), InitRand(), kaldi::InitRand(), kaldi::InitRandNonsingular(), rand_posdef_spmatrix(), kaldi::RandFullCova(), kaldi::unittest::RandPosdefSpMatrix(), RandPosdefSpMatrix(), kaldi::UnitTestPldaEstimation(), and kaldi::UnitTestRegtreeFmllrDiagGmm().

1542  {
1543  KALDI_ASSERT(num_rows_ > 0&&num_cols_ > 0);
1544  Vector<Real> singular_values(std::min(num_rows_, num_cols_));
1545  Svd(&singular_values); // Get singular values...
1546  Real min = singular_values(0), max = singular_values(0); // both absolute values...
1547  for (MatrixIndexT i = 1;i < singular_values.Dim();i++) {
1548  min = std::min((Real)std::abs(singular_values(i)), min); max = std::max((Real)std::abs(singular_values(i)), max);
1549  }
1550  if (min > 0) return max/min;
1551  else return std::numeric_limits<Real>::infinity();
1552 }
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyColFromVec ( const VectorBase< Real > &  v,
const MatrixIndexT  col 
)

Copy vector into specific column of matrix.

Definition at line 951 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), and KALDI_ASSERT.

Referenced by LdaEstimate::AddMeanOffset(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), AmSgmm2::InitializeMw(), main(), ArbitraryResample::Resample(), kaldi::UnitTestCuMathNormalizePerRow(), and kaldi::UnitTestSpliceRows().

952  {
953  KALDI_ASSERT(rv.Dim() == num_rows_ &&
954  static_cast<UnsignedMatrixIndexT>(col) <
955  static_cast<UnsignedMatrixIndexT>(num_cols_));
956 
957  const Real *rv_data = rv.Data();
958  Real *col_data = data_ + col;
959 
960  for (MatrixIndexT r = 0; r < num_rows_; r++)
961  col_data[r * stride_] = rv_data[r];
962 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
uint32 UnsignedMatrixIndexT
Definition: matrix-common.h:98
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyCols ( const MatrixBase< Real > &  src,
const MatrixIndexT indices 
)

Copies column r from column indices[r] of src.

As a special case, if indexes[i] == -1, sets column i to zero. all elements of "indices" must be in [-1, src.NumCols()-1], and src.NumRows() must equal this.NumRows()

Definition at line 2614 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::stride_.

2615  {
2616  KALDI_ASSERT(NumRows() == src.NumRows());
2617  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2618  this_stride = stride_, src_stride = src.stride_;
2619  Real *this_data = this->data_;
2620  const Real *src_data = src.data_;
2621 #ifdef KALDI_PARANOID
2622  MatrixIndexT src_cols = src.NumCols();
2623  for (MatrixIndexT i = 0; i < num_cols; i++)
2624  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2625 #endif
2626 
2627  // For the sake of memory locality we do this row by row, rather
2628  // than doing it column-wise using cublas_Xcopy
2629  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2630  const MatrixIndexT *index_ptr = &(indices[0]);
2631  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2632  if (*index_ptr < 0) this_data[c] = 0;
2633  else this_data[c] = src_data[*index_ptr];
2634  }
2635  }
2636 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyColsFromVec ( const VectorBase< Real > &  v)

Copies vector into matrix, column-by-column.

Note that rv.Dim() must either equal NumRows()*NumCols() or NumRows(); this has two modes of operation.

Definition at line 902 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), and KALDI_ERR.

Referenced by kaldi::UnitTestCuMatrixCopyColsFromVec(), and kaldi::UnitTestSpliceRows().

902  {
903  if (rv.Dim() == num_rows_*num_cols_) {
904  const Real *v_inc_data = rv.Data();
905  Real *m_inc_data = data_;
906 
907  for (MatrixIndexT c = 0; c < num_cols_; c++) {
908  for (MatrixIndexT r = 0; r < num_rows_; r++) {
909  m_inc_data[r * stride_] = v_inc_data[r];
910  }
911  v_inc_data += num_rows_;
912  m_inc_data ++;
913  }
914  } else if (rv.Dim() == num_rows_) {
915  const Real *v_inc_data = rv.Data();
916  Real *m_inc_data = data_;
917  for (MatrixIndexT r = 0; r < num_rows_; r++) {
918  Real value = *(v_inc_data++);
919  for (MatrixIndexT c = 0; c < num_cols_; c++)
920  m_inc_data[c] = value;
921  m_inc_data += stride_;
922  }
923  } else {
924  KALDI_ERR << "Wrong size of arguments.";
925  }
926 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void CopyDiagFromVec ( const VectorBase< Real > &  v)

Copy vector into diagonal of matrix.

Definition at line 942 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), VectorBase< Real >::Dim(), and KALDI_ASSERT.

Referenced by kaldi::UnitTestAddVec2Sp(), kaldi::UnitTestSpliceRows(), and kaldi::UnitTestSvd().

942  {
943  KALDI_ASSERT(rv.Dim() == std::min(num_cols_, num_rows_));
944  const Real *rv_data = rv.Data(), *rv_end = rv_data + rv.Dim();
945  Real *my_data = this->Data();
946  for (; rv_data != rv_end; rv_data++, my_data += (this->stride_+1))
947  *my_data = *rv_data;
948 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:73
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromMat ( const MatrixBase< OtherReal > &  M,
MatrixTransposeType  trans = kNoTrans 
)

Copy given matrix. (no resize is done).

Definition at line 711 of file kaldi-matrix.cc.

References MatrixBase< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Row(), and MatrixBase< Real >::Stride().

Referenced by DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::ApplyFeatureTransformToStats(), CuMatrixBase< Real >::ApplyLogSoftMaxPerRow(), CuMatrixBase< Real >::ApplySoftMaxPerRow(), kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::ComposeTransforms(), kaldi::ComputeFmllrMatrixDiagGmmDiagonal(), kaldi::ComputeFmllrMatrixDiagGmmFull(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), AmSgmm2::ComputeFmllrPreXform(), kaldi::ComputeLdaTransform(), kaldi::ComputeMllrMatrix(), MleAmSgmm2Updater::ComputeMPrior(), OnlineCmvn::ComputeStatsForFrame(), Fmpe::ComputeStddevs(), MatrixExponential< Real >::ComputeTaylor(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), WaveData::CopyFrom(), CompressedAffineXformStats::CopyFromAffineXformStats(), DiagGmmNormal::CopyFromDiagGmm(), FullGmm::CopyFromDiagGmm(), DiagGmm::CopyFromDiagGmm(), FullGmm::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), AffineXformStats::CopyStats(), CompressedAffineXformStats::CopyToAffineXformStats(), DiagGmmNormal::CopyToDiagGmm(), CompressedMatrix::CopyToMat(), CuMatrixBase< Real >::CopyToMat(), GeneralMatrix::CopyToMat(), LdaEstimate::Estimate(), FeatureTransformEstimate::EstimateInternal(), kaldi::ExtractObjectRange(), kaldi::FmllrAuxfGradient(), kaldi::FmllrInnerUpdate(), FmllrSgmm2Accs::FmllrObjGradient(), kaldi::nnet2::FormatNnetInput(), kaldi::generate_features(), OnlineCmvn::GetFrame(), LogisticRegression::GetLogPosteriors(), DiagGmm::GetMeans(), AmSgmm2::GetNtransSigmaInv(), LinearVtln::GetTransform(), DiagGmm::GetVars(), RegtreeFmllrDiagGmm::GetXformMatrix(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), main(), MleAmSgmm2Updater::MapUpdateM(), Matrix< Real >::Matrix(), MfccComputer::MfccComputer(), LogisticRegression::MixUp(), kaldi::nnet2::NnetComputationChunked(), Matrix< BaseFloat >::operator=(), OnlinePreconditionerSimple::PreconditionDirections(), OnlineNaturalGradientSimple::PreconditionDirections(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), AffineXformStats::Read(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), LogisticRegression::SetWeights(), kaldi::SlidingWindowCmn(), kaldi::SolveQuadraticMatrixProblem(), MatrixBase< Real >::Svd(), SpMatrix< Real >::TopEigs(), LogisticRegression::Train(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddMatMatElements(), kaldi::UnitTestCuCopy(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffSoftmax(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMatrixCopyCross2(), kaldi::UnitTestCuSoftmax(), kaldi::UnitTestDiagGmm(), UnitTestEstimateDiagGmm(), kaldi::UnitTestEstimateMmieDiagGmm(), UnitTestFullGmm(), kaldi::UnitTestIoCross(), kaldi::UnitTestMatrixExponential(), kaldi::UnitTestMax2(), kaldi::UnitTestMulElements(), kaldi::UnitTestPca2(), kaldi::UnitTestRegtreeFmllrDiagGmm(), kaldi::UnitTestScale(), kaldi::UnitTestSvd(), kaldi::UnitTestSvdZero(), kaldi::UnitTestSwapCu2M(), kaldi::UnitTestTrace(), kaldi::UnitTestTrain(), MlltAccs::Update(), HldaAccsDiagGmm::Update(), FmllrDiagGmmAccs::Update(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), PldaUnsupervisedAdaptor::UpdatePlda(), MleAmSgmm2Updater::UpdateW(), and AccumDiagGmm::Write().

712  {
713  if (sizeof(Real) == sizeof(OtherReal) &&
714  static_cast<const void*>(M.Data()) ==
715  static_cast<const void*>(this->Data())) {
716  // CopyFromMat called on same data. Nothing to do (except sanity checks).
717  KALDI_ASSERT(Trans == kNoTrans && M.NumRows() == NumRows() &&
718  M.NumCols() == NumCols() && M.Stride() == Stride());
719  return;
720  }
721  if (Trans == kNoTrans) {
722  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == M.NumCols());
723  for (MatrixIndexT i = 0; i < num_rows_; i++)
724  (*this).Row(i).CopyFromVec(M.Row(i));
725  } else {
726  KALDI_ASSERT(num_cols_ == M.NumRows() && num_rows_ == M.NumCols());
727  int32 this_stride = stride_, other_stride = M.Stride();
728  Real *this_data = data_;
729  const OtherReal *other_data = M.Data();
730  for (MatrixIndexT i = 0; i < num_rows_; i++)
731  for (MatrixIndexT j = 0; j < num_cols_; j++)
732  this_data[i * this_stride + j] = other_data[j * other_stride + i];
733  }
734 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:64
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:73
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromMat ( const CompressedMatrix M)

Copy from compressed matrix.

Definition at line 1903 of file kaldi-matrix.cc.

References CompressedMatrix::CopyToMat().

1903  {
1904  mat.CopyToMat(this);
1905 }
void CopyFromMat ( const CuMatrixBase< OtherReal > &  M,
MatrixTransposeType  trans = kNoTrans 
)

Copy from CUDA matrix. Implemented in ../cudamatrix/cu-matrix.h.

Definition at line 850 of file cu-matrix.h.

851  {
852  cu.CopyToMat(this, trans);
853 }
void CopyFromSp ( const SpMatrix< OtherReal > &  M)

Copy given spmatrix. (no resize is done).

Definition at line 787 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by AmSgmm2::GetNtransSigmaInv(), Matrix< BaseFloat >::Matrix(), kaldi::RandFullCova(), kaldi::UnitTestAddMat2(), and kaldi::UnitTestTransposeScatter().

787  {
788  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
789  // MORE EFFICIENT IF LOWER TRIANGULAR! Reverse code otherwise.
790  for (MatrixIndexT i = 0; i < num_rows_; i++) {
791  for (MatrixIndexT j = 0; j < i; j++) {
792  (*this)(j, i) = (*this)(i, j) = M(i, j);
793  }
794  (*this)(i, i) = M(i, i);
795  }
796 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromSp ( const SpMatrix< float > &  M)

Definition at line 753 of file kaldi-matrix.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

753  {
754  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
755  MatrixIndexT num_rows = num_rows_, stride = stride_;
756  const float *Mdata = M.Data();
757  float *row_data = data_, *col_data = data_;
758  for (MatrixIndexT i = 0; i < num_rows; i++) {
759  cblas_scopy(i+1, Mdata, 1, row_data, 1); // copy to the row.
760  cblas_scopy(i, Mdata, 1, col_data, stride); // copy to the column.
761  Mdata += i+1;
762  row_data += stride;
763  col_data += 1;
764  }
765 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromSp ( const SpMatrix< double > &  M)

Definition at line 770 of file kaldi-matrix.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

770  {
772  MatrixIndexT num_rows = num_rows_, stride = stride_;
773  const double *Mdata = M.Data();
774  double *row_data = data_, *col_data = data_;
775  for (MatrixIndexT i = 0; i < num_rows; i++) {
776  cblas_dcopy(i+1, Mdata, 1, row_data, 1); // copy to the row.
777  cblas_dcopy(i, Mdata, 1, col_data, stride); // copy to the column.
778  Mdata += i+1;
779  row_data += stride;
780  col_data += 1;
781  }
782 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
template void CopyFromTp ( const TpMatrix< OtherReal > &  M,
MatrixTransposeType  trans = kNoTrans 
)

Copy given tpmatrix. (no resize is done).

Definition at line 807 of file kaldi-matrix.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, and PackedMatrix< Real >::NumRows().

Referenced by kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), kaldi::ComputeNormalizingTransform(), BasisFmllrEstimate::EstimateFmllrBasis(), Matrix< BaseFloat >::Matrix(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeXt1(), OnlinePreconditioner::ReorthogonalizeXt1(), kaldi::UnitTestCuMatrixCopyFromTp(), and kaldi::UnitTestMul().

808  {
809  if (Trans == kNoTrans) {
810  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
811  SetZero();
812  Real *out_i = data_;
813  const OtherReal *in_i = M.Data();
814  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i += stride_, in_i += i) {
815  for (MatrixIndexT j = 0; j <= i; j++)
816  out_i[j] = in_i[j];
817  }
818  } else {
819  SetZero();
820  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
821  MatrixIndexT stride = stride_;
822  Real *out_i = data_;
823  const OtherReal *in_i = M.Data();
824  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i ++, in_i += i) {
825  for (MatrixIndexT j = 0; j <= i; j++)
826  out_i[j*stride] = in_i[j];
827  }
828  }
829 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void SetZero()
Sets matrix to zero.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyLowerToUpper ( )

Copy lower triangle to upper triangle (symmetrize)

Definition at line 205 of file kaldi-matrix.cc.

References data_, rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by OnlineNaturalGradient::PreconditionDirectionsInternal(), and OnlinePreconditioner::PreconditionDirectionsInternal().

205  {
207  Real *data = data_;
208  MatrixIndexT num_rows = num_rows_, stride = stride_;
209  for (int32 i = 0; i < num_rows; i++)
210  for (int32 j = 0; j < i; j++)
211  data[j * stride + i ] = data[i * stride + j];
212 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowFromVec ( const VectorBase< Real > &  v,
const MatrixIndexT  row 
)

Copy vector into specific row of matrix.

Definition at line 930 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), VectorBase< Real >::Dim(), and KALDI_ASSERT.

Referenced by kaldi::ClusterGaussiansToUbm(), kaldi::FmllrAuxfGradient(), kaldi::GetOutput(), main(), kaldi::MapDiagGmmUpdate(), kaldi::MleDiagGmmUpdate(), kaldi::MleFullGmmUpdate(), DiagGmm::SetComponentMean(), kaldi::UnitTestSpliceRows(), and kaldi::UpdateEbwDiagGmm().

930  {
931  KALDI_ASSERT(rv.Dim() == num_cols_ &&
932  static_cast<UnsignedMatrixIndexT>(row) <
933  static_cast<UnsignedMatrixIndexT>(num_rows_));
934 
935  const Real *rv_data = rv.Data();
936  Real *row_data = RowData(row);
937 
938  std::memcpy(row_data, rv_data, num_cols_ * sizeof(Real));
939 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
uint32 UnsignedMatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRows ( const MatrixBase< Real > &  src,
const MatrixIndexT indices 
)

Copies row r from row indices[r] of src (does nothing As a special case, if indexes[i] == -1, sets row i to zero.

all elements of "indices" must be in [-1, src.NumRows()-1], and src.NumCols() must equal this.NumCols()

Definition at line 2665 of file kaldi-matrix.cc.

References kaldi::cblas_Xcopy(), data_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::RowData().

2666  {
2667  KALDI_ASSERT(NumCols() == src.NumCols());
2668  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2669  this_stride = stride_;
2670  Real *this_data = this->data_;
2671 
2672  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2673  MatrixIndexT index = indices[r];
2674  if (index < 0) memset(this_data, 0, sizeof(Real) * num_cols_);
2675  else cblas_Xcopy(num_cols, src.RowData(index), 1, this_data, 1);
2676  }
2677 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRows ( const Real *const *  src)

Copies row r of this matrix from an array of floats at the location given by src[r].

If any src[r] is NULL then this.Row(r) will be set to zero. Note: we are using "pointer to const pointer to const object" for "src", because we may create "src" by calling Data() of const CuArray

Definition at line 2680 of file kaldi-matrix.cc.

References kaldi::cblas_Xcopy(), and data_.

2680  {
2681  MatrixIndexT num_rows = num_rows_,
2682  num_cols = num_cols_, this_stride = stride_;
2683  Real *this_data = this->data_;
2684 
2685  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2686  const Real *const src_data = src[r];
2687  if (src_data == NULL) memset(this_data, 0, sizeof(Real) * num_cols);
2688  else cblas_Xcopy(num_cols, src_data, 1, this_data, 1);
2689  }
2690 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void CopyRowsFromVec ( const VectorBase< Real > &  v)

This function has two modes of operation.

If v.Dim() == NumRows() * NumCols(), then treats the vector as a row-by-row concatenation of a matrix and copies to *this. if v.Dim() == NumCols(), it sets each row of *this to a copy of v.

Definition at line 846 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), data_, VectorBase< Real >::Dim(), and KALDI_ERR.

Referenced by DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::nnet2::CombineNnets(), FastNnetCombiner::ComputeCurrentNnet(), FmllrRawAccs::ConvertToPerRowStats(), kaldi::CuVectorUnitTestCopyFromMat(), LogisticRegression::DoStep(), kaldi::nnet2::FormatNnetInput(), DiagGmm::LogLikelihoods(), kaldi::nnet2::PerturbTrainingExample(), LogisticRegression::TrainParameters(), kaldi::UnitTestCopyRowsAndCols(), kaldi::UnitTestCuMatrixCopyRowsFromVec(), and kaldi::UnitTestSpliceRows().

846  {
847  if (rv.Dim() == num_rows_*num_cols_) {
848  if (stride_ == num_cols_) {
849  // one big copy operation.
850  const Real *rv_data = rv.Data();
851  std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_);
852  } else {
853  const Real *rv_data = rv.Data();
854  for (MatrixIndexT r = 0; r < num_rows_; r++) {
855  Real *row_data = RowData(r);
856  for (MatrixIndexT c = 0; c < num_cols_; c++) {
857  row_data[c] = rv_data[c];
858  }
859  rv_data += num_cols_;
860  }
861  }
862  } else if (rv.Dim() == num_cols_) {
863  const Real *rv_data = rv.Data();
864  for (MatrixIndexT r = 0; r < num_rows_; r++)
865  std::memcpy(RowData(r), rv_data, sizeof(Real)*num_cols_);
866  } else {
867  KALDI_ERR << "Wrong sized arguments";
868  }
869 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void CopyRowsFromVec ( const CuVectorBase< Real > &  v)

This version of CopyRowsFromVec is implemented in ../cudamatrix/cu-vector.cc.

Definition at line 220 of file cu-vector.cc.

References CuVectorBase< Real >::Data(), data_, CuVectorBase< Real >::Dim(), KALDI_ASSERT, and CuVectorBase< Real >::Vec().

220  {
221  KALDI_ASSERT(v.Dim() == NumCols() * NumRows());
222 #if HAVE_CUDA == 1
223  if (CuDevice::Instantiate().Enabled()) {
224  if (num_rows_ == 0) return;
225  CuTimer tim;
226  if (Stride() == NumCols()) {
227  CU_SAFE_CALL(cudaMemcpy(data_, v.Data(),
228  sizeof(Real)*v.Dim(),
229  cudaMemcpyDeviceToHost));
230  } else {
231  const Real* vec_data = v.Data();
232  for (MatrixIndexT r = 0; r < NumRows(); r++) {
233  CU_SAFE_CALL(cudaMemcpy(RowData(r), vec_data,
234  sizeof(Real) * NumCols(),
235  cudaMemcpyDeviceToHost));
236  vec_data += NumCols();
237  }
238  }
239  CuDevice::Instantiate().AccuProfile(__func__, tim);
240  } else
241 #endif
242  {
243  CopyRowsFromVec(v.Vec());
244  }
245 }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:64
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
void CopyRowsFromVec ( const VectorBase< OtherReal > &  v)

Definition at line 873 of file kaldi-matrix.cc.

References VectorBase< Real >::Data(), VectorBase< Real >::Dim(), and KALDI_ERR.

873  {
874  if (rv.Dim() == num_rows_*num_cols_) {
875  const OtherReal *rv_data = rv.Data();
876  for (MatrixIndexT r = 0; r < num_rows_; r++) {
877  Real *row_data = RowData(r);
878  for (MatrixIndexT c = 0; c < num_cols_; c++) {
879  row_data[c] = static_cast<Real>(rv_data[c]);
880  }
881  rv_data += num_cols_;
882  }
883  } else if (rv.Dim() == num_cols_) {
884  const OtherReal *rv_data = rv.Data();
885  Real *first_row_data = RowData(0);
886  for (MatrixIndexT c = 0; c < num_cols_; c++)
887  first_row_data[c] = rv_data[c];
888  for (MatrixIndexT r = 1; r < num_rows_; r++)
889  std::memcpy(RowData(r), first_row_data, sizeof(Real)*num_cols_);
890  } else {
891  KALDI_ERR << "Wrong sized arguments.";
892  }
893 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void CopyToRows ( Real *const *  dst) const

Copies row r of this matrix to the array of floats at the location given by dst[r].

If dst[r] is NULL, does not copy anywhere. Requires that none of the memory regions pointed to by the pointers in "dst" overlap (e.g. none of the pointers should be the same).

Definition at line 2693 of file kaldi-matrix.cc.

References kaldi::cblas_Xcopy(), and data_.

Referenced by kaldi::UnitTestCopyToRows().

2693  {
2694  MatrixIndexT num_rows = num_rows_,
2695  num_cols = num_cols_, this_stride = stride_;
2696  const Real *this_data = this->data_;
2697 
2698  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2699  Real *const dst_data = dst[r];
2700  if (dst_data != NULL)
2701  cblas_Xcopy(num_cols, this_data, 1, dst_data, 1);
2702  }
2703 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void CopyUpperToLower ( )

Copy upper triangle to lower triangle (symmetrize)

Definition at line 216 of file kaldi-matrix.cc.

References data_, rnnlm::i, rnnlm::j, and KALDI_ASSERT.

216  {
218  Real *data = data_;
219  MatrixIndexT num_rows = num_rows_, stride = stride_;
220  for (int32 i = 0; i < num_rows; i++)
221  for (int32 j = 0; j < i; j++)
222  data[i * stride + j] = data[j * stride + i];
223 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real* Data ( )
inline

Gives pointer to raw data (non-const).

Definition at line 78 of file kaldi-matrix.h.

78 { return data_; }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
Real* Data_workaround ( ) const
inlineprotected

A workaround that allows SubMatrix to get a pointer to non-const data for const Matrix.

Unfortunately C++ does not allow us to declare a "public const" inheritance or anything like that, so it would require a lot of work to make the SubMatrix class totally const-correct– we would have to override many of the Matrix functions.

Definition at line 723 of file kaldi-matrix.h.

Referenced by SubMatrix< Real >::SubMatrix().

723  {
724  return data_;
725  }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void DestructiveSvd ( VectorBase< Real > *  s,
MatrixBase< Real > *  U,
MatrixBase< Real > *  Vt 
)

Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= NumCols), and we return the "skinny" Svd, i.e.

the matrix in the middle is diagonal, and the one on the left is rectangular.

In Svd, *this = U*diag(S)*Vt. Null pointers for U and/or Vt at input mean we do not want that output. We expect that S.Dim() == m, U is either NULL or m by n, and v is either NULL or n by n. The singular values are not sorted (use SortSvd for that).

Definition at line 1627 of file kaldi-matrix.cc.

References VectorBase< Real >::Dim(), KALDI_ASSERT, KALDI_ERR, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, VectorBase< Real >::Scale(), and MatrixBase< Real >::Transpose().

Referenced by kaldi::ComputePca(), AffineComponent::LimitRank(), and MatrixBase< Real >::Svd().

1627  {
1628  // Svd, *this = U*diag(s)*Vt.
1629  // With (*this).num_rows_ == m, (*this).num_cols_ == n,
1630  // Support only skinny Svd with m>=n (NumRows>=NumCols), and zero sizes for U and Vt mean
1631  // we do not want that output. We expect that s.Dim() == m,
1632  // U is either 0 by 0 or m by n, and rv is either 0 by 0 or n by n.
1633  // Throws exception on error.
1634 
1635  KALDI_ASSERT(num_rows_>=num_cols_ && "Svd requires that #rows by >= #cols."); // For compatibility with JAMA code.
1636  KALDI_ASSERT(s->Dim() == num_cols_); // s should be the smaller dim.
1637  KALDI_ASSERT(U == NULL || (U->num_rows_ == num_rows_&&U->num_cols_ == num_cols_));
1638  KALDI_ASSERT(Vt == NULL || (Vt->num_rows_ == num_cols_&&Vt->num_cols_ == num_cols_));
1639 
1640  Real prescale = 1.0;
1641  if ( std::abs((*this)(0, 0) ) < 1.0e-30) { // Very tiny value... can cause problems in Svd.
1642  Real max_elem = LargestAbsElem();
1643  if (max_elem != 0) {
1644  prescale = 1.0 / max_elem;
1645  if (std::abs(prescale) == std::numeric_limits<Real>::infinity()) { prescale = 1.0e+40; }
1646  (*this).Scale(prescale);
1647  }
1648  }
1649 
1650 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD)
1651  // "S" == skinny Svd (only one we support because of compatibility with Jama one which is only skinny),
1652  // "N"== no eigenvectors wanted.
1653  LapackGesvd(s, U, Vt);
1654 #else
1655  /* if (num_rows_ > 1 && num_cols_ > 1 && (*this)(0, 0) == (*this)(1, 1)
1656  && Max() == Min() && (*this)(0, 0) != 0.0) { // special case that JamaSvd sometimes crashes on.
1657  KALDI_WARN << "Jama SVD crashes on this type of matrix, perturbing it to prevent crash.";
1658  for(int32 i = 0; i < NumRows(); i++)
1659  (*this)(i, i) *= 1.00001;
1660  }*/
1661  bool ans = JamaSvd(s, U, Vt);
1662  if (Vt != NULL) Vt->Transpose(); // possibly to do: change this and also the transpose inside the JamaSvd routine. note, Vt is square.
1663  if (!ans) {
1664  KALDI_ERR << "Error doing Svd"; // This one will be caught.
1665  }
1666 #endif
1667  if (prescale != 1.0) s->Scale(1.0/prescale);
1668 }
void LapackGesvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
Real LargestAbsElem() const
largest absolute value.
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DiffSigmoid ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

Definition at line 2765 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::SameDim(), and MatrixBase< Real >::stride_.

Referenced by kaldi::UnitTestSigmoid().

2766  {
2767  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
2768  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2769  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
2770  Real *data = data_;
2771  const Real *value_data = value.data_, *diff_data = diff.data_;
2772  for (MatrixIndexT r = 0; r < num_rows; r++) {
2773  for (MatrixIndexT c = 0; c < num_cols; c++)
2774  data[c] = diff_data[c] * value_data[c] * (1.0 - value_data[c]);
2775  data += stride;
2776  value_data += value_stride;
2777  diff_data += diff_stride;
2778  }
2779 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DiffTanh ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

Definition at line 2782 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, KALDI_ASSERT, kaldi::SameDim(), and MatrixBase< Real >::stride_.

Referenced by kaldi::UnitTestTanh().

2783  {
2784  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
2785  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2786  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
2787  Real *data = data_;
2788  const Real *value_data = value.data_, *diff_data = diff.data_;
2789  for (MatrixIndexT r = 0; r < num_rows; r++) {
2790  for (MatrixIndexT c = 0; c < num_cols; c++)
2791  data[c] = diff_data[c] * (1.0 - (value_data[c] * value_data[c]));
2792  data += stride;
2793  value_data += value_stride;
2794  diff_data += diff_stride;
2795  }
2796 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DivElements ( const MatrixBase< Real > &  A)

Divide each element by the corresponding element of a given matrix.

Definition at line 1006 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by kaldi::UnitTestCuMatrixDivElements().

1006  {
1007  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
1008  MatrixIndexT i;
1009  MatrixIndexT j;
1010 
1011  for (i = 0; i < num_rows_; i++) {
1012  for (j = 0; j < num_cols_; j++) {
1013  (*this)(i, j) /= a(i, j);
1014  }
1015  }
1016 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Eig ( MatrixBase< Real > *  P,
VectorBase< Real > *  eigs_real,
VectorBase< Real > *  eigs_imag 
) const

Eigenvalue Decomposition of a square NxN matrix into the form (*this) = P D P^{-1}.

Be careful: the relationship of D to the eigenvalues we output is slightly complicated, due to the need for P to be real. In the symmetric case D is diagonal and real, but in the non-symmetric case there may be complex-conjugate pairs of eigenvalues. In this case, for the equation (*this) = P D P^{-1} to hold, D must actually be block diagonal, with 2x2 blocks corresponding to any such pairs. If a pair is lambda +- i*mu, D will have a corresponding 2x2 block [lambda, mu; -mu, lambda]. Note that if the input matrix (*this) is non-invertible, P may not be invertible so in this case instead of the equation (*this) = P D P^{-1} holding, we have instead (*this) P = P D.

The non-member function CreateEigenvalueMatrix creates D from eigs_real and eigs_imag.

Definition at line 2068 of file kaldi-matrix.cc.

References EigenvalueDecomposition< Real >::GetImagEigenvalues(), EigenvalueDecomposition< Real >::GetRealEigenvalues(), and EigenvalueDecomposition< Real >::GetV().

Referenced by kaldi::UnitTestEig(), kaldi::UnitTestEigSymmetric(), and kaldi::UnitTestMatrixExponential().

2070  {
2071  EigenvalueDecomposition<Real> eig(*this);
2072  if (P) eig.GetV(P);
2073  if (r) eig.GetRealEigenvalues(r);
2074  if (i) eig.GetImagEigenvalues(i);
2075 }
bool Equal ( const MatrixBase< Real > &  other) const

Tests for exact equality. It's usually preferable to use ApproxEqual.

Definition at line 1771 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ERR, MatrixBase< Real >::num_cols_, and MatrixBase< Real >::num_rows_.

1771  {
1772  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1773  KALDI_ERR << "Equal: size mismatch.";
1774  for (MatrixIndexT i = 0; i < num_rows_; i++)
1775  for (MatrixIndexT j = 0; j < num_cols_; j++)
1776  if ( (*this)(i, j) != other(i, j))
1777  return false;
1778  return true;
1779 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
Real FrobeniusNorm ( ) const

Frobenius norm, which is the sqrt of sum of square elements.

Same as Schatten 2-norm, or just "2-norm".

Definition at line 1756 of file kaldi-matrix.cc.

References kaldi::kTrans, and kaldi::TraceMatMat().

Referenced by MatrixBase< Real >::ApproxEqual(), MleAmSgmm2Accs::Check(), MatrixExponential< Real >::ComputeN(), MatrixExponential< Real >::ComputeTaylor(), MatrixBase< Real >::SymPosSemiDefEig(), kaldi::UnitTestCompressedMatrix(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestNorm(), and kaldi::UnitTestSparseMatrixFrobeniusNorm().

1756  {
1757  return std::sqrt(TraceMatMat(*this, *this, kTrans));
1758 }
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
void GroupMax ( const MatrixBase< Real > &  src)

Apply the function y(i) = (max_{j = i*G}^{(i+1)*G-1} x_j Requires src.NumRows() == this->NumRows() and src.NumCols() % this->NumCols() == 0.

Definition at line 2594 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

Referenced by kaldi::UnitTestCuMatrixGroupMax(), and kaldi::UnitTestCuMatrixGroupMaxDeriv().

2594  {
2595  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2596  src.NumRows() == this->NumRows());
2597  int group_size = src.NumCols() / this->NumCols(),
2598  num_rows = this->NumRows(), num_cols = this->NumCols();
2599  for (MatrixIndexT i = 0; i < num_rows; i++) {
2600  const Real *src_row_data = src.RowData(i);
2601  for (MatrixIndexT j = 0; j < num_cols; j++) {
2602  Real max_val = -1e20;
2603  for (MatrixIndexT k = 0; k < group_size; k++) {
2604  Real src_data = src_row_data[j * group_size + k];
2605  if (src_data > max_val)
2606  max_val = src_data;
2607  }
2608  (*this)(i, j) = max_val;
2609  }
2610  }
2611 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void GroupMaxDeriv ( const MatrixBase< Real > &  input,
const MatrixBase< Real > &  output 
)

Calculate derivatives for the GroupMax function above, where "input" is the input to the GroupMax function above (i.e.

the "src" variable), and "output" is the result of the computation (i.e. the "this" of that function call), and *this must have the same dimension as "input". Each element of *this will be set to 1 if the corresponding input equals the output of the group, and 0 otherwise. The equals the function derivative where it is defined (it's not defined where multiple inputs in the group are equal to the output).

Definition at line 1148 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by kaldi::UnitTestCuMatrixGroupMaxDeriv().

1149  {
1150  KALDI_ASSERT(input.NumCols() == this->NumCols() &&
1151  input.NumRows() == this->NumRows());
1152  KALDI_ASSERT(this->NumCols() % output.NumCols() == 0 &&
1153  this->NumRows() == output.NumRows());
1154 
1155  int group_size = this->NumCols() / output.NumCols(),
1156  num_rows = this->NumRows(), num_cols = this->NumCols();
1157 
1158  for (MatrixIndexT i = 0; i < num_rows; i++) {
1159  for (MatrixIndexT j = 0; j < num_cols; j++) {
1160  Real input_val = input(i, j);
1161  Real output_val = output(i, j / group_size);
1162  (*this)(i, j) = (input_val == output_val ? 1 : 0);
1163  }
1164  }
1165 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void GroupPnorm ( const MatrixBase< Real > &  src,
Real  power 
)

Apply the function y(i) = (sum_{j = i*G}^{(i+1)*G-1} x_j^(power))^(1 / p).

Requires src.NumRows() == this->NumRows() and src.NumCols() % this->NumCols() == 0.

Definition at line 2583 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Row().

Referenced by kaldi::UnitTestCuMatrixDiffGroupPnorm(), and kaldi::UnitTestCuMatrixGroupPnorm().

2583  {
2584  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2585  src.NumRows() == this->NumRows());
2586  int group_size = src.NumCols() / this->NumCols(),
2587  num_rows = this->NumRows(), num_cols = this->NumCols();
2588  for (MatrixIndexT i = 0; i < num_rows; i++)
2589  for (MatrixIndexT j = 0; j < num_cols; j++)
2590  (*this)(i, j) = src.Row(i).Range(j * group_size, group_size).Norm(power);
2591 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void GroupPnormDeriv ( const MatrixBase< Real > &  input,
const MatrixBase< Real > &  output,
Real  power 
)

Calculate derivatives for the GroupPnorm function above...

if "input" is the input to the GroupPnorm function above (i.e. the "src" variable), and "output" is the result of the computation (i.e. the "this" of that function call), and *this has the same dimension as "input", then it sets each element of *this to the derivative d(output-elem)/d(input-elem) for each element of "input", where "output-elem" is whichever element of output depends on that input element.

Definition at line 1104 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by kaldi::UnitTestCuMatrixDiffGroupPnorm().

1106  {
1107  KALDI_ASSERT(input.NumCols() == this->NumCols() && input.NumRows() == this->NumRows());
1108  KALDI_ASSERT(this->NumCols() % output.NumCols() == 0 &&
1109  this->NumRows() == output.NumRows());
1110 
1111  int group_size = this->NumCols() / output.NumCols(),
1112  num_rows = this->NumRows(), num_cols = this->NumCols();
1113 
1114  if (power == 1.0) {
1115  for (MatrixIndexT i = 0; i < num_rows; i++) {
1116  for (MatrixIndexT j = 0; j < num_cols; j++) {
1117  Real input_val = input(i, j);
1118  (*this)(i, j) = (input_val == 0 ? 0 : (input_val > 0 ? 1 : -1));
1119  }
1120  }
1121  } else if (power == std::numeric_limits<Real>::infinity()) {
1122  for (MatrixIndexT i = 0; i < num_rows; i++) {
1123  for (MatrixIndexT j = 0; j < num_cols; j++) {
1124  Real output_val = output(i, j / group_size), input_val = input(i, j);
1125  if (output_val == 0)
1126  (*this)(i, j) = 0;
1127  else
1128  (*this)(i, j) = (std::abs(input_val) == output_val ? 1.0 : 0.0)
1129  * (input_val >= 0 ? 1 : -1);
1130  }
1131  }
1132  } else {
1133  for (MatrixIndexT i = 0; i < num_rows; i++) {
1134  for (MatrixIndexT j = 0; j < num_cols; j++) {
1135  Real output_val = output(i, j / group_size),
1136  input_val = input(i, j);
1137  if (output_val == 0)
1138  (*this)(i, j) = 0;
1139  else
1140  (*this)(i, j) = pow(std::abs(input_val), power - 1) *
1141  pow(output_val, 1 - power) * (input_val >= 0 ? 1 : -1) ;
1142  }
1143  }
1144  }
1145 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Heaviside ( const MatrixBase< Real > &  src)

Sets each element to the Heaviside step function (x > 0 ? 1 : 0) of the corresponding element in "src".

Note: in general you can make different choices for x = 0, but for now please leave it as it (i.e. returning zero) because it affects the RectifiedLinearComponent in the neural net code.

Definition at line 2006 of file kaldi-matrix.cc.

References MatrixBase< Real >::Data(), data_, KALDI_ASSERT, kaldi::SameDim(), and MatrixBase< Real >::stride_.

2006  {
2007  KALDI_ASSERT(SameDim(*this, src));
2008  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2009  Real *row_data = data_;
2010  const Real *src_row_data = src.Data();
2011  for (MatrixIndexT row = 0; row < num_rows;
2012  row++,row_data += stride_, src_row_data += src.stride_) {
2013  for (MatrixIndexT col = 0; col < num_cols; col++)
2014  row_data[col] = (src_row_data[col] > 0 ? 1.0 : 0.0);
2015  }
2016 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real& Index ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

Indexing operator, provided for ease of debugging (gdb doesn't work with parenthesis operator).

Definition at line 105 of file kaldi-matrix.h.

105 { return (*this)(r, c); }
void Invert ( Real *  log_det = NULL,
Real *  det_sign = NULL,
bool  inverse_needed = true 
)

matrix inverse.

if inverse_needed = false, will fill matrix with garbage. (only useful if logdet wanted).

Definition at line 33 of file kaldi-matrix.cc.

References kaldi::clapack_Xgetrf2(), kaldi::clapack_Xgetri2(), data_, rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_MEMALIGN, KALDI_MEMALIGN_FREE, and kaldi::Log().

Referenced by kaldi::CholeskyUnitTestTr(), LdaEstimate::Estimate(), FeatureTransformEstimate::EstimateInternal(), kaldi::FmllrInnerUpdate(), kaldi::generate_features(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), MatrixBase< Real >::InvertDouble(), MatrixBase< Real >::LogDet(), MatrixBase< Real >::Power(), MleAmSgmm2Updater::RenormalizeV(), kaldi::SolveDoubleQuadraticMatrixProblem(), IvectorExtractor::TransformIvectors(), kaldi::UnitTestEig(), kaldi::UnitTestNonsymmetricPower(), MlltAccs::Update(), and PldaUnsupervisedAdaptor::UpdatePlda().

34  {
36  if (num_rows_ == 0) {
37  if (det_sign) *det_sign = 1;
38  if (log_det) *log_det = 0.0;
39  return;
40  }
41 #ifndef HAVE_ATLAS
42  KaldiBlasInt *pivot = new KaldiBlasInt[num_rows_];
43  KaldiBlasInt M = num_rows_;
44  KaldiBlasInt N = num_cols_;
45  KaldiBlasInt LDA = stride_;
46  KaldiBlasInt result = -1;
47  KaldiBlasInt l_work = std::max<KaldiBlasInt>(1, N);
48  Real *p_work;
49  void *temp;
50  if ((p_work = static_cast<Real*>(
51  KALDI_MEMALIGN(16, sizeof(Real)*l_work, &temp))) == NULL)
52  throw std::bad_alloc();
53 
54  clapack_Xgetrf2(&M, &N, data_, &LDA, pivot, &result);
55  const int pivot_offset = 1;
56 #else
57  int *pivot = new int[num_rows_];
58  int result;
59  clapack_Xgetrf(num_rows_, num_cols_, data_, stride_, pivot, &result);
60  const int pivot_offset = 0;
61 #endif
62  KALDI_ASSERT(result >= 0 && "Call to CLAPACK sgetrf_ or ATLAS clapack_sgetrf "
63  "called with wrong arguments");
64  if (result > 0) {
65  if (inverse_needed) {
66  KALDI_ERR << "Cannot invert: matrix is singular";
67  } else {
68  if (log_det) *log_det = -std::numeric_limits<Real>::infinity();
69  if (det_sign) *det_sign = 0;
70  delete[] pivot;
71 #ifndef HAVE_ATLAS
72  KALDI_MEMALIGN_FREE(p_work);
73 #endif
74  return;
75  }
76  }
77  if (det_sign != NULL) {
78  int sign = 1;
79  for (MatrixIndexT i = 0; i < num_rows_; i++)
80  if (pivot[i] != static_cast<int>(i) + pivot_offset) sign *= -1;
81  *det_sign = sign;
82  }
83  if (log_det != NULL || det_sign != NULL) { // Compute log determinant.
84  if (log_det != NULL) *log_det = 0.0;
85  Real prod = 1.0;
86  for (MatrixIndexT i = 0; i < num_rows_; i++) {
87  prod *= (*this)(i, i);
88  if (i == num_rows_ - 1 || std::fabs(prod) < 1.0e-10 ||
89  std::fabs(prod) > 1.0e+10) {
90  if (log_det != NULL) *log_det += Log(std::fabs(prod));
91  if (det_sign != NULL) *det_sign *= (prod > 0 ? 1.0 : -1.0);
92  prod = 1.0;
93  }
94  }
95  }
96 #ifndef HAVE_ATLAS
97  if (inverse_needed) clapack_Xgetri2(&M, data_, &LDA, pivot, p_work, &l_work,
98  &result);
99  delete[] pivot;
100  KALDI_MEMALIGN_FREE(p_work);
101 #else
102  if (inverse_needed)
103  clapack_Xgetri(num_rows_, data_, stride_, pivot, &result);
104  delete [] pivot;
105 #endif
106  KALDI_ASSERT(result == 0 && "Call to CLAPACK sgetri_ or ATLAS clapack_sgetri "
107  "called with wrong arguments");
108 }
void clapack_Xgetri2(KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *stride, KaldiBlasInt *pivot, float *p_work, KaldiBlasInt *l_work, KaldiBlasInt *result)
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void clapack_Xgetrf2(KaldiBlasInt *num_rows, KaldiBlasInt *num_cols, float *Mdata, KaldiBlasInt *stride, KaldiBlasInt *pivot, KaldiBlasInt *result)
int32 MatrixIndexT
Definition: matrix-common.h:96
double Log(double x)
Definition: kaldi-math.h:100
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_MEMALIGN(align, size, pp_orig)
Definition: kaldi-utils.h:54
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:56
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void InvertDouble ( Real *  LogDet = NULL,
Real *  det_sign = NULL,
bool  inverse_needed = true 
)

matrix inverse [double].

if inverse_needed = false, will fill matrix with garbage (only useful if logdet wanted). Does inversion in double precision even if matrix was not double.

Definition at line 1892 of file kaldi-matrix.cc.

References MatrixBase< Real >::Invert().

Referenced by kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::ComputeFeatureNormalizingTransform(), and BasisFmllrEstimate::ComputeTransform().

1893  {
1894  double log_det_tmp, det_sign_tmp;
1895  Matrix<double> dmat(*this);
1896  dmat.Invert(&log_det_tmp, &det_sign_tmp, inverse_needed);
1897  if (inverse_needed) (*this).CopyFromMat(dmat);
1898  if (log_det) *log_det = log_det_tmp;
1899  if (det_sign) *det_sign = det_sign_tmp;
1900 }
void InvertElements ( )

Inverts all the elements of the matrix.

Definition at line 1916 of file kaldi-matrix.cc.

Referenced by kaldi::ClusterGaussiansToUbm(), DiagGmmNormal::CopyFromDiagGmm(), DiagGmmNormal::CopyToDiagGmm(), DiagGmm::GetMeans(), DiagGmm::GetVars(), init_rand_diag_gmm(), DiagGmm::Merge(), KlHmm::PropagateFnc(), rand_diag_gmm(), DiagGmm::SetInvVars(), kaldi::UnitTestCuMatrixInvertElements(), kaldi::UnitTestDiagGmm(), UnitTestEstimateDiagGmm(), kaldi::UnitTestEstimateMmieDiagGmm(), and kaldi::UnitTestRegtreeFmllrDiagGmm().

1916  {
1917  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1918  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1919  (*this)(r, c) = static_cast<Real>(1.0 / (*this)(r, c));
1920  }
1921  }
1922 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
bool IsDiagonal ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Diagonal.

Definition at line 1710 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::ApplyModelTransformToStats(), UnitTestEstimateLda(), and kaldi::UnitTestSimple().

1710  {
1711  MatrixIndexT R = num_rows_, C = num_cols_;
1712  Real bad_sum = 0.0, good_sum = 0.0;
1713  for (MatrixIndexT i = 0;i < R;i++) {
1714  for (MatrixIndexT j = 0;j < C;j++) {
1715  if (i == j) good_sum += std::abs((*this)(i, j));
1716  else bad_sum += std::abs((*this)(i, j));
1717  }
1718  }
1719  return (!(bad_sum > good_sum * cutoff));
1720 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
bool IsSymmetric ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Symmetric.

Definition at line 1694 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by BasisFmllrEstimate::ComputeAmDiagPrecond(), EigenvalueDecomposition< Real >::EigenvalueDecomposition(), and kaldi::UnitTestSimple().

1694  {
1695  MatrixIndexT R = num_rows_, C = num_cols_;
1696  if (R != C) return false;
1697  Real bad_sum = 0.0, good_sum = 0.0;
1698  for (MatrixIndexT i = 0;i < R;i++) {
1699  for (MatrixIndexT j = 0;j < i;j++) {
1700  Real a = (*this)(i, j), b = (*this)(j, i), avg = 0.5*(a+b), diff = 0.5*(a-b);
1701  good_sum += std::abs(avg); bad_sum += std::abs(diff);
1702  }
1703  good_sum += std::abs((*this)(i, i));
1704  }
1705  if (bad_sum > cutoff*good_sum) return false;
1706  return true;
1707 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
bool IsUnit ( Real  cutoff = 1.0e-05) const

Returns true if the matrix is all zeros, except for ones on diagonal.

(it does not have to be square). More specifically, this function returns false if for any i, j, (*this)(i, j) differs by more than cutoff from the expression (i == j ? 1 : 0).

Definition at line 1736 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::CholeskyUnitTestTr(), kaldi::ComputeFmllrMatrixDiagGmm(), kaldi::nnet3::PerturbImage(), TestSgmm2PreXform(), kaldi::UnitTestDct(), UnitTestEstimateLda(), kaldi::UnitTestLimitCondInvert(), kaldi::UnitTestOrthogonalizeRows(), kaldi::UnitTestPca(), kaldi::UnitTestSimple(), kaldi::UnitTestSpInvert(), and kaldi::UnitTestTpInvert().

1736  {
1737  MatrixIndexT R = num_rows_, C = num_cols_;
1738  Real bad_max = 0.0;
1739  for (MatrixIndexT i = 0; i < R;i++)
1740  for (MatrixIndexT j = 0; j < C;j++)
1741  bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*this)(i, j) - (i == j?1.0:0.0))));
1742  return (bad_max <= cutoff);
1743 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
bool IsZero ( Real  cutoff = 1.0e-05) const

Returns true if matrix is all zeros.

Definition at line 1746 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by BasisFmllrEstimate::ComputeTransform(), FmpeStats::DoChecks(), test_io(), kaldi::TestFmpe(), kaldi::UnitTestDeterminant(), kaldi::UnitTestResize(), kaldi::UnitTestSimple(), FmllrDiagGmmAccs::Update(), and FmllrRawAccs::Update().

1746  {
1747  MatrixIndexT R = num_rows_, C = num_cols_;
1748  Real bad_max = 0.0;
1749  for (MatrixIndexT i = 0;i < R;i++)
1750  for (MatrixIndexT j = 0;j < C;j++)
1751  bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*this)(i, j) )));
1752  return (bad_max <= cutoff);
1753 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
KALDI_DISALLOW_COPY_AND_ASSIGN ( MatrixBase< Real >  )
private
void LapackGesvd ( VectorBase< Real > *  s,
MatrixBase< Real > *  U,
MatrixBase< Real > *  Vt 
)

Impementation notes: Lapack works in column-order, therefore the dimensions of *this are swapped as well as the U and V matrices.

Definition at line 510 of file kaldi-matrix.cc.

References kaldi::clapack_Xgesvd(), VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::Dim(), KALDI_ASSERT, KALDI_MEMALIGN, KALDI_MEMALIGN_FREE, KALDI_WARN, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, Matrix< Real >::Resize(), and MatrixBase< Real >::Stride().

511  {
512  KALDI_ASSERT(s != NULL && U_in != this && V_in != this);
513 
514  Matrix<Real> tmpU, tmpV;
515  if (U_in == NULL) tmpU.Resize(this->num_rows_, 1); // work-space if U_in empty.
516  if (V_in == NULL) tmpV.Resize(1, this->num_cols_); // work-space if V_in empty.
517 
521 
522  KaldiBlasInt M = num_cols_;
523  KaldiBlasInt N = num_rows_;
524  KaldiBlasInt LDA = Stride();
525 
526  KALDI_ASSERT(N>=M); // NumRows >= columns.
527 
528  if (U_in) {
529  KALDI_ASSERT((int)U_in->num_rows_ == N && (int)U_in->num_cols_ == M);
530  }
531  if (V_in) {
532  KALDI_ASSERT((int)V_in->num_rows_ == M && (int)V_in->num_cols_ == M);
533  }
534  KALDI_ASSERT((int)s->Dim() == std::min(M, N));
535 
536  MatrixBase<Real> *U = (U_in ? U_in : &tmpU);
537  MatrixBase<Real> *V = (V_in ? V_in : &tmpV);
538 
539  KaldiBlasInt V_stride = V->Stride();
540  KaldiBlasInt U_stride = U->Stride();
541 
542  // Original LAPACK recipe
543  // KaldiBlasInt l_work = std::max(std::max<long int>
544  // (1, 3*std::min(M, N)+std::max(M, N)), 5*std::min(M, N))*2;
545  KaldiBlasInt l_work = -1;
546  Real work_query;
547  KaldiBlasInt result;
548 
549  // query for work space
550  char *u_job = const_cast<char*>(U_in ? "s" : "N"); // "s" == skinny, "N" == "none."
551  char *v_job = const_cast<char*>(V_in ? "s" : "N"); // "s" == skinny, "N" == "none."
552  clapack_Xgesvd(v_job, u_job,
553  &M, &N, data_, &LDA,
554  s->Data(),
555  V->Data(), &V_stride,
556  U->Data(), &U_stride,
557  &work_query, &l_work,
558  &result);
559 
560  KALDI_ASSERT(result >= 0 && "Call to CLAPACK dgesvd_ called with wrong arguments");
561 
562  l_work = static_cast<KaldiBlasInt>(work_query);
563  Real *p_work;
564  void *temp;
565  if ((p_work = static_cast<Real*>(
566  KALDI_MEMALIGN(16, sizeof(Real)*l_work, &temp))) == NULL)
567  throw std::bad_alloc();
568 
569  // perform svd
570  clapack_Xgesvd(v_job, u_job,
571  &M, &N, data_, &LDA,
572  s->Data(),
573  V->Data(), &V_stride,
574  U->Data(), &U_stride,
575  p_work, &l_work,
576  &result);
577 
578  KALDI_ASSERT(result >= 0 && "Call to CLAPACK dgesvd_ called with wrong arguments");
579 
580  if (result != 0) {
581  KALDI_WARN << "CLAPACK sgesvd_ : some weird convergence not satisfied";
582  }
583  KALDI_MEMALIGN_FREE(p_work);
584 }
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void clapack_Xgesvd(char *v, char *u, KaldiBlasInt *num_cols, KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *stride, float *sv, float *Vdata, KaldiBlasInt *vstride, float *Udata, KaldiBlasInt *ustride, float *p_work, KaldiBlasInt *l_work, KaldiBlasInt *result)
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:64
#define KALDI_MEMALIGN(align, size, pp_orig)
Definition: kaldi-utils.h:54
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:56
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
Real LargestAbsElem ( ) const

largest absolute value.

Definition at line 1783 of file kaldi-matrix.cc.

References rnnlm::i, and rnnlm::j.

1783  {
1784  MatrixIndexT R = num_rows_, C = num_cols_;
1785  Real largest = 0.0;
1786  for (MatrixIndexT i = 0;i < R;i++)
1787  for (MatrixIndexT j = 0;j < C;j++)
1788  largest = std::max(largest, (Real)std::abs((*this)(i, j)));
1789  return largest;
1790 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
Real LogDet ( Real *  det_sign = NULL) const

Returns logdet of matrix.

Definition at line 1884 of file kaldi-matrix.cc.

References MatrixBase< Real >::Invert().

Referenced by kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::ComputeFmllrLogDet(), RegtreeFmllrDiagGmm::ComputeLogDets(), kaldi::FmllrAuxfGradient(), kaldi::FmllrAuxFuncDiagGmm(), FmllrSgmm2Accs::FmllrObjGradient(), FmllrRawAccs::GetAuxf(), main(), kaldi::UnitTestDeterminant(), kaldi::UnitTestDeterminantSign(), kaldi::UnitTestEig(), UnitTestEstimateFullGmm(), kaldi::UnitTestSimpleForMat(), and FmllrSgmm2Accs::Update().

1884  {
1885  Real log_det;
1886  Matrix<Real> tmp(*this);
1887  tmp.Invert(&log_det, det_sign, false); // false== output not needed (saves some computation).
1888  return log_det;
1889 }
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
Real LogSumExp ( Real  prune = -1.0) const

Returns log(sum(exp())) without exp overflow If prune > 0.0, it uses a pruning beam, discarding terms less than (max - prune).

Note: in future we may change this so that if prune = 0.0, it takes the max, so use -1 if you don't want to prune.

Definition at line 2516 of file kaldi-matrix.cc.

References kaldi::Exp(), rnnlm::i, rnnlm::j, kaldi::kLogZeroDouble, kaldi::kLogZeroFloat, kaldi::kMinLogDiffDouble, kaldi::kMinLogDiffFloat, and kaldi::Log().

Referenced by kaldi::UnitTestSimpleForMat().

2516  {
2517  Real sum;
2518  if (sizeof(sum) == 8) sum = kLogZeroDouble;
2519  else sum = kLogZeroFloat;
2520  Real max_elem = Max(), cutoff;
2521  if (sizeof(Real) == 4) cutoff = max_elem + kMinLogDiffFloat;
2522  else cutoff = max_elem + kMinLogDiffDouble;
2523  if (prune > 0.0 && max_elem - prune > cutoff) // explicit pruning...
2524  cutoff = max_elem - prune;
2525 
2526  double sum_relto_max_elem = 0.0;
2527 
2528  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2529  for (MatrixIndexT j = 0; j < num_cols_; j++) {
2530  BaseFloat f = (*this)(i, j);
2531  if (f >= cutoff)
2532  sum_relto_max_elem += Exp(f - max_elem);
2533  }
2534  }
2535  return max_elem + Log(sum_relto_max_elem);
2536 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns maximum element of matrix.
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:96
double Log(double x)
Definition: kaldi-math.h:100
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
static const double kMinLogDiffDouble
Definition: kaldi-math.h:124
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
static const float kMinLogDiffFloat
Definition: kaldi-math.h:125
const float kLogZeroFloat
Definition: kaldi-math.h:128
const double kLogZeroDouble
Definition: kaldi-math.h:129
Real Max ( ) const

Returns maximum element of matrix.

Definition at line 1563 of file kaldi-matrix.cc.

References data_, and KALDI_ASSERT.

Referenced by AmSgmm2::ComponentPosteriors(), CompressedMatrix::ComputeGlobalHeader(), AmSgmm2::LogLikelihood(), kaldi::UnitTestCuMatrixMax(), kaldi::UnitTestCuMatrixReduceMax(), kaldi::UnitTestMax2(), and kaldi::UnitTestMaxMin().

1563  {
1564  KALDI_ASSERT(num_rows_ > 0 && num_cols_ > 0);
1565  Real ans= *data_;
1566  for (MatrixIndexT r = 0; r < num_rows_; r++)
1567  for (MatrixIndexT c = 0; c < num_cols_; c++)
1568  if (data_[c + stride_*r] > ans)
1569  ans = data_[c + stride_*r];
1570  return ans;
1571 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Max ( const MatrixBase< Real > &  A)

Set, element-by-element, *this = max(*this, A)

Definition at line 1031 of file kaldi-matrix.cc.

References KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

1031  {
1032  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
1033  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1034  Real *row_data = RowData(row);
1035  const Real *other_row_data = A.RowData(row);
1036  MatrixIndexT num_cols = num_cols_;
1037  for (MatrixIndexT col = 0; col < num_cols; col++) {
1038  row_data[col] = std::max(row_data[col],
1039  other_row_data[col]);
1040  }
1041  }
1042 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real Min ( ) const

Returns minimum element of matrix.

Definition at line 1574 of file kaldi-matrix.cc.

References data_, and KALDI_ASSERT.

Referenced by CompressedMatrix::ComputeGlobalHeader(), kaldi::UnitTestCuMatrixMin(), kaldi::UnitTestCuMatrixReduceMin(), kaldi::UnitTestMaxMin(), and Fmpe::Update().

1574  {
1575  KALDI_ASSERT(num_rows_ > 0 && num_cols_ > 0);
1576  Real ans= *data_;
1577  for (MatrixIndexT r = 0; r < num_rows_; r++)
1578  for (MatrixIndexT c = 0; c < num_cols_; c++)
1579  if (data_[c + stride_*r] < ans)
1580  ans = data_[c + stride_*r];
1581  return ans;
1582 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Min ( const MatrixBase< Real > &  A)

Set, element-by-element, *this = min(*this, A)

Definition at line 1044 of file kaldi-matrix.cc.

References KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

1044  {
1045  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
1046  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1047  Real *row_data = RowData(row);
1048  const Real *other_row_data = A.RowData(row);
1049  MatrixIndexT num_cols = num_cols_;
1050  for (MatrixIndexT col = 0; col < num_cols; col++) {
1051  row_data[col] = std::min(row_data[col],
1052  other_row_data[col]);
1053  }
1054  }
1055 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real MinSingularValue ( ) const
inline

Returns smallest singular value.

Definition at line 406 of file kaldi-matrix.h.

406  {
407  Vector<Real> tmp(std::min(NumRows(), NumCols()));
408  Svd(&tmp);
409  return tmp.Min();
410  }
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
void MulColsVec ( const VectorBase< Real > &  scale)

Equivalent to (*this) = (*this) * diag(scale).

Scaling each column by a scalar taken from that dimension of the vector.

Definition at line 1168 of file kaldi-matrix.cc.

References VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by kaldi::ApplyCmvn(), kaldi::ApplyCmvnReverse(), SpMatrix< Real >::ApplyFloor(), SpMatrix< Real >::ApplyPow(), AmSgmm2::ComponentPosteriors(), SpMatrix< Real >::LimitCond(), MleAmSgmm2Updater::RenormalizeN(), kaldi::SolveQuadraticMatrixProblem(), MatrixBase< Real >::SymPosSemiDefEig(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestCuMatrixMulColsVec(), kaldi::UnitTestEigSymmetric(), and kaldi::UnitTestScale().

1168  {
1169  KALDI_ASSERT(scale.Dim() == num_cols_);
1170  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1171  for (MatrixIndexT j = 0; j < num_cols_; j++) {
1172  Real this_scale = scale(j);
1173  (*this)(i, j) *= this_scale;
1174  }
1175  }
1176 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void MulElements ( const MatrixBase< Real > &  A)

Element by element multiplication with a given matrix.

Definition at line 989 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, rnnlm::i, KALDI_ASSERT, kaldi::mul_elements(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::stride_.

Referenced by DiagGmmNormal::CopyFromDiagGmm(), DiagGmm::CopyFromFullGmm(), DiagGmmNormal::CopyToDiagGmm(), DiagGmm::GetMeans(), DiagGmm::Merge(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), DiagGmm::SetMeans(), kaldi::UnitTestAddMatMatElements(), kaldi::UnitTestCuDiffSoftmax(), kaldi::UnitTestCuMatrixApplyExp(), kaldi::UnitTestCuMatrixApplyLog(), kaldi::UnitTestCuMatrixApplyPow(), kaldi::UnitTestCuMatrixMulElements(), kaldi::UnitTestCuMatrixSigmoid(), kaldi::UnitTestCuMatrixSoftHinge(), kaldi::UnitTestMulElements(), and kaldi::UnitTestSimpleForMat().

989  {
990  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
991 
992  if (num_cols_ == stride_ && num_cols_ == a.stride_) {
993  mul_elements(num_rows_ * num_cols_, a.data_, data_);
994  } else {
995  MatrixIndexT a_stride = a.stride_, stride = stride_;
996  Real *data = data_, *a_data = a.data_;
997  for (MatrixIndexT i = 0; i < num_rows_; i++) {
998  mul_elements(num_cols_, a_data, data);
999  a_data += a_stride;
1000  data += stride;
1001  }
1002  }
1003 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
void mul_elements(const MatrixIndexT dim, const double *a, double *b)
This is not really a wrapper for CBLAS as CBLAS does not have this; in future we could extend this so...
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void MulRowsGroupMat ( const MatrixBase< Real > &  src)

Divide each row into src.NumCols() equal groups, and then scale i'th row's j'th group of elements by src(i, j).

Requires src.NumRows() == this->NumRows() and this->NumCols() % src.NumCols() == 0.

Definition at line 1087 of file kaldi-matrix.cc.

References kaldi::cblas_Xscal(), rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by kaldi::UnitTestCuMatrixDiffGroupPnorm(), and kaldi::UnitTestCuMatrixMulRowsGroupMat().

1087  {
1088  KALDI_ASSERT(src.NumRows() == this->NumRows() &&
1089  this->NumCols() % src.NumCols() == 0);
1090  int32 group_size = this->NumCols() / src.NumCols(),
1091  num_groups = this->NumCols() / group_size,
1092  num_rows = this->NumRows();
1093 
1094  for (MatrixIndexT i = 0; i < num_rows; i++) {
1095  Real *data = this->RowData(i);
1096  for (MatrixIndexT j = 0; j < num_groups; j++, data += group_size) {
1097  Real scale = src(i, j);
1098  cblas_Xscal(group_size, scale, data, 1);
1099  }
1100  }
1101 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:81
int32 MatrixIndexT
Definition: matrix-common.h:96
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void MulRowsVec ( const VectorBase< Real > &  scale)

Equivalent to (*this) = diag(scale) * (*this).

Scaling each row by a scalar taken from that dimension of the vector.

Definition at line 1073 of file kaldi-matrix.cc.

References VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by FeatureTransformEstimate::EstimateInternal(), AffineComponent::LimitRank(), LimitRankClass::operator()(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), KlHmm::PropagateFnc(), AccumDiagGmm::SmoothStats(), Plda::SmoothWithinClassCovariance(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuMatrixDivRowsVec(), kaldi::UnitTestCuMatrixMulRowsVec(), kaldi::UnitTestScale(), and IvectorExtractorStats::UpdatePrior().

1073  {
1074  KALDI_ASSERT(scale.Dim() == num_rows_);
1075  MatrixIndexT M = num_rows_, N = num_cols_;
1076 
1077  for (MatrixIndexT i = 0; i < M; i++) {
1078  Real this_scale = scale(i);
1079  for (MatrixIndexT j = 0; j < N; j++) {
1080  (*this)(i, j) *= this_scale;
1081  }
1082  }
1083 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumCols ( ) const
inline

Returns number of columns (or zero for emtpy matrix).

Definition at line 61 of file kaldi-matrix.h.

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), DecodableMatrixMappedOffset::AcceptLoglikes(), NnetChainCombiner::AcceptNnet(), NnetCombiner::AcceptNnet(), IvectorExtractorUtteranceStats::AccStats(), Fmpe::AccStats(), IvectorExtractorStats::AccStatsForUtterance(), KlHmm::Accumulate(), FmpeStats::AccumulateChecks(), kaldi::AccumulateTreeStats(), MatrixBase< Real >::AddCols(), VectorBase< Real >::AddColSumMat(), VectorBase< Real >::AddDiagMat2(), VectorBase< Real >::AddDiagMatMat(), MatrixBase< Real >::AddDiagVecMat(), SpMatrix< Real >::AddMat2(), SpMatrix< Real >::AddMat2Sp(), SpMatrix< Real >::AddMat2Vec(), CuMatrixBase< Real >::AddMatBlocks(), MatrixBase< Real >::AddMatDiagVec(), MatrixBase< Real >::AddMatMatElements(), VectorBase< Real >::AddMatSvec(), VectorBase< Real >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< Real >::AddRows(), VectorBase< Real >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< Real >::AddSmat2Sp(), SparseMatrix< Real >::AddToMat(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::nnet2::AppendDiscriminativeExamples(), kaldi::AppendPostToFeats(), kaldi::AppendVectorToFeats(), kaldi::nnet3::ApplyAffineTransform(), kaldi::ApplyAffineTransform(), Fmpe::ApplyC(), kaldi::ApplyCmvn(), kaldi::ApplyCmvnReverse(), Fmpe::ApplyContext(), Fmpe::ApplyContextReverse(), kaldi::ApplyFeatureTransformToStats(), ApplyFmllrXform(), kaldi::ApplyModelTransformToStats(), Sgmm2Project::ApplyProjection(), kaldi::AssertSameDim(), kaldi::nnet2::AverageConstPart(), MatrixExponential< Real >::Backprop(), MatrixExponential< Real >::BackpropTaylor(), MleAmSgmm2Accs::Check(), AmSgmm2::Check(), IvectorExtractorStats::CheckDims(), kaldi::ComposeTransforms(), MatrixExponential< Real >::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeDctMatrix(), kaldi::ComputeDeltas(), Fmpe::ComputeFeatures(), kaldi::ComputeFmllrLogDet(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), NnetUpdater::ComputeForMinibatch(), kaldi::ComputeGconsts(), CompressedMatrix::ComputeGlobalHeader(), kaldi::ComputeLdaTransform(), kaldi::ComputePca(), Sgmm2Project::ComputeProjection(), kaldi::ComputeShiftedDeltas(), Fmpe::ComputeStddevs(), MatrixExponential< Real >::ComputeTaylor(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), GaussClusterable::Copy(), VectorBase< Real >::CopyColFromMat(), MatrixBase< Real >::CopyCols(), VectorBase< Real >::CopyColsFromMat(), VectorBase< Real >::CopyDiagFromMat(), CompressedAffineXformStats::CopyFromAffineXformStats(), DiagGmm::CopyFromDiagGmm(), TpMatrix< Real >::CopyFromMat(), SpMatrix< Real >::CopyFromMat(), CompressedMatrix::CopyFromMat(), MatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyRowFromMat(), MatrixBase< Real >::CopyRows(), CuVectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::CopyRowsFromMat(), DiagGmmNormal::CopyToDiagGmm(), FullGmmNormal::CopyToFullGmm(), SparseMatrix< Real >::CopyToMat(), CompressedMatrix::CopyToMat(), CuMatrixBase< Real >::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), kaldi::CreateEigenvalueMatrix(), CuMatrix< Real >::CuMatrix(), DecodableMatrixScaledMapped::DecodableMatrixScaledMapped(), FmpeStats::DerivMinus(), FmpeStats::DerivPlus(), FullGmm::Dim(), DiagGmmNormal::Dim(), LdaEstimate::Dim(), DiagGmm::Dim(), OnlineMatrixFeature::Dim(), MatrixBase< Real >::DivElements(), FmpeStats::DoChecks(), DiscriminativeExampleSplitter::DoExcise(), LogisticRegression::DoStep(), WaveData::Duration(), SpMatrix< Real >::Eig(), EigenvalueDecomposition< Real >::EigenvalueDecomposition(), DecodableNnetSimple::EnsureFrameIsComputed(), FeatureTransformEstimateMulti::Estimate(), FeatureTransformEstimate::EstimateInternal(), FeatureTransformEstimateMulti::EstimateTransformPart(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), kaldi::FakeStatsForSomeDims(), kaldi::FilterMatrixRows(), IvectorExtractorStats::FlushCache(), kaldi::FmllrInnerUpdate(), FmllrRawAccs::FmllrRawAccs(), DiagGmm::GaussianSelection(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), OnlineTransform::GetFrame(), NnetDiscriminativeUpdater::GetInputFeatures(), DecodableNnetSimple::GetIvectorDim(), DecodableNnetSimpleLooped::GetIvectorDim(), LogisticRegression::GetLogPosteriors(), NnetChainCombiner::GetNnetParameters(), NnetCombiner::GetNnetParameters(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), EigenvalueDecomposition< Real >::GetV(), NnetChainCombiner::GetWeightsDeriv(), NnetCombiner::GetWeightsDeriv(), MatrixBase< Real >::GroupMax(), MatrixBase< Real >::GroupMaxDeriv(), MatrixBase< Real >::GroupPnorm(), MatrixBase< Real >::GroupPnormDeriv(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), kaldi::IncreaseTransformDimension(), OnlinePreconditionerSimple::Init(), OnlineNaturalGradientSimple::Init(), kaldi::InitGmmFromRandomFrames(), AmSgmm2::InitializeMw(), InitRand(), kaldi::InitRand(), kaldi::InitRandNonsingular(), PitchInterpolator::InitValues(), kaldi::LatticeAcousticRescore(), kaldi::nnet2::LatticeToDiscriminativeExample(), AffineComponent::LimitRank(), AmSgmm2::LogLikelihood(), DiagGmm::LogLikelihoods(), main(), Matrix< Real >::Matrix(), MatrixBase< Real >::Max(), MatrixBase< Real >::Min(), MatrixBase< BaseFloat >::MinSingularValue(), LogisticRegression::MixUp(), kaldi::nnet1::MomentStatistics(), MatrixBase< Real >::MulElements(), MatrixBase< Real >::MulRowsGroupMat(), kaldi::nnet2::NnetComputationChunked(), NnetChainCombiner::NnetParameterDim(), NnetCombiner::NnetParameterDim(), kaldi::NonOrthogonality(), GeneralMatrix::NumCols(), DecodableMatrixScaled::NumIndices(), GaussClusterable::Objf(), OnlineTransform::OnlineTransform(), CuMatrix< BaseFloat >::operator=(), Matrix< BaseFloat >::operator=(), DiscriminativeExampleSplitter::OutputOneSplit(), kaldi::nnet3::PerturbImage(), kaldi::nnet2::PerturbTrainingExample(), AmSgmm2::PhoneSpaceDim(), kaldi::PlaceNansInGaps(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), kaldi::nnet2::ProcessFile(), kaldi::nnet3::ProcessFile(), Sgmm2Project::ProjectVariance(), NnetDiscriminativeUpdater::Propagate(), KlHmm::PropagateFnc(), kaldi::QrInternal(), kaldi::QrStep(), FullGmmNormal::Rand(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::RandFullCova(), kaldi::unittest::RandFullGaussFeatures(), kaldi::RandZeroToOneMatrix(), AffineXformStats::Read(), WaveData::Read(), FullGmm::Read(), MatrixBase< Real >::Read(), Matrix< Real >::Read(), kaldi::ReadData(), KlHmm::ReadData(), kaldi::ReadHtk(), kaldi::cu::RegularizeL1(), ArbitraryResample::Resample(), DiagGmmNormal::Resize(), FullGmmNormal::Resize(), FullGmm::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), kaldi::ReverseFrames(), MatrixBase< BaseFloat >::Row(), kaldi::SameDim(), LogisticRegression::ScalePriors(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), LinearTransform::SetLinearity(), MatrixBase< Real >::SetMatMatDivMat(), FullGmm::SetMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), LinearVtln::SetTransform(), LogisticRegression::SetWeights(), CuMatrixBase< Real >::SetZeroAboveDiag(), kaldi::SlidingWindowCmn(), kaldi::SlidingWindowCmnInternal(), OnlineCmvn::SmoothOnlineCmvnStats(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SortSvd(), kaldi::cu::Splice(), kaldi::SpliceFrames(), SubVector< Real >::SubVector(), kaldi::TestCuFindRowMaxId(), kaldi::TestFmpe(), kaldi::TestIvectorExtraction(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineTransform(), SpMatrix< Real >::TopEigs(), kaldi::TraceMatMat(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSmat(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), LogisticRegression::Train(), LogisticRegression::TrainParameters(), SpMatrix< Real >::Tridiagonalize(), kaldi::TypeOneUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToRows(), UnitTestCompareWithDeltaFeatures(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuDiffSigmoid(), kaldi::UnitTestCuDiffTanh(), kaldi::UnitTestCuFindRowMaxId(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMatrixHeaviside(), kaldi::UnitTestCuSigmoid(), kaldi::UnitTestCuTanh(), kaldi::UnitTestDelay(), UnitTestEndEffects(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), UnitTestParams(), kaldi::UnitTestRangesMatrix(), kaldi::UnitTestSimple(), kaldi::UnitTestSnipEdges(), kaldi::UnitTestSvdNodestroy(), kaldi::UnitTestTrain(), kaldi::UnitTestTransposeScatter(), MlltAccs::Update(), HldaAccsDiagGmm::Update(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), Fmpe::Update(), kaldi::nnet2::UpdateHash(), IvectorExtractorStats::UpdatePrior(), UpdateWClass::UpdateWClass(), kaldi::VecMatVec(), AccumDiagGmm::Write(), WaveData::Write(), SphinxMatrixHolder< kFeatDim >::Write(), kaldi::WriteHtk(), and kaldi::WriteSphinx().

61 { return num_cols_; }
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows ( ) const
inline

Returns number of rows (or zero for emtpy matrix).

Definition at line 58 of file kaldi-matrix.h.

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), kaldi::AccCmvnStatsWrapper(), DecodableMatrixMappedOffset::AcceptLoglikes(), NnetChainCombiner::AcceptNnet(), NnetCombiner::AcceptNnet(), CovarianceStats::AccStats(), IvectorExtractorUtteranceStats::AccStats(), Fmpe::AccStats(), IvectorExtractorStats::AccStatsForUtterance(), KlHmm::Accumulate(), FmpeStats::AccumulateChecks(), kaldi::AccumulateForUtterance(), AccumulateMultiThreadedClass::AccumulateMultiThreadedClass(), kaldi::AccumulateTreeStats(), MatrixBase< Real >::AddCols(), VectorBase< Real >::AddColSumMat(), VectorBase< Real >::AddDiagMat2(), VectorBase< Real >::AddDiagMatMat(), MatrixBase< Real >::AddDiagVecMat(), SpMatrix< Real >::AddMat2(), SpMatrix< Real >::AddMat2Sp(), SpMatrix< Real >::AddMat2Vec(), CuMatrixBase< Real >::AddMatBlocks(), MatrixBase< Real >::AddMatDiagVec(), MatrixBase< Real >::AddMatMatElements(), VectorBase< Real >::AddMatSvec(), VectorBase< Real >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< Real >::AddRows(), VectorBase< Real >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< Real >::AddSmat2Sp(), SparseMatrix< Real >::AddToMat(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::nnet2::AppendDiscriminativeExamples(), kaldi::AppendPostToFeats(), kaldi::AppendVectorToFeats(), kaldi::nnet3::ApplyAffineTransform(), kaldi::ApplyAffineTransform(), Fmpe::ApplyC(), kaldi::ApplyCmvn(), kaldi::ApplyCmvnReverse(), Fmpe::ApplyContext(), Fmpe::ApplyContextReverse(), kaldi::ApplyFeatureTransformToStats(), ApplyFmllrXform(), kaldi::ApplyHessianXformToGradient(), kaldi::ApplyInvHessianXformToChange(), kaldi::ApplyInvPreXformToChange(), CuMatrixBase< Real >::ApplyLogSoftMaxPerRow(), kaldi::ApplyModelTransformToStats(), kaldi::ApplyPreXformToGradient(), Sgmm2Project::ApplyProjection(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), CuMatrixBase< Real >::ApplySoftMaxPerRow(), kaldi::AssertSameDim(), MatrixExponential< Real >::Backprop(), MatrixExponential< Real >::BackpropTaylor(), kaldi::CalBasisFmllrStepSize(), MleAmSgmm2Accs::Check(), DiscriminativeNnetExample::Check(), AmSgmm2::Check(), IvectorExtractorStats::CheckDims(), IvectorExtractorStats::CommitStatsForM(), kaldi::ComposeTransforms(), GeneralMatrix::Compress(), MatrixExponential< Real >::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeDctMatrix(), kaldi::ComputeDeltas(), Fmpe::ComputeFeatures(), kaldi::ComputeFmllrLogDet(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), DecodableNnet2Online::ComputeForFrame(), NnetUpdater::ComputeForMinibatch(), kaldi::ComputeGconsts(), CompressedMatrix::ComputeGlobalHeader(), kaldi::ComputeLdaTransform(), kaldi::ComputePca(), Sgmm2Project::ComputeProjection(), kaldi::ComputeShiftedDeltas(), Fmpe::ComputeStddevs(), MatrixExponential< Real >::ComputeTaylor(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), kaldi::ComputeVadEnergy(), kaldi::nnet3::time_height_convolution::ConvolveForwardSimple(), GaussClusterable::Copy(), kaldi::cu::Copy(), VectorBase< Real >::CopyColFromMat(), MatrixBase< Real >::CopyCols(), VectorBase< Real >::CopyColsFromMat(), VectorBase< Real >::CopyDiagFromMat(), CompressedAffineXformStats::CopyFromAffineXformStats(), TpMatrix< Real >::CopyFromMat(), SpMatrix< Real >::CopyFromMat(), CompressedMatrix::CopyFromMat(), MatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyRowFromMat(), CuVectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::CopyRowsFromMat(), CompressedAffineXformStats::CopyToAffineXformStats(), SparseMatrix< Real >::CopyToMat(), CompressedMatrix::CopyToMat(), CuMatrixBase< Real >::CopyToMat(), GeneralMatrix::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), kaldi::CreateEigenvalueMatrix(), CuMatrix< Real >::CuMatrix(), DecodableAmSgmm2::DecodableAmSgmm2(), DecodableNnetSimple::DecodableNnetSimple(), DecodableNnetSimpleLooped::DecodableNnetSimpleLooped(), FmpeStats::DerivMinus(), FmpeStats::DerivPlus(), MatrixBase< Real >::DivElements(), DecodableNnetSimple::DoNnetComputation(), LogisticRegression::DoStep(), SpMatrix< Real >::Eig(), EigenvalueDecomposition< Real >::EigenvalueDecomposition(), DecodableNnetLoopedOnlineBase::EnsureFrameIsComputed(), DecodableNnetSimple::EnsureFrameIsComputed(), FeatureTransformEstimate::EstimateInternal(), kaldi::EstimateIvectorsOnline(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), kaldi::FakeStatsForSomeDims(), kaldi::FilterMatrixRows(), NnetChainCombiner::FinishPreprocessingInput(), NnetCombiner::FinishPreprocessingInput(), kaldi::FmllrInnerUpdate(), FmllrRawAccs::FmllrRawAccs(), FmllrRawAccs::FullDim(), DiagGmm::GaussianSelection(), GeneralMatrix::GetCompressedMatrix(), DecodableNnetSimple::GetCurrentIvector(), DecodableNnetSimpleLooped::GetCurrentIvector(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), OnlineCmvn::GetFrame(), kaldi::GetGmmLike(), NnetChainCombiner::GetInitialParameters(), NnetCombiner::GetInitialParameters(), NnetDiscriminativeUpdater::GetInputFeatures(), LogisticRegression::GetLogPosteriors(), GeneralMatrix::GetMatrix(), NnetChainCombiner::GetNnetParameters(), NnetCombiner::GetNnetParameters(), NnetChainCombiner::GetNormalizedWeights(), NnetCombiner::GetNormalizedWeights(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), DecodableNnetSimple::GetOutput(), DecodableNnetSimpleLooped::GetOutput(), DecodableNnetSimple::GetOutputForFrame(), DecodableNnetSimpleLooped::GetOutputForFrame(), NnetChainCombiner::GetParamsDeriv(), NnetCombiner::GetParamsDeriv(), GeneralMatrix::GetSparseMatrix(), OnlineCmvn::GetState(), NnetChainCombiner::GetSumToOnePenalty(), NnetCombiner::GetSumToOnePenalty(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), NnetChainCombiner::GetUnnormalizedWeightsDeriv(), NnetCombiner::GetUnnormalizedWeightsDeriv(), EigenvalueDecomposition< Real >::GetV(), NnetChainCombiner::GetWeights(), NnetCombiner::GetWeights(), NnetChainCombiner::GetWeightsDeriv(), NnetCombiner::GetWeightsDeriv(), MatrixBase< Real >::GroupMax(), MatrixBase< Real >::GroupMaxDeriv(), MatrixBase< Real >::GroupPnorm(), MatrixBase< Real >::GroupPnormDeriv(), AmSgmm2::HasSpeakerDependentWeights(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), kaldi::IncreaseTransformDimension(), OnlinePreconditionerSimple::Init(), OnlineNaturalGradientSimple::Init(), kaldi::InitGmmFromRandomFrames(), AmSgmm2::InitializeMw(), InitRand(), kaldi::InitRand(), kaldi::InitRandNonsingular(), PitchInterpolator::InitValues(), Sgmm2FmllrGlobalParams::IsEmpty(), OnlineMatrixFeature::IsLastFrame(), IvectorExtractor::IvectorDependentWeights(), kaldi::LatticeAcousticRescore(), kaldi::nnet1::LatticeAcousticRescore(), kaldi::nnet2::LatticeToDiscriminativeExample(), AffineComponent::LimitRank(), kaldi::LinearlyInterpolatePitch(), DecodableNnet2Online::LogLikelihood(), DecodableMatrixMappedOffset::LogLikelihood(), DiagGmm::LogLikelihoods(), main(), Matrix< Real >::Matrix(), MatrixBase< Real >::Max(), MatrixBase< Real >::Min(), MatrixBase< BaseFloat >::MinSingularValue(), LogisticRegression::MixUp(), kaldi::nnet1::MomentStatistics(), MatrixBase< Real >::MulElements(), MatrixBase< Real >::MulRowsGroupMat(), kaldi::nnet2::NnetComputationChunked(), NnetDiscriminativeUpdater::NnetDiscriminativeUpdater(), NnetIo::NnetIo(), kaldi::NonOrthogonality(), LdaEstimate::NumClasses(), DecodableAmDiagGmmRegtreeFmllr::NumFramesReady(), DecodableMatrixScaledMapped::NumFramesReady(), DecodableAmDiagGmmUnmapped::NumFramesReady(), DecodableAmSgmm2::NumFramesReady(), DecodableAmNnet::NumFramesReady(), DecodableAmDiagGmmRegtreeMllr::NumFramesReady(), DecodableMatrixMappedOffset::NumFramesReady(), OnlineMatrixFeature::NumFramesReady(), DecodableMatrixScaled::NumFramesReady(), GeneralMatrix::NumRows(), OnlineTransform::OnlineTransform(), LimitRankClass::operator()(), AccumulateMultiThreadedClass::operator()(), CuMatrix< BaseFloat >::operator=(), Matrix< BaseFloat >::operator=(), NnetChainCombiner::ParameterDim(), NnetCombiner::ParameterDim(), kaldi::nnet3::PerturbImage(), kaldi::nnet2::PerturbTrainingExample(), kaldi::PlaceNansInGaps(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), NnetChainCombiner::PrintParams(), NnetCombiner::PrintParams(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), kaldi::nnet2::ProcessFile(), kaldi::nnet3::ProcessFile(), kaldi::ProcessPovFeatures(), kaldi::ProcessUtterance(), Sgmm2Project::ProjectVariance(), NnetDiscriminativeUpdater::Propagate(), KlHmm::PropagateFnc(), SpMatrix< Real >::Qr(), FullGmmNormal::Rand(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::RandFullCova(), kaldi::unittest::RandFullGaussFeatures(), kaldi::RandZeroToOneMatrix(), AffineXformStats::Read(), LogisticRegression::Read(), MatrixBuffer::Read(), WaveData::Read(), MatrixBase< Real >::Read(), Matrix< Real >::Read(), SphinxMatrixHolder< kFeatDim >::Read(), kaldi::ReadData(), KlHmm::ReadData(), kaldi::ReadHtk(), kaldi::cu::RegularizeL1(), ArbitraryResample::Resample(), DiagGmmNormal::Resize(), FullGmm::Resize(), FullGmmNormal::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), kaldi::ReverseFrames(), DiscriminativeExampleSplitter::RightContext(), kaldi::RunPerSpeaker(), kaldi::SameDim(), GeneralMatrix::Scale(), LogisticRegression::ScalePriors(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), LinearTransform::SetLinearity(), MatrixBase< Real >::SetMatMatDivMat(), FullGmm::SetMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), kaldi::SetToMovingAverage(), LinearVtln::SetTransform(), LogisticRegression::SetWeights(), CuMatrixBase< Real >::SetZeroAboveDiag(), kaldi::ShiftFeatureMatrix(), kaldi::SlidingWindowCmn(), kaldi::SlidingWindowCmnInternal(), OnlineCmvn::SmoothOnlineCmvnStats(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SortSvd(), kaldi::cu::Splice(), kaldi::SpliceFrames(), kaldi::SubtractMovingAverage(), GeneralMatrix::SwapCompressedMatrix(), GeneralMatrix::SwapSparseMatrix(), kaldi::TakeLogOfPitch(), test_flags_driven_update(), test_io(), TestAmDiagGmmAccsIO(), TestComponentAcc(), kaldi::TestCuFindRowMaxId(), kaldi::TestFmpe(), kaldi::TestIvectorExtraction(), TestMllrAccsIO(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineMfcc(), kaldi::TestOnlinePlp(), kaldi::TestOnlineTransform(), TestSgmm2AccsIO(), TestSgmm2FmllrAccsIO(), TestSgmm2FmllrSubspace(), TestSgmm2PreXform(), TestXformMean(), SpMatrix< Real >::TopEigs(), kaldi::TraceMatMat(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSmat(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), LogisticRegression::Train(), kaldi::TrainOneIter(), LogisticRegression::TrainParameters(), kaldi::nnet2::TransformTrainingExample(), SpMatrix< Real >::Tridiagonalize(), kaldi::TypeOneUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToRows(), UnitTestCompareWithDeltaFeatures(), kaldi::UnitTestComputeGPE(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuDiffSigmoid(), kaldi::UnitTestCuDiffSoftmax(), kaldi::UnitTestCuDiffTanh(), kaldi::UnitTestCuDiffXent(), kaldi::UnitTestCuFindRowMaxId(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMatrixHeaviside(), kaldi::UnitTestCuSigmoid(), kaldi::UnitTestCuSoftmax(), kaldi::UnitTestCuTanh(), UnitTestEndEffects(), UnitTestEstimateFullGmm(), kaldi::UnitTestGeneralMatrix(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), kaldi::nnet2::UnitTestNnetComputeChunked(), UnitTestParams(), kaldi::UnitTestPosteriors(), kaldi::UnitTestRangesMatrix(), kaldi::UnitTestSimple(), kaldi::UnitTestSnipEdges(), kaldi::UnitTestSvdNodestroy(), kaldi::UnitTestSymAddMat2(), kaldi::UnitTestTrain(), kaldi::UnitTestTransposeScatter(), MlltAccs::Update(), HldaAccsDiagGmm::Update(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), Fmpe::Update(), kaldi::nnet2::UpdateHash(), IvectorExtractorStats::UpdatePrior(), UpdateWClass::UpdateWClass(), kaldi::VecMatVec(), NnetChainCombiner::WeightDim(), NnetCombiner::WeightDim(), AccumDiagGmm::Write(), WaveData::Write(), MleAmSgmm2Accs::Write(), SphinxMatrixHolder< kFeatDim >::Write(), kaldi::WriteHtk(), and kaldi::WriteSphinx().

58 { return num_rows_; }
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
Real& operator() ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

Indexing operator, non-const (only checks sizes if compiled with -DKALDI_PARANOID)

Definition at line 96 of file kaldi-matrix.h.

96  {
97  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
98  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
99  static_cast<UnsignedMatrixIndexT>(c) <
100  static_cast<UnsignedMatrixIndexT>(num_cols_));
101  return *(data_ + r * stride_ + c);
102  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:182
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
const Real operator() ( MatrixIndexT  r,
MatrixIndexT  c 
) const
inline

Indexing operator, const (only checks sizes if compiled with -DKALDI_PARANOID)

Definition at line 109 of file kaldi-matrix.h.

109  {
110  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
111  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
112  static_cast<UnsignedMatrixIndexT>(c) <
113  static_cast<UnsignedMatrixIndexT>(num_cols_));
114  return *(data_ + r * stride_ + c);
115  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:182
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void OrthogonalizeRows ( )

This function orthogonalizes the rows of a matrix using the Gram-Schmidt process.

It is only applicable if NumRows() <= NumCols(). It will use random number generation to fill in rows with something nonzero, in cases where the original matrix was of deficient row rank.

Definition at line 1794 of file kaldi-matrix.cc.

References rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, KALDI_WARN, and kaldi::VecVec().

Referenced by kaldi::ComputePca(), OnlineNaturalGradient::ReorthogonalizeXt1(), and OnlinePreconditioner::ReorthogonalizeXt1().

1794  {
1795  KALDI_ASSERT(NumRows() <= NumCols());
1796  MatrixIndexT num_rows = num_rows_;
1797  for (MatrixIndexT i = 0; i < num_rows; i++) {
1798  int32 counter = 0;
1799  while (1) {
1800  Real start_prod = VecVec(this->Row(i), this->Row(i));
1801  if (start_prod - start_prod != 0.0 || start_prod == 0.0) {
1802  KALDI_WARN << "Self-product of row " << i << " of matrix is "
1803  << start_prod << ", randomizing.";
1804  this->Row(i).SetRandn();
1805  counter++;
1806  continue; // loop again.
1807  }
1808  for (MatrixIndexT j = 0; j < i; j++) {
1809  Real prod = VecVec(this->Row(i), this->Row(j));
1810  this->Row(i).AddVec(-prod, this->Row(j));
1811  }
1812  Real end_prod = VecVec(this->Row(i), this->Row(i));
1813  if (end_prod <= 0.01 * start_prod) { // We removed
1814  // almost all of the vector during orthogonalization,
1815  // so we have reason to doubt (for roundoff reasons)
1816  // that it's still orthogonal to the other vectors.
1817  // We need to orthogonalize again.
1818  if (end_prod == 0.0) { // Row is exactly zero:
1819  // generate random direction.
1820  this->Row(i).SetRandn();
1821  }
1822  counter++;
1823  if (counter > 100)
1824  KALDI_ERR << "Loop detected while orthogalizing matrix.";
1825  } else {
1826  this->Row(i).Scale(1.0 / std::sqrt(end_prod));
1827  break;
1828  }
1829  }
1830  }
1831 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
bool Power ( Real  pow)

The Power method attempts to take the matrix to a power using a method that works in general for fractional and negative powers.

The input matrix must be invertible and have reasonable condition (or we don't guarantee the results. The method is based on the eigenvalue decomposition. It will return false and leave the matrix unchanged, if at entry the matrix had real negative eigenvalues (or if it had zero eigenvalues and the power was negative).

Definition at line 2020 of file kaldi-matrix.cc.

References MatrixBase< Real >::AddMatMat(), kaldi::AttemptComplexPower(), kaldi::CreateEigenvalueMatrix(), rnnlm::i, MatrixBase< Real >::Invert(), KALDI_ASSERT, kaldi::kNoTrans, and rnnlm::n.

Referenced by kaldi::UnitTestNonsymmetricPower().

2020  {
2023  Matrix<Real> P(n, n);
2024  Vector<Real> re(n), im(n);
2025  this->Eig(&P, &re, &im);
2026  // Now attempt to take the complex eigenvalues to this power.
2027  for (MatrixIndexT i = 0; i < n; i++)
2028  if (!AttemptComplexPower(&(re(i)), &(im(i)), power))
2029  return false; // e.g. real and negative, or zero, eigenvalues.
2030 
2031  Matrix<Real> D(n, n); // D to the power.
2032  CreateEigenvalueMatrix(re, im, &D);
2033 
2034  Matrix<Real> tmp(n, n); // P times D
2035  tmp.AddMatMat(1.0, P, kNoTrans, D, kNoTrans, 0.0); // tmp := P*D
2036  P.Invert();
2037  // next line is: *this = tmp * P^{-1} = P * D * P^{-1}
2038  (*this).AddMatMat(1.0, tmp, kNoTrans, P, kNoTrans, 0.0);
2039  return true;
2040 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
struct rnnlm::@11::@12 n
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CreateEigenvalueMatrix(const VectorBase< Real > &re, const VectorBase< Real > &im, MatrixBase< Real > *D)
Creates the eigenvalue matrix D that is part of the decomposition used Matrix::Eig.
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
void Eig(MatrixBase< Real > *P, VectorBase< Real > *eigs_real, VectorBase< Real > *eigs_imag) const
Eigenvalue Decomposition of a square NxN matrix into the form (*this) = P D P^{-1}.
bool AttemptComplexPower(Real *x_re, Real *x_im, Real power)
The following function is used in Matrix::Power, and separately tested, so we declare it here mainly ...
SubMatrix<Real> Range ( const MatrixIndexT  row_offset,
const MatrixIndexT  num_rows,
const MatrixIndexT  col_offset,
const MatrixIndexT  num_cols 
) const
inline

Return a sub-part of matrix.

Definition at line 196 of file kaldi-matrix.h.

Referenced by kaldi::nnet2::AppendDiscriminativeExamples(), kaldi::AppendFeats(), kaldi::ApplyInvPreXformToChange(), kaldi::ApplyPreXformToGradient(), Fmpe::ApplyProjection(), kaldi::nnet2::AverageConstPart(), BasisFmllrEstimate::ComputeAmDiagPrecond(), kaldi::ComputeFmllrMatrixDiagGmmDiagonal(), AmSgmm2::ComputeFmllrPreXform(), Sgmm2Project::ComputeProjection(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), kaldi::ConcatFeats(), LdaEstimate::Estimate(), FeatureTransformEstimateMulti::Estimate(), FeatureTransformEstimate::EstimateInternal(), kaldi::ExtractObjectRange(), IvectorExtractorStats::FlushCache(), kaldi::FmllrAuxfGradient(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::FmllrInnerUpdate(), FmllrSgmm2Accs::FmllrObjGradient(), FmllrRawAccs::FmllrRawAccs(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), AmSgmm2::InitializeMw(), AmSgmm2::InitializeNu(), kaldi::nnet2::LatticeToDiscriminativeExample(), main(), kaldi::MergeFullGmm(), SoftmaxComponent::MixUp(), kaldi::nnet2::NnetComputationChunked(), OnlineTransform::OnlineTransform(), DiscriminativeExampleSplitter::OutputOneSplit(), Matrix< Real >::Resize(), FullGmm::Split(), DiagGmm::Split(), SpMatrix< Real >::TopEigs(), UnitTestFullGmm(), and kaldi::UnitTestRange().

199  {
200  return SubMatrix<Real>(*this, row_offset, num_rows,
201  col_offset, num_cols);
202  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:524
void Read ( std::istream &  in,
bool  binary,
bool  add = false 
)

stream read.

Use instead of stream<<*this, if you want to add to existing contents.

Definition at line 1272 of file kaldi-matrix.cc.

References KALDI_ERR, MatrixBase< Real >::num_cols_, MatrixBase< Real >::num_rows_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and Matrix< Real >::Read().

Referenced by kaldi::operator>>().

1272  {
1273  if (add) {
1275  tmp.Read(is, binary, false); // read without adding.
1276  if (tmp.num_rows_ != this->num_rows_ || tmp.num_cols_ != this->num_cols_)
1277  KALDI_ERR << "MatrixBase::Read, size mismatch "
1278  << this->num_rows_ << ", " << this->num_cols_
1279  << " vs. " << tmp.num_rows_ << ", " << tmp.num_cols_;
1280  this->AddMat(1.0, tmp);
1281  return;
1282  }
1283  // now assume add == false.
1284 
1285  // In order to avoid rewriting this, we just declare a Matrix and
1286  // use it to read the data, then copy.
1287  Matrix<Real> tmp;
1288  tmp.Read(is, binary, false);
1289  if (tmp.NumRows() != NumRows() || tmp.NumCols() != NumCols()) {
1290  KALDI_ERR << "MatrixBase<Real>::Read, size mismatch "
1291  << NumRows() << " x " << NumCols() << " versus "
1292  << tmp.NumRows() << " x " << tmp.NumCols();
1293  }
1294  CopyFromMat(tmp);
1295 }
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
friend class Matrix< Real >
Definition: kaldi-matrix.h:48
const SubVector<Real> Row ( MatrixIndexT  i) const
inline

Return specific row of matrix [const].

Definition at line 182 of file kaldi-matrix.h.

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), NnetChainCombiner::AcceptNnet(), NnetCombiner::AcceptNnet(), OnlinePitchFeatureImpl::AcceptWaveform(), IvectorExtractorUtteranceStats::AccStats(), kaldi::AccStatsForUtterance(), BasisFmllrAccus::AccuGradientScatter(), LdaEstimate::Accumulate(), KlHmm::Accumulate(), AccumFullGmm::AccumulateForComponent(), AccumDiagGmm::AccumulateForComponent(), RegtreeMllrDiagGmmAccs::AccumulateForGaussian(), RegtreeMllrDiagGmmAccs::AccumulateForGmm(), kaldi::AccumulateForUtterance(), AccumFullGmm::AccumulateFromPosteriors(), FmllrRawAccs::AccumulateFromPosteriors(), MleAmSgmm2Accs::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), kaldi::AccumulateTreeStats(), GaussClusterable::AddStats(), AccumDiagGmm::AddStatsForComponent(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::ApplyCmvn(), kaldi::ApplyCmvnReverse(), Fmpe::ApplyContext(), Fmpe::ApplyContextReverse(), CuMatrixBase< Real >::ApplyLogSoftMaxPerRow(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), CuMatrixBase< Real >::ApplySoftMaxPerRow(), kaldi::CalBasisFmllrStepSize(), kaldi::ClusterGaussiansToUbm(), IvectorExtractorStats::CommitStatsForM(), FullGmm::ComponentLogLikelihood(), DiagGmm::ComponentLogLikelihood(), AmSgmm2::ComponentLogLikes(), BasisFmllrEstimate::ComputeAmDiagPrecond(), Fmpe::ComputeC(), IvectorExtractor::ComputeDerivedVars(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), FullGmm::ComputeGconsts(), Sgmm2Project::ComputeLdaStats(), kaldi::ComputeLdaTransform(), kaldi::ComputeMllrMatrix(), AmSgmm2::ComputeNormalizersInternal(), kaldi::ComputePca(), AmSgmm2::ComputePerFrameVars(), AmSgmm2::ComputePerSpkDerivedVars(), EbwAmSgmm2Updater::ComputePhoneVecStats(), ComputeScores(), OnlineCmvn::ComputeStatsForFrame(), BasisFmllrEstimate::ComputeTransform(), FmllrRawAccs::ConvertToSimpleStats(), FullGmmNormal::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), MatrixBase< Real >::CopyFromMat(), FullGmmNormal::CopyToFullGmm(), DiagGmm::DiagGmm(), kaldi::DiagGmmToStats(), LdaEstimate::Estimate(), BasisFmllrEstimate::EstimateFmllrBasis(), FeatureTransformEstimate::EstimateInternal(), kaldi::EstimateIvectorsOnline(), NnetChainCombiner::FinishPreprocessingInput(), NnetCombiner::FinishPreprocessingInput(), kaldi::FmllrAuxfGradient(), kaldi::FmllrAuxFuncDiagGmm(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::FmllrInnerUpdate(), GaussClusterable::GaussClusterable(), kaldi::GenRandStats(), IvectorExtractor::GetAcousticAuxfMean(), IvectorExtractor::GetAcousticAuxfVariance(), FullGmm::GetComponentMean(), FullGmm::GetCovarsAndMeans(), DecodableNnetSimple::GetCurrentIvector(), DecodableNnetSimpleLooped::GetCurrentIvector(), kaldi::GetFeatureMeanAndVariance(), OnlineMatrixFeature::GetFrame(), OnlineCmvn::GetFrame(), kaldi::GetGmmLike(), GetLogLikeTest(), LogisticRegression::GetLogPosteriors(), FullGmm::GetMeans(), LogisticRegression::GetObjfAndGrad(), DecodableNnetSimple::GetOutputForFrame(), DecodableNnetSimpleLooped::GetOutputForFrame(), OnlineCmvn::GetState(), AmSgmm2::GetSubstateSpeakerMean(), RegtreeMllrDiagGmm::GetTransformedMeans(), MatrixBase< Real >::GroupPnorm(), kaldi::InitGmmFromRandomFrames(), IvectorExtractor::IvectorExtractor(), kaldi::nnet2::LatticeToDiscriminativeExample(), FullGmm::LogLikelihoodsPreselect(), DiagGmm::LogLikelihoodsPreselect(), DecodableAmDiagGmmRegtreeFmllr::LogLikelihoodZeroBased(), DecodableAmDiagGmmUnmapped::LogLikelihoodZeroBased(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), kaldi::MapDiagGmmUpdate(), FullGmm::Merge(), DiagGmm::Merge(), FullGmm::MergePreselect(), LogisticRegression::MixUp(), kaldi::MleDiagGmmUpdate(), kaldi::MleFullGmmUpdate(), kaldi::MllrAuxFunction(), kaldi::nnet2::NnetComputationChunked(), AccumulateMultiThreadedClass::operator()(), FullGmm::Perturb(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), kaldi::nnet2::ProcessFile(), kaldi::nnet3::ProcessFile(), FullGmmNormal::Rand(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::unittest::RandFullGaussFeatures(), kaldi::cu::Randomize(), AccumFullGmm::Read(), LdaEstimate::Read(), MleAmSgmm2Updater::RenormalizeV(), DiagGmm::SetComponentInvVar(), FullGmm::SetInvCovars(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetMeans(), kaldi::ShiftFeatureMatrix(), AccumDiagGmm::SmoothWithAccum(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SortSvd(), FullGmm::Split(), DiagGmm::Split(), AmSgmm2::SplitSubstatesInGroup(), test_flags_driven_update(), test_io(), TestAmDiagGmmAccsIO(), TestComponentAcc(), kaldi::TestIvectorExtraction(), TestMllrAccsIO(), kaldi::TestOnlineTransform(), TestSgmm2AccsIO(), TestSgmm2Fmllr(), TestSgmm2FmllrAccsIO(), TestSgmm2FmllrSubspace(), TestXformMean(), kaldi::TraceMatSmat(), kaldi::UnitTestAddDiagVecMat(), UnitTestArbitraryResample(), kaldi::UnitTestCompressedMatrix(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMatrixApplyHeaviside(), kaldi::UnitTestCuMatrixApplyPow(), kaldi::UnitTestCuMatrixApplyPowAbs(), kaldi::UnitTestCuMatrixHeaviside(), kaldi::UnitTestCuSoftmax(), kaldi::UnitTestDeterminantSign(), UnitTestEstimateDiagGmm(), UnitTestEstimateFullGmm(), UnitTestEstimateLda(), UnitTestEstimateSgmm2(), UnitTestFullGmm(), UnitTestFullGmmEst(), kaldi::UnitTestGeneralMatrix(), UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), UnitTestLinearResample(), UnitTestMleAmDiagGmm(), kaldi::UnitTestPosteriors(), UnitTestRegressionTree(), FmllrRawAccs::Update(), kaldi::UpdateEbwDiagGmm(), kaldi::nnet2::UpdateHash(), MleAmSgmm2Updater::UpdatePhoneVectorsInternal(), PldaUnsupervisedAdaptor::UpdatePlda(), EbwAmSgmm2Updater::UpdateU(), MleAmSgmm2Updater::UpdateU(), IvectorExtractorStats::UpdateVariances(), EbwAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateW(), IvectorExtractorStats::UpdateWeight(), MleAmSgmm2Updater::UpdateWGetStats(), MleSgmm2SpeakerAccs::UpdateWithU(), AccumFullGmm::Write(), LdaEstimate::Write(), GaussClusterable::x2_stats(), and GaussClusterable::x_stats().

182  {
183  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
184  static_cast<UnsignedMatrixIndexT>(num_rows_));
185  return SubVector<Real>(data_ + (i * stride_), NumCols());
186  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
SubVector<Real> Row ( MatrixIndexT  i)
inline

Return specific row of matrix.

Definition at line 189 of file kaldi-matrix.h.

189  {
190  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
191  static_cast<UnsignedMatrixIndexT>(num_rows_));
192  return SubVector<Real>(data_ + (i * stride_), NumCols());
193  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const Real* RowData ( MatrixIndexT  i) const
inline

Returns pointer to data for one row (const)

Definition at line 88 of file kaldi-matrix.h.

88  {
89  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
90  static_cast<UnsignedMatrixIndexT>(num_rows_));
91  return data_ + i * stride_;
92  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
SubMatrix<Real> RowRange ( const MatrixIndexT  row_offset,
const MatrixIndexT  num_rows 
) const
inline

Definition at line 203 of file kaldi-matrix.h.

Referenced by DecodableMatrixMappedOffset::AcceptLoglikes(), kaldi::ComputeAndProcessKaldiPitch(), kaldi::ComputeKaldiPitchFirstPass(), DecodableNnetSimple::EnsureFrameIsComputed(), main(), and UnitTestMatrixRandomizer().

204  {
205  return SubMatrix<Real>(*this, row_offset, num_rows, 0, num_cols_);
206  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:524
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void Scale ( Real  alpha)

Multiply each element with a scalar value.

Definition at line 1058 of file kaldi-matrix.cc.

References kaldi::cblas_Xscal(), data_, and rnnlm::i.

Referenced by BasisFmllrAccus::AccuGradientScatter(), FmllrSgmm2Accs::Accumulate(), MleAmSgmm2Accs::Accumulate(), MleSgmm2SpeakerAccs::Accumulate(), kaldi::AccumulateForUtterance(), MatrixExponential< Real >::Backprop(), AmSgmm2::ComponentPosteriors(), MleAmSgmm2Updater::ComputeMPrior(), BasisFmllrEstimate::ComputeTransform(), kaldi::FmllrAuxfGradient(), FmllrSgmm2Accs::FmllrObjGradient(), kaldi::generate_features(), LogisticRegression::GetObjfAndGrad(), IvectorExtractor::IvectorExtractor(), main(), DiagGmm::Merge(), LdaEstimate::Scale(), GaussClusterable::Scale(), IvectorExtractorUtteranceStats::Scale(), AccumFullGmm::Scale(), AccumDiagGmm::Scale(), GeneralMatrix::Scale(), kaldi::nnet2::TransformTrainingExample(), kaldi::TypeOneUsage(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddMatMatElements(), kaldi::nnet2::UnitTestAffineComponent(), kaldi::nnet2::UnitTestAffineComponentPreconditioned(), kaldi::nnet2::UnitTestAffineComponentPreconditionedOnline(), kaldi::UnitTestCompressedMatrix(), kaldi::nnet2::UnitTestConvolutional1dComponent(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMatrixScale(), kaldi::UnitTestCuSoftmax(), kaldi::UnitTestDeterminantSign(), UnitTestEstimateFullGmm(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), kaldi::UnitTestMatrixExponential(), kaldi::UnitTestMatrixExponentialBackprop(), kaldi::UnitTestNorm(), kaldi::UnitTestScale(), FmllrSgmm2Accs::Update(), and LstmNonlinearityComponent::Write().

1058  {
1059  if (alpha == 1.0) return;
1060  if (num_rows_ == 0) return;
1061  if (num_cols_ == stride_) {
1062  cblas_Xscal(static_cast<size_t>(num_rows_) * static_cast<size_t>(num_cols_),
1063  alpha, data_,1);
1064  } else {
1065  Real *data = data_;
1066  for (MatrixIndexT i = 0; i < num_rows_; ++i, data += stride_) {
1067  cblas_Xscal(num_cols_, alpha, data,1);
1068  }
1069  }
1070 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:736
Real * data_
data memory area
Definition: kaldi-matrix.h:728
int32 MatrixIndexT
Definition: matrix-common.h:96
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void Set ( Real  value)

Sets all elements to a specific value.

Definition at line 1188 of file kaldi-matrix.cc.

Referenced by LogisticRegression::GetLogPosteriors(), main(), DiagGmm::Resize(), kaldi::ResizeModel(), kaldi::UnitTestCuMatrixSet(), kaldi::nnet3::UnitTestPreconditionDirectionsOnline(), kaldi::nnet2::UnitTestPreconditionDirectionsOnline(), kaldi::UnitTestSvdBad(), and kaldi::UnitTestTridiagonalize().

1188  {
1189  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1190  for (MatrixIndexT col = 0; col < num_cols_; col++) {
1191  (*this)(row, col) = value;
1192  }
1193  }
1194 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:733
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:732
void SetMatMatDivMat ( const MatrixBase< Real > &  A,
const MatrixBase< Real > &  B,
const MatrixBase< Real > &  C 
)

*this = a * b / c (by element; when c = 0, *this = a)

o / i is either zero or "scale".

Just imagine the scale was 1.0. This is somehow true in

expectation; anyway, this case should basically never happen so it doesn't really matter.

Definition at line 182 of file kaldi-matrix.cc.

References rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

184  {
185  KALDI_ASSERT(A.NumRows() == B.NumRows() && A.NumCols() == B.NumCols());
186  KALDI_ASSERT(A.NumRows() == C.NumRows() && A.NumCols() == C.NumCols());
187  for (int32 r = 0; r < A.NumRows(); r++) { // each frame...
188  for (int32 c = 0; c < A.NumCols(); c++) {
189  BaseFloat i = C(r, c), o = B(r, c), od = A(r, c),
190  id;
191  if (i != 0.0) {
192  id = od * (o / i);
193  } else {
194  id = od;
195  }
198  (*this)(r, c) = id;
199  }
200  }
201 }
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void SetRandn ( )

Sets to random values of a normal distribution.

Definition at line 1204 of file kaldi-matrix.cc.

References kaldi::RandGauss(), and kaldi::RandGauss2().

Referenced by IvectorExtractorStats::CommitStatsForW(), kaldi::CuVectorUnitTestCopyDiagFromMat(), kaldi::generate_features(), kaldi::nnet3::GenerateSimpleNnetTrainingExample(), rand_posdef_spmatrix(), kaldi::RandFullCova(), kaldi::unittest::RandPosdefSpMatrix(), RandPosdefSpMatrix(), kaldi::TestCuFindRowMaxId(), kaldi::TestOnlineMatrixCacheFeature(), kaldi::TestOnlineTransform(), kaldi::UnitTestAddColSumMatSpeed(), kaldi::UnitTestAddDiagMat2(), kaldi::UnitTestAddDiagMatMat(), kaldi::UnitTestAddDiagVecMat(), kaldi::UnitTestAddMat2(), kaldi::UnitTestAddMat2Sp(), kaldi::UnitTestAddMatSmat(), kaldi::UnitTestAddRows(), kaldi::UnitTestAddRowSumMatSpeed(), kaldi::UnitTestAddToRows(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToColsSpeed(), kaldi::UnitTestAddVecToRows(), kaldi::UnitTestAddVecToRowsSpeed(), kaldi::UnitTestAddVecVec(), kaldi::nnet2::UnitTestAffineComponent(), kaldi::nnet2::UnitTestAffineComponentPreconditioned(), kaldi::nnet2::UnitTestAffineComponentPreconditionedOnline(), kaldi::UnitTestBackpropLstmNonlinearity(), kaldi::UnitTestCheck(), kaldi::UnitTestCompressedMatrix(), kaldi::nnet2::UnitTestConvolutional1dComponent(), kaldi::UnitTestCopyCols(), kaldi::UnitTestCopyRows(), kaldi::UnitTestCopyToRows(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuDiffSigmoid(), kaldi::UnitTestCuDiffSoftmax(), kaldi::UnitTestCuDiffTanh(), kaldi::UnitTestCuFindRowMaxId(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMathComputeLstmNonlinearity(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMatrixAdd(), kaldi::UnitTestCuMatrixAddMat(), kaldi::UnitTestCuMatrixAddMatMat(), kaldi::UnitTestCuMatrixAddMatMatBatched(), kaldi::UnitTestCuMatrixAddMatTp(), kaldi::UnitTestCuMatrixAddRowRanges(), kaldi::UnitTestCuMatrixAddTpMat(), kaldi::UnitTestCuMatrixAddVecToCols(), kaldi::UnitTestCuMatrixAddVecToRows(), kaldi::UnitTestCuMatrixApplyCeiling(), kaldi::UnitTestCuMatrixApplyExp(),