MatrixBase< Real > Class Template 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 empty matrix). More...
 
MatrixIndexT NumCols () const
 Returns number of columns (or zero for empty 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 ApplyPow (Real power)
 
void ApplyPowAbs (Real power, bool include_sign=false)
 
void ApplyHeaviside ()
 
void ApplyFloor (Real floor_val)
 
void ApplyCeiling (Real ceiling_val)
 
void ApplyExp ()
 
void ApplyExpSpecial ()
 
void ApplyExpLimited (Real lower_limit, Real upper_limit)
 
void ApplyLog ()
 
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 Exp (const MatrixBase< Real > &src)
 
void Pow (const MatrixBase< Real > &src, Real power)
 
void Log (const MatrixBase< Real > &src)
 
void PowAbs (const MatrixBase< Real > &src, Real power, bool include_sign=false)
 Apply power to the absolute value of each element. More...
 
void Floor (const MatrixBase< Real > &src, Real floor_val)
 
void Ceiling (const MatrixBase< Real > &src, Real ceiling_val)
 
void ExpSpecial (const MatrixBase< Real > &src)
 For each element x of the matrix, set it to (x < 0 ? exp(x) : x + 1). More...
 
void ExpLimited (const MatrixBase< Real > &src, Real lower_limit, Real upper_limit)
 This is equivalent to running: Floor(src, lower_limit); Ceiling(src, upper_limit); Exp(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<>
void AddVecVec (const float alpha, const VectorBase< float > &a, const VectorBase< float > &rb)
 
template<>
void AddVecVec (const double alpha, const VectorBase< double > &a, const VectorBase< double > &rb)
 
template<>
void CopyFromSp (const SpMatrix< float > &M)
 
template<>
void CopyFromSp (const SpMatrix< double > &M)
 

Protected Member Functions

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

Protected Attributes

Real * data_
 data memory area More...
 
MatrixIndexT num_cols_
 these attributes 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>
class 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 49 of file kaldi-matrix.h.

Constructor & Destructor Documentation

◆ MatrixBase() [1/2]

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

Initializer, callable only from child.

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

784  :
785  data_(data), num_cols_(cols), num_rows_(rows), stride_(stride) {
787  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:137

◆ MatrixBase() [2/2]

MatrixBase ( )
inlineexplicitprotected

Initializer, callable only from child.

Empty initializer, for un-initialized matrix.

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

791  : data_(NULL) {
793  }
Real * data_
data memory area
Definition: kaldi-matrix.h:808
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:137

◆ ~MatrixBase()

~MatrixBase ( )
inlineprotected

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

796 { }

Member Function Documentation

◆ Add()

void Add ( const Real  alpha)

Add a scalar to each element.

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

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

1677  {
1678  Real *data = data_;
1679  MatrixIndexT stride = stride_;
1680  for (MatrixIndexT r = 0; r < num_rows_; r++)
1681  for (MatrixIndexT c = 0; c < num_cols_; c++)
1682  data[c + stride*r] += alpha;
1683 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ AddCols()

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

Referenced by MatrixBase< float >::ColRange().

2853  {
2854  KALDI_ASSERT(NumRows() == src.NumRows());
2855  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2856  this_stride = stride_, src_stride = src.stride_;
2857  Real *this_data = this->data_;
2858  const Real *src_data = src.data_;
2859 #ifdef KALDI_PARANOID
2860  MatrixIndexT src_cols = src.NumCols();
2861  for (MatrixIndexT i = 0; i < num_cols; i++)
2862  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2863 #endif
2864 
2865  // For the sake of memory locality we do this row by row, rather
2866  // than doing it column-wise using cublas_Xcopy
2867  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2868  const MatrixIndexT *index_ptr = &(indices[0]);
2869  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2870  if (*index_ptr >= 0)
2871  this_data[c] += src_data[*index_ptr];
2872  }
2873  }
2874 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ AddDiagVecMat()

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

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

580  {
581  if (beta != 1.0) this->Scale(beta);
582 
583  if (transM == kNoTrans) {
584  KALDI_ASSERT(SameDim(*this, M));
585  } else {
586  KALDI_ASSERT(M.NumRows() == NumCols() && M.NumCols() == NumRows());
587  }
588  KALDI_ASSERT(v.Dim() == this->NumRows());
589 
590  MatrixIndexT M_row_stride = M.Stride(), M_col_stride = 1, stride = stride_,
591  num_rows = num_rows_, num_cols = num_cols_;
592  if (transM == kTrans) std::swap(M_row_stride, M_col_stride);
593  Real *data = data_;
594  const Real *Mdata = M.Data(), *vdata = v.Data();
595  if (num_rows_ == 0) return;
596  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride, Mdata += M_row_stride, vdata++)
597  cblas_Xaxpy(num_cols, alpha * *vdata, Mdata, M_col_stride, data, 1);
598 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
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:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ AddMat()

void AddMat ( const Real  alpha,
const MatrixBase< Real > &  M,
MatrixTransposeType  transA = kNoTrans 
)

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

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

Referenced by Fmpe::AccStats(), BasisFmllrAccus::AccuGradientScatter(), AffineXformStats::Add(), AccumDiagGmm::Add(), IvectorExtractorStats::Add(), CuMatrixBase< float >::AddMatBlocks(), GeneralMatrix::AddToMat(), MatrixBase< float >::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(), MatrixBase< float >::SymPosSemiDefEig(), test_io(), kaldi::TestFmpe(), kaldi::TypeOneUsage(), kaldi::TypeOneUsageAverage(), kaldi::TypeThreeUsage(), 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(), and MleAmSgmm2Updater::UpdateWGetStats().

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

◆ AddMatDiagVec()

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

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

◆ AddMatMat()

void AddMatMat ( const Real  alpha,
const MatrixBase< Real > &  A,
MatrixTransposeType  transA,
const MatrixBase< Real > &  B,
MatrixTransposeType  transB,
const Real  beta 
)

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

Referenced by MatrixBase< float >::AddMatMatMat(), MatrixBase< float >::AddMatSp(), MatrixBase< float >::AddMatTp(), MatrixBase< float >::AddSpMat(), MatrixBase< float >::AddTpMat(), MatrixBase< float >::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(), OnlineTransform::GetFrames(), LogisticRegression::GetLogPosteriors(), PldaEstimator::GetOutput(), DiagGmm::LogLikelihoods(), main(), kaldi::nnet3::PerturbImage(), MatrixBase< float >::Power(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), kaldi::nnet3::ReduceRankOfComponents(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), kaldi::SlowMatMul(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::TestOnlineLdaInput(), TestSgmm2PreXform(), SpMatrix< float >::TopEigs(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSpMat(), IvectorExtractor::TransformIvectors(), OnlineLdaInput::TransformToOutput(), 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().

176  {
177  KALDI_ASSERT((transA == kNoTrans && transB == kNoTrans && A.num_cols_ == B.num_rows_ && A.num_rows_ == num_rows_ && B.num_cols_ == num_cols_)
178  || (transA == kTrans && transB == kNoTrans && A.num_rows_ == B.num_rows_ && A.num_cols_ == num_rows_ && B.num_cols_ == num_cols_)
179  || (transA == kNoTrans && transB == kTrans && A.num_cols_ == B.num_cols_ && A.num_rows_ == num_rows_ && B.num_rows_ == num_cols_)
180  || (transA == kTrans && transB == kTrans && A.num_rows_ == B.num_cols_ && A.num_cols_ == num_rows_ && B.num_rows_ == num_cols_));
181  KALDI_ASSERT(&A != this && &B != this);
182  if (num_rows_ == 0) return;
183  cblas_Xgemm(alpha, transA, A.data_, A.num_rows_, A.num_cols_, A.stride_,
184  transB, B.data_, B.stride_, beta, data_, num_rows_, num_cols_, stride_);
185 
186 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
void cblas_Xgemm(const float alpha, MatrixTransposeType transA, const float *Adata, MatrixIndexT a_num_rows, MatrixIndexT a_num_cols, MatrixIndexT a_stride, MatrixTransposeType transB, const float *Bdata, MatrixIndexT b_stride, const float beta, float *Mdata, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT stride)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ AddMatMatElements()

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

639  {
640  KALDI_ASSERT(A.NumRows() == B.NumRows() && A.NumCols() == B.NumCols());
641  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
642  Real *data = data_;
643  const Real *dataA = A.Data();
644  const Real *dataB = B.Data();
645 
646  for (MatrixIndexT i = 0; i < num_rows_; i++) {
647  for (MatrixIndexT j = 0; j < num_cols_; j++) {
648  data[j] = beta*data[j] + alpha*dataA[j]*dataB[j];
649  }
650  data += Stride();
651  dataA += A.Stride();
652  dataB += B.Stride();
653  }
654 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:70
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ AddMatMatMat()

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

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

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

◆ AddMatSmat() [1/2]

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

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

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

◆ AddMatSmat() [2/2]

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

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

◆ AddMatSp()

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

711  {
712  Matrix<Real> M(B);
713  return AddMatMat(alpha, A, transA, M, kNoTrans, beta);
714  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddMatTp()

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

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

728  {
729  Matrix<Real> M(B);
730  return AddMatMat(alpha, A, transA, M, transB, beta);
731  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddRows() [1/2]

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

Referenced by MatrixBase< float >::ColRange().

2920  {
2921  KALDI_ASSERT(NumCols() == src.NumCols());
2922  MatrixIndexT num_rows = num_rows_,
2923  num_cols = num_cols_, this_stride = stride_;
2924  Real *this_data = this->data_;
2925 
2926  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2927  MatrixIndexT index = indexes[r];
2928  KALDI_ASSERT(index >= -1 && index < src.NumRows());
2929  if (index != -1)
2930  cblas_Xaxpy(num_cols, alpha, src.RowData(index), 1, this_data, 1);
2931  }
2932 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
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:185

◆ AddRows() [2/2]

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

2935  {
2936  MatrixIndexT num_rows = num_rows_,
2937  num_cols = num_cols_, this_stride = stride_;
2938  Real *this_data = this->data_;
2939 
2940  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2941  const Real *const src_data = src[r];
2942  if (src_data != NULL)
2943  cblas_Xaxpy(num_cols, alpha, src_data, 1, this_data, 1);
2944  }
2945 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)

◆ AddSmat()

void AddSmat ( Real  alpha,
const SparseMatrix< Real > &  A,
MatrixTransposeType  trans = kNoTrans 
)

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

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

Referenced by kaldi::UnitTextCuMatrixAddSmat().

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

◆ AddSmatMat() [1/2]

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

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

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

◆ AddSmatMat() [2/2]

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

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

◆ AddSp()

template void AddSp ( const Real  alpha,
const SpMatrix< OtherReal > &  S 
)

*this += alpha * S

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

Referenced by RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), and kaldi::UnitTestAddSp().

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

◆ AddSpMat()

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

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

695  {
696  Matrix<Real> M(A);
697  return AddMatMat(alpha, M, kNoTrans, B, transB, beta);
698  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddSpMatSp()

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

720  {
721  Matrix<Real> M(A), N(C);
722  return AddMatMatMat(alpha, M, kNoTrans, B, transB, N, kNoTrans, beta);
723  }
void AddMatMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const Real beta)
this <– beta*this + alpha*A*B*C.
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddSpSp()

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

Referenced by MatrixBase< float >::AddTpTp(), and kaldi::UnitTestMmulSym().

343  {
344  MatrixIndexT sz = num_rows_;
345  KALDI_ASSERT(sz == num_cols_ && sz == A_in.NumRows() && sz == B_in.NumRows());
346 
347  Matrix<Real> A(A_in), B(B_in);
348  // CblasLower or CblasUpper would work below as symmetric matrix is copied
349  // fully (to save work, we used the matrix constructor from SpMatrix).
350  // CblasLeft means A is on the left: C <-- alpha A B + beta C
351  if (sz == 0) return;
352  cblas_Xsymm(alpha, sz, A.data_, A.stride_, B.data_, B.stride_, beta, data_, stride_);
353 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
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 attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddToDiag()

void AddToDiag ( const Real  alpha)

Add a scalar to each diagonal element.

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

Referenced by kaldi::UnitTestCuMatrixAddToDiag().

1686  {
1687  Real *data = data_;
1688  MatrixIndexT this_stride = stride_ + 1,
1689  num_to_add = std::min(num_rows_, num_cols_);
1690  for (MatrixIndexT r = 0; r < num_to_add; r++)
1691  data[r * this_stride] += alpha;
1692 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ AddToRows() [1/2]

void AddToRows ( Real  alpha,
Real *const *  dst 
) const

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

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

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

Referenced by MatrixBase< float >::ColRange(), and kaldi::UnitTestAddToRows().

2965  {
2966  MatrixIndexT num_rows = num_rows_,
2967  num_cols = num_cols_, this_stride = stride_;
2968  const Real *this_data = this->data_;
2969 
2970  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2971  Real *const dst_data = dst[r];
2972  if (dst_data != NULL)
2973  cblas_Xaxpy(num_cols, alpha, this_data, 1, dst_data, 1);
2974  }
2975 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)

◆ AddToRows() [2/2]

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.

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

2950  {
2951  KALDI_ASSERT(NumCols() == dst->NumCols());
2952  MatrixIndexT num_rows = num_rows_,
2953  num_cols = num_cols_, this_stride = stride_;
2954  Real *this_data = this->data_;
2955 
2956  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2957  MatrixIndexT index = indexes[r];
2958  KALDI_ASSERT(index >= -1 && index < dst->NumRows());
2959  if (index != -1)
2960  cblas_Xaxpy(num_cols, alpha, this_data, 1, dst->RowData(index), 1);
2961  }
2962 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
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:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ AddTpMat()

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

Referenced by kaldi::UnitTestCuMatrixAddTpMat().

703  {
704  Matrix<Real> M(A);
705  return AddMatMat(alpha, M, transA, B, transB, beta);
706  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddTpTp()

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

737  {
738  Matrix<Real> M(A), N(B);
739  return AddMatMat(alpha, M, transA, N, transB, beta);
740  }
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ AddVecToCols()

template void AddVecToCols ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

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

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

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

3061  {
3062  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3063  stride = stride_;
3064  KALDI_ASSERT(v.Dim() == num_rows);
3065 
3066  if (num_rows <= 64) {
3067  Real *data = data_;
3068  const OtherReal *vdata = v.Data();
3069  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
3070  Real to_add = alpha * vdata[i];
3071  for (MatrixIndexT j = 0; j < num_cols; j++)
3072  data[j] += to_add;
3073  }
3074 
3075  } else {
3076  Vector<OtherReal> ones(num_cols);
3077  ones.Set(1.0);
3078  this->AddVecVec(alpha, v, ones);
3079  }
3080 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T

◆ AddVecToRows()

template void AddVecToRows ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

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

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

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

3030  {
3031  const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3032  stride = stride_;
3033  KALDI_ASSERT(v.Dim() == num_cols);
3034  if(num_cols <= 64) {
3035  Real *data = data_;
3036  const OtherReal *vdata = v.Data();
3037  for (MatrixIndexT i = 0; i < num_rows; i++, data += stride) {
3038  for (MatrixIndexT j = 0; j < num_cols; j++)
3039  data[j] += alpha * vdata[j];
3040  }
3041 
3042  } else {
3043  Vector<OtherReal> ones(num_rows);
3044  ones.Set(1.0);
3045  this->AddVecVec(alpha, ones, v);
3046  }
3047 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T

◆ AddVecVec() [1/5]

void AddVecVec ( const float  alpha,
const VectorBase< float > &  ra,
const VectorBase< float > &  rb 
)

◆ AddVecVec() [2/5]

void AddVecVec ( const double  alpha,
const VectorBase< double > &  ra,
const VectorBase< double > &  rb 
)

◆ AddVecVec() [3/5]

void AddVecVec ( const float  alpha,
const VectorBase< float > &  a,
const VectorBase< float > &  rb 
)

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

121  {
122  KALDI_ASSERT(a.Dim() == num_rows_ && rb.Dim() == num_cols_);
123  cblas_Xger(a.Dim(), rb.Dim(), alpha, a.Data(), 1, rb.Data(),
124  1, data_, stride_);
125 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
Real * Data()
Returns a pointer to the start of the vector&#39;s data.
Definition: kaldi-vector.h:70
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)

◆ AddVecVec() [4/5]

void AddVecVec ( const double  alpha,
const VectorBase< double > &  a,
const VectorBase< double > &  rb 
)

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

163  {
164  KALDI_ASSERT(a.Dim() == num_rows_ && rb.Dim() == num_cols_);
165  if (num_rows_ == 0) return;
166  cblas_Xger(a.Dim(), rb.Dim(), alpha, a.Data(), 1, rb.Data(),
167  1, data_, stride_);
168 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)

◆ AddVecVec() [5/5]

void AddVecVec ( const Real  alpha,
const VectorBase< OtherReal > &  a,
const VectorBase< OtherReal > &  b 
)

*this += alpha * a * b^T

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

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

131  {
132  KALDI_ASSERT(a.Dim() == num_rows_ && b.Dim() == num_cols_);
133  if (num_rows_ * num_cols_ > 100) { // It's probably worth it to allocate
134  // temporary vectors of the right type and use BLAS.
135  Vector<Real> temp_a(a), temp_b(b);
136  cblas_Xger(num_rows_, num_cols_, alpha, temp_a.Data(), 1,
137  temp_b.Data(), 1, data_, stride_);
138  } else {
139  const OtherReal *a_data = a.Data(), *b_data = b.Data();
140  Real *row_data = data_;
141  for (MatrixIndexT i = 0; i < num_rows_; i++, row_data += stride_) {
142  BaseFloat alpha_ai = alpha * a_data[i];
143  for (MatrixIndexT j = 0; j < num_cols_; j++)
144  row_data[j] += alpha_ai * b_data[j];
145  }
146  }
147 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)

◆ ApplyCeiling()

void ApplyCeiling ( Real  ceiling_val)
inline

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

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

358  {
359  this -> Ceiling(*this, ceiling_val);
360  }
void Ceiling(const MatrixBase< Real > &src, Real ceiling_val)

◆ ApplyExp()

void ApplyExp ( )
inline

◆ ApplyExpLimited()

void ApplyExpLimited ( Real  lower_limit,
Real  upper_limit 
)
inline

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

370  {
371  this -> ExpLimited(*this, lower_limit, upper_limit);
372  }
void ExpLimited(const MatrixBase< Real > &src, Real lower_limit, Real upper_limit)
This is equivalent to running: Floor(src, lower_limit); Ceiling(src, upper_limit); Exp(src) ...

◆ ApplyExpSpecial()

void ApplyExpSpecial ( )
inline

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

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

366  {
367  this -> ExpSpecial(*this);
368  }
void ExpSpecial(const MatrixBase< Real > &src)
For each element x of the matrix, set it to (x < 0 ? exp(x) : x + 1).

◆ ApplyFloor()

◆ ApplyHeaviside()

void ApplyHeaviside ( )
inline

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

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

350  {
351  this -> Heaviside(*this);
352  }
void Heaviside(const MatrixBase< Real > &src)
Sets each element to the Heaviside step function (x > 0 ? 1 : 0) of the corresponding element in "src...

◆ ApplyLog()

void ApplyLog ( )
inline

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

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

374  {
375  this -> Log(*this);
376  }
void Log(const MatrixBase< Real > &src)

◆ ApplyPow()

void ApplyPow ( Real  power)
inline

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

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

341  {
342  this -> Pow(*this, power);
343  }
void Pow(const MatrixBase< Real > &src, Real power)

◆ ApplyPowAbs()

void ApplyPowAbs ( Real  power,
bool  include_sign = false 
)
inline

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

Referenced by kaldi::UnitTestCuMatrixApplyPowAbs().

346  {
347  this -> PowAbs(*this, power, include_sign);
348  }
void PowAbs(const MatrixBase< Real > &src, Real power, bool include_sign=false)
Apply power to the absolute value of each element.

◆ ApplySoftMax()

Real ApplySoftMax ( )

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

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestSimpleForMat().

2751  {
2752  Real max = this->Max(), sum = 0.0;
2753  // the 'max' helps to get in good numeric range.
2754  for (MatrixIndexT i = 0; i < num_rows_; i++)
2755  for (MatrixIndexT j = 0; j < num_cols_; j++)
2756  sum += ((*this)(i, j) = kaldi::Exp((*this)(i, j) - max));
2757  this->Scale(1.0 / sum);
2758  return max + kaldi::Log(sum);
2759 }
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ ApproxEqual()

bool ApproxEqual ( const MatrixBase< Real > &  other,
float  tol = 0.01 
) const

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

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

Referenced by kaldi::ApproxEqual(), AccumDiagGmm::AssertEqual(), kaldi::AssertEqual(), MatrixBase< float >::MinSingularValue(), SingleUtteranceGmmDecoder::RescoringIsNeeded(), kaldi::TestOnlineCmnInput(), kaldi::TestOnlineDeltaFeature(), kaldi::TestOnlineDeltaInput(), kaldi::TestOnlineLdaInput(), kaldi::TestOnlineSpliceFrames(), kaldi::UnitTestExtractCompressedMatrix(), kaldi::UnitTestNorm(), kaldi::UnitTestPosteriors(), and kaldi::UnitTestTableRandomBothDoubleMatrix().

1915  {
1916  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1917  KALDI_ERR << "ApproxEqual: size mismatch.";
1918  Matrix<Real> tmp(*this);
1919  tmp.AddMat(-1.0, other);
1920  return (tmp.FrobeniusNorm() <= static_cast<Real>(tol) *
1921  this->FrobeniusNorm());
1922 }
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
friend class Matrix< Real >
Definition: kaldi-matrix.h:52

◆ Ceiling()

void Ceiling ( const MatrixBase< Real > &  src,
Real  ceiling_val 
)

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

Referenced by MatrixBase< float >::ApplyCeiling(), and MatrixBase< float >::MinSingularValue().

2173  {
2174  KALDI_ASSERT(SameDim(*this, src));
2175  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2176  Real *row_data = data_;
2177  const Real *src_row_data = src.Data();
2178  for (MatrixIndexT row = 0; row < num_rows;
2179  row++,row_data += stride_, src_row_data += src.stride_) {
2180  for (MatrixIndexT col = 0; col < num_cols; col++)
2181  row_data[col] = (src_row_data[col] > ceiling_val ? ceiling_val : src_row_data[col]);
2182  }
2183 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ColRange()

SubMatrix<Real> ColRange ( const MatrixIndexT  col_offset,
const MatrixIndexT  num_cols 
) const
inline

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

Referenced by kaldi::AppendPostToFeats(), kaldi::AppendVectorToFeats(), OnlineFeaturePipeline::Init(), and main().

214  {
215  return SubMatrix<Real>(*this, 0, num_rows_, col_offset, num_cols);
216  }
friend class SubMatrix< Real >
Definition: kaldi-matrix.h:586
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813

◆ Cond()

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

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

1696  {
1697  KALDI_ASSERT(num_rows_ > 0&&num_cols_ > 0);
1698  Vector<Real> singular_values(std::min(num_rows_, num_cols_));
1699  Svd(&singular_values); // Get singular values...
1700  Real min = singular_values(0), max = singular_values(0); // both absolute values...
1701  for (MatrixIndexT i = 1;i < singular_values.Dim();i++) {
1702  min = std::min((Real)std::abs(singular_values(i)), min); max = std::max((Real)std::abs(singular_values(i)), max);
1703  }
1704  if (min > 0) return max/min;
1705  else return std::numeric_limits<Real>::infinity();
1706 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.

◆ CopyColFromVec()

void CopyColFromVec ( const VectorBase< Real > &  v,
const MatrixIndexT  col 
)

Copy vector into specific column of matrix.

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

Referenced by LdaEstimate::AddMeanOffset(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), AmSgmm2::InitializeMw(), main(), MatrixBase< float >::operator()(), ArbitraryResample::Resample(), kaldi::UnitTestCuMathNormalizePerRow(), and kaldi::UnitTestSpliceRows().

1103  {
1104  KALDI_ASSERT(rv.Dim() == num_rows_ &&
1105  static_cast<UnsignedMatrixIndexT>(col) <
1106  static_cast<UnsignedMatrixIndexT>(num_cols_));
1107 
1108  const Real *rv_data = rv.Data();
1109  Real *col_data = data_ + col;
1110 
1111  for (MatrixIndexT r = 0; r < num_rows_; r++)
1112  col_data[r * stride_] = rv_data[r];
1113 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
uint32 UnsignedMatrixIndexT
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyCols()

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

Referenced by MatrixBase< float >::ColRange().

2827  {
2828  KALDI_ASSERT(NumRows() == src.NumRows());
2829  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2830  this_stride = stride_, src_stride = src.stride_;
2831  Real *this_data = this->data_;
2832  const Real *src_data = src.data_;
2833 #ifdef KALDI_PARANOID
2834  MatrixIndexT src_cols = src.NumCols();
2835  for (MatrixIndexT i = 0; i < num_cols; i++)
2836  KALDI_ASSERT(indices[i] >= -1 && indices[i] < src_cols);
2837 #endif
2838 
2839  // For the sake of memory locality we do this row by row, rather
2840  // than doing it column-wise using cublas_Xcopy
2841  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2842  const MatrixIndexT *index_ptr = &(indices[0]);
2843  for (MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2844  if (*index_ptr < 0) this_data[c] = 0;
2845  else this_data[c] = src_data[*index_ptr];
2846  }
2847  }
2848 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ CopyColsFromVec()

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

Referenced by MatrixBase< float >::operator()(), kaldi::UnitTestCuMatrixCopyColsFromVec(), and kaldi::UnitTestSpliceRows().

1053  {
1054  if (rv.Dim() == num_rows_*num_cols_) {
1055  const Real *v_inc_data = rv.Data();
1056  Real *m_inc_data = data_;
1057 
1058  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1059  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1060  m_inc_data[r * stride_] = v_inc_data[r];
1061  }
1062  v_inc_data += num_rows_;
1063  m_inc_data ++;
1064  }
1065  } else if (rv.Dim() == num_rows_) {
1066  const Real *v_inc_data = rv.Data();
1067  Real *m_inc_data = data_;
1068  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1069  Real value = *(v_inc_data++);
1070  for (MatrixIndexT c = 0; c < num_cols_; c++)
1071  m_inc_data[c] = value;
1072  m_inc_data += stride_;
1073  }
1074  } else {
1075  KALDI_ERR << "Wrong size of arguments.";
1076  }
1077 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ CopyDiagFromVec()

void CopyDiagFromVec ( const VectorBase< Real > &  v)

Copy vector into diagonal of matrix.

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

Referenced by MatrixBase< float >::operator()(), kaldi::UnitTestAddVec2Sp(), kaldi::UnitTestSpliceRows(), and kaldi::UnitTestSvd().

1093  {
1094  KALDI_ASSERT(rv.Dim() == std::min(num_cols_, num_rows_));
1095  const Real *rv_data = rv.Data(), *rv_end = rv_data + rv.Dim();
1096  Real *my_data = this->Data();
1097  for (; rv_data != rv_end; rv_data++, my_data += (this->stride_+1))
1098  *my_data = *rv_data;
1099 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:79
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromMat() [1/3]

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

Copy given matrix. (no resize is done).

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

Referenced by DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::ApplyFeatureTransformToStats(), kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), kaldi::ComposeTransforms(), OnlineMatrixInput::Compute(), kaldi::ComputeFmllrMatrixDiagGmmDiagonal(), kaldi::ComputeFmllrMatrixDiagGmmFull(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), AmSgmm2::ComputeFmllrPreXform(), kaldi::ComputeLdaTransform(), kaldi::ComputeMllrMatrix(), MleAmSgmm2Updater::ComputeMPrior(), Fmpe::ComputeStddevs(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), CompressedAffineXformStats::CopyFromAffineXformStats(), DiagGmmNormal::CopyFromDiagGmm(), FullGmm::CopyFromDiagGmm(), DiagGmm::CopyFromDiagGmm(), FullGmm::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), AffineXformStats::CopyStats(), CompressedAffineXformStats::CopyToAffineXformStats(), DiagGmmNormal::CopyToDiagGmm(), CompressedMatrix::CopyToMat(), CuMatrixBase< float >::CopyToMat(), GeneralMatrix::CopyToMat(), OnlineDeltaInput::DeltaComputation(), 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(), OnlineCmvn::GetMostRecentCachedFrame(), AmSgmm2::GetNtransSigmaInv(), LinearVtln::GetTransform(), DiagGmm::GetVars(), RegtreeFmllrDiagGmm::GetXformMatrix(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), CuMatrixBase< float >::LogSoftMaxPerRow(), main(), MleAmSgmm2Updater::MapUpdateM(), MfccComputer::MfccComputer(), LogisticRegression::MixUp(), OnlineLdaInput::OnlineLdaInput(), MatrixBase< float >::operator()(), 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(), CuMatrixBase< float >::SoftMaxPerRow(), kaldi::SolveQuadraticMatrixProblem(), MatrixBase< float >::Svd(), NoOpTransform::TestingForward(), SpMatrix< float >::TopEigs(), LogisticRegression::Train(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddMatMatElements(), kaldi::UnitTestCuCopy(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuDiffSoftmax(), kaldi::UnitTestCuLogSoftmax(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMathNormalizePerRow_v2(), 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().

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

◆ CopyFromMat() [2/3]

void CopyFromMat ( const CompressedMatrix M)

Copy from compressed matrix.

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

2057  {
2058  mat.CopyToMat(this);
2059 }

◆ CopyFromMat() [3/3]

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

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

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

976  {
977  cu.CopyToMat(this, trans);
978 }

◆ CopyFromSp() [1/3]

void CopyFromSp ( const SpMatrix< OtherReal > &  M)

Copy given spmatrix. (no resize is done).

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

Referenced by AmSgmm2::GetNtransSigmaInv(), SpMatrix< float >::Invert(), Matrix< BaseFloat >::Matrix(), MatrixBase< float >::operator()(), kaldi::RandFullCova(), kaldi::UnitTestAddMat2(), and kaldi::UnitTestTransposeScatter().

938  {
939  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
940  // MORE EFFICIENT IF LOWER TRIANGULAR! Reverse code otherwise.
941  for (MatrixIndexT i = 0; i < num_rows_; i++) {
942  for (MatrixIndexT j = 0; j < i; j++) {
943  (*this)(j, i) = (*this)(i, j) = M(i, j);
944  }
945  (*this)(i, i) = M(i, i);
946  }
947 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromSp() [2/3]

void CopyFromSp ( const SpMatrix< float > &  M)

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

904  {
906  MatrixIndexT num_rows = num_rows_, stride = stride_;
907  const float *Mdata = M.Data();
908  float *row_data = data_, *col_data = data_;
909  for (MatrixIndexT i = 0; i < num_rows; i++) {
910  cblas_scopy(i+1, Mdata, 1, row_data, 1); // copy to the row.
911  cblas_scopy(i, Mdata, 1, col_data, stride); // copy to the column.
912  Mdata += i+1;
913  row_data += stride;
914  col_data += 1;
915  }
916 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromSp() [3/3]

void CopyFromSp ( const SpMatrix< double > &  M)

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

921  {
923  MatrixIndexT num_rows = num_rows_, stride = stride_;
924  const double *Mdata = M.Data();
925  double *row_data = data_, *col_data = data_;
926  for (MatrixIndexT i = 0; i < num_rows; i++) {
927  cblas_dcopy(i+1, Mdata, 1, row_data, 1); // copy to the row.
928  cblas_dcopy(i, Mdata, 1, col_data, stride); // copy to the column.
929  Mdata += i+1;
930  row_data += stride;
931  col_data += 1;
932  }
933 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromTp()

template void CopyFromTp ( const TpMatrix< OtherReal > &  M,
MatrixTransposeType  trans = kNoTrans 
)

Copy given tpmatrix. (no resize is done).

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

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

959  {
960  if (Trans == kNoTrans) {
961  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
962  SetZero();
963  Real *out_i = data_;
964  const OtherReal *in_i = M.Data();
965  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i += stride_, in_i += i) {
966  for (MatrixIndexT j = 0; j <= i; j++)
967  out_i[j] = in_i[j];
968  }
969  } else {
970  SetZero();
971  KALDI_ASSERT(num_rows_ == M.NumRows() && num_cols_ == num_rows_);
972  MatrixIndexT stride = stride_;
973  Real *out_i = data_;
974  const OtherReal *in_i = M.Data();
975  for (MatrixIndexT i = 0; i < num_rows_; i++, out_i ++, in_i += i) {
976  for (MatrixIndexT j = 0; j <= i; j++)
977  out_i[j*stride] = in_i[j];
978  }
979  }
980 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
void SetZero()
Sets matrix to zero.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyLowerToUpper()

void CopyLowerToUpper ( )

Copy lower triangle to upper triangle (symmetrize)

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

Referenced by MatrixBase< float >::AddTpTp(), OnlinePreconditioner::PreconditionDirectionsInternal(), and OnlineNaturalGradient::PreconditionDirectionsInternal().

212  {
214  Real *data = data_;
215  MatrixIndexT num_rows = num_rows_, stride = stride_;
216  for (int32 i = 0; i < num_rows; i++)
217  for (int32 j = 0; j < i; j++)
218  data[j * stride + i ] = data[i * stride + j];
219 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
kaldi::int32 int32
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyRowFromVec()

void CopyRowFromVec ( const VectorBase< Real > &  v,
const MatrixIndexT  row 
)

Copy vector into specific row of matrix.

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

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

1081  {
1082  KALDI_ASSERT(rv.Dim() == num_cols_ &&
1083  static_cast<UnsignedMatrixIndexT>(row) <
1084  static_cast<UnsignedMatrixIndexT>(num_rows_));
1085 
1086  const Real *rv_data = rv.Data();
1087  Real *row_data = RowData(row);
1088 
1089  std::memcpy(row_data, rv_data, num_cols_ * sizeof(Real));
1090 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
uint32 UnsignedMatrixIndexT
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyRows() [1/2]

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

Referenced by MatrixBase< float >::ColRange().

2878  {
2879  KALDI_ASSERT(NumCols() == src.NumCols());
2880  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2881  this_stride = stride_;
2882  Real *this_data = this->data_;
2883 
2884  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2885  MatrixIndexT index = indices[r];
2886  if (index < 0) memset(this_data, 0, sizeof(Real) * num_cols_);
2887  else cblas_Xcopy(num_cols, src.RowData(index), 1, this_data, 1);
2888  }
2889 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyRows() [2/2]

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

2892  {
2893  MatrixIndexT num_rows = num_rows_,
2894  num_cols = num_cols_, this_stride = stride_;
2895  Real *this_data = this->data_;
2896 
2897  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2898  const Real *const src_data = src[r];
2899  if (src_data == NULL) memset(this_data, 0, sizeof(Real) * num_cols);
2900  else cblas_Xcopy(num_cols, src_data, 1, this_data, 1);
2901  }
2902 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ CopyRowsFromVec() [1/3]

void CopyRowsFromVec ( const VectorBase< Real > &  v)

This function has two modes of operation.

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

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

Referenced by DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::nnet2::CombineNnets(), FastNnetCombiner::ComputeCurrentNnet(), FmllrRawAccs::ConvertToPerRowStats(), kaldi::CuVectorUnitTestCopyFromMat(), LogisticRegression::DoStep(), kaldi::nnet2::FormatNnetInput(), OnlineTransform::GetFrames(), DiagGmm::LogLikelihoods(), MatrixBase< float >::operator()(), LogisticRegression::TrainParameters(), kaldi::UnitTestCopyRowsAndCols(), kaldi::UnitTestCuMatrixCopyRowsFromVec(), and kaldi::UnitTestSpliceRows().

997  {
998  if (rv.Dim() == num_rows_*num_cols_) {
999  if (stride_ == num_cols_) {
1000  // one big copy operation.
1001  const Real *rv_data = rv.Data();
1002  std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_);
1003  } else {
1004  const Real *rv_data = rv.Data();
1005  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1006  Real *row_data = RowData(r);
1007  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1008  row_data[c] = rv_data[c];
1009  }
1010  rv_data += num_cols_;
1011  }
1012  }
1013  } else if (rv.Dim() == num_cols_) {
1014  const Real *rv_data = rv.Data();
1015  for (MatrixIndexT r = 0; r < num_rows_; r++)
1016  std::memcpy(RowData(r), rv_data, sizeof(Real)*num_cols_);
1017  } else {
1018  KALDI_ERR << "Wrong sized arguments";
1019  }
1020 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ CopyRowsFromVec() [2/3]

void CopyRowsFromVec ( const CuVectorBase< Real > &  v)

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

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

246  {
247  KALDI_ASSERT(v.Dim() == NumCols() * NumRows());
248 #if HAVE_CUDA == 1
249  if (CuDevice::Instantiate().Enabled()) {
250  if (num_rows_ == 0) return;
251  CuTimer tim;
252  if (Stride() == NumCols()) {
253  CU_SAFE_CALL(cudaMemcpyAsync(data_, v.Data(),
254  sizeof(Real)*v.Dim(),
255  cudaMemcpyDeviceToHost,
256  cudaStreamPerThread));
257  } else {
258  const Real* vec_data = v.Data();
259  for (MatrixIndexT r = 0; r < NumRows(); r++) {
260  CU_SAFE_CALL(cudaMemcpyAsync(RowData(r), vec_data,
261  sizeof(Real) * NumCols(),
262  cudaMemcpyDeviceToHost,
263  cudaStreamPerThread));
264  vec_data += NumCols();
265  }
266  }
267  CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread));
268  CuDevice::Instantiate().AccuProfile(__func__, tim);
269  } else
270 #endif
271  {
272  CopyRowsFromVec(v.Vec());
273  }
274 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * data_
data memory area
Definition: kaldi-matrix.h:808
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:70
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.

◆ CopyRowsFromVec() [3/3]

void CopyRowsFromVec ( const VectorBase< OtherReal > &  v)

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

1024  {
1025  if (rv.Dim() == num_rows_*num_cols_) {
1026  const OtherReal *rv_data = rv.Data();
1027  for (MatrixIndexT r = 0; r < num_rows_; r++) {
1028  Real *row_data = RowData(r);
1029  for (MatrixIndexT c = 0; c < num_cols_; c++) {
1030  row_data[c] = static_cast<Real>(rv_data[c]);
1031  }
1032  rv_data += num_cols_;
1033  }
1034  } else if (rv.Dim() == num_cols_) {
1035  const OtherReal *rv_data = rv.Data();
1036  Real *first_row_data = RowData(0);
1037  for (MatrixIndexT c = 0; c < num_cols_; c++)
1038  first_row_data[c] = rv_data[c];
1039  for (MatrixIndexT r = 1; r < num_rows_; r++)
1040  std::memcpy(RowData(r), first_row_data, sizeof(Real)*num_cols_);
1041  } else {
1042  KALDI_ERR << "Wrong sized arguments.";
1043  }
1044 }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ CopyToRows()

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

Referenced by MatrixBase< float >::ColRange(), and kaldi::UnitTestCopyToRows().

2905  {
2906  MatrixIndexT num_rows = num_rows_,
2907  num_cols = num_cols_, this_stride = stride_;
2908  const Real *this_data = this->data_;
2909 
2910  for (MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2911  Real *const dst_data = dst[r];
2912  if (dst_data != NULL)
2913  cblas_Xcopy(num_cols, this_data, 1, dst_data, 1);
2914  }
2915 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ CopyUpperToLower()

void CopyUpperToLower ( )

Copy upper triangle to lower triangle (symmetrize)

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

Referenced by MatrixBase< float >::AddTpTp().

223  {
225  Real *data = data_;
226  MatrixIndexT num_rows = num_rows_, stride = stride_;
227  for (int32 i = 0; i < num_rows; i++)
228  for (int32 j = 0; j < i; j++)
229  data[i * stride + j] = data[j * stride + i];
230 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
kaldi::int32 int32
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Data() [1/2]

const Real* Data ( ) const
inline

Gives pointer to raw data (const).

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

Referenced by VectorBase< float >::AddDiagMat2(), VectorBase< float >::AddDiagMatMat(), MatrixBase< float >::AddDiagVecMat(), SpMatrix< float >::AddMat2(), SpMatrix< float >::AddMat2Sp(), SpMatrix< float >::AddMat2Vec(), MatrixBase< float >::AddMatDiagVec(), MatrixBase< float >::AddMatMatElements(), MatrixBase< float >::AddMatSmat(), VectorBase< float >::AddMatSvec(), VectorBase< float >::AddMatVec(), kaldi::AddOuterProductPlusMinus(), VectorBase< float >::AddRowSumMat(), SpMatrix< float >::AddSmat2Sp(), SparseMatrix< float >::AddToMat(), MatrixBase< float >::Ceiling(), VectorBase< float >::CopyColsFromMat(), VectorBase< float >::CopyDiagFromMat(), TpMatrix< float >::CopyFromMat(), SpMatrix< float >::CopyFromMat(), CompressedMatrix::CopyFromMat(), MatrixBase< float >::CopyFromMat(), CuMatrixBase< float >::CopyFromMat(), CuVectorBase< float >::CopyRowsFromMat(), VectorBase< float >::CopyRowsFromMat(), SparseMatrix< float >::CopyToMat(), CuMatrixBase< float >::CopyToMat(), kaldi::cu::CpuComputeLstmNonlinearity(), DecodableMatrixMapped::DecodableMatrixMapped(), MatrixBase< float >::Exp(), MatrixBase< float >::ExpLimited(), MatrixBase< float >::ExpSpecial(), MatrixBase< float >::Floor(), MatrixBase< float >::Heaviside(), TpMatrix< float >::Invert(), MatrixBase< float >::LapackGesvd(), MatrixBase< float >::Log(), MatrixBase< float >::Pow(), MatrixBase< float >::PowAbs(), kaldi::QrStep(), MatrixBase< float >::SymAddMat2(), kaldi::TraceMatSmat(), kaldi::TraceSpMat(), SpMatrix< float >::Tridiagonalize(), kaldi::UnitTestCuDiffNormalizePerRow(), and kaldi::UnitTestCuMathNormalizePerRow().

79  {
80  return data_;
81  }
Real * data_
data memory area
Definition: kaldi-matrix.h:808

◆ Data() [2/2]

Real* Data ( )
inline

Gives pointer to raw data (non-const).

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

84 { return data_; }
Real * data_
data memory area
Definition: kaldi-matrix.h:808

◆ Data_workaround()

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

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

803  {
804  return data_;
805  }
Real * data_
data memory area
Definition: kaldi-matrix.h:808

◆ DestructiveSvd()

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

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

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

◆ DiffSigmoid()

void DiffSigmoid ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestSigmoid().

2995  {
2996  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
2997  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2998  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
2999  Real *data = data_;
3000  const Real *value_data = value.data_, *diff_data = diff.data_;
3001  for (MatrixIndexT r = 0; r < num_rows; r++) {
3002  for (MatrixIndexT c = 0; c < num_cols; c++)
3003  data[c] = diff_data[c] * value_data[c] * (1.0 - value_data[c]);
3004  data += stride;
3005  value_data += value_stride;
3006  diff_data += diff_stride;
3007  }
3008 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ DiffTanh()

void DiffTanh ( const MatrixBase< Real > &  value,
const MatrixBase< Real > &  diff 
)

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestTanh().

3012  {
3013  KALDI_ASSERT(SameDim(*this, value) && SameDim(*this, diff));
3014  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3015  stride = stride_, value_stride = value.stride_, diff_stride = diff.stride_;
3016  Real *data = data_;
3017  const Real *value_data = value.data_, *diff_data = diff.data_;
3018  for (MatrixIndexT r = 0; r < num_rows; r++) {
3019  for (MatrixIndexT c = 0; c < num_cols; c++)
3020  data[c] = diff_data[c] * (1.0 - (value_data[c] * value_data[c]));
3021  data += stride;
3022  value_data += value_stride;
3023  diff_data += diff_stride;
3024  }
3025 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ DivElements()

void DivElements ( const MatrixBase< Real > &  A)

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

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

Referenced by MatrixBase< float >::ColRange(), and kaldi::UnitTestCuMatrixDivElements().

1157  {
1158  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
1159  MatrixIndexT i;
1160  MatrixIndexT j;
1161 
1162  for (i = 0; i < num_rows_; i++) {
1163  for (j = 0; j < num_cols_; j++) {
1164  (*this)(i, j) /= a(i, j);
1165  }
1166  }
1167 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Eig()

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

Referenced by MatrixBase< float >::ApplyLog(), kaldi::UnitTestEig(), and kaldi::UnitTestEigSymmetric().

2282  {
2283  EigenvalueDecomposition<Real> eig(*this);
2284  if (P) eig.GetV(P);
2285  if (r) eig.GetRealEigenvalues(r);
2286  if (i) eig.GetImagEigenvalues(i);
2287 }

◆ Equal()

bool Equal ( const MatrixBase< Real > &  other) const

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

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

Referenced by MatrixBase< float >::MinSingularValue().

1925  {
1926  if (num_rows_ != other.num_rows_ || num_cols_ != other.num_cols_)
1927  KALDI_ERR << "Equal: size mismatch.";
1928  for (MatrixIndexT i = 0; i < num_rows_; i++)
1929  for (MatrixIndexT j = 0; j < num_cols_; j++)
1930  if ( (*this)(i, j) != other(i, j))
1931  return false;
1932  return true;
1933 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ Exp()

void Exp ( const MatrixBase< Real > &  src)

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

Referenced by MatrixBase< float >::ApplyExp(), and MatrixBase< float >::MinSingularValue().

2115  {
2116  KALDI_ASSERT(SameDim(*this, src));
2117  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2118  Real *row_data = data_;
2119  const Real *src_row_data = src.Data();
2120  for (MatrixIndexT row = 0; row < num_rows;
2121  row++,row_data += stride_, src_row_data += src.stride_) {
2122  for (MatrixIndexT col = 0; col < num_cols; col++)
2123  row_data[col] = kaldi::Exp(src_row_data[col]);
2124  }
2125 }
double Exp(double x)
Definition: kaldi-math.h:83
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ExpLimited()

void ExpLimited ( const MatrixBase< Real > &  src,
Real  lower_limit,
Real  upper_limit 
)

This is equivalent to running: Floor(src, lower_limit); Ceiling(src, upper_limit); Exp(src)

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

Referenced by MatrixBase< float >::ApplyExpLimited(), and MatrixBase< float >::MinSingularValue().

2212  {
2213  KALDI_ASSERT(SameDim(*this, src));
2214  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2215  Real *row_data = data_;
2216  const Real *src_row_data = src.Data();
2217  for (MatrixIndexT row = 0; row < num_rows;
2218  row++,row_data += stride_, src_row_data += src.stride_) {
2219  for (MatrixIndexT col = 0; col < num_cols; col++) {
2220  const Real x = src_row_data[col];
2221  if (!(x >= lower_limit))
2222  row_data[col] = kaldi::Exp(lower_limit);
2223  else if (x > upper_limit)
2224  row_data[col] = kaldi::Exp(upper_limit);
2225  else
2226  row_data[col] = kaldi::Exp(x);
2227  }
2228  }
2229 }
double Exp(double x)
Definition: kaldi-math.h:83
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ExpSpecial()

void ExpSpecial ( const MatrixBase< Real > &  src)

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

Referenced by MatrixBase< float >::ApplyExpSpecial(), and MatrixBase< float >::MinSingularValue().

2199  {
2200  KALDI_ASSERT(SameDim(*this, src));
2201  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2202  Real *row_data = data_;
2203  const Real *src_row_data = src.Data();
2204  for (MatrixIndexT row = 0; row < num_rows;
2205  row++,row_data += stride_, src_row_data += src.stride_) {
2206  for (MatrixIndexT col = 0; col < num_cols; col++)
2207  row_data[col] = (src_row_data[col] < Real(0) ? kaldi::Exp(src_row_data[col]) : (src_row_data[col] + Real(1)));
2208  }
2209 }
double Exp(double x)
Definition: kaldi-math.h:83
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Floor()

void Floor ( const MatrixBase< Real > &  src,
Real  floor_val 
)

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

Referenced by MatrixBase< float >::ApplyFloor(), and MatrixBase< float >::MinSingularValue().

2160  {
2161  KALDI_ASSERT(SameDim(*this, src));
2162  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2163  Real *row_data = data_;
2164  const Real *src_row_data = src.Data();
2165  for (MatrixIndexT row = 0; row < num_rows;
2166  row++,row_data += stride_, src_row_data += src.stride_) {
2167  for (MatrixIndexT col = 0; col < num_cols; col++)
2168  row_data[col] = (src_row_data[col] < floor_val ? floor_val : src_row_data[col]);
2169  }
2170 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FrobeniusNorm()

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

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

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

◆ GroupMax()

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

Referenced by MatrixBase< float >::MinSingularValue(), kaldi::UnitTestCuMatrixGroupMax(), and kaldi::UnitTestCuMatrixGroupMaxDeriv().

2806  {
2807  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2808  src.NumRows() == this->NumRows());
2809  int group_size = src.NumCols() / this->NumCols(),
2810  num_rows = this->NumRows(), num_cols = this->NumCols();
2811  for (MatrixIndexT i = 0; i < num_rows; i++) {
2812  const Real *src_row_data = src.RowData(i);
2813  for (MatrixIndexT j = 0; j < num_cols; j++) {
2814  Real max_val = -1e20;
2815  for (MatrixIndexT k = 0; k < group_size; k++) {
2816  Real src_data = src_row_data[j * group_size + k];
2817  if (src_data > max_val)
2818  max_val = src_data;
2819  }
2820  (*this)(i, j) = max_val;
2821  }
2822  }
2823 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ GroupMaxDeriv()

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestCuMatrixGroupMaxDeriv().

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

◆ GroupPnorm()

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

Referenced by MatrixBase< float >::MinSingularValue(), kaldi::UnitTestCuMatrixDiffGroupPnorm(), and kaldi::UnitTestCuMatrixGroupPnorm().

2795  {
2796  KALDI_ASSERT(src.NumCols() % this->NumCols() == 0 &&
2797  src.NumRows() == this->NumRows());
2798  int group_size = src.NumCols() / this->NumCols(),
2799  num_rows = this->NumRows(), num_cols = this->NumCols();
2800  for (MatrixIndexT i = 0; i < num_rows; i++)
2801  for (MatrixIndexT j = 0; j < num_cols; j++)
2802  (*this)(i, j) = src.Row(i).Range(j * group_size, group_size).Norm(power);
2803 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ GroupPnormDeriv()

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestCuMatrixDiffGroupPnorm().

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

◆ Heaviside()

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

Referenced by MatrixBase< float >::ApplyHeaviside(), and MatrixBase< float >::MinSingularValue().

2102  {
2103  KALDI_ASSERT(SameDim(*this, src));
2104  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2105  Real *row_data = data_;
2106  const Real *src_row_data = src.Data();
2107  for (MatrixIndexT row = 0; row < num_rows;
2108  row++,row_data += stride_, src_row_data += src.stride_) {
2109  for (MatrixIndexT col = 0; col < num_cols; col++)
2110  row_data[col] = (src_row_data[col] > 0 ? 1.0 : 0.0);
2111  }
2112 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Index()

Real& Index ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

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

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

111 { return (*this)(r, c); }

◆ Invert()

void Invert ( Real *  log_det = NULL,
Real *  det_sign = NULL,
bool  inverse_needed = true 
)

matrix inverse.

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

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

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

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

◆ InvertDouble()

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

Referenced by kaldi::CalBasisFmllrStepSize(), kaldi::CalcFmllrStepSize(), MatrixBase< float >::ColRange(), kaldi::ComputeFeatureNormalizingTransform(), and BasisFmllrEstimate::ComputeTransform().

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

◆ InvertElements()

void InvertElements ( )

Inverts all the elements of the matrix.

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

Referenced by kaldi::ClusterGaussiansToUbm(), MatrixBase< float >::ColRange(), 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().

2070  {
2071  for (MatrixIndexT r = 0; r < num_rows_; r++) {
2072  for (MatrixIndexT c = 0; c < num_cols_; c++) {
2073  (*this)(r, c) = static_cast<Real>(1.0 / (*this)(r, c));
2074  }
2075  }
2076 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ IsDiagonal()

bool IsDiagonal ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Diagonal.

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

Referenced by kaldi::ApplyModelTransformToStats(), MatrixBase< float >::MinSingularValue(), UnitTestEstimateLda(), and kaldi::UnitTestSimple().

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

◆ IsSymmetric()

bool IsSymmetric ( Real  cutoff = 1.0e-05) const

Returns true if matrix is Symmetric.

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

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

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

◆ IsUnit()

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

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

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

◆ IsZero()

bool IsZero ( Real  cutoff = 1.0e-05) const

Returns true if matrix is all zeros.

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

Referenced by BasisFmllrEstimate::ComputeTransform(), MatrixBase< float >::MinSingularValue(), test_io(), kaldi::TestFmpe(), kaldi::UnitTestDeterminant(), kaldi::UnitTestResize(), kaldi::UnitTestSimple(), FmllrDiagGmmAccs::Update(), and FmllrRawAccs::Update().

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

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( MatrixBase< Real >  )
private

◆ LapackGesvd()

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

Referenced by MatrixBase< float >::AddTpTp(), and MatrixBase< float >::DestructiveSvd().

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

◆ LargestAbsElem()

Real LargestAbsElem ( ) const

largest absolute value.

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

Referenced by MatrixBase< float >::DestructiveSvd(), and MatrixBase< float >::MinSingularValue().

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

◆ Log()

void Log ( const MatrixBase< Real > &  src)

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

Referenced by MatrixBase< float >::ApplyLog(), and MatrixBase< float >::MinSingularValue().

2186  {
2187  KALDI_ASSERT(SameDim(*this, src));
2188  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2189  Real *row_data = data_;
2190  const Real *src_row_data = src.Data();
2191  for (MatrixIndexT row = 0; row < num_rows;
2192  row++,row_data += stride_, src_row_data += src.stride_) {
2193  for (MatrixIndexT col = 0; col < num_cols; col++)
2194  row_data[col] = kaldi::Log(src_row_data[col]);
2195  }
2196 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ LogDet()

Real LogDet ( Real *  det_sign = NULL) const

◆ LogSumExp()

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

Referenced by MatrixBase< float >::MinSingularValue(), and kaldi::UnitTestSimpleForMat().

2728  {
2729  Real sum;
2730  if (sizeof(sum) == 8) sum = kLogZeroDouble;
2731  else sum = kLogZeroFloat;
2732  Real max_elem = Max(), cutoff;
2733  if (sizeof(Real) == 4) cutoff = max_elem + kMinLogDiffFloat;
2734  else cutoff = max_elem + kMinLogDiffDouble;
2735  if (prune > 0.0 && max_elem - prune > cutoff) // explicit pruning...
2736  cutoff = max_elem - prune;
2737 
2738  double sum_relto_max_elem = 0.0;
2739 
2740  for (MatrixIndexT i = 0; i < num_rows_; i++) {
2741  for (MatrixIndexT j = 0; j < num_cols_; j++) {
2742  BaseFloat f = (*this)(i, j);
2743  if (f >= cutoff)
2744  sum_relto_max_elem += kaldi::Exp(f - max_elem);
2745  }
2746  }
2747  return max_elem + kaldi::Log(sum_relto_max_elem);
2748 }
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:813
static const double kMinLogDiffDouble
Definition: kaldi-math.h:124
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
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

◆ Max() [1/2]

Real Max ( ) const

Returns maximum element of matrix.

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

Referenced by MatrixBase< float >::ColRange(), AmSgmm2::ComponentPosteriors(), CompressedMatrix::ComputeGlobalHeader(), AmSgmm2::LogLikelihood(), MatrixBase< float >::Svd(), kaldi::UnitTestCuMatrixMax(), kaldi::UnitTestCuMatrixReduceMax(), kaldi::UnitTestMax2(), and kaldi::UnitTestMaxMin().

1717  {
1718  KALDI_ASSERT(num_rows_ > 0 && num_cols_ > 0);
1719  Real ans= *data_;
1720  for (MatrixIndexT r = 0; r < num_rows_; r++)
1721  for (MatrixIndexT c = 0; c < num_cols_; c++)
1722  if (data_[c + stride_*r] > ans)
1723  ans = data_[c + stride_*r];
1724  return ans;
1725 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Max() [2/2]

void Max ( const MatrixBase< Real > &  A)

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

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

1182  {
1183  KALDI_ASSERT(A.NumRows() == NumRows() && A.NumCols() == NumCols());
1184  for (MatrixIndexT row = 0; row < num_rows_; row++) {
1185  Real *row_data = RowData(row);
1186  const Real *other_row_data = A.RowData(row);
1187  MatrixIndexT num_cols = num_cols_;
1188  for (MatrixIndexT col = 0; col < num_cols; col++) {
1189  row_data[col] = std::max(row_data[col],
1190  other_row_data[col]);
1191  }
1192  }
1193 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ Min() [1/2]

Real Min ( ) const

Returns minimum element of matrix.

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

Referenced by MatrixBase< float >::ColRange(), CompressedMatrix::ComputeGlobalHeader(), MatrixBase< float >::Svd(), kaldi::UnitTestCuMatrixMin(), kaldi::UnitTestCuMatrixReduceMin(), kaldi::UnitTestMaxMin(), and Fmpe::Update().

1728  {
1729  KALDI_ASSERT(num_rows_ > 0 && num_cols_ > 0);
1730  Real ans= *data_;
1731  for (MatrixIndexT r = 0; r < num_rows_; r++)
1732  for (MatrixIndexT c = 0; c < num_cols_; c++)
1733  if (data_[c + stride_*r] < ans)
1734  ans = data_[c + stride_*r];
1735  return ans;
1736 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Min() [2/2]

void Min ( const MatrixBase< Real > &  A)

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

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

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

◆ MinSingularValue()

Real MinSingularValue ( ) const
inline

Returns smallest singular value.

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

430  {
431  Vector<Real> tmp(std::min(NumRows(), NumCols()));
432  Svd(&tmp);
433  return tmp.Min();
434  }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.

◆ MulColsVec()

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

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

1319  {
1320  KALDI_ASSERT(scale.Dim() == num_cols_);
1321  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1322  for (MatrixIndexT j = 0; j < num_cols_; j++) {
1323  Real this_scale = scale(j);
1324  (*this)(i, j) *= this_scale;
1325  }
1326  }
1327 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ MulElements()

void MulElements ( const MatrixBase< Real > &  A)

Element by element multiplication with a given matrix.

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

Referenced by MatrixBase< float >::ColRange(), 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().

1140  {
1141  KALDI_ASSERT(a.NumRows() == num_rows_ && a.NumCols() == num_cols_);
1142 
1143  if (num_cols_ == stride_ && num_cols_ == a.stride_) {
1144  mul_elements(num_rows_ * num_cols_, a.data_, data_);
1145  } else {
1146  MatrixIndexT a_stride = a.stride_, stride = stride_;
1147  Real *data = data_, *a_data = a.data_;
1148  for (MatrixIndexT i = 0; i < num_rows_; i++) {
1149  mul_elements(num_cols_, a_data, data);
1150  a_data += a_stride;
1151  data += stride;
1152  }
1153  }
1154 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ MulRowsGroupMat()

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

Referenced by MatrixBase< float >::ColRange(), kaldi::UnitTestCuMatrixDiffGroupPnorm(), and kaldi::UnitTestCuMatrixMulRowsGroupMat().

1238  {
1239  KALDI_ASSERT(src.NumRows() == this->NumRows() &&
1240  this->NumCols() % src.NumCols() == 0);
1241  int32 group_size = this->NumCols() / src.NumCols(),
1242  num_groups = this->NumCols() / group_size,
1243  num_rows = this->NumRows();
1244 
1245  for (MatrixIndexT i = 0; i < num_rows; i++) {
1246  Real *data = this->RowData(i);
1247  for (MatrixIndexT j = 0; j < num_groups; j++, data += group_size) {
1248  Real scale = src(i, j);
1249  cblas_Xscal(group_size, scale, data, 1);
1250  }
1251  }
1252 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
kaldi::int32 int32
int32 MatrixIndexT
Definition: matrix-common.h:98
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64

◆ MulRowsVec()

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

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

1224  {
1225  KALDI_ASSERT(scale.Dim() == num_rows_);
1226  MatrixIndexT M = num_rows_, N = num_cols_;
1227 
1228  for (MatrixIndexT i = 0; i < M; i++) {
1229  Real this_scale = scale(i);
1230  for (MatrixIndexT j = 0; j < N; j++) {
1231  (*this)(i, j) *= this_scale;
1232  }
1233  }
1234 }
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NumCols()

MatrixIndexT NumCols ( ) const
inline

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

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

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), DecodableMatrixMappedOffset::AcceptLoglikes(), IvectorExtractorUtteranceStats::AccStats(), Fmpe::AccStats(), OnlineIvectorEstimationStats::AccStats(), IvectorExtractorStats::AccStatsForUtterance(), KlHmm::Accumulate(), FmpeStats::AccumulateChecks(), kaldi::AccumulateTreeStats(), BatchedXvectorComputer::AddChunkToBatch(), MatrixBase< float >::AddCols(), VectorBase< float >::AddColSumMat(), VectorBase< float >::AddDiagMat2(), VectorBase< float >::AddDiagMatMat(), MatrixBase< float >::AddDiagVecMat(), SpMatrix< float >::AddMat2(), SpMatrix< float >::AddMat2Sp(), SpMatrix< float >::AddMat2Vec(), CuMatrixBase< float >::AddMatBlocks(), MatrixBase< float >::AddMatDiagVec(), MatrixBase< float >::AddMatMatElements(), MatrixBase< float >::AddMatSmat(), VectorBase< float >::AddMatSvec(), VectorBase< float >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< float >::AddRows(), VectorBase< float >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< float >::AddSmat2Sp(), MatrixBase< float >::AddSmatMat(), SparseMatrix< float >::AddToMat(), MatrixBase< float >::AddToRows(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), kaldi::nnet2::AppendDiscriminativeExamples(), OnlineDeltaInput::AppendFrames(), 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::AssertEqual(), kaldi::AssertSameDim(), kaldi::nnet2::AverageConstPart(), MleAmSgmm2Accs::Check(), OnlineIvectorExtractionInfo::Check(), IvectorExtractorStats::CheckDims(), kaldi::ComposeTransforms(), OnlineMatrixInput::Compute(), OnlineCmnInput::Compute(), OnlineLdaInput::Compute(), OnlineDeltaInput::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeDctMatrix(), kaldi::ComputeDeltas(), Fmpe::ComputeFeatures(), kaldi::ComputeFmllrLogDet(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), NnetUpdater::ComputeForMinibatch(), kaldi::ComputeGconsts(), CompressedMatrix::ComputeGlobalHeader(), OnlineCmnInput::ComputeInternal(), kaldi::ComputeLdaTransform(), kaldi::ComputePca(), Sgmm2Project::ComputeProjection(), kaldi::ComputeShiftedDeltas(), Fmpe::ComputeStddevs(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), VectorBase< float >::CopyColFromMat(), MatrixBase< float >::CopyCols(), VectorBase< float >::CopyColsFromMat(), VectorBase< float >::CopyDiagFromMat(), CompressedAffineXformStats::CopyFromAffineXformStats(), DiagGmm::CopyFromDiagGmm(), TpMatrix< float >::CopyFromMat(), SpMatrix< float >::CopyFromMat(), CompressedMatrix::CopyFromMat(), MatrixBase< float >::CopyFromMat(), CuMatrixBase< float >::CopyFromMat(), VectorBase< float >::CopyRowFromMat(), MatrixBase< float >::CopyRows(), CuVectorBase< float >::CopyRowsFromMat(), VectorBase< float >::CopyRowsFromMat(), DiagGmmNormal::CopyToDiagGmm(), FullGmmNormal::CopyToFullGmm(), CompressedMatrix::CopyToMat(), SparseMatrix< float >::CopyToMat(), CuMatrixBase< float >::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), kaldi::CreateEigenvalueMatrix(), CuMatrix< float >::CuMatrix(), DecodableMatrixMapped::DecodableMatrixMapped(), DecodableMatrixScaledMapped::DecodableMatrixScaledMapped(), NnetBatchDecoder::Decode(), OnlineMatrixInput::Dim(), FullGmm::Dim(), DiagGmmNormal::Dim(), DiagGmm::Dim(), MatrixBase< float >::DivElements(), DiscriminativeExampleSplitter::DoExcise(), LogisticRegression::DoStep(), SpMatrix< float >::Eig(), EigenvalueDecomposition< Real >::EigenvalueDecomposition(), FeatureTransformEstimateMulti::Estimate(), FeatureTransformEstimate::EstimateInternal(), FeatureTransformEstimateMulti::EstimateTransformPart(), kaldi::EstPca(), OnlineIvectorExtractionInfo::ExpectedFeatureDim(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), kaldi::FakeStatsForSomeDims(), kaldi::FilterMatrixRows(), IvectorExtractorStats::FlushCache(), kaldi::FmllrInnerUpdate(), FmllrRawAccs::FmllrRawAccs(), DiagGmm::GaussianSelection(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), OnlineTransform::GetFrame(), OnlineTransform::GetFrames(), NnetDiscriminativeUpdater::GetInputFeatures(), LogisticRegression::GetLogPosteriors(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), EigenvalueDecomposition< Real >::GetV(), MatrixBase< float >::GroupMax(), MatrixBase< float >::GroupMaxDeriv(), MatrixBase< float >::GroupPnorm(), MatrixBase< float >::GroupPnormDeriv(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), kaldi::IncreaseTransformDimension(), OnlinePreconditionerSimple::Init(), OnlineNaturalGradientSimple::Init(), OnlineFeaturePipeline::Init(), kaldi::InitGmmFromRandomFrames(), AmSgmm2::InitializeMw(), InitRand(), kaldi::InitRand(), kaldi::InitRandNonsingular(), PitchInterpolator::InitValues(), kaldi::LatticeAcousticRescore(), kaldi::nnet2::LatticeToDiscriminativeExample(), OnlineIvectorExtractorAdaptationState::LimitFrames(), AffineComponent::LimitRank(), DecodableMatrixScaled::LogLikelihood(), AmSgmm2::LogLikelihood(), DiagGmm::LogLikelihoods(), main(), Matrix< BaseFloat >::Matrix(), MatrixBase< float >::Max(), kaldi::nnet3::MergeTaskOutput(), MatrixBase< float >::Min(), MatrixBase< float >::MinSingularValue(), LogisticRegression::MixUp(), kaldi::nnet1::MomentStatistics(), MatrixBase< float >::MulElements(), MatrixBase< float >::MulRowsGroupMat(), kaldi::NonOrthogonality(), DecodableMatrixScaled::NumIndices(), OnlineLdaInput::OnlineLdaInput(), OnlineTransform::OnlineTransform(), CuMatrix< float >::operator=(), Matrix< BaseFloat >::operator=(), MatrixBase< float >::OrthogonalizeRows(), DiscriminativeExampleSplitter::OutputOneSplit(), kaldi::nnet3::PerturbImage(), 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(), FullGmm::Read(), MatrixBase< float >::Read(), Matrix< BaseFloat >::Read(), kaldi::ReadData(), KlHmm::ReadData(), kaldi::ReadHtk(), kaldi::cu::RegularizeL1(), ArbitraryResample::Resample(), FullGmmNormal::Resize(), FullGmm::Resize(), DiagGmmNormal::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), kaldi::ReverseFrames(), MatrixBase< float >::Row(), kaldi::SameDim(), LogisticRegression::ScalePriors(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), LinearTransform::SetLinearity(), MatrixBase< float >::SetMatMatDivMat(), FullGmm::SetMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), LinearVtln::SetTransform(), LogisticRegression::SetWeights(), CuMatrixBase< float >::SetZeroAboveDiag(), kaldi::SlidingWindowCmn(), kaldi::SlidingWindowCmnInternal(), OnlineCmvn::SmoothOnlineCmvnStats(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SortSvd(), kaldi::cu::Splice(), kaldi::SpliceFrames(), OnlineLdaInput::SpliceFrames(), NnetBatchComputer::SplitUtteranceIntoTasks(), SubVector< Real >::SubVector(), kaldi::TestCuFindRowMaxId(), kaldi::TestFmpe(), kaldi::TestIvectorExtraction(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineLdaInput(), kaldi::TestOnlineTransform(), SpMatrix< float >::TopEigs(), kaldi::TraceMatMat(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSmat(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), LogisticRegression::Train(), LogisticRegression::TrainParameters(), SpMatrix< float >::Tridiagonalize(), kaldi::TypeOneUsage(), kaldi::TypeOneUsageAverage(), kaldi::TypeThreeUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToRows(), UnitTestCompareWithDeltaFeatures(), kaldi::UnitTestCuDiffNormalizePerRow(), kaldi::UnitTestCuDiffSigmoid(), kaldi::UnitTestCuDiffTanh(), kaldi::UnitTestCuFindRowMaxId(), kaldi::UnitTestCuMathNormalizePerRow(), kaldi::UnitTestCuMathNormalizePerRow_v2(), 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(), SphinxMatrixHolder< kFeatDim >::Write(), kaldi::nnet3::WriteExamples(), kaldi::WriteHtk(), and kaldi::WriteSphinx().

67 { return num_cols_; }
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ NumRows()

MatrixIndexT NumRows ( ) const
inline

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

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

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsForPair(), kaldi::AccCmvnStatsWrapper(), DecodableMatrixMappedOffset::AcceptLoglikes(), BatchedXvectorComputer::AcceptUtterance(), CovarianceStats::AccStats(), IvectorExtractorUtteranceStats::AccStats(), Fmpe::AccStats(), IvectorExtractorStats::AccStatsForUtterance(), KlHmm::Accumulate(), FmpeStats::AccumulateChecks(), kaldi::AccumulateForUtterance(), kaldi::AccumulateTreeStats(), BatchedXvectorComputer::AddChunkToBatch(), MatrixBase< float >::AddCols(), VectorBase< float >::AddColSumMat(), MatrixRandomizer::AddData(), VectorBase< float >::AddDiagMat2(), VectorBase< float >::AddDiagMatMat(), MatrixBase< float >::AddDiagVecMat(), SpMatrix< float >::AddMat2(), SpMatrix< float >::AddMat2Sp(), SpMatrix< float >::AddMat2Vec(), CuMatrixBase< float >::AddMatBlocks(), MatrixBase< float >::AddMatDiagVec(), MatrixBase< float >::AddMatMatElements(), MatrixBase< float >::AddMatSmat(), VectorBase< float >::AddMatSvec(), VectorBase< float >::AddMatVec(), LdaEstimate::AddMeanOffset(), kaldi::AddOuterProductPlusMinus(), MatrixBase< float >::AddRows(), VectorBase< float >::AddRowSumMat(), PldaStats::AddSamples(), SpMatrix< float >::AddSmat2Sp(), MatrixBase< float >::AddSmatMat(), kaldi::AddToConfusionMatrix(), CuMatrixBase< float >::AddToElements(), SparseMatrix< float >::AddToMat(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), SingleUtteranceGmmDecoder::AdvanceDecoding(), AgglomerativeClusterer::AgglomerativeClusterer(), kaldi::nnet2::AppendDiscriminativeExamples(), OnlineDeltaInput::AppendFrames(), 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(), kaldi::ApplyModelTransformToStats(), kaldi::ApplyPca(), kaldi::ApplyPreXformToGradient(), Sgmm2Project::ApplyProjection(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), kaldi::ApplySoftMaxPerRow(), Plda::ApplyTransform(), kaldi::AssertEqual(), kaldi::AssertSameDim(), kaldi::CalBasisFmllrStepSize(), MleAmSgmm2Accs::Check(), OnlineIvectorExtractionInfo::Check(), IvectorExtractorStats::CheckDims(), IvectorExtractorStats::CommitStatsForM(), kaldi::ComposeTransforms(), OnlineMatrixInput::Compute(), OnlineCmnInput::Compute(), OnlineCacheInput::Compute(), OnlineLdaInput::Compute(), OnlineDeltaInput::Compute(), OnlineFeInput< E >::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeDctMatrix(), kaldi::ComputeDeltas(), Fmpe::ComputeFeatures(), kaldi::ComputeFmllrLogDet(), kaldi::ComputeFmllrMatrixDiagGmmOffset(), DecodableNnet2Online::ComputeForFrame(), NnetUpdater::ComputeForMinibatch(), kaldi::ComputeGconsts(), CompressedMatrix::ComputeGlobalHeader(), OnlineCmnInput::ComputeInternal(), kaldi::ComputeLdaTransform(), OnlineLdaInput::ComputeNextRemainder(), kaldi::ComputePca(), Sgmm2Project::ComputeProjection(), kaldi::ComputeShiftedDeltas(), Fmpe::ComputeStddevs(), LinearVtln::ComputeTransform(), BasisFmllrEstimate::ComputeTransform(), kaldi::ComputeVadEnergy(), kaldi::nnet3::time_height_convolution::ConvolveForwardSimple(), kaldi::cu::Copy(), VectorBase< float >::CopyColFromMat(), MatrixBase< float >::CopyCols(), VectorBase< float >::CopyColsFromMat(), VectorBase< float >::CopyDiagFromMat(), CompressedAffineXformStats::CopyFromAffineXformStats(), TpMatrix< float >::CopyFromMat(), SpMatrix< float >::CopyFromMat(), CompressedMatrix::CopyFromMat(), MatrixBase< float >::CopyFromMat(), CuMatrixBase< float >::CopyFromMat(), VectorBase< float >::CopyRowFromMat(), CuVectorBase< float >::CopyRowsFromMat(), VectorBase< float >::CopyRowsFromMat(), CompressedAffineXformStats::CopyToAffineXformStats(), CompressedMatrix::CopyToMat(), SparseMatrix< float >::CopyToMat(), CuMatrixBase< float >::CopyToMat(), GeneralMatrix::CopyToMat(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::cu::CpuComputeLstmNonlinearity(), kaldi::CreateEigenvalueMatrix(), CuMatrix< float >::CuMatrix(), DecodableAmSgmm2::DecodableAmSgmm2(), OnlineDeltaInput::DeltaComputation(), MatrixBase< float >::DivElements(), DecodableNnetSimple::DoNnetComputation(), LogisticRegression::DoStep(), SpMatrix< float >::Eig(), EigenvalueDecomposition< Real >::EigenvalueDecomposition(), DecodableNnetLoopedOnlineBase::EnsureFrameIsComputed(), DecodableNnetSimple::EnsureFrameIsComputed(), SingleUtteranceGmmDecoder::EstimateFmllr(), FeatureTransformEstimate::EstimateInternal(), kaldi::EstimateIvectorsOnline(), kaldi::EstPca(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), kaldi::FakeStatsForSomeDims(), kaldi::FilterMatrixRows(), kaldi::FmllrInnerUpdate(), FmllrRawAccs::FmllrRawAccs(), DiagGmm::GaussianSelection(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), OnlineCmvn::GetFrame(), OnlineFeatureInterface::GetFrames(), OnlineTransform::GetFrames(), kaldi::GetGmmLike(), NnetDiscriminativeUpdater::GetInputFeatures(), LogisticRegression::GetLogPosteriors(), OnlineFeatureMatrix::GetNextFeatures(), LogisticRegression::GetObjfAndGrad(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), DecodableNnetSimpleLooped::GetOutputForFrame(), OnlineCmvn::GetState(), LinearVtln::GetTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), EigenvalueDecomposition< Real >::GetV(), MatrixBase< float >::GroupMax(), MatrixBase< float >::GroupMaxDeriv(), MatrixBase< float >::GroupPnorm(), MatrixBase< float >::GroupPnormDeriv(), AmSgmm2::IncreasePhoneSpaceDim(), AmSgmm2::IncreaseSpkSpaceDim(), kaldi::IncreaseTransformDimension(), OnlineNaturalGradientSimple::Init(), OnlinePreconditionerSimple::Init(), OnlineFeaturePipeline::Init(), kaldi::InitGmmFromRandomFrames(), AmSgmm2::InitializeMw(), InitRand(), kaldi::InitRand(), kaldi::InitRandNonsingular(), PitchInterpolator::InitValues(), kaldi::LatticeAcousticRescore(), kaldi::nnet1::LatticeAcousticRescore(), kaldi::nnet2::LatticeToDiscriminativeExample(), OnlineIvectorExtractorAdaptationState::LimitFrames(), AffineComponent::LimitRank(), kaldi::LinearlyInterpolatePitch(), DecodableNnet2Online::LogLikelihood(), DecodableMatrixScaled::LogLikelihood(), DiagGmm::LogLikelihoods(), CuMatrixBase< float >::LogSoftMaxPerRow(), main(), Matrix< BaseFloat >::Matrix(), MatrixBase< float >::Max(), MatrixBase< float >::Min(), MatrixBase< float >::MinSingularValue(), LogisticRegression::MixUp(), kaldi::nnet1::MomentStatistics(), MatrixBase< float >::MulElements(), MatrixBase< float >::MulRowsGroupMat(), NnetDiscriminativeUpdater::NnetDiscriminativeUpdater(), NnetIo::NnetIo(), kaldi::NonOrthogonality(), DecodableAmDiagGmmRegtreeFmllr::NumFramesReady(), DecodableMatrixScaledMapped::NumFramesReady(), DecodableAmDiagGmmUnmapped::NumFramesReady(), DecodableAmSgmm2::NumFramesReady(), DecodableAmNnet::NumFramesReady(), DecodableAmDiagGmmRegtreeMllr::NumFramesReady(), DecodableAmNnetParallel::NumFramesReady(), DecodableMatrixScaled::NumFramesReady(), OnlineLdaInput::OnlineLdaInput(), OnlineTransform::OnlineTransform(), LimitRankClass::operator()(), CuMatrix< float >::operator=(), Matrix< BaseFloat >::operator=(), MatrixBase< float >::OrthogonalizeRows(), kaldi::nnet3::PerturbImage(), kaldi::PlaceNansInGaps(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), kaldi::nnet2::ProcessFile(), kaldi::nnet3::ProcessFile(), kaldi::ProcessPovFeatures(), kaldi::ProcessUtterance(), Sgmm2Project::ProjectVariance(), NnetDiscriminativeUpdater::Propagate(), KlHmm::PropagateFnc(), SpMatrix< float >::Qr(), FullGmmNormal::Rand(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::RandFullCova(), kaldi::unittest::RandFullGaussFeatures(), kaldi::RandZeroToOneMatrix(), AffineXformStats::Read(), LogisticRegression::Read(), MatrixBuffer::Read(), MatrixBase< float >::Read(), Matrix< BaseFloat >::Read(), kaldi::ReadData(), KlHmm::ReadData(), kaldi::ReadHtk(), kaldi::cu::RegularizeL1(), ArbitraryResample::Resample(), SingleUtteranceGmmDecoder::RescoringIsNeeded(), FullGmmNormal::Resize(), FullGmm::Resize(), DiagGmmNormal::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), kaldi::ReverseFrames(), DiscriminativeExampleSplitter::RightContext(), kaldi::nnet3::RunNnetComputation(), SingleUtteranceNnet2DecoderThreaded::RunNnetEvaluationInternal(), kaldi::RunPerSpeaker(), kaldi::SameDim(), LogisticRegression::ScalePriors(), FullGmm::SetInvCovarsAndMeans(), FullGmm::SetInvCovarsAndMeansInvCovars(), DiagGmm::SetInvVars(), DiagGmm::SetInvVarsAndMeans(), LinearTransform::SetLinearity(), MatrixBase< float >::SetMatMatDivMat(), FullGmm::SetMeans(), DiagGmm::SetMeans(), KlHmm::SetStats(), kaldi::SetToMovingAverage(), LinearVtln::SetTransform(), OnlineFeaturePipeline::SetTransform(), LogisticRegression::SetWeights(), CuMatrixBase< float >::SetZeroAboveDiag(), kaldi::ShiftFeatureMatrix(), kaldi::SlidingWindowCmn(), kaldi::SlidingWindowCmnInternal(), OnlineCmvn::SmoothOnlineCmvnStats(), CuMatrixBase< float >::SoftMaxPerRow(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SortSvd(), SparseMatrix< float >::SparseMatrix(), kaldi::cu::Splice(), kaldi::SpliceFrames(), OnlineLdaInput::SpliceFrames(), NnetBatchComputer::SplitUtteranceIntoTasks(), SpMatrix< float >::SpMatrix(), kaldi::SubtractMovingAverage(), kaldi::TakeLogOfPitch(), test_flags_driven_update(), test_io(), TestAmDiagGmmAccsIO(), TestComponentAcc(), kaldi::TestCuFindRowMaxId(), kaldi::TestFmpe(), kaldi::TestIvectorExtraction(), TestMllrAccsIO(), kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineCmnInput(), kaldi::TestOnlineLdaInput(), kaldi::TestOnlineMfcc(), kaldi::TestOnlinePlp(), kaldi::TestOnlineTransform(), TestSgmm2AccsIO(), TestSgmm2FmllrAccsIO(), TestSgmm2FmllrSubspace(), TestSgmm2PreXform(), TestXformMean(), SpMatrix< float >::TopEigs(), kaldi::TraceMatMat(), kaldi::TraceMatMatMat(), kaldi::TraceMatMatMatMat(), kaldi::TraceMatSmat(), kaldi::TraceMatSpMat(), kaldi::TraceMatSpMatSp(), kaldi::TraceSpMat(), LogisticRegression::Train(), kaldi::TrainOneIter(), LogisticRegression::TrainParameters(), kaldi::TransformIvectors(), OnlineLdaInput::TransformToOutput(), SpMatrix< float >::Tridiagonalize(), kaldi::TypeOneUsage(), kaldi::TypeOneUsageAverage(), kaldi::TypeThreeUsage(), 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::UnitTestCuMathNormalizePerRow_v2(), 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(), MleAmSgmm2Accs::Write(), SphinxMatrixHolder< kFeatDim >::Write(), kaldi::nnet3::WriteExamples(), kaldi::WriteHtk(), and kaldi::WriteSphinx().

64 { return num_rows_; }
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813

◆ operator()() [1/2]

Real& operator() ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

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

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

102  {
103  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
104  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
105  static_cast<UnsignedMatrixIndexT>(c) <
106  static_cast<UnsignedMatrixIndexT>(num_cols_));
107  return *(data_ + r * stride_ + c);
108  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ operator()() [2/2]

const Real operator() ( MatrixIndexT  r,
MatrixIndexT  c 
) const
inline

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

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

115  {
116  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
117  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
118  static_cast<UnsignedMatrixIndexT>(c) <
119  static_cast<UnsignedMatrixIndexT>(num_cols_));
120  return *(data_ + r * stride_ + c);
121  }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812

◆ OrthogonalizeRows()

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

Referenced by MatrixBase< float >::AddTpTp(), kaldi::ComputePca(), OnlineNaturalGradient::ReorthogonalizeRt1(), and OnlinePreconditioner::ReorthogonalizeXt1().

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

◆ Pow()

void Pow ( const MatrixBase< Real > &  src,
Real  power 
)

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

Referenced by MatrixBase< float >::ApplyPow(), and MatrixBase< float >::MinSingularValue().

2128  {
2129  KALDI_ASSERT(SameDim(*this, src));
2130  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2131  Real *row_data = data_;
2132  const Real *src_row_data = src.Data();
2133  for (MatrixIndexT row = 0; row < num_rows;
2134  row++,row_data += stride_, src_row_data += src.stride_) {
2135  for (MatrixIndexT col = 0; col < num_cols; col++) {
2136  row_data[col] = pow(src_row_data[col], power);
2137  }
2138  }
2139 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ PowAbs()

void PowAbs ( const MatrixBase< Real > &  src,
Real  power,
bool  include_sign = false 
)

Apply power to the absolute value of each element.

If include_sign is true, the result will be multiplied with the sign of the input value. If the power is negative and the input to the power is zero, The output will be set zero. If include_sign is true, it will multiply the result by the sign of the input.

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

Referenced by MatrixBase< float >::ApplyPowAbs(), and MatrixBase< float >::MinSingularValue().

2142  {
2143  KALDI_ASSERT(SameDim(*this, src));
2144  MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2145  Real *row_data = data_;
2146  const Real *src_row_data = src.Data();
2147  for (MatrixIndexT row = 0; row < num_rows;
2148  row++,row_data += stride_, src_row_data += src.stride_) {
2149  for (MatrixIndexT col = 0; col < num_cols; col ++) {
2150  if (include_sign == true && src_row_data[col] < 0) {
2151  row_data[col] = -pow(std::abs(src_row_data[col]), power);
2152  } else {
2153  row_data[col] = pow(std::abs(src_row_data[col]), power);
2154  }
2155  }
2156  }
2157 }
MatrixIndexT stride_
< Number of rows
Definition: kaldi-matrix.h:816
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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:813
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Power()

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

Referenced by MatrixBase< float >::ApplyLog(), and kaldi::UnitTestNonsymmetricPower().

2232  {
2233  KALDI_ASSERT(num_rows_ > 0 &&