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 AddToRows (Real alpha, const MatrixIndexT *indexes, MatrixBase< Real > *dst) const
 For each row i of *this, adds this->Row(i) to dst->Row(indexes(i)) if indexes(i) >= 0, else do nothing. 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 ApplyExpSpecial ()
 For each element x of the matrix, set it to (x < 0 ? exp(x) : x + 1). 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 AddSmat (Real alpha, const SparseMatrix< Real > &A, MatrixTransposeType trans=kNoTrans)
 *this += alpha * A [or A^T]. More...
 
void AddSmatMat (Real alpha, const SparseMatrix< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, Real beta)
 (*this) = alpha * op(A) * B + beta * (*this), where A is sparse. More...
 
void AddMatSmat (Real alpha, const MatrixBase< Real > &A, const SparseMatrix< Real > &B, MatrixTransposeType transB, Real beta)
 (*this) = alpha * A * op(B) + beta * (*this), where B is sparse and op(B) is either B or trans(B) depending on the 'transB' argument. 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<typename OtherReal >
void AddSp (const Real alpha, const SpMatrix< OtherReal > &S)
 
template<typename OtherReal >
void CopyFromMat (const MatrixBase< OtherReal > &M, MatrixTransposeType Trans)
 
template<typename OtherReal >
void CopyFromSp (const SpMatrix< OtherReal > &M)
 
template<typename OtherReal >
void CopyFromTp (const TpMatrix< OtherReal > &M, MatrixTransposeType Trans)
 
template<typename Real >
void CopyRowsFromVec (const VectorBase< Real > &rv)
 
template<typename OtherReal >
void CopyRowsFromVec (const VectorBase< OtherReal > &rv)
 
template<class Real >
void CopyFromMat (const CompressedMatrix &mat)
 
template<typename OtherReal >
void AddVecToRows (const Real alpha, const VectorBase< OtherReal > &v)
 
template<typename OtherReal >
void AddVecToCols (const Real alpha, const VectorBase< OtherReal > &v)
 

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 SparseMatrix< Real >
 
class SparseMatrix< float >
 
class SparseMatrix< double >
 
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 46 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 738 of file kaldi-matrix.h.

738  :
739  data_(data), num_cols_(cols), num_rows_(rows), stride_(stride) {
741  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:137
MatrixBase ( )
inlineexplicitprotected

Initializer, callable only from child.

Empty initializer, for un-initialized matrix.

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

745  : data_(NULL) {
747  }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:137
~MatrixBase ( )
inlineprotected

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

750 { }

Member Function Documentation

void Add ( const Real  alpha)

Add a scalar to each element.

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

References data_.

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

1676  {
1677  Real *data = data_;
1678  MatrixIndexT stride = stride_;
1679  for (MatrixIndexT r = 0; r < num_rows_; r++)
1680  for (MatrixIndexT c = 0; c < num_cols_; c++)
1681  data[c + stride*r] += alpha;
1682 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 2806 of file kaldi-matrix.cc.

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

2807  {
2808  KALDI_ASSERT(NumRows() == src.NumRows());
2809  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2810  this_stride = stride_, src_stride = src.stride_;
2811  Real *this_data = this->data_;
2812  const Real *src_data = src.data_;
2813 #ifdef KALDI_PARANOID
2814  MatrixIndexT src_cols = src.NumCols();
2815  for (MatrixIndexT i = 0; i < num_cols; i++)
2816  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2817 #endif
2818 
2819  // For the sake of memory locality we do this row by row, rather
2820  // than doing it column-wise using cublas_Xcopy
2821  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2822  const MatrixIndexT *index_ptr = &(indices[0]);
2823  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2824  if (*index_ptr >= 0)
2825  this_data[c] += src_data[*index_ptr];
2826  }
2827  }
2828 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#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 575 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::Dim(), 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().

579  {
580  if (beta != 1.0) this->Scale(beta);
581 
582  if (transM == kNoTrans) {
583  KALDI_ASSERT(SameDim(*this, M));
584  } else {
585  KALDI_ASSERT(M.NumRows() == NumCols() && M.NumCols() == NumRows());
586  }
587  KALDI_ASSERT(v.Dim() == this->NumRows());
588 
589  MatrixIndexT M_row_stride = M.Stride(), M_col_stride = 1, stride = stride_,
590  num_rows = num_rows_, num_cols = num_cols_;
591  if (transM == kTrans) std::swap(M_row_stride, M_col_stride);
592  Real *data = data_;
593  const Real *Mdata = M.Data(), *vdata = v.Data();
594  if (num_rows_ == 0) return;
595  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride, Mdata += M_row_stride, vdata++)
596  cblas_Xaxpy(num_cols, alpha * *vdata, Mdata, M_col_stride, data, 1);
597 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:98
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
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 355 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), data_, MatrixBase< Real >::data_, if(), 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(), 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::TypeOneUsage(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddMatMatElements(), kaldi::UnitTestAddMatSelf(), kaldi::UnitTestAddOuterProductPlusMinus(), kaldi::UnitTestAxpy(), kaldi::UnitTestCompressedMatrix(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuMatrixAddMat(), kaldi::UnitTestDeterminant(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestSparseMatrixAddToMat(), FmllrSgmm2Accs::Update(), EbwAmSgmm2Updater::UpdateM(), EbwAmSgmm2Updater::UpdateN(), IvectorExtractorStats::UpdateVariances(), MleAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateWGetStats(), and UpdateWClass::~UpdateWClass().

356  {
357  if (&A == this) {
358  if (transA == kNoTrans) {
359  Scale(alpha + 1.0);
360  } else {
361  KALDI_ASSERT(num_rows_ == num_cols_ && "AddMat: adding to self (transposed): not symmetric.");
362  Real *data = data_;
363  if (alpha == 1.0) { // common case-- handle separately.
364  for (MatrixIndexT row = 0; row < num_rows_; row++) {
365  for (MatrixIndexT col = 0; col < row; col++) {
366  Real *lower = data + (row * stride_) + col, *upper = data + (col
367  * stride_) + row;
368  Real sum = *lower + *upper;
369  *lower = *upper = sum;
370  }
371  *(data + (row * stride_) + row) *= 2.0; // diagonal.
372  }
373  } else {
374  for (MatrixIndexT row = 0; row < num_rows_; row++) {
375  for (MatrixIndexT col = 0; col < row; col++) {
376  Real *lower = data + (row * stride_) + col, *upper = data + (col
377  * stride_) + row;
378  Real lower_tmp = *lower;
379  *lower += alpha * *upper;
380  *upper += alpha * lower_tmp;
381  }
382  *(data + (row * stride_) + row) *= (1.0 + alpha); // diagonal.
383  }
384  }
385  }
386  } else {
387  int aStride = (int) A.stride_, stride = stride_;
388  Real *adata = A.data_, *data = data_;
389  if (transA == kNoTrans) {
390  KALDI_ASSERT(A.num_rows_ == num_rows_ && A.num_cols_ == num_cols_);
391  if (num_rows_ == 0) return;
392  for (MatrixIndexT row = 0; row < num_rows_; row++, adata += aStride,
393  data += stride) {
394  cblas_Xaxpy(num_cols_, alpha, adata, 1, data, 1);
395  }
396  } else {
397  KALDI_ASSERT(A.num_cols_ == num_rows_ && A.num_rows_ == num_cols_);
398  if (num_rows_ == 0) return;
399  for (MatrixIndexT row = 0; row < num_rows_; row++, adata++, data += stride)
400  cblas_Xaxpy(num_cols_, alpha, adata, aStride, data, 1);
401  }
402  }
403 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
if(num_rows_==0) return
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 600 of file kaldi-matrix.cc.

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

604  {
605 
606  if (beta != 1.0) this->Scale(beta);
607 
608  if (transM == kNoTrans) {
609  KALDI_ASSERT(SameDim(*this, M));
610  } else {
611  KALDI_ASSERT(M.NumRows() == NumCols() && M.NumCols() == NumRows());
612  }
613  KALDI_ASSERT(v.Dim() == this->NumCols());
614 
615  MatrixIndexT M_row_stride = M.Stride(),
616  M_col_stride = 1,
617  stride = stride_,
618  num_rows = num_rows_,
619  num_cols = num_cols_;
620 
621  if (transM == kTrans)
622  std::swap(M_row_stride, M_col_stride);
623 
624  Real *data = data_;
625  const Real *Mdata = M.Data(), *vdata = v.Data();
626  if (num_rows_ == 0) return;
627  for (MatrixIndexT i = 0; i < num_rows; i++){
628  for(MatrixIndexT j = 0; j < num_cols; j ++ ){
629  data[i*stride + j] += alpha * vdata[j] * Mdata[i*M_row_stride + j*M_col_stride];
630  }
631  }
632 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:98
void Scale(Real alpha)
Multiply each element with a scalar value.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 
)

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::ApplyPca(), kaldi::ApplyPreXformToGradient(), Sgmm2Project::ApplyProjection(), Fmpe::ApplyProjection(), Plda::ApplyTransform(), kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::CholeskyUnitTestTr(), kaldi::ComposeTransforms(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), kaldi::ComputeLdaTransform(), AmSgmm2::ComputeNormalizersInternal(), kaldi::ComputePca(), MleAmSgmm2Updater::ComputeSMeans(), 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(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), kaldi::nnet3::ReduceRankOfComponents(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), kaldi::SlowMatMul(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), TestSgmm2PreXform(), SpMatrix< Real >::TopEigs(), 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::UnitTestMatrixAddMatSmat(), kaldi::UnitTestMatrixAddSmatMat(), 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().

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 635 of file kaldi-matrix.cc.

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

638  {
639  KALDI_ASSERT(A.NumRows() == B.NumRows() && A.NumCols() == B.NumCols());
640  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
641  Real *data = data_;
642  const Real *dataA = A.Data();
643  const Real *dataB = B.Data();
644 
645  for (MatrixIndexT i = 0; i < num_rows_; i++) {
646  for (MatrixIndexT j = 0; j < num_cols_; j++) {
647  data[j] = beta*data[j] + alpha*dataA[j]*dataB[j];
648  }
649  data += Stride();
650  dataA += A.Stride();
651  dataB += B.Stride();
652  }
653 }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:67
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 1740 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().

1744  {
1745  // Note on time taken with different orders of computation. Assume not transposed in this /
1746  // discussion. Firstly, normalize expressions using A.NumCols == B.NumRows and B.NumCols == C.NumRows, prefer
1747  // rows where there is a choice.
1748  // time taken for (AB) is: A.NumRows*B.NumRows*C.Rows
1749  // time taken for (AB)C is A.NumRows*C.NumRows*C.Cols
1750  // so this order is A.NumRows*B.NumRows*C.NumRows + A.NumRows*C.NumRows*C.NumCols.
1751 
1752  // time taken for (BC) is: B.NumRows*C.NumRows*C.Cols
1753  // time taken for A(BC) is: A.NumRows*B.NumRows*C.Cols
1754  // so this order is B.NumRows*C.NumRows*C.NumCols + A.NumRows*B.NumRows*C.Cols
1755 
1756  MatrixIndexT ARows = A.num_rows_, ACols = A.num_cols_, BRows = B.num_rows_, BCols = B.num_cols_,
1757  CRows = C.num_rows_, CCols = C.num_cols_;
1758  if (transA == kTrans) std::swap(ARows, ACols);
1759  if (transB == kTrans) std::swap(BRows, BCols);
1760  if (transC == kTrans) std::swap(CRows, CCols);
1761 
1762  MatrixIndexT AB_C_time = ARows*BRows*CRows + ARows*CRows*CCols;
1763  MatrixIndexT A_BC_time = BRows*CRows*CCols + ARows*BRows*CCols;
1764 
1765  if (AB_C_time < A_BC_time) {
1766  Matrix<Real> AB(ARows, BCols);
1767  AB.AddMatMat(1.0, A, transA, B, transB, 0.0); // AB = A * B.
1768  (*this).AddMatMat(alpha, AB, kNoTrans, C, transC, beta);
1769  } else {
1770  Matrix<Real> BC(BRows, CCols);
1771  BC.AddMatMat(1.0, B, transB, C, transC, 0.0); // BC = B * C.
1772  (*this).AddMatMat(alpha, A, transA, BC, kNoTrans, beta);
1773  }
1774 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
int32 MatrixIndexT
Definition: matrix-common.h:98
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
void AddMatSmat ( Real  alpha,
const MatrixBase< Real > &  A,
const SparseMatrix< Real > &  B,
MatrixTransposeType  transB,
Real  beta 
)

(*this) = alpha * A * op(B) + beta * (*this), where B is sparse and op(B) is either B or trans(B) depending on the 'transB' argument.

This is multiplication of a dense by a sparse matrix. See also AddSmatMat.

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

References kaldi::cblas_Xaxpy(), MatrixBase< Real >::Data(), SparseVector< Real >::GetElement(), rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), SparseMatrix< Real >::NumCols(), SparseVector< Real >::NumElements(), MatrixBase< Real >::NumRows(), SparseMatrix< Real >::NumRows(), SparseMatrix< Real >::Row(), and MatrixBase< Real >::stride_.

Referenced by kaldi::UnitTestMatrixAddMatSmat(), and kaldi::UnitTextCuMatrixAddMatSmat().

492  {
493  if (transB == kNoTrans) {
494  KALDI_ASSERT(NumRows() == A.NumRows());
495  KALDI_ASSERT(NumCols() == B.NumCols());
496  KALDI_ASSERT(A.NumCols() == B.NumRows());
497 
498  this->Scale(beta);
499  MatrixIndexT b_num_rows = B.NumRows(),
500  this_num_rows = this->NumRows();
501  // Iterate over the rows of sparse matrix B and columns of A.
502  for (MatrixIndexT k = 0; k < b_num_rows; ++k) {
503  const SparseVector<Real> &B_row_k = B.Row(k);
504  MatrixIndexT num_elems = B_row_k.NumElements();
505  const Real *a_col_k = A.Data() + k;
506  for (MatrixIndexT e = 0; e < num_elems; ++e) {
507  const std::pair<MatrixIndexT, Real> &p = B_row_k.GetElement(e);
508  MatrixIndexT j = p.first;
509  Real alpha_B_kj = alpha * p.second;
510  Real *this_col_j = this->Data() + j;
511  // Add to entire 'j'th column of *this at once using cblas_Xaxpy.
512  // pass stride to write a colmun as matrices are stored in row major order.
513  cblas_Xaxpy(this_num_rows, alpha_B_kj, a_col_k, A.stride_,
514  this_col_j, this->stride_);
515  //for (MatrixIndexT i = 0; i < this_num_rows; ++i)
516  // this_col_j[i*this->stride_] += alpha_B_kj * a_col_k[i*A.stride_];
517  }
518  }
519  } else {
520  KALDI_ASSERT(NumRows() == A.NumRows());
521  KALDI_ASSERT(NumCols() == B.NumRows());
522  KALDI_ASSERT(A.NumCols() == B.NumCols());
523 
524  this->Scale(beta);
525  MatrixIndexT b_num_rows = B.NumRows(),
526  this_num_rows = this->NumRows();
527  // Iterate over the rows of sparse matrix B and columns of *this.
528  for (MatrixIndexT j = 0; j < b_num_rows; ++j) {
529  const SparseVector<Real> &B_row_j = B.Row(j);
530  MatrixIndexT num_elems = B_row_j.NumElements();
531  Real *this_col_j = this->Data() + j;
532  for (MatrixIndexT e = 0; e < num_elems; ++e) {
533  const std::pair<MatrixIndexT, Real> &p = B_row_j.GetElement(e);
534  MatrixIndexT k = p.first;
535  Real alpha_B_jk = alpha * p.second;
536  const Real *a_col_k = A.Data() + k;
537  // Add to entire 'j'th column of *this at once using cblas_Xaxpy.
538  // pass stride to write a column as matrices are stored in row major order.
539  cblas_Xaxpy(this_num_rows, alpha_B_jk, a_col_k, A.stride_,
540  this_col_j, this->stride_);
541  //for (MatrixIndexT i = 0; i < this_num_rows; ++i)
542  // this_col_j[i*this->stride_] += alpha_B_jk * a_col_k[i*A.stride_];
543  }
544  }
545  }
546 }
int32 MatrixIndexT
Definition: matrix-common.h:98
void Scale(Real alpha)
Multiply each element with a scalar value.
KALDI_ASSERT & A
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:76
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
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 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 266 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().

271  {
272  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
273  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
274  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
275  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
276  KALDI_ASSERT(&A != this && &B != this);
277 
278  // We iterate over the columns of B.
279 
280  MatrixIndexT Astride = A.stride_, Bstride = B.stride_, stride = this->stride_,
281  Arows = A.num_rows_, Acols = A.num_cols_;
282  Real *data = this->data_, *Adata = A.data_, *Bdata = B.data_;
283  MatrixIndexT num_cols = this->num_cols_;
284  if (transB == kNoTrans) {
285  // Iterate over the columns of *this and of B.
286  for (MatrixIndexT c = 0; c < num_cols; c++) {
287  // for each column of *this, do
288  // [this column] = [alpha * A * this column of B] + [beta * this column]
289  Xgemv_sparsevec(transA, Arows, Acols, alpha, Adata, Astride,
290  Bdata + c, Bstride, beta, data + c, stride);
291  }
292  } else {
293  // Iterate over the columns of *this and the rows of B.
294  for (MatrixIndexT c = 0; c < num_cols; c++) {
295  // for each column of *this, do
296  // [this column] = [alpha * A * this row of B] + [beta * this column]
297  Xgemv_sparsevec(transA, Arows, Acols, alpha, Adata, Astride,
298  Bdata + (c * Bstride), 1, beta, data + c, stride);
299  }
300  }
301 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
#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 662 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().

665  {
666  Matrix<Real> M(B);
667  return AddMatMat(alpha, A, transA, M, kNoTrans, beta);
668  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 679 of file kaldi-matrix.h.

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

682  {
683  Matrix<Real> M(B);
684  return AddMatMat(alpha, A, transA, M, transB, beta);
685  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 2872 of file kaldi-matrix.cc.

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

2874  {
2875  KALDI_ASSERT(NumCols() == src.NumCols());
2876  MatrixIndexT num_rows = num_rows_,
2877  num_cols = num_cols_, this_stride = stride_;
2878  Real *this_data = this->data_;
2879 
2880  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2881  MatrixIndexT index = indexes[r];
2882  KALDI_ASSERT(index >= -1 && index < src.NumRows());
2883  if (index != -1)
2884  cblas_Xaxpy(num_cols, alpha, src.RowData(index), 1, this_data, 1);
2885  }
2886 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
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 2889 of file kaldi-matrix.cc.

References kaldi::cblas_Xaxpy(), and data_.

2889  {
2890  MatrixIndexT num_rows = num_rows_,
2891  num_cols = num_cols_, this_stride = stride_;
2892  Real *this_data = this->data_;
2893 
2894  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2895  const Real *const src_data = src[r];
2896  if (src_data != NULL)
2897  cblas_Xaxpy(num_cols, alpha, src_data, 1, this_data, 1);
2898  }
2899 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
void AddSmat ( Real  alpha,
const SparseMatrix< Real > &  A,
MatrixTransposeType  trans = kNoTrans 
)

*this += alpha * A [or A^T].

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

References SparseVector< Real >::GetElement(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, SparseMatrix< Real >::NumCols(), SparseVector< Real >::NumElements(), SparseMatrix< Real >::NumRows(), and SparseMatrix< Real >::Row().

Referenced by GeneralMatrix::AddToMat(), and kaldi::UnitTextCuMatrixAddSmat().

407  {
408  if (trans == kNoTrans) {
409  KALDI_ASSERT(NumRows() == A.NumRows());
410  KALDI_ASSERT(NumCols() == A.NumCols());
411  MatrixIndexT a_num_rows = A.NumRows();
412  for (MatrixIndexT i = 0; i < a_num_rows; ++i) {
413  const SparseVector<Real> &row = A.Row(i);
414  MatrixIndexT num_elems = row.NumElements();
415  for (MatrixIndexT id = 0; id < num_elems; ++id) {
416  (*this)(i, row.GetElement(id).first) += alpha
417  * row.GetElement(id).second;
418  }
419  }
420  } else {
421  KALDI_ASSERT(NumRows() == A.NumCols());
422  KALDI_ASSERT(NumCols() == A.NumRows());
423  MatrixIndexT a_num_rows = A.NumRows();
424  for (MatrixIndexT i = 0; i < a_num_rows; ++i) {
425  const SparseVector<Real> &row = A.Row(i);
426  MatrixIndexT num_elems = row.NumElements();
427  for (MatrixIndexT id = 0; id < num_elems; ++id) {
428  (*this)(row.GetElement(id).first, i) += alpha
429  * row.GetElement(id).second;
430  }
431  }
432  }
433 }
int32 MatrixIndexT
Definition: matrix-common.h:98
KALDI_ASSERT & A
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddSmatMat ( Real  alpha,
const SparseMatrix< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
Real  beta 
)

(*this) = alpha * op(A) * B + beta * (*this), where A is sparse.

Multiplication of sparse with dense matrix. See also AddMatSmat.

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

References kaldi::cblas_Xaxpy(), SparseVector< Real >::GetElement(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kSetZero, MatrixBase< Real >::NumCols(), SparseMatrix< Real >::NumCols(), SparseVector< Real >::NumElements(), MatrixBase< Real >::NumRows(), SparseMatrix< Real >::NumRows(), SparseMatrix< Real >::Row(), and MatrixBase< Real >::RowData().

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

438  {
439  if (transA == kNoTrans) {
440  KALDI_ASSERT(NumRows() == A.NumRows());
441  KALDI_ASSERT(NumCols() == B.NumCols());
442  KALDI_ASSERT(A.NumCols() == B.NumRows());
443 
444  this->Scale(beta);
445  MatrixIndexT a_num_rows = A.NumRows(),
446  this_num_cols = this->NumCols();
447  for (MatrixIndexT i = 0; i < a_num_rows; ++i) {
448  Real *this_row_i = this->RowData(i);
449  const SparseVector<Real> &A_row_i = A.Row(i);
450  MatrixIndexT num_elems = A_row_i.NumElements();
451  for (MatrixIndexT e = 0; e < num_elems; ++e) {
452  const std::pair<MatrixIndexT, Real> &p = A_row_i.GetElement(e);
453  MatrixIndexT k = p.first;
454  Real alpha_A_ik = alpha * p.second;
455  const Real *b_row_k = B.RowData(k);
456  cblas_Xaxpy(this_num_cols, alpha_A_ik, b_row_k, 1,
457  this_row_i, 1);
458  //for (MatrixIndexT j = 0; j < this_num_cols; ++j)
459  // this_row_i[j] += alpha_A_ik * b_row_k[j];
460  }
461  }
462  } else {
463  KALDI_ASSERT(NumRows() == A.NumCols());
464  KALDI_ASSERT(NumCols() == B.NumCols());
465  KALDI_ASSERT(A.NumRows() == B.NumRows());
466 
467  this->Scale(beta);
469  MatrixIndexT a_num_rows = A.NumRows(),
470  this_num_cols = this->NumCols();
471  for (int k = 0; k < a_num_rows; ++k) {
472  const Real *b_row_k = B.RowData(k);
473  const SparseVector<Real> &A_row_k = A.Row(k);
474  MatrixIndexT num_elems = A_row_k.NumElements();
475  for (MatrixIndexT e = 0; e < num_elems; ++e) {
476  const std::pair<MatrixIndexT, Real> &p = A_row_k.GetElement(e);
477  MatrixIndexT i = p.first;
478  Real alpha_A_ki = alpha * p.second;
479  Real *this_row_i = this->RowData(i);
480  cblas_Xaxpy(this_num_cols, alpha_A_ki, b_row_k, 1,
481  this_row_i, 1);
482  //for (MatrixIndexT j = 0; j < this_num_cols; ++j)
483  // this_row_i[j] += alpha_A_ki * b_row_k[j];
484  }
485  }
486  }
487 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
void Scale(Real alpha)
Multiply each element with a scalar value.
KALDI_ASSERT & A
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
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
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 304 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().

309  {
310  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
311  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
312  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
313  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
314  KALDI_ASSERT(&A != this && &B != this);
315 
316  MatrixIndexT Astride = A.stride_, Bstride = B.stride_, stride = this->stride_,
317  Brows = B.num_rows_, Bcols = B.num_cols_;
318  MatrixTransposeType invTransB = (transB == kTrans ? kNoTrans : kTrans);
319  Real *data = this->data_, *Adata = A.data_, *Bdata = B.data_;
320  MatrixIndexT num_rows = this->num_rows_;
321  if (transA == kNoTrans) {
322  // Iterate over the rows of *this and of A.
323  for (MatrixIndexT r = 0; r < num_rows; r++) {
324  // for each row of *this, do
325  // [this row] = [alpha * (this row of A) * B^T] + [beta * this row]
326  Xgemv_sparsevec(invTransB, Brows, Bcols, alpha, Bdata, Bstride,
327  Adata + (r * Astride), 1, beta, data + (r * stride), 1);
328  }
329  } else {
330  // Iterate over the rows of *this and the columns of A.
331  for (MatrixIndexT r = 0; r < num_rows; r++) {
332  // for each row of *this, do
333  // [this row] = [alpha * (this column of A) * B^T] + [beta * this row]
334  Xgemv_sparsevec(invTransB, Brows, Bcols, alpha, Bdata, Bstride,
335  Adata + r, Astride, beta, data + (r * stride), 1);
336  }
337  }
338 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixTransposeType
Definition: matrix-common.h:32
void AddSp ( const Real  alpha,
const SpMatrix< OtherReal > &  S 
)

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

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

550  {
551  KALDI_ASSERT(S.NumRows() == NumRows() && S.NumRows() == NumCols());
552  Real *data = data_; const OtherReal *sdata = S.Data();
553  MatrixIndexT num_rows = NumRows(), stride = Stride();
554  for (MatrixIndexT i = 0; i < num_rows; i++) {
555  for (MatrixIndexT j = 0; j < i; j++, sdata++) {
556  data[i*stride + j] += alpha * *sdata;
557  data[j*stride + i] += alpha * *sdata;
558  }
559  data[i*stride + i] += alpha * *sdata++;
560  }
561 }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:67
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddSp ( const Real  alpha,
const SpMatrix< OtherReal > &  S 
)

*this += alpha * S

Referenced by kaldi::UnitTestAddSp().

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 646 of file kaldi-matrix.h.

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

649  {
650  Matrix<Real> M(A);
651  return AddMatMat(alpha, M, kNoTrans, B, transB, beta);
652  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 670 of file kaldi-matrix.h.

674  {
675  Matrix<Real> M(A), N(C);
676  return AddMatMatMat(alpha, M, kNoTrans, B, transB, N, kNoTrans, beta);
677  }
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.
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 341 of file kaldi-matrix.cc.

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

Referenced by kaldi::UnitTestMmulSym().

342  {
343  MatrixIndexT sz = num_rows_;
344  KALDI_ASSERT(sz == num_cols_ && sz == A_in.NumRows() && sz == B_in.NumRows());
345 
346  Matrix<Real> A(A_in), B(B_in);
347  // CblasLower or CblasUpper would work below as symmetric matrix is copied
348  // fully (to save work, we used the matrix constructor from SpMatrix).
349  // CblasLeft means A is on the left: C <-- alpha A B + beta C
350  if (sz == 0) return;
351  cblas_Xsymm(alpha, sz, A.data_, A.stride_, B.data_, B.stride_, beta, data_, stride_);
352 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
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:766
KALDI_ASSERT & A
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
void AddToDiag ( const Real  alpha)

Add a scalar to each diagonal element.

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

References data_.

Referenced by kaldi::UnitTestCuMatrixAddToDiag().

1685  {
1686  Real *data = data_;
1687  MatrixIndexT this_stride = stride_ + 1,
1688  num_to_add = std::min(num_rows_, num_cols_);
1689  for (MatrixIndexT r = 0; r < num_to_add; r++)
1690  data[r * this_stride] += alpha;
1691 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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).

Referenced by kaldi::UnitTestAddToRows().

void AddToRows ( Real  alpha,
const MatrixIndexT indexes,
MatrixBase< Real > *  dst 
) const

For each row i of *this, adds this->Row(i) to dst->Row(indexes(i)) if indexes(i) >= 0, else do nothing.

Requires that all the indexes[i] that are >= 0 be distinct, otherwise the behavior is undefined.

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 654 of file kaldi-matrix.h.

Referenced by kaldi::UnitTestCuMatrixAddTpMat().

657  {
658  Matrix<Real> M(A);
659  return AddMatMat(alpha, M, transA, B, transB, beta);
660  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 688 of file kaldi-matrix.h.

691  {
692  Matrix<Real> M(A), N(B);
693  return AddMatMat(alpha, M, transA, N, transB, beta);
694  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
KALDI_ASSERT & A
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
void AddVecToCols ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)
void AddVecToCols ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

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

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

3015  {
3016  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3017  stride = stride_;
3018  KALDI_ASSERT(v.Dim() == num_rows);
3019 
3020  if (num_rows <= 64) {
3021  Real *data = data_;
3022  const OtherReal *vdata = v.Data();
3023  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
3024  Real to_add = alpha * vdata[i];
3025  for (MatrixIndexT j = 0; j < num_cols; j++)
3026  data[j] += to_add;
3027  }
3028 
3029  } else {
3030  Vector<OtherReal> ones(num_cols);
3031  ones.Set(1.0);
3032  this->AddVecVec(alpha, v, ones);
3033  }
3034 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 AddVecToRows ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

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

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

2984  {
2985  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2986  stride = stride_;
2987  KALDI_ASSERT(v.Dim() == num_cols);
2988  if(num_cols <= 64) {
2989  Real *data = data_;
2990  const OtherReal *vdata = v.Data();
2991  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
2992  for (MatrixIndexT j = 0; j < num_cols; j++)
2993  data[j] += alpha * vdata[j];
2994  }
2995 
2996  } else {
2997  Vector<OtherReal> ones(num_rows);
2998  ones.Set(1.0);
2999  this->AddVecVec(alpha, ones, v);
3000  }
3001 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 ApplyCeiling ( Real  ceiling_val)

Applies floor to all matrix elements.

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

References rnnlm::j.

Referenced by kaldi::UnitTestCuMatrixApplyCeiling(), and kaldi::UnitTestCuMatrixApplyExpLimited().

2111  {
2112  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2113  for (MatrixIndexT i = 0; i < num_rows; i++) {
2114  Real *data = this->RowData(i);
2115  for (MatrixIndexT j = 0; j < num_cols; j++)
2116  data[j] = (data[j] > ceiling_val ? ceiling_val : data[j]);
2117  }
2118 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void ApplyExp ( )

Exponentiate each of the elements.

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

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

2128  {
2129  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2130  Row(i).ApplyExp();
2131  }
2132 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
void ApplyExpSpecial ( )

For each element x of the matrix, set it to (x < 0 ? exp(x) : x + 1).

This function is used in our RNNLM training.

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

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

Referenced by kaldi::UnitTestApplyExpSpecial(), and kaldi::UnitTestCuMatrixApplyExpSpecial().

2135  {
2136  int32 num_rows = num_rows_, num_cols = num_cols_,
2137  stride = stride_;
2138  Real *data = data_;
2139  for (MatrixIndexT i = 0; i < num_rows; ++i) {
2140  for (MatrixIndexT j = 0; j < num_cols; ++j) {
2141  Real &x = *(data + j + stride * i);
2142  x = x < Real(0) ? Exp(x) : x + Real(1);
2143  }
2144  }
2145 }
double Exp(double x)
Definition: kaldi-math.h:83
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void ApplyFloor ( Real  floor_val)

Applies floor to all matrix elements.

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

References rnnlm::j.

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

2101  {
2102  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2103  for (MatrixIndexT i = 0; i < num_rows; i++) {
2104  Real *data = this->RowData(i);
2105  for (MatrixIndexT j = 0; j < num_cols; j++)
2106  data[j] = (data[j] < floor_val ? floor_val : data[j]);
2107  }
2108 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 2162 of file kaldi-matrix.cc.

References rnnlm::j.

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

2162  {
2163  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2164  for (MatrixIndexT i = 0; i < num_rows; i++) {
2165  Real *data = this->RowData(i);
2166  for (MatrixIndexT j = 0; j < num_cols; j++)
2167  data[j] = (data[j] > 0 ? 1.0 : 0.0);
2168  }
2169 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void ApplyLog ( )

Calculates log of all the matrix elemnts.

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

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

2121  {
2122  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2123  Row(i).ApplyLog();
2124  }
2125 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
void ApplyPow ( Real  power)

Applies power to all matrix elements.

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

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

2148  {
2149  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2150  Row(i).ApplyPow(power);
2151  }
2152 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
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 2155 of file kaldi-matrix.cc.

Referenced by kaldi::UnitTestCuMatrixApplyPowAbs().

2155  {
2156  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2157  Row(i).ApplyPowAbs(power, include_sign);
2158  }
2159 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
Real ApplySoftMax ( )

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

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

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

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

2705  {
2706  Real max = this->Max(), sum = 0.0;
2707  // the 'max' helps to get in good numeric range.
2708  for (MatrixIndexT i = 0; i < num_rows_; i++)
2709  for (MatrixIndexT j = 0; j < num_cols_; j++)
2710  sum += ((*this)(i, j) = Exp((*this)(i, j) - max));
2711  this->Scale(1.0 / sum);
2712  return max + Log(sum);
2713 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns maximum element of matrix.
int32 MatrixIndexT
Definition: matrix-common.h:98
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:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
bool ApproxEqual ( const MatrixBase< Real > &  other,
float  tol = 0.01 
) const

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

Definition at line 1914 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().

1914  {
1915  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1916  KALDI_ERR << "ApproxEqual: size mismatch.";
1917  Matrix<Real> tmp(*this);
1918  tmp.AddMat(-1.0, other);
1919  return (tmp.FrobeniusNorm() <= static_cast<Real>(tol) *
1920  this->FrobeniusNorm());
1921 }
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
SubMatrix<Real> ColRange ( const MatrixIndexT  col_offset,
const MatrixIndexT  num_cols 
) const
inline

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

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

211  {
212  return SubMatrix<Real>(*this, 0, num_rows_, col_offset, num_cols);
213  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:540
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
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 1695 of file kaldi-matrix.cc.

References VectorBase< Real >::Dim(), 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().

1695  {
1696  KALDI_ASSERT(num_rows_ > 0&&num_cols_ > 0);
1697  Vector<Real> singular_values(std::min(num_rows_, num_cols_));
1698  Svd(&singular_values); // Get singular values...
1699  Real min = singular_values(0), max = singular_values(0); // both absolute values...
1700  for (MatrixIndexT i = 1;i < singular_values.Dim();i++) {
1701  min = std::min((Real)std::abs(singular_values(i)), min); max = std::max((Real)std::abs(singular_values(i)), max);
1702  }
1703  if (min > 0) return max/min;
1704  else return std::numeric_limits<Real>::infinity();
1705 }
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:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 1101 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().

1102  {
1103  KALDI_ASSERT(rv.Dim() == num_rows_ &&
1104  static_cast<UnsignedMatrixIndexT>(col) <
1105  static_cast<UnsignedMatrixIndexT>(num_cols_));
1106 
1107  const Real *rv_data = rv.Data();
1108  Real *col_data = data_ + col;
1109 
1110  for (MatrixIndexT r = 0; r < num_rows_; r++)
1111  col_data[r * stride_] = rv_data[r];
1112 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
uint32 UnsignedMatrixIndexT
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 2780 of file kaldi-matrix.cc.

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

2781  {
2782  KALDI_ASSERT(NumRows() == src.NumRows());
2783  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2784  this_stride = stride_, src_stride = src.stride_;
2785  Real *this_data = this->data_;
2786  const Real *src_data = src.data_;
2787 #ifdef KALDI_PARANOID
2788  MatrixIndexT src_cols = src.NumCols();
2789  for (MatrixIndexT i = 0; i < num_cols; i++)
2790  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2791 #endif
2792 
2793  // For the sake of memory locality we do this row by row, rather
2794  // than doing it column-wise using cublas_Xcopy
2795  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2796  const MatrixIndexT *index_ptr = &(indices[0]);
2797  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2798  if (*index_ptr < 0) this_data[c] = 0;
2799  else this_data[c] = src_data[*index_ptr];
2800  }
2801  }
2802 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#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 1052 of file kaldi-matrix.cc.

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

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

1052  {
1053  if (rv.Dim() == num_rows_*num_cols_) {
1054  const Real *v_inc_data = rv.Data();
1055  Real *m_inc_data = data_;
1056 
1057  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1058  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1059  m_inc_data[r * stride_] = v_inc_data[r];
1060  }
1061  v_inc_data += num_rows_;
1062  m_inc_data ++;
1063  }
1064  } else if (rv.Dim() == num_rows_) {
1065  const Real *v_inc_data = rv.Data();
1066  Real *m_inc_data = data_;
1067  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1068  Real value = *(v_inc_data++);
1069  for (MatrixIndexT c = 0; c < num_cols_; c++)
1070  m_inc_data[c] = value;
1071  m_inc_data += stride_;
1072  }
1073  } else {
1074  KALDI_ERR << "Wrong size of arguments.";
1075  }
1076 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
void CopyDiagFromVec ( const VectorBase< Real > &  v)

Copy vector into diagonal of matrix.

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

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

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

1092  {
1093  KALDI_ASSERT(rv.Dim() == std::min(num_cols_, num_rows_));
1094  const Real *rv_data = rv.Data(), *rv_end = rv_data + rv.Dim();
1095  Real *my_data = this->Data();
1096  for (; rv_data != rv_end; rv_data++, my_data += (this->stride_+1))
1097  *my_data = *rv_data;
1098 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:76
#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).

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(), 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::EstPca(), 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=(), OnlineNaturalGradientSimple::PreconditionDirections(), OnlinePreconditionerSimple::PreconditionDirections(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), AffineXformStats::Read(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), LogisticRegression::SetWeights(), kaldi::SlidingWindowCmn(), kaldi::SolveQuadraticMatrixProblem(), 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::UnitTestMax2(), kaldi::UnitTestMulElements(), kaldi::UnitTestPca2(), kaldi::UnitTestRegtreeFmllrDiagGmm(), kaldi::UnitTestScale(), kaldi::UnitTestSvd(), kaldi::UnitTestSvdZero(), kaldi::UnitTestSwapCu2M(), kaldi::UnitTestTrace(), kaldi::UnitTestTrain(), MlltAccs::Update(), FmllrDiagGmmAccs::Update(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), PldaUnsupervisedAdaptor::UpdatePlda(), MleAmSgmm2Updater::UpdateW(), and AccumDiagGmm::Write().

void CopyFromMat ( const CompressedMatrix M)

Copy from compressed matrix.

void CopyFromMat ( const CuMatrixBase< OtherReal > &  M,
MatrixTransposeType  trans = kNoTrans 
)

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

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

919  {
920  cu.CopyToMat(this, trans);
921 }
void CopyFromMat ( const MatrixBase< OtherReal > &  M,
MatrixTransposeType  Trans 
)

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

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

862  {
863  if (sizeof(Real) == sizeof(OtherReal) &&
864  static_cast<const void*>(M.Data()) ==
865  static_cast<const void*>(this->Data())) {
866  // CopyFromMat called on same data. Nothing to do (except sanity checks).
867  KALDI_ASSERT(Trans == kNoTrans && M.NumRows() == NumRows() &&
868  M.NumCols() == NumCols() && M.Stride() == Stride());
869  return;
870  }
871  if (Trans == kNoTrans) {
872  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == M.NumCols());
873  for (MatrixIndexT i = 0; i < num_rows_; i++)
874  (*this).Row(i).CopyFromVec(M.Row(i));
875  } else {
876  KALDI_ASSERT(num_cols_ == M.NumRows() && num_rows_ == M.NumCols());
877  int32 this_stride = stride_, other_stride = M.Stride();
878  Real *this_data = data_;
879  const OtherReal *other_data = M.Data();
880  for (MatrixIndexT i = 0; i < num_rows_; i++)
881  for (MatrixIndexT j = 0; j < num_cols_; j++)
882  this_data[i * this_stride + j] = other_data[j * other_stride + i];
883  }
884 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:67
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:76
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromMat ( const CompressedMatrix mat)

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

References CompressedMatrix::CopyToMat().

2056  {
2057  mat.CopyToMat(this);
2058 }
void CopyFromSp ( const SpMatrix< OtherReal > &  M)
void CopyFromSp ( const SpMatrix< OtherReal > &  M)

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

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

937  {
938  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
939  // MORE EFFICIENT IF LOWER TRIANGULAR! Reverse code otherwise.
940  for (MatrixIndexT i = 0; i < num_rows_; i++) {
941  for (MatrixIndexT j = 0; j < i; j++) {
942  (*this)(j, i) = (*this)(i, j) = M(i, j);
943  }
944  (*this)(i, i) = M(i, i);
945  }
946 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromTp ( const TpMatrix< OtherReal > &  M,
MatrixTransposeType  Trans 
)

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

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

958  {
959  if (Trans == kNoTrans) {
960  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
961  SetZero();
962  Real *out_i = data_;
963  const OtherReal *in_i = M.Data();
964  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i += stride_, in_i += i) {
965  for (MatrixIndexT j = 0; j <= i; j++)
966  out_i[j] = in_i[j];
967  }
968  } else {
969  SetZero();
970  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
971  MatrixIndexT stride = stride_;
972  Real *out_i = data_;
973  const OtherReal *in_i = M.Data();
974  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i ++, in_i += i) {
975  for (MatrixIndexT j = 0; j <= i; j++)
976  out_i[j*stride] = in_i[j];
977  }
978  }
979 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 211 of file kaldi-matrix.cc.

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

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

211  {
213  Real *data = data_;
214  MatrixIndexT num_rows = num_rows_, stride = stride_;
215  for (int32 i = 0; i < num_rows; i++)
216  for (int32 j = 0; j < i; j++)
217  data[j * stride + i ] = data[i * stride + j];
218 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 1080 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().

1080  {
1081  KALDI_ASSERT(rv.Dim() == num_cols_ &&
1082  static_cast<UnsignedMatrixIndexT>(row) <
1083  static_cast<UnsignedMatrixIndexT>(num_rows_));
1084 
1085  const Real *rv_data = rv.Data();
1086  Real *row_data = RowData(row);
1087 
1088  std::memcpy(row_data, rv_data, num_cols_ * sizeof(Real));
1089 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
uint32 UnsignedMatrixIndexT
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 2831 of file kaldi-matrix.cc.

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

2832  {
2833  KALDI_ASSERT(NumCols() == src.NumCols());
2834  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2835  this_stride = stride_;
2836  Real *this_data = this->data_;
2837 
2838  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2839  MatrixIndexT index = indices[r];
2840  if (index < 0) memset(this_data, 0, sizeof(Real) * num_cols_);
2841  else cblas_Xcopy(num_cols, src.RowData(index), 1, this_data, 1);
2842  }
2843 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 2846 of file kaldi-matrix.cc.

References kaldi::cblas_Xcopy(), and data_.

2846  {
2847  MatrixIndexT num_rows = num_rows_,
2848  num_cols = num_cols_, this_stride = stride_;
2849  Real *this_data = this->data_;
2850 
2851  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2852  const Real *const src_data = src[r];
2853  if (src_data == NULL) memset(this_data, 0, sizeof(Real) * num_cols);
2854  else cblas_Xcopy(num_cols, src_data, 1, this_data, 1);
2855  }
2856 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void CopyRowsFromVec ( const CuVectorBase< Real > &  v)

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

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

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

242  {
243  KALDI_ASSERT(v.Dim() == NumCols() * NumRows());
244 #if HAVE_CUDA == 1
245  if (CuDevice::Instantiate().Enabled()) {
246  if (num_rows_ == 0) return;
247  CuTimer tim;
248  if (Stride() == NumCols()) {
249  CU_SAFE_CALL(cudaMemcpy(data_, v.Data(),
250  sizeof(Real)*v.Dim(),
251  cudaMemcpyDeviceToHost));
252  } else {
253  const Real* vec_data = v.Data();
254  for (MatrixIndexT r = 0; r < NumRows(); r++) {
255  CU_SAFE_CALL(cudaMemcpy(RowData(r), vec_data,
256  sizeof(Real) * NumCols(),
257  cudaMemcpyDeviceToHost));
258  vec_data += NumCols();
259  }
260  }
261  CuDevice::Instantiate().AccuProfile(__func__, tim);
262  } else
263 #endif
264  {
265  CopyRowsFromVec(v.Vec());
266  }
267 }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:67
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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)
void CopyRowsFromVec ( const VectorBase< Real > &  rv)

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

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

996  {
997  if (rv.Dim() == num_rows_*num_cols_) {
998  if (stride_ == num_cols_) {
999  // one big copy operation.
1000  const Real *rv_data = rv.Data();
1001  std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_);
1002  } else {
1003  const Real *rv_data = rv.Data();
1004  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1005  Real *row_data = RowData(r);
1006  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1007  row_data[c] = rv_data[c];
1008  }
1009  rv_data += num_cols_;
1010  }
1011  }
1012  } else if (rv.Dim() == num_cols_) {
1013  const Real *rv_data = rv.Data();
1014  for (MatrixIndexT r = 0; r < num_rows_; r++)
1015  std::memcpy(RowData(r), rv_data, sizeof(Real)*num_cols_);
1016  } else {
1017  KALDI_ERR << "Wrong sized arguments";
1018  }
1019 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
void CopyRowsFromVec ( const VectorBase< OtherReal > &  rv)

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

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

1023  {
1024  if (rv.Dim() == num_rows_*num_cols_) {
1025  const OtherReal *rv_data = rv.Data();
1026  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1027  Real *row_data = RowData(r);
1028  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1029  row_data[c] = static_cast<Real>(rv_data[c]);
1030  }
1031  rv_data += num_cols_;
1032  }
1033  } else if (rv.Dim() == num_cols_) {
1034  const OtherReal *rv_data = rv.Data();
1035  Real *first_row_data = RowData(0);
1036  for (MatrixIndexT c = 0; c < num_cols_; c++)
1037  first_row_data[c] = rv_data[c];
1038  for (MatrixIndexT r = 1; r < num_rows_; r++)
1039  std::memcpy(RowData(r), first_row_data, sizeof(Real)*num_cols_);
1040  } else {
1041  KALDI_ERR << "Wrong sized arguments.";
1042  }
1043 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
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 2859 of file kaldi-matrix.cc.

References kaldi::cblas_Xcopy(), and data_.

Referenced by kaldi::UnitTestCopyToRows().

2859  {
2860  MatrixIndexT num_rows = num_rows_,
2861  num_cols = num_cols_, this_stride = stride_;
2862  const Real *this_data = this->data_;
2863 
2864  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2865  Real *const dst_data = dst[r];
2866  if (dst_data != NULL)
2867  cblas_Xcopy(num_cols, this_data, 1, dst_data, 1);
2868  }
2869 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void CopyUpperToLower ( )

Copy upper triangle to lower triangle (symmetrize)

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

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

222  {
224  Real *data = data_;
225  MatrixIndexT num_rows = num_rows_, stride = stride_;
226  for (int32 i = 0; i < num_rows; i++)
227  for (int32 j = 0; j < i; j++)
228  data[i * stride + j] = data[j * stride + i];
229 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const Real* Data ( ) const
inline
Real* Data ( )
inline

Gives pointer to raw data (non-const).

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

81 { return data_; }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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 757 of file kaldi-matrix.h.

757  {
758  return data_;
759  }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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 1780 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(), and AffineComponent::LimitRank().

1780  {
1781  // Svd, *this = U*diag(s)*Vt.
1782  // With (*this).num_rows_ == m, (*this).num_cols_ == n,
1783  // Support only skinny Svd with m>=n (NumRows>=NumCols), and zero sizes for U and Vt mean
1784  // we do not want that output. We expect that s.Dim() == m,
1785  // U is either 0 by 0 or m by n, and rv is either 0 by 0 or n by n.
1786  // Throws exception on error.
1787 
1788  KALDI_ASSERT(num_rows_>=num_cols_ && "Svd requires that #rows by >= #cols."); // For compatibility with JAMA code.
1789  KALDI_ASSERT(s->Dim() == num_cols_); // s should be the smaller dim.
1790  KALDI_ASSERT(U == NULL || (U->num_rows_ == num_rows_&&U->num_cols_ == num_cols_));
1791  KALDI_ASSERT(Vt == NULL || (Vt->num_rows_ == num_cols_&&Vt->num_cols_ == num_cols_));
1792 
1793  Real prescale = 1.0;
1794  if ( std::abs((*this)(0, 0) ) < 1.0e-30) { // Very tiny value... can cause problems in Svd.
1795  Real max_elem = LargestAbsElem();
1796  if (max_elem != 0) {
1797  prescale = 1.0 / max_elem;
1798  if (std::abs(prescale) == std::numeric_limits<Real>::infinity()) { prescale = 1.0e+40; }
1799  (*this).Scale(prescale);
1800  }
1801  }
1802 
1803 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD)
1804  // "S" == skinny Svd (only one we support because of compatibility with Jama one which is only skinny),
1805  // "N"== no eigenvectors wanted.
1806  LapackGesvd(s, U, Vt);
1807 #else
1808  /* if (num_rows_ > 1 && num_cols_ > 1 && (*this)(0, 0) == (*this)(1, 1)
1809  && Max() == Min() && (*this)(0, 0) != 0.0) { // special case that JamaSvd sometimes crashes on.
1810  KALDI_WARN << "Jama SVD crashes on this type of matrix, perturbing it to prevent crash.";
1811  for(int32 i = 0; i < NumRows(); i++)
1812  (*this)(i, i) *= 1.00001;
1813  }*/
1814  bool ans = JamaSvd(s, U, Vt);
1815  if (Vt != NULL) Vt->Transpose(); // possibly to do: change this and also the transpose inside the JamaSvd routine. note, Vt is square.
1816  if (!ans) {
1817  KALDI_ERR << "Error doing Svd"; // This one will be caught.
1818  }
1819 #endif
1820  if (prescale != 1.0) s->Scale(1.0/prescale);
1821 }
void LapackGesvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
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:766
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DiffSigmoid ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

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

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

Referenced by kaldi::UnitTestSigmoid().

2949  {
2950  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
2951  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2952  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
2953  Real *data = data_;
2954  const Real *value_data = value.data_, *diff_data = diff.data_;
2955  for (MatrixIndexT r = 0; r < num_rows; r++) {
2956  for (MatrixIndexT c = 0; c < num_cols; c++)
2957  data[c] = diff_data[c] * value_data[c] * (1.0 - value_data[c]);
2958  data += stride;
2959  value_data += value_stride;
2960  diff_data += diff_stride;
2961  }
2962 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DiffTanh ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

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

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

Referenced by kaldi::UnitTestTanh().

2966  {
2967  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
2968  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2969  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
2970  Real *data = data_;
2971  const Real *value_data = value.data_, *diff_data = diff.data_;
2972  for (MatrixIndexT r = 0; r < num_rows; r++) {
2973  for (MatrixIndexT c = 0; c < num_cols; c++)
2974  data[c] = diff_data[c] * (1.0 - (value_data[c] * value_data[c]));
2975  data += stride;
2976  value_data += value_stride;
2977  diff_data += diff_stride;
2978  }
2979 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 1156 of file kaldi-matrix.cc.

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

Referenced by kaldi::UnitTestCuMatrixDivElements().

1156  {
1157  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
1158  MatrixIndexT i;
1159  MatrixIndexT j;
1160 
1161  for (i = 0; i < num_rows_; i++) {
1162  for (j = 0; j < num_cols_; j++) {
1163  (*this)(i, j) /= a(i, j);
1164  }
1165  }
1166 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 2234 of file kaldi-matrix.cc.

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

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

2236  {
2237  EigenvalueDecomposition<Real> eig(*this);
2238  if (P) eig.GetV(P);
2239  if (r) eig.GetRealEigenvalues(r);
2240  if (i) eig.GetImagEigenvalues(i);
2241 }
bool Equal ( const MatrixBase< Real > &  other) const

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

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

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

1924  {
1925  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1926  KALDI_ERR << "Equal: size mismatch.";
1927  for (MatrixIndexT i = 0; i < num_rows_; i++)
1928  for (MatrixIndexT j = 0; j < num_cols_; j++)
1929  if ( (*this)(i, j) != other(i, j))
1930  return false;
1931  return true;
1932 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
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 1909 of file kaldi-matrix.cc.

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

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

1909  {
1910  return std::sqrt(TraceMatMat(*this, *this, kTrans));
1911 }
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans=kNoTrans)
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 2760 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().

2760  {
2761  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2762  src.NumRows() == this->NumRows());
2763  int group_size = src.NumCols() / this->NumCols(),
2764  num_rows = this->NumRows(), num_cols = this->NumCols();
2765  for (MatrixIndexT i = 0; i < num_rows; i++) {
2766  const Real *src_row_data = src.RowData(i);
2767  for (MatrixIndexT j = 0; j < num_cols; j++) {
2768  Real max_val = -1e20;
2769  for (MatrixIndexT k = 0; k < group_size; k++) {
2770  Real src_data = src_row_data[j * group_size + k];
2771  if (src_data > max_val)
2772  max_val = src_data;
2773  }
2774  (*this)(i, j) = max_val;
2775  }
2776  }
2777 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 1298 of file kaldi-matrix.cc.

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

Referenced by kaldi::UnitTestCuMatrixGroupMaxDeriv().

1299  {
1300  KALDI_ASSERT(input.NumCols() == this->NumCols() &&
1301  input.NumRows() == this->NumRows());
1302  KALDI_ASSERT(this->NumCols() % output.NumCols() == 0 &&
1303  this->NumRows() == output.NumRows());
1304 
1305  int group_size = this->NumCols() / output.NumCols(),
1306  num_rows = this->NumRows(), num_cols = this->NumCols();
1307 
1308  for (MatrixIndexT i = 0; i < num_rows; i++) {
1309  for (MatrixIndexT j = 0; j < num_cols; j++) {
1310  Real input_val = input(i, j);
1311  Real output_val = output(i, j / group_size);
1312  (*this)(i, j) = (input_val == output_val ? 1 : 0);
1313  }
1314  }
1315 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 2749 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().

2749  {
2750  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2751  src.NumRows() == this->NumRows());
2752  int group_size = src.NumCols() / this->NumCols(),
2753  num_rows = this->NumRows(), num_cols = this->NumCols();
2754  for (MatrixIndexT i = 0; i < num_rows; i++)
2755  for (MatrixIndexT j = 0; j < num_cols; j++)
2756  (*this)(i, j) = src.Row(i).Range(j * group_size, group_size).Norm(power);
2757 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 1254 of file kaldi-matrix.cc.

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

Referenced by kaldi::UnitTestCuMatrixDiffGroupPnorm().

1256  {
1257  KALDI_ASSERT(input.NumCols() == this->NumCols() && input.NumRows() == this->NumRows());
1258  KALDI_ASSERT(this->NumCols() % output.NumCols() == 0 &&
1259  this->NumRows() == output.NumRows());
1260 
1261  int group_size = this->NumCols() / output.NumCols(),
1262  num_rows = this->NumRows(), num_cols = this->NumCols();
1263 
1264  if (power == 1.0) {
1265  for (MatrixIndexT i = 0; i < num_rows; i++) {
1266  for (MatrixIndexT j = 0; j < num_cols; j++) {
1267  Real input_val = input(i, j);
1268  (*this)(i, j) = (input_val == 0 ? 0 : (input_val > 0 ? 1 : -1));
1269  }
1270  }
1271  } else if (power == std::numeric_limits<Real>::infinity()) {
1272  for (MatrixIndexT i = 0; i < num_rows; i++) {
1273  for (MatrixIndexT j = 0; j < num_cols; j++) {
1274  Real output_val = output(i, j / group_size), input_val = input(i, j);
1275  if (output_val == 0)
1276  (*this)(i, j) = 0;
1277  else
1278  (*this)(i, j) = (std::abs(input_val) == output_val ? 1.0 : 0.0)
1279  * (input_val >= 0 ? 1 : -1);
1280  }
1281  }
1282  } else {
1283  for (MatrixIndexT i = 0; i < num_rows; i++) {
1284  for (MatrixIndexT j = 0; j < num_cols; j++) {
1285  Real output_val = output(i, j / group_size),
1286  input_val = input(i, j);
1287  if (output_val == 0)
1288  (*this)(i, j) = 0;
1289  else
1290  (*this)(i, j) = pow(std::abs(input_val), power - 1) *
1291  pow(output_val, 1 - power) * (input_val >= 0 ? 1 : -1) ;
1292  }
1293  }
1294  }
1295 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 2172 of file kaldi-matrix.cc.

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

2172  {
2173  KALDI_ASSERT(SameDim(*this, src));
2174  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2175  Real *row_data = data_;
2176  const Real *src_row_data = src.Data();
2177  for (MatrixIndexT row = 0; row < num_rows;
2178  row++,row_data += stride_, src_row_data += src.stride_) {
2179  for (MatrixIndexT col = 0; col < num_cols; col++)
2180  row_data[col] = (src_row_data[col] > 0 ? 1.0 : 0.0);
2181  }
2182 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 108 of file kaldi-matrix.h.

108 { return (*this)(r, c); }
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 2045 of file kaldi-matrix.cc.

References MatrixBase< Real >::Invert().

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

2046  {
2047  double log_det_tmp, det_sign_tmp;
2048  Matrix<double> dmat(*this);
2049  dmat.Invert(&log_det_tmp, &det_sign_tmp, inverse_needed);
2050  if (inverse_needed) (*this).CopyFromMat(dmat);
2051  if (log_det) *log_det = log_det_tmp;
2052  if (det_sign) *det_sign = det_sign_tmp;
2053 }
void InvertElements ( )

Inverts all the elements of the matrix.

Definition at line 2069 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().

2069  {
2070  for (MatrixIndexT r = 0; r < num_rows_; r++) {
2071  for (MatrixIndexT c = 0; c < num_cols_; c++) {
2072  (*this)(r, c) = static_cast<Real>(1.0 / (*this)(r, c));
2073  }
2074  }
2075 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
bool IsDiagonal ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Diagonal.

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

References rnnlm::j.

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

1863  {
1864  MatrixIndexT R = num_rows_, C = num_cols_;
1865  Real bad_sum = 0.0, good_sum = 0.0;
1866  for (MatrixIndexT i = 0;i < R;i++) {
1867  for (MatrixIndexT j = 0;j < C;j++) {
1868  if (i == j) good_sum += std::abs((*this)(i, j));
1869  else bad_sum += std::abs((*this)(i, j));
1870  }
1871  }
1872  return (!(bad_sum > good_sum * cutoff));
1873 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
bool IsSymmetric ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Symmetric.

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

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

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

1847  {
1848  MatrixIndexT R = num_rows_, C = num_cols_;
1849  if (R != C) return false;
1850  Real bad_sum = 0.0, good_sum = 0.0;
1851  for (MatrixIndexT i = 0;i < R;i++) {
1852  for (MatrixIndexT j = 0;j < i;j++) {
1853  Real a = (*this)(i, j), b = (*this)(j, i), avg = 0.5*(a+b), diff = 0.5*(a-b);
1854  good_sum += std::abs(avg); bad_sum += std::abs(diff);
1855  }
1856  good_sum += std::abs((*this)(i, i));
1857  }
1858  if (bad_sum > cutoff*good_sum) return false;
1859  return true;
1860 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 1889 of file kaldi-matrix.cc.

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

1889  {
1890  MatrixIndexT R = num_rows_, C = num_cols_;
1891  Real bad_max = 0.0;
1892  for (MatrixIndexT i = 0; i < R;i++)
1893  for (MatrixIndexT j = 0; j < C;j++)
1894  bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*this)(i, j) - (i == j?1.0:0.0))));
1895  return (bad_max <= cutoff);
1896 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
bool IsZero ( Real  cutoff = 1.0e-05) const

Returns true if matrix is all zeros.

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

References rnnlm::j.

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

1899  {
1900  MatrixIndexT R = num_rows_, C = num_cols_;
1901  Real bad_max = 0.0;
1902  for (MatrixIndexT i = 0;i < R;i++)
1903  for (MatrixIndexT j = 0;j < C;j++)
1904  bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*this)(i, j) )));
1905  return (bad_max <= cutoff);
1906 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 659 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().

660  {
661  KALDI_ASSERT(s != NULL && U_in != this && V_in != this);
662 
663  Matrix<Real> tmpU, tmpV;
664  if (U_in == NULL) tmpU.Resize(this->num_rows_, 1); // work-space if U_in empty.
665  if (V_in == NULL) tmpV.Resize(1, this->num_cols_); // work-space if V_in empty.
666 
670 
671  KaldiBlasInt M = num_cols_;
672  KaldiBlasInt N = num_rows_;
673  KaldiBlasInt LDA = Stride();
674 
675  KALDI_ASSERT(N>=M); // NumRows >= columns.
676 
677  if (U_in) {
678  KALDI_ASSERT((int)U_in->num_rows_ == N && (int)U_in->num_cols_ == M);
679  }
680  if (V_in) {
681  KALDI_ASSERT((int)V_in->num_rows_ == M && (int)V_in->num_cols_ == M);
682  }
683  KALDI_ASSERT((int)s->Dim() == std::min(M, N));
684 
685  MatrixBase<Real> *U = (U_in ? U_in : &tmpU);
686  MatrixBase<Real> *V = (V_in ? V_in : &tmpV);
687 
688  KaldiBlasInt V_stride = V->Stride();
689  KaldiBlasInt U_stride = U->Stride();
690 
691  // Original LAPACK recipe
692  // KaldiBlasInt l_work = std::max(std::max<long int>
693  // (1, 3*std::min(M, N)+std::max(M, N)), 5*std::min(M, N))*2;
694  KaldiBlasInt l_work = -1;
695  Real work_query;
696  KaldiBlasInt result;
697 
698  // query for work space
699  char *u_job = const_cast<char*>(U_in ? "s" : "N"); // "s" == skinny, "N" == "none."
700  char *v_job = const_cast<char*>(V_in ? "s" : "N"); // "s" == skinny, "N" == "none."
701  clapack_Xgesvd(v_job, u_job,
702  &M, &N, data_, &LDA,
703  s->Data(),
704  V->Data(), &V_stride,
705  U->Data(), &U_stride,
706  &work_query, &l_work,
707  &result);
708 
709  KALDI_ASSERT(result >= 0 && "Call to CLAPACK dgesvd_ called with wrong arguments");
710 
711  l_work = static_cast<KaldiBlasInt>(work_query);
712  Real *p_work;
713  void *temp;
714  if ((p_work = static_cast<Real*>(
715  KALDI_MEMALIGN(16, sizeof(Real)*l_work, &temp))) == NULL)
716  throw std::bad_alloc();
717 
718  // perform svd
719  clapack_Xgesvd(v_job, u_job,
720  &M, &N, data_, &LDA,
721  s->Data(),
722  V->Data(), &V_stride,
723  U->Data(), &U_stride,
724  p_work, &l_work,
725  &result);
726 
727  KALDI_ASSERT(result >= 0 && "Call to CLAPACK dgesvd_ called with wrong arguments");
728 
729  if (result != 0) {
730  KALDI_WARN << "CLAPACK sgesvd_ : some weird convergence not satisfied";
731  }
732  KALDI_MEMALIGN_FREE(p_work);
733 }
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:767
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:67
#define KALDI_MEMALIGN(align, size, pp_orig)
Definition: kaldi-utils.h:58
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:60
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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:49
Real LargestAbsElem ( ) const

largest absolute value.

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

References rnnlm::j.

1936  {
1937  MatrixIndexT R = num_rows_, C = num_cols_;
1938  Real largest = 0.0;
1939  for (MatrixIndexT i = 0;i < R;i++)
1940  for (MatrixIndexT j = 0;j < C;j++)
1941  largest = std::max(largest, (Real)std::abs((*this)(i, j)));
1942  return largest;
1943 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
Real LogDet ( Real *  det_sign = NULL) const

Returns logdet of matrix.

Definition at line 2037 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(), kaldi::UnitTestDeterminant(), kaldi::UnitTestDeterminantSign(), kaldi::UnitTestEig(), UnitTestEstimateFullGmm(), kaldi::UnitTestSimpleForMat(), and FmllrSgmm2Accs::Update().

2037  {
2038  Real log_det;
2039  Matrix<Real> tmp(*this);
2040  tmp.Invert(&log_det, det_sign, false); // false== output not needed (saves some computation).
2041  return log_det;
2042 }
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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 2682 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().

2682  {
2683  Real sum;
2684  if (sizeof(sum) == 8) sum = kLogZeroDouble;
2685  else sum = kLogZeroFloat;
2686  Real max_elem = Max(), cutoff;
2687  if (sizeof(Real) == 4) cutoff = max_elem + kMinLogDiffFloat;
2688  else cutoff = max_elem + kMinLogDiffDouble;
2689  if (prune > 0.0 && max_elem - prune > cutoff) // explicit pruning...
2690  cutoff = max_elem - prune;
2691 
2692  double sum_relto_max_elem = 0.0;
2693 
2694  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2695  for (MatrixIndexT j = 0; j < num_cols_; j++) {
2696  BaseFloat f = (*this)(i, j);
2697  if (f >= cutoff)
2698  sum_relto_max_elem += Exp(f - max_elem);
2699  }
2700  }
2701  return max_elem + Log(sum_relto_max_elem);
2702 }
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:98
double Log(double x)
Definition: kaldi-math.h:100
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
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:766
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
void Max ( const MatrixBase< Real > &  A)

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

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

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

1181  {
1182  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
1183  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1184  Real *row_data = RowData(row);
1185  const Real *other_row_data = A.RowData(row);
1186  MatrixIndexT num_cols = num_cols_;
1187  for (MatrixIndexT col = 0; col < num_cols; col++) {
1188  row_data[col] = std::max(row_data[col],
1189  other_row_data[col]);
1190  }
1191  }
1192 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 1194 of file kaldi-matrix.cc.

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

1194  {
1195  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
1196  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1197  Real *row_data = RowData(row);
1198  const Real *other_row_data = A.RowData(row);
1199  MatrixIndexT num_cols = num_cols_;
1200  for (MatrixIndexT col = 0; col < num_cols; col++) {
1201  row_data[col] = std::min(row_data[col],
1202  other_row_data[col]);
1203  }
1204  }
1205 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
KALDI_ASSERT & A
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real MinSingularValue ( ) const
inline

Returns smallest singular value.

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

422  {
423  Vector<Real> tmp(std::min(NumRows(), NumCols()));
424  Svd(&tmp);
425  return tmp.Min();
426  }
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:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
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 1318 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(), SvdApplier::DecomposeComponent(), SpMatrix< Real >::LimitCond(), main(), MleAmSgmm2Updater::RenormalizeN(), kaldi::SolveQuadraticMatrixProblem(), MatrixBase< Real >::SymPosSemiDefEig(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestCuMatrixMulColsVec(), kaldi::UnitTestEigSymmetric(), and kaldi::UnitTestScale().

1318  {
1319  KALDI_ASSERT(scale.Dim() == num_cols_);
1320  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1321  for (MatrixIndexT j = 0; j < num_cols_; j++) {
1322  Real this_scale = scale(j);
1323  (*this)(i, j) *= this_scale;
1324  }
1325  }
1326 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 1139 of file kaldi-matrix.cc.

References data_, MatrixBase< Real >::data_, 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::UnitTestCuMatrixApplyLog(), kaldi::UnitTestCuMatrixApplyPow(), kaldi::UnitTestCuMatrixMulElements(), kaldi::UnitTestCuMatrixSigmoid(), kaldi::UnitTestCuMatrixSoftHinge(), kaldi::UnitTestMulElements(), and kaldi::UnitTestSimpleForMat().

1139  {
1140  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
1141 
1142  if (num_cols_ == stride_ && num_cols_ == a.stride_) {
1143  mul_elements(num_rows_ * num_cols_, a.data_, data_);
1144  } else {
1145  MatrixIndexT a_stride = a.stride_, stride = stride_;
1146  Real *data = data_, *a_data = a.data_;
1147  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1148  mul_elements(num_cols_, a_data, data);
1149  a_data += a_stride;
1150  data += stride;
1151  }
1152  }
1153 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
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:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 1237 of file kaldi-matrix.cc.

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

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

1237  {
1238  KALDI_ASSERT(src.NumRows() == this->NumRows() &&
1239  this->NumCols() % src.NumCols() == 0);
1240  int32 group_size = this->NumCols() / src.NumCols(),
1241  num_groups = this->NumCols() / group_size,
1242  num_rows = this->NumRows();
1243 
1244  for (MatrixIndexT i = 0; i < num_rows; i++) {
1245  Real *data = this->RowData(i);
1246  for (MatrixIndexT j = 0; j < num_groups; j++, data += group_size) {
1247  Real scale = src(i, j);
1248  cblas_Xscal(group_size, scale, data, 1);
1249  }
1250  }
1251 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:84
int32 MatrixIndexT
Definition: matrix-common.h:98
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:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 1223 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().

1223  {
1224  KALDI_ASSERT(scale.Dim() == num_rows_);
1225  MatrixIndexT M = num_rows_, N = num_cols_;
1226 
1227  for (MatrixIndexT i = 0; i < M; i++) {
1228  Real this_scale = scale(i);
1229  for (MatrixIndexT j = 0; j < N; j++) {
1230  (*this)(i, j) *= this_scale;
1231  }
1232  }
1233 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
#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 64 of file kaldi-matrix.h.

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), DecodableMatrixMappedOffset::AcceptLoglikes(), 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(), MatrixBase< Real >::AddMatSmat(), VectorBase< Real >::AddMatSvec(), VectorBase< Real >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< Real >::AddRows(), VectorBase< Real >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< Real >::AddSmat2Sp(), MatrixBase< Real >::AddSmatMat(), 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(), kaldi::ApplyPca(), Sgmm2Project::ApplyProjection(), Plda::ApplyTransform(), kaldi::AssertSameDim(), kaldi::nnet2::AverageConstPart(), MleAmSgmm2Accs::Check(), AmSgmm2::Check(), IvectorExtractorStats::CheckDims(), kaldi::ComposeTransforms(), 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(), 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(), CuMatrixBase< Real >::CopyFromMat(), MatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyRowFromMat(), MatrixBase< Real >::CopyRows(), CuVectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::CopyRowsFromMat(), DiagGmmNormal::CopyToDiagGmm(), FullGmmNormal::CopyToFullGmm(), CompressedMatrix::CopyToMat(), SparseMatrix< Real >::CopyToMat(), CuMatrixBase< Real >::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), 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::EstPca(), 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(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), EigenvalueDecomposition< Real >::GetV(), 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(), DecodableMatrixScaled::LogLikelihood(), 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(), kaldi::NonOrthogonality(), GeneralMatrix::NumCols(), DecodableMatrixScaled::NumIndices(), GaussClusterable::Objf(), OnlineTransform::OnlineTransform(), CuMatrix< BaseFloat >::operator=(), Matrix< BaseFloat >::operator=(), DiscriminativeExampleSplitter::OutputOneSplit(), kaldi::nnet3::PerturbImage(), AmSgmm2::PhoneSpaceDim(), kaldi::PlaceNansInGaps(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::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::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::cu::Splice(), kaldi::SpliceFrames(), SubVector< Real >::SubVector(), kaldi::TestCuFindRowMaxId(), kaldi::TestFmpe(), kaldi::TestIvectorExtraction(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineTransform(), SpMatrix< Real >::TopEigs(), 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(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), Fmpe::Update(), kaldi::nnet2::UpdateHash(), IvectorExtractorStats::UpdatePrior(), UpdateWClass::UpdateWClass(), kaldi::VecMatVec(), AccumDiagGmm::Write(), WaveData::Write(), SphinxMatrixHolder< kFeatDim >::Write(), and kaldi::nnet3::WriteExamples().

64 { 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:766
MatrixIndexT NumRows ( ) const
inline

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

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

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), kaldi::AccCmvnStatsWrapper(), DecodableMatrixMappedOffset::AcceptLoglikes(), CovarianceStats::AccStats(), IvectorExtractorUtteranceStats::AccStats(), Fmpe::AccStats(), IvectorExtractorStats::AccStatsForUtterance(), KlHmm::Accumulate(), FmpeStats::AccumulateChecks(), kaldi::AccumulateForUtterance(), AccumulateMultiThreadedClass::AccumulateMultiThreadedClass(), kaldi::AccumulateTreeStats(), MatrixBase< Real >::AddCols(), VectorBase< Real >::AddColSumMat(), MatrixRandomizer::AddData(), 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(), MatrixBase< Real >::AddMatSmat(), VectorBase< Real >::AddMatSvec(), VectorBase< Real >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< Real >::AddRows(), VectorBase< Real >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< Real >::AddSmat2Sp(), MatrixBase< Real >::AddSmatMat(), CuMatrixBase< Real >::AddToElements(), SparseMatrix< Real >::AddToMat(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), AgglomerativeClusterer::AgglomerativeClusterer(), 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::ApplyPca(), kaldi::ApplyPreXformToGradient(), Sgmm2Project::ApplyProjection(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), kaldi::ApplySoftMaxPerRow(), CuMatrixBase< Real >::ApplySoftMaxPerRow(), Plda::ApplyTransform(), kaldi::AssertSameDim(), kaldi::CalBasisFmllrStepSize(), MleAmSgmm2Accs::Check(), DiscriminativeNnetExample::Check(), AmSgmm2::Check(), IvectorExtractorStats::CheckDims(), IvectorExtractorStats::CommitStatsForM(), kaldi::ComposeTransforms(), GeneralMatrix::Compress(), 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(), 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(), CuMatrixBase< Real >::CopyFromMat(), MatrixBase< Real >::CopyFromMat(), VectorBase< Real >::CopyRowFromMat(), CuVectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::CopyRowsFromMat(), CompressedAffineXformStats::CopyToAffineXformStats(), CompressedMatrix::CopyToMat(), SparseMatrix< Real >::CopyToMat(), CuMatrixBase< Real >::CopyToMat(), GeneralMatrix::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), 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::EstPca(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), kaldi::FakeStatsForSomeDims(), kaldi::FilterMatrixRows(), kaldi::FmllrInnerUpdate(), FmllrRawAccs::FmllrRawAccs(), FmllrRawAccs::FullDim(), DiagGmm::GaussianSelection(), GeneralMatrix::GetCompressedMatrix(), DecodableNnetSimple::GetCurrentIvector(), DecodableNnetSimpleLooped::GetCurrentIvector(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), OnlineCmvn::GetFrame(), kaldi::GetGmmLike(), NnetDiscriminativeUpdater::GetInputFeatures(), LogisticRegression::GetLogPosteriors(), GeneralMatrix::GetMatrix(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), DecodableNnetSimple::GetOutput(), DecodableNnetSimpleLooped::GetOutput(), DecodableNnetSimple::GetOutputForFrame(), DecodableNnetSimpleLooped::GetOutputForFrame(), GeneralMatrix::GetSparseMatrix(), OnlineCmvn::GetState(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), EigenvalueDecomposition< Real >::GetV(), MatrixBase< Real >::GroupMax(), MatrixBase< Real >::GroupMaxDeriv(), MatrixBase< Real >::GroupPnorm(), MatrixBase< Real >::GroupPnormDeriv(), AmSgmm2::HasSpeakerDependentWeights(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), kaldi::IncreaseTransformDimension(), OnlineNaturalGradientSimple::Init(), OnlinePreconditionerSimple::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(), DecodableMatrixScaled::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=(), kaldi::nnet3::PerturbImage(), kaldi::PlaceNansInGaps(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), 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::cu::RegularizeL1(), ArbitraryResample::Resample(), FullGmmNormal::Resize(), FullGmm::Resize(), DiagGmmNormal::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), kaldi::ReverseFrames(), DiscriminativeExampleSplitter::RightContext(), kaldi::nnet3::RunNnetComputation(), 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::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::TraceMatSmat(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), LogisticRegression::Train(), kaldi::TrainOneIter(), LogisticRegression::TrainParameters(), kaldi::TransformIvectors(), 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(), FmllrRawAccs::Update(), FmllrSgmm2Accs::Update(), Fmpe::Update(), kaldi::nnet2::UpdateHash(), IvectorExtractorStats::UpdatePrior(), UpdateWClass::UpdateWClass(), kaldi::VecMatVec(), AccumDiagGmm::Write(), WaveData::Write(), MleAmSgmm2Accs::Write(), SphinxMatrixHolder< kFeatDim >::Write(), and kaldi::nnet3::WriteExamples().

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

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

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

99  {
100  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
101  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
102  static_cast<UnsignedMatrixIndexT>(c) <
103  static_cast<UnsignedMatrixIndexT>(num_cols_));
104  return *(data_ + r * stride_ + c);
105  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
const Real operator() ( MatrixIndexT  r,
MatrixIndexT  c 
) const
inline

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

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

112  {
113  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
114  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
115  static_cast<UnsignedMatrixIndexT>(c) <
116  static_cast<UnsignedMatrixIndexT>(num_cols_));
117  return *(data_ + r * stride_ + c);
118  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:766
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 1947 of file kaldi-matrix.cc.

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

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

1947  {
1948  KALDI_ASSERT(NumRows() <= NumCols());
1949  MatrixIndexT num_rows = num_rows_;
1950  for (MatrixIndexT i = 0; i < num_rows; i++) {
1951  int32 counter = 0;
1952  while (1) {
1953  Real start_prod = VecVec(this->Row(i), this->Row(i));
1954  if (start_prod - start_prod != 0.0 || start_prod == 0.0) {
1955  KALDI_WARN << "Self-product of row " << i << " of matrix is "
1956  << start_prod << ", randomizing.";
1957  this->Row(i).SetRandn();
1958  counter++;
1959  continue; // loop again.
1960  }
1961  for (MatrixIndexT j = 0; j < i; j++) {
1962  Real prod = VecVec(this->Row(i), this->Row(j));
1963  this->Row(i).AddVec(-prod, this->Row(j));
1964  }
1965  Real end_prod = VecVec(this->Row(i), this->Row(i));
1966  if (end_prod <= 0.01 * start_prod) { // We removed
1967  // almost all of the vector during orthogonalization,
1968  // so we have reason to doubt (for roundoff reasons)
1969  // that it's still orthogonal to the other vectors.
1970  // We need to orthogonalize again.
1971  if (end_prod == 0.0) { // Row is exactly zero:
1972  // generate random direction.
1973  this->Row(i).SetRandn();
1974  }
1975  counter++;
1976  if (counter > 100)
1977  KALDI_ERR << "Loop detected while orthogalizing matrix.";
1978  } else {
1979  this->Row(i).Scale(1.0 / std::sqrt(end_prod));
1980  break;
1981  }
1982  }
1983  }
1984 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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:37
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 2186 of file kaldi-matrix.cc.

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

Referenced by kaldi::UnitTestNonsymmetricPower().

2186  {
2189  Matrix<Real> P(n, n);
2190  Vector<Real> re(n), im(n);
2191  this->Eig(&P, &re, &im);
2192  // Now attempt to take the complex eigenvalues to this power.
2193  for (MatrixIndexT i = 0; i < n; i++)
2194  if (!AttemptComplexPower(&(re(i)), &(im(i)), power))
2195  return false; // e.g. real and negative, or zero, eigenvalues.
2196 
2197  Matrix<Real> D(n, n); // D to the power.
2198  CreateEigenvalueMatrix(re, im, &D);
2199 
2200  Matrix<Real> tmp(n, n); // P times D
2201  tmp.AddMatMat(1.0, P, kNoTrans, D, kNoTrans, 0.0); // tmp := P*D
2202  P.Invert();
2203  // next line is: *this = tmp * P^{-1} = P * D * P^{-1}
2204  (*this).AddMatMat(1.0, tmp, kNoTrans, P, kNoTrans, 0.0);
2205  return true;
2206 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
struct rnnlm::@11::@12 n
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 ...
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void CreateEigenvalueMatrix(const VectorBase< Real > &real, const VectorBase< Real > &imag, MatrixBase< Real > *D)
Creates the eigenvalue matrix D that is part of the decomposition used Matrix::Eig.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
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}.
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 199 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().

202  {
203  return SubMatrix<Real>(*this, row_offset, num_rows,
204  col_offset, num_cols);
205  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:540
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 1422 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>>().

1422  {
1423  if (add) {
1425  tmp.Read(is, binary, false); // read without adding.
1426  if (tmp.num_rows_ != this->num_rows_ || tmp.num_cols_ != this->num_cols_)
1427  KALDI_ERR << "MatrixBase::Read, size mismatch "
1428  << this->num_rows_ << ", " << this->num_cols_
1429  << " vs. " << tmp.num_rows_ << ", " << tmp.num_cols_;
1430  this->AddMat(1.0, tmp);
1431  return;
1432  }
1433  // now assume add == false.
1434 
1435  // In order to avoid rewriting this, we just declare a Matrix and
1436  // use it to read the data, then copy.
1437  Matrix<Real> tmp;
1438  tmp.Read(is, binary, false);
1439  if (tmp.NumRows() != NumRows() || tmp.NumCols() != NumCols()) {
1440  KALDI_ERR << "MatrixBase<Real>::Read, size mismatch "
1441  << NumRows() << " x " << NumCols() << " versus "
1442  << tmp.NumRows() << " x " << tmp.NumCols();
1443  }
1444  CopyFromMat(tmp);
1445 }
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:767
#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:766
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
friend class Matrix< Real >
Definition: kaldi-matrix.h:49
const SubVector<Real> Row ( MatrixIndexT  i) const
inline

Return specific row of matrix [const].

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

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), 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(), kaldi::ApplySoftMaxPerRow(), 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(), 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(), 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::TransformIvectors(), 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().

185  {
186  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
187  static_cast<UnsignedMatrixIndexT>(num_rows_));
188  return SubVector<Real>(data_ + (i * stride_), NumCols());
189  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
SubVector<Real> Row ( MatrixIndexT  i)
inline

Return specific row of matrix.

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

192  {
193  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
194  static_cast<UnsignedMatrixIndexT>(num_rows_));
195  return SubVector<Real>(data_ + (i * stride_), NumCols());
196  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:64
#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 91 of file kaldi-matrix.h.

91  {
92  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
93  static_cast<UnsignedMatrixIndexT>(num_rows_));
94  return data_ + i * stride_;
95  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
#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 206 of file kaldi-matrix.h.

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

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

Multiply each element with a scalar value.

Definition at line 1208 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(), 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::TypeOneUsage(), kaldi::TypeThreeUsage(), 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::UnitTestNorm(), kaldi::UnitTestScale(), FmllrSgmm2Accs::Update(), and LstmNonlinearityComponent::Write().

1208  {
1209  if (alpha == 1.0) return;
1210  if (num_rows_ == 0) return;
1211  if (num_cols_ == stride_) {
1212  cblas_Xscal(static_cast<size_t>(num_rows_) * static_cast<size_t>(num_cols_),
1213  alpha, data_,1);
1214  } else {
1215  Real *data = data_;
1216  for (MatrixIndexT i = 0; i < num_rows_; ++i, data += stride_) {
1217  cblas_Xscal(num_cols_, alpha, data,1);
1218  }
1219  }
1220 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:770
Real * data_
data memory area
Definition: kaldi-matrix.h:762
int32 MatrixIndexT
Definition: matrix-common.h:98
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
void Set ( Real  value)

Sets all elements to a specific value.

Definition at line 1338 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().

1338  {
1339  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1340  for (MatrixIndexT col = 0; col < num_cols_; col++) {
1341  (*this)(row, col) = value;
1342  }
1343  }
1344 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:767
MatrixIndexT num_cols_
these atributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:766
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 188 of file kaldi-matrix.cc.

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

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

Sets to random values of a normal distribution.

Definition at line 1354 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::UnitTestApplyExpSpecial(), 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(), kaldi::UnitTestCuMatrixApplyExpLimited(), kaldi::UnitTestCuMatrixApplyExpSpecial(), kaldi::UnitTestCuMatrixApplyFloor(), kaldi::UnitTestCuMatrixApplyHeaviside(), kaldi::UnitTestCuMatrixApplyLog(), kaldi::UnitTestCuMatrixApplyPow(), kaldi::UnitTestCuMatrixApplyPowAbs(), kaldi::UnitTestCuMatrixCopyFromMat(), kaldi::UnitTestCuMatrixDiffGroupPnorm(), kaldi::UnitTestCuMatrixDivElements(), kaldi::UnitTestCuMatrixDivRowsVec(), kaldi::UnitTestCuMatrixGroupMax(), kaldi::UnitTestCuMatrixGroupMaxDeriv(), kaldi::UnitTestCuMatrixGroupPnorm(), kaldi::UnitTestCuMatrixHeaviside(), kaldi::UnitTestCuMatrixMax(), kaldi::UnitTestCuMatrixMin(), kaldi::UnitTestCuMatrixMulColsVec(), kaldi::UnitTestCuMatrixMulElements(), kaldi::UnitTestCuMatrixMulRowsGroupMat(), kaldi::UnitTestCuMatrixMulRowsVec(), kaldi::UnitTestCuMatrixScale(), kaldi::UnitTestCuMatrixSetRandn(), kaldi::UnitTestCuMatrixSigmoid(), kaldi::UnitTestCuMatrixSoftHinge(), kaldi::UnitTestCuMatrixSumColumnRanges(), kaldi::UnitTestCuSigmoid(), kaldi::UnitTestCuSoftmax(), kaldi::UnitTestCuSpMatrixAddMat2(), kaldi::UnitTestCuSpMatrixConstructor(), kaldi::UnitTestCuTanh(), kaldi::UnitTestCuTpMatrixCholesky(), kaldi::UnitTestCuVectorAddColSumMat(),