24 #ifndef KALDI_MATRIX_KALDI_MATRIX_H_ 25 #define KALDI_MATRIX_KALDI_MATRIX_H_ 1 37 template<
typename Real>
38 Real
TraceMatMat(
const MatrixBase<Real> &A,
const MatrixBase<Real> &B,
48 template<
typename Real>
79 inline const Real*
Data()
const {
89 static_cast<UnsignedMatrixIndexT>(
num_rows_));
96 static_cast<UnsignedMatrixIndexT>(
num_rows_));
104 static_cast<UnsignedMatrixIndexT>(
num_rows_) &&
105 static_cast<UnsignedMatrixIndexT>(c) <
106 static_cast<UnsignedMatrixIndexT>(
num_cols_));
117 static_cast<UnsignedMatrixIndexT>(
num_rows_) &&
118 static_cast<UnsignedMatrixIndexT>(c) <
119 static_cast<UnsignedMatrixIndexT>(
num_cols_));
140 template<
typename OtherReal>
148 template<
typename OtherReal>
152 template<
typename OtherReal>
157 template<
typename OtherReal>
170 template<
typename OtherReal>
190 static_cast<UnsignedMatrixIndexT>(
num_rows_));
197 static_cast<UnsignedMatrixIndexT>(
num_rows_));
207 col_offset, num_cols);
222 Real
Trace(
bool check_square =
true)
const;
237 void Scale(Real alpha);
258 Real
LogDet(Real *det_sign = NULL)
const;
263 void Invert(Real *log_det = NULL, Real *det_sign = NULL,
264 bool inverse_needed =
true);
269 void InvertDouble(Real *LogDet = NULL, Real *det_sign = NULL,
270 bool inverse_needed =
true);
306 void CopyRows(
const Real *
const *src);
324 void AddRows(Real alpha,
const Real *
const *src);
331 void AddToRows(Real alpha, Real *
const *dst)
const;
342 this ->
Pow(*
this, power);
347 this ->
PowAbs(*
this, power, include_sign);
355 this ->
Floor(*
this, floor_val);
359 this ->
Ceiling(*
this, ceiling_val);
371 this ->
ExpLimited(*
this, lower_limit, upper_limit);
403 bool Power(Real pow);
453 bool IsUnit(Real cutoff = 1.0e-05)
const;
456 bool IsZero(Real cutoff = 1.0e-05)
const;
575 Real check_thresh = 0.001);
579 #if defined(__NVCC__) && defined(_MSC_VER) 580 template<
typename Real>
589 void Add(
const Real alpha);
595 template<
typename OtherReal>
600 template<
typename OtherReal>
604 template<
typename OtherReal>
655 template<
typename OtherReal>
705 return AddMatMat(alpha, M, transA, B, transB, beta);
730 return AddMatMat(alpha, A, transA, M, transB, beta);
739 return AddMatMat(alpha, M, transA, N, transB, beta);
763 void Read(std::istream & in,
bool binary,
bool add =
false);
765 void Write(std::ostream & out,
bool binary)
const;
768 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD) 822 template<
typename Real>
833 MatrixBase<Real>() { Resize(r, c, resize_type, stride_type); }
837 template<
typename OtherReal>
857 template<
typename OtherReal>
863 template<
typename OtherReal>
873 template <
typename OtherReal>
887 void Read(std::istream & in,
bool binary,
bool add =
false);
967 template<
typename Real>
971 template<
typename Real>
975 template<
typename Real>
987 template<
typename Real>
1028 template<
typename Real>
1034 template<
typename Real>
1041 template <
typename Real>
1046 template <
typename Real>
1052 template <
typename Real>
1074 bool sort_on_absolute_value =
true);
1082 template<
typename Real>
1090 template<
typename Real>
1099 template<
typename Real>
1100 std::ostream & operator << (std::ostream & Out, const MatrixBase<Real> & M);
1102 template<
typename Real>
1106 template<
typename Real>
1110 template<
typename Real>
1127 #endif // KALDI_MATRIX_KALDI_MATRIX_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void Add(const Real alpha)
Add a scalar to each element.
void InvertDouble(Real *LogDet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse [double].
Real Min() const
Returns minimum element of matrix.
void AddCols(const MatrixBase< Real > &src, const MatrixIndexT *indices)
Add column indices[r] of src to column r.
Real TraceMatMatMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC)
Returns tr(A B C)
Packed symetric matrix class.
void Write(std::ostream &out, bool binary) const
write to stream.
Real LargestAbsElem() const
largest absolute value.
Real * Data()
Gives pointer to raw data (non-const).
void CopyColFromVec(const VectorBase< Real > &v, const MatrixIndexT col)
Copy vector into specific column of matrix.
void CopyDiagFromVec(const VectorBase< Real > &v)
Copy vector into diagonal of matrix.
void Tanh(const MatrixBase< Real > &src)
Set each element to the tanh of the corresponding element of "src".
bool IsDiagonal(Real cutoff=1.0e-05) const
Returns true if matrix is Diagonal.
const Real * RowData(MatrixIndexT i) const
Returns pointer to data for one row (const)
void Floor(const MatrixBase< Real > &src, Real floor_val)
bool Power(Real pow)
The Power method attempts to take the matrix to a power using a method that works in general for frac...
void SymAddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA, Real beta)
*this = beta * *this + alpha * M M^T, for symmetric matrices.
void DiffTanh(const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
MatrixIndexT stride_
< Number of rows
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...
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 ...
void Svd(VectorBase< Real > *s) const
Compute SVD but only retain the singular values.
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.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Real Cond() const
Returns condition number by computing Svd.
double TraceMat(const MatrixBase< Real > &A)
Returns trace of matrix.
Base class which provides matrix operations not involving resizing or allocation. ...
const Real * Data() const
Gives pointer to raw data (const).
void Exp(const MatrixBase< Real > &src)
Real MinSingularValue() const
Returns smallest singular value.
Matrix(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Basic constructor.
Real * data_
data memory area
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
Real Max() const
Returns maximum element of matrix.
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
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...
Real Trace(bool check_square=true) const
Returns trace of matrix.
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
bool WriteSphinx(std::ostream &os, const MatrixBase< Real > &M)
void CopyUpperToLower()
Copy upper triangle to lower triangle (symmetrize)
void GroupPnormDeriv(const MatrixBase< Real > &input, const MatrixBase< Real > &output, Real power)
Calculate derivatives for the GroupPnorm function above...
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
void LapackGesvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
void AddToDiag(const Real alpha)
Add a scalar to each diagonal element.
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 ...
Real * Data_workaround() const
A workaround that allows SubMatrix to get a pointer to non-const data for const Matrix.
Matrix(const SpMatrix< OtherReal > &M)
Copy constructor taking SpMatrix...
A class for storing matrices.
void Read(std::istream &in, bool binary, bool add=false)
stream read.
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void DivElements(const MatrixBase< Real > &A)
Divide each element by the corresponding element of a given matrix.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
MatrixIndexT NumRows() const
void PowAbs(const MatrixBase< Real > &src, Real power, bool include_sign=false)
Apply power to the absolute value of each element.
void SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
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() an...
void SetRandUniform()
Sets to numbers uniformly distributed on (0, 1)
void AddSpSp(const Real alpha, const SpMatrix< Real > &A, const SpMatrix< Real > &B, const Real beta)
this <– beta*this + alpha*A*B.
void DestructiveSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= Nu...
Matrix(const TpMatrix< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy constructor taking TpMatrix...
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
void CopyFromSp(const SpMatrix< OtherReal > &M)
Copy given spmatrix. (no resize is done).
void Ceiling(const MatrixBase< Real > &src, Real ceiling_val)
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).
void TestUninitialized() const
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)
SubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
bool IsZero(Real cutoff=1.0e-05) const
Returns true if matrix is all zeros.
void SoftHinge(const MatrixBase< Real > &src)
Set each element to y = log(1 + exp(x))
void CopyToRows(Real *const *dst) const
Copies row r of this matrix to the array of floats at the location given by dst[r].
MatrixIndexT NumCols() const
bool IsSymmetric(Real cutoff=1.0e-05) const
Returns true if matrix is Symmetric.
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
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.
void Log(const MatrixBase< Real > &src)
Packed matrix: base class for triangular and symmetric matrices.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void Pow(const MatrixBase< Real > &src, Real power)
void ExpSpecial(const MatrixBase< Real > &src)
For each element x of the matrix, set it to (x < 0 ? exp(x) : x + 1).
void Scale(Real alpha)
Multiply each element with a scalar value.
SubVector< Real > Row(MatrixIndexT i)
Return specific row of matrix.
MatrixIndexT num_rows_
< Number of columns
MatrixBase(Real *data, MatrixIndexT cols, MatrixIndexT rows, MatrixIndexT stride)
Initializer, callable only from child.
Real & Index(MatrixIndexT r, MatrixIndexT c)
Indexing operator, provided for ease of debugging (gdb doesn't work with parenthesis operator)...
void AddVecToRows(const Real alpha, const VectorBase< OtherReal > &v)
[each row of *this] += alpha * v
void Transpose()
Transpose the matrix.
void AddMatSp(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const Real beta)
this <– beta*this + alpha*A*B.
void CopyFromTp(const TpMatrix< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given tpmatrix. (no resize is done).
void SetRandn()
Sets to random values of a normal distribution.
void CopyCols(const MatrixBase< Real > &src, const MatrixIndexT *indices)
Copies column r from column indices[r] of src.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
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 ...
void Heaviside(const MatrixBase< Real > &src)
Sets each element to the Heaviside step function (x > 0 ? 1 : 0) of the corresponding element in "src...
#define KALDI_PARANOID_ASSERT(cond)
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.
Packed symetric matrix class.
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
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.
This class is used for a piece of a CuMatrix.
Real Sum() const
Returns sum of all elements in matrix.
void SetZero()
Sets matrix to zero.
void ApplyCeiling(Real ceiling_val)
void MulElements(const MatrixBase< Real > &A)
Element by element multiplication with a given matrix.
KALDI_DISALLOW_COPY_AND_ASSIGN(MatrixBase)
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}.
void AddRows(Real alpha, const MatrixBase< Real > &src, const MatrixIndexT *indexes)
Does for each row r, this.Row(r) += alpha * src.row(indexes[r]).
void MulColsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = (*this) * diag(scale).
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
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.
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).
Matrix for CUDA computing.
void InvertElements()
Inverts all the elements of the matrix.
MatrixBase()
Initializer, callable only from child.
void DiffSigmoid(const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
SubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
A class representing a vector.
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.
void AddSmat(Real alpha, const SparseMatrix< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A [or A^T].
void AddVecToCols(const Real alpha, const VectorBase< OtherReal > &v)
[each col of *this] += alpha * v
#define KALDI_ASSERT(cond)
Real & operator()(MatrixIndexT r, MatrixIndexT c)
Indexing operator, non-const (only checks sizes if compiled with -DKALDI_PARANOID) ...
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
Real LogDet(Real *det_sign=NULL) const
Returns logdet of matrix.
size_t SizeInBytes() const
Returns size in bytes of the data held by the matrix.
void CreateEigenvalueMatrix(const VectorBase< Real > &re, const VectorBase< Real > &im, MatrixBase< Real > *D)
Creates the eigenvalue matrix D that is part of the decomposition used Matrix::Eig.
Real ApplySoftMax()
Apply soft-max to the collection of all elements of the matrix and return normalizer (log sum of expo...
void AddSp(const Real alpha, const SpMatrix< OtherReal > &S)
*this += alpha * S
Matrix for CUDA computing.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
void CopyRowFromVec(const VectorBase< Real > &v, const MatrixIndexT row)
Copy vector into specific row of matrix.
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.
bool Equal(const MatrixBase< Real > &other) const
Tests for exact equality. It's usually preferable to use ApproxEqual.
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
void OrthogonalizeRows()
This function orthogonalizes the rows of a matrix using the Gram-Schmidt process. ...
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].
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
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 fun...
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.
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) ...
Provides a vector abstraction class.
void ApplyFloor(Real floor_val)
bool IsUnit(Real cutoff=1.0e-05) const
Returns true if the matrix is all zeros, except for ones on diagonal.
void ApplyPow(Real power)
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
~Matrix()
Distructor to free matrices.
void ApplyExpLimited(Real lower_limit, Real upper_limit)
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) dep...
void AddSpMat(const Real alpha, const SpMatrix< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
this <– beta*this + alpha*SpA*B.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
Sub-matrix representation.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void CopyColsFromVec(const VectorBase< Real > &v)
Copies vector into matrix, column-by-column.
bool AttemptComplexPower(Real *x_re, Real *x_im, Real power)
The following function is used in Matrix::Power, and separately tested, so we declare it here mainly ...
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
void CopyLowerToUpper()
Copy lower triangle to upper triangle (symmetrize)
void Set(Real)
Sets all elements to a specific value.
Vector for CUDA computing.
void ApplyPowAbs(Real power, bool include_sign=false)
Real TraceMatMatMatMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const MatrixBase< Real > &D, MatrixTransposeType transD)
Returns tr(A B C D)
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)
void Sigmoid(const MatrixBase< Real > &src)
Set each element to the sigmoid of the corresponding element of "src".