tp-matrix.h
Go to the documentation of this file.
1 // matrix/tp-matrix.h
2 
3 // Copyright 2009-2011 Ondrej Glembek; Lukas Burget; Microsoft Corporation;
4 // Saarland University; Yanmin Qian; Haihua Xu
5 // 2013 Johns Hopkins Universith (author: Daniel Povey)
6 
7 
8 // See ../../COPYING for clarification regarding multiple authors
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 
14 // http://www.apache.org/licenses/LICENSE-2.0
15 
16 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19 // MERCHANTABLITY OR NON-INFRINGEMENT.
20 // See the Apache 2 License for the specific language governing permissions and
21 // limitations under the License.
22 #ifndef KALDI_MATRIX_TP_MATRIX_H_
23 #define KALDI_MATRIX_TP_MATRIX_H_
24 
25 
26 #include "matrix/packed-matrix.h"
27 
28 namespace kaldi {
31 
32 template<typename Real> class TpMatrix;
33 
35 template<typename Real>
36 class TpMatrix : public PackedMatrix<Real> {
37  friend class CuTpMatrix<float>;
38  friend class CuTpMatrix<double>;
39  public:
40  TpMatrix() : PackedMatrix<Real>() {}
41  explicit TpMatrix(MatrixIndexT r, MatrixResizeType resize_type = kSetZero)
42  : PackedMatrix<Real>(r, resize_type) {}
43  TpMatrix(const TpMatrix<Real>& orig) : PackedMatrix<Real>(orig) {}
44 
47  explicit TpMatrix(const CuTpMatrix<Real> &cu);
48 
49 
50  template<typename OtherReal> explicit TpMatrix(const TpMatrix<OtherReal>& orig)
51  : PackedMatrix<Real>(orig) {}
52 
54  if (static_cast<UnsignedMatrixIndexT>(c) >
55  static_cast<UnsignedMatrixIndexT>(r)) {
56  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(c) <
57  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
58  return 0;
59  }
60  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
61  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
62  // c<=r now so don't have to check c.
63  return *(this->data_ + (r*(r+1)) / 2 + c);
64  // Duplicating code from PackedMatrix.h
65  }
66 
68  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
69  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
70  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(c) <=
71  static_cast<UnsignedMatrixIndexT>(r) &&
72  "you cannot access the upper triangle of TpMatrix using "
73  "a non-const matrix object.");
74  return *(this->data_ + (r*(r+1)) / 2 + c);
75  // Duplicating code from PackedMatrix.h
76  }
77  // Note: Cholesky may throw KaldiFatalError.
78  void Cholesky(const SpMatrix<Real>& orig);
79 
80  void Invert();
81 
82  // Inverts in double precision.
83  void InvertDouble() {
84  TpMatrix<double> dmat(*this);
85  dmat.Invert();
86  (*this).CopyFromTp(dmat);
87  }
88 
90  void Swap(TpMatrix<Real> *other);
91 
93  Real Determinant();
94 
97  void CopyFromMat(const MatrixBase<Real> &M,
99 
101  void CopyFromMat(const CuTpMatrix<Real> &other);
102 
104  void CopyFromTp(const TpMatrix<Real> &other) {
106  }
107 
108  template<typename OtherReal> void CopyFromTp(const TpMatrix<OtherReal> &other) {
110  }
111 
113  void AddTp(const Real alpha, const TpMatrix<Real> &M) {
114  this->AddPacked(alpha, M);
115  }
116 
119  return *this;
120  }
121 
123 
124  void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero) {
125  PackedMatrix<Real>::Resize(nRows, resize_type);
126  }
127 };
128 
130 
131 } // namespace kaldi
132 
133 
134 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Packed symetric matrix class.
Definition: matrix-common.h:62
MatrixResizeType
Definition: matrix-common.h:37
Real operator()(MatrixIndexT r, MatrixIndexT c) const
Definition: tp-matrix.h:53
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Definition: tp-matrix.h:124
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
TpMatrix(MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
Definition: tp-matrix.h:41
void AddTp(const Real alpha, const TpMatrix< Real > &M)
AddTp does *this += alpha * M.
Definition: tp-matrix.h:113
MatrixIndexT num_rows_
Real Determinant()
Returns the determinant of the matrix (product of diagonals)
Definition: tp-matrix.cc:71
void CopyFromTp(const TpMatrix< OtherReal > &other)
Definition: tp-matrix.h:108
TpMatrix(const TpMatrix< OtherReal > &orig)
Definition: tp-matrix.h:50
void Cholesky(const SpMatrix< Real > &orig)
Definition: tp-matrix.cc:88
int32 MatrixIndexT
Definition: matrix-common.h:98
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
Packed symetric matrix class.
Definition: matrix-common.h:63
TpMatrix< Real > & operator=(const TpMatrix< Real > &other)
Definition: tp-matrix.h:117
void CopyFromTp(const TpMatrix< Real > &other)
CopyFromTp copies another triangular matrix into this one.
Definition: tp-matrix.h:104
PackedMatrix< Real > & operator=(const PackedMatrix< Real > &other)
Definition: packed-matrix.h:68
TpMatrix(const TpMatrix< Real > &orig)
Definition: tp-matrix.h:43
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixTransposeType
Definition: matrix-common.h:32
void CopyFromMat(const MatrixBase< Real > &M, MatrixTransposeType Trans=kNoTrans)
CopyFromMat copies the lower triangle of M into *this (or the upper triangle, if Trans == kTrans)...
Definition: tp-matrix.cc:117
void InvertDouble()
Definition: tp-matrix.h:83
void Swap(TpMatrix< Real > *other)
Shallow swap.
Definition: tp-matrix.cc:81
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).