21 #ifndef KALDI_MATRIX_SP_MATRIX_H_ 22 #define KALDI_MATRIX_SP_MATRIX_H_ 34 template<
typename Real>
class SpMatrix;
40 template<
typename Real>
41 class SpMatrix :
public PackedMatrix<Real> {
45 friend class std::vector<
Matrix<Real> >;
60 template<
typename OtherReal>
89 template<
typename OtherReal>
97 #else // different default arg if non-paranoid mode. 105 if (static_cast<UnsignedMatrixIndexT>(c) >
106 static_cast<UnsignedMatrixIndexT>(r))
110 static_cast<UnsignedMatrixIndexT>(this->
num_rows_));
111 return *(this->
data_ + (r*(r+1)) / 2 + c);
116 if (static_cast<UnsignedMatrixIndexT>(c) >
117 static_cast<UnsignedMatrixIndexT>(r))
121 static_cast<UnsignedMatrixIndexT>(this->
num_rows_));
122 return *(this->
data_ + (r * (r + 1)) / 2 + c);
136 void Invert(Real *logdet = NULL, Real *det_sign= NULL,
137 bool inverse_needed =
true);
141 void InvertDouble(Real *logdet = NULL, Real *det_sign = NULL,
142 bool inverse_needed =
true);
161 Real tolerance = 0.001)
const;
207 KALDI_LOG <<
"PrintEigs: " << name <<
": " << s;
221 Real
LogDet(Real *det_sign = NULL)
const;
224 template<
typename OtherReal>
236 template<
typename OtherReal>
254 const Real beta = 0.0);
260 const Real beta = 0.0);
270 const Real beta = 0.0);
287 const Real beta = 0.0);
299 bool verbose =
false);
309 bool IsUnit(Real cutoff = 1.0e-05)
const;
310 bool IsZero(Real cutoff = 1.0e-05)
const;
336 (*this).CopyFromSp(dmat);
360 Real tolerance,
int recurse)
const;
374 template<
typename Real>
377 return A.ApproxEqual(B, tol);
380 template<
typename Real>
389 template<
typename Real,
typename OtherReal>
397 template<
typename Real>
403 template<
typename Real>
408 template<
typename Real>
415 template<
typename Real>
424 template<
typename Real>
451 K(1.0e+4), eps(1.0e-40), name(name),
452 optimize_delta(true), diagonal_precondition(false),
453 print_debug_output(true) { }
455 optimize_delta(true), diagonal_precondition(false),
456 print_debug_output(true) { }
468 template<
typename Real>
486 template<
typename Real>
497 template<
typename Real>
517 #endif // KALDI_MATRIX_SP_MATRIX_H_ void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void AddVec2Sp(const Real alpha, const VectorBase< Real > &v, const SpMatrix< Real > &S, const Real beta)
Does *this = beta * *thi + alpha * diag(v) * S * diag(v)
bool IsUnit(Real cutoff=1.0e-05) const
bool IsDiagonal(Real cutoff=1.0e-05) const
Packed symetric matrix class.
SpMatrix(const SpMatrix< OtherReal > &orig)
This class describes the options for maximizing various quadratic objective functions.
double SolveQuadraticProblem(const SpMatrix< double > &H, const VectorBase< double > &g, const SolverOptions &opts, VectorBase< double > *x)
bool ApproxEqual(const SpMatrix< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol*(*this).FrobeniusNorma()
void Qr(MatrixBase< Real > *Q)
The symmetric QR algorithm.
void AddVecVec(const Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &w)
rank-two update, this <– this + alpha (v w' + w v').
void AddMat2Vec(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const VectorBase< Real > &v, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * diag(v) * M^T.
Real Cond() const
Returns condition number by computing Svd.
Real SolveQuadraticMatrixProblem(const SpMatrix< Real > &Q, const MatrixBase< Real > &Y, const SpMatrix< Real > &SigmaInv, const SolverOptions &opts, MatrixBase< Real > *M)
Maximizes the auxiliary function : Like a numerically stable version of .
Base class which provides matrix operations not involving resizing or allocation. ...
SolverOptions(const std::string &name)
SpMatrix(const SpMatrix< Real > &orig)
void InvertDouble(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
Real FrobeniusNorm() const
sqrt of sum of square elements.
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
Real TraceSpMat(const SpMatrix< Real > &A, const MatrixBase< Real > &B)
Returns tr(A B).
SpMatrix(const MatrixBase< Real > &orig, SpCopyType copy_type=kTakeMean)
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void ApplyPow(Real exponent)
Takes matrix to a fraction power via Svd.
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
A class for storing matrices.
bool diagonal_precondition
bool IsTridiagonal(Real cutoff=1.0e-05) const
MatrixIndexT NumRows() const
Real SolveDoubleQuadraticMatrixProblem(const MatrixBase< Real > &G, const SpMatrix< Real > &P1, const SpMatrix< Real > &P2, const SpMatrix< Real > &Q1, const SpMatrix< Real > &Q2, const SolverOptions &opts, MatrixBase< Real > *M)
Maximizes the auxiliary function : Encountered in matrix update with a prior.
void CopyFromSp(const SpMatrix< Real > &other)
void Tridiagonalize(MatrixBase< Real > *Q)
Tridiagonalize the matrix with an orthogonal transformation.
void AddTp2Sp(const Real alpha, const TpMatrix< Real > &T, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
The following function does: this <– beta*this + alpha * T * A * T^T.
void PrintEigs(const char *name)
int ApplyFloor(const SpMatrix< Real > &Floor, Real alpha=1.0, bool verbose=false)
Floors this symmetric matrix to the matrix alpha * Floor, where the matrix Floor is positive definite...
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v'
void AddSmat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
This is a version of AddMat2Sp specialized for when M is fairly sparse.
Real operator()(MatrixIndexT r, MatrixIndexT c) const
Real LogPosDefDet() const
Computes log determinant but only for +ve-def matrices (it uses Cholesky).
Packed matrix: base class for triangular and symmetric matrices.
Real TraceMatSpMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const MatrixBase< Real > &C, MatrixTransposeType transC)
Returns tr(A B C) (A and C may be transposed as specified by transA and transC).
MatrixIndexT LimitCond(Real maxCond=1.0e+5, bool invert=false)
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
SpMatrix(MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
void AddTp2(const Real alpha, const TpMatrix< Real > &T, MatrixTransposeType transM, const Real beta=0.0)
The following function does: this <– beta*this + alpha * T * T^T.
void CopyFromSp(const SpMatrix< OtherReal > &other)
void TopEigs(VectorBase< Real > *s, MatrixBase< Real > *P, MatrixIndexT lanczos_dim=0) const
This function gives you, approximately, the largest eigenvalues of the symmetric matrix and the corre...
double TraceSpSp(const SpMatrix< double > &A, const SpMatrix< double > &B)
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Real TraceSpSpLower(const SpMatrix< Real > &A, const SpMatrix< Real > &B)
Packed symetric matrix class.
Real TraceMatSpMatSp(const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const MatrixBase< Real > &C, MatrixTransposeType transC, const SpMatrix< Real > &D)
Returns tr (A B C D) (A and C may be transposed as specified by transA and transB).
void Swap(SpMatrix *other)
Shallow swap.
void CopyFromMat(const MatrixBase< Real > &orig, SpCopyType copy_type=kTakeMean)
PackedMatrix< Real > & operator=(const PackedMatrix< Real > &other)
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
A class representing a vector.
void EigInternal(VectorBase< Real > *s, MatrixBase< Real > *P, Real tolerance, int recurse) const
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void AddMat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * A * M^T.
Real Cond() const
Returns maximum ratio of singular values.
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
void SymPosSemiDefEig(VectorBase< Real > *s, MatrixBase< Real > *P, Real tolerance=0.001) const
This is the version of SVD that we implement for symmetric positive definite matrices.
Provides a vector abstraction class.
void Invert(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
bool IsZero(Real cutoff=1.0e-05) const
SpMatrix< Real > & operator=(const SpMatrix< Real > &other)
Real MaxAbsEig() const
Returns the maximum of the absolute values of any of the eigenvalues.
void AddDiagVec(const Real alpha, const VectorBase< OtherReal > &v)
diagonal update, this <– this + diag(v)
Real LogDet(Real *det_sign=NULL) const
MatrixIndexT LimitCondDouble(Real maxCond=1.0e+5, bool invert=false)
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).