TpMatrix< Real > Class Template Reference

Packed symetric matrix class. More...

#include <matrix-common.h>

Inheritance diagram for TpMatrix< Real >:
Collaboration diagram for TpMatrix< Real >:

Public Member Functions

 TpMatrix ()
 
 TpMatrix (MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
 
 TpMatrix (const TpMatrix< Real > &orig)
 
 TpMatrix (const CuTpMatrix< Real > &cu)
 Copy constructor from CUDA TpMatrix This is defined in ../cudamatrix/cu-tp-matrix.cc. More...
 
template<typename OtherReal >
 TpMatrix (const TpMatrix< OtherReal > &orig)
 
Real operator() (MatrixIndexT r, MatrixIndexT c) const
 
Real & operator() (MatrixIndexT r, MatrixIndexT c)
 
void Cholesky (const SpMatrix< Real > &orig)
 
void Invert ()
 
void InvertDouble ()
 
void Swap (TpMatrix< Real > *other)
 Shallow swap. More...
 
Real Determinant ()
 Returns the determinant of the matrix (product of diagonals) More...
 
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). More...
 
void CopyFromMat (const CuTpMatrix< Real > &other)
 This is implemented in ../cudamatrix/cu-tp-matrix.cc. More...
 
void CopyFromTp (const TpMatrix< Real > &other)
 CopyFromTp copies another triangular matrix into this one. More...
 
template<typename OtherReal >
void CopyFromTp (const TpMatrix< OtherReal > &other)
 
void AddTp (const Real alpha, const TpMatrix< Real > &M)
 AddTp does *this += alpha * M. More...
 
TpMatrix< Real > & operator= (const TpMatrix< Real > &other)
 
void Resize (MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
 
- Public Member Functions inherited from PackedMatrix< Real >
 PackedMatrix ()
 
 PackedMatrix (MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
 
 PackedMatrix (const PackedMatrix< Real > &orig)
 
template<typename OtherReal >
 PackedMatrix (const PackedMatrix< OtherReal > &orig)
 
void SetZero ()
 
void SetUnit ()
 < Set to zero More...
 
void SetRandn ()
 < Set to unit matrix. More...
 
Real Trace () const
 < Set to random values of a normal distribution More...
 
PackedMatrix< Real > & operator= (const PackedMatrix< Real > &other)
 
 ~PackedMatrix ()
 
void Resize (MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
 Set packed matrix to a specified size (can be zero). More...
 
void AddToDiag (const Real r)
 
void ScaleDiag (const Real alpha)
 
void SetDiag (const Real alpha)
 
template<typename OtherReal >
void CopyFromPacked (const PackedMatrix< OtherReal > &orig)
 
template<typename OtherReal >
void CopyFromVec (const SubVector< OtherReal > &orig)
 CopyFromVec just interprets the vector as having the same layout as the packed matrix. More...
 
Real * Data ()
 
const Real * Data () const
 
MatrixIndexT NumRows () const
 
MatrixIndexT NumCols () const
 
size_t SizeInBytes () const
 
Real operator() (MatrixIndexT r, MatrixIndexT c) const
 
Real & operator() (MatrixIndexT r, MatrixIndexT c)
 
Real Max () const
 
Real Min () const
 
void Scale (Real c)
 
void Read (std::istream &in, bool binary, bool add=false)
 
void Write (std::ostream &out, bool binary) const
 
void Destroy ()
 
void Swap (PackedMatrix< Real > *other)
 Swaps the contents of *this and *other. Shallow swap. More...
 
void Swap (Matrix< Real > *other)
 

Friends

class CuTpMatrix< float >
 
class CuTpMatrix< double >
 

Additional Inherited Members

- Protected Member Functions inherited from PackedMatrix< Real >
void AddPacked (const Real alpha, const PackedMatrix< Real > &M)
 
- Protected Attributes inherited from PackedMatrix< Real >
Real * data_
 
MatrixIndexT num_rows_
 

Detailed Description

template<typename Real>
class kaldi::TpMatrix< Real >

Packed symetric matrix class.

Definition at line 63 of file matrix-common.h.

Constructor & Destructor Documentation

◆ TpMatrix() [1/5]

TpMatrix ( )
inline

Definition at line 40 of file tp-matrix.h.

Referenced by TpMatrix< float >::TpMatrix().

40 : PackedMatrix<Real>() {}

◆ TpMatrix() [2/5]

TpMatrix ( MatrixIndexT  r,
MatrixResizeType  resize_type = kSetZero 
)
inlineexplicit

Definition at line 41 of file tp-matrix.h.

42  : PackedMatrix<Real>(r, resize_type) {}

◆ TpMatrix() [3/5]

TpMatrix ( const TpMatrix< Real > &  orig)
inline

Definition at line 43 of file tp-matrix.h.

43 : PackedMatrix<Real>(orig) {}

◆ TpMatrix() [4/5]

TpMatrix ( const CuTpMatrix< Real > &  cu)
explicit

Copy constructor from CUDA TpMatrix This is defined in ../cudamatrix/cu-tp-matrix.cc.

Definition at line 114 of file cu-tp-matrix.cc.

114  {
115  this->Resize(cu.NumRows());
116  this->CopyFromMat(cu);
117 }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Definition: tp-matrix.h:124
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

◆ TpMatrix() [5/5]

TpMatrix ( const TpMatrix< OtherReal > &  orig)
inlineexplicit

Definition at line 50 of file tp-matrix.h.

51  : PackedMatrix<Real>(orig) {}

Member Function Documentation

◆ AddTp()

void AddTp ( const Real  alpha,
const TpMatrix< Real > &  M 
)
inline

AddTp does *this += alpha * M.

Definition at line 113 of file tp-matrix.h.

113  {
114  this->AddPacked(alpha, M);
115  }
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)

◆ Cholesky()

void Cholesky ( const SpMatrix< Real > &  orig)

Definition at line 88 of file tp-matrix.cc.

Referenced by SpMatrix< float >::ApplyFloor(), CuMatrixBase< float >::Cholesky(), kaldi::CholeskyUnitTestTr(), IvectorExtractorStats::CommitStatsForW(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), Sgmm2Project::ComputeLdaTransform(), kaldi::ComputeNormalizingTransform(), LdaEstimate::Estimate(), BasisFmllrEstimate::EstimateFmllrBasis(), FeatureTransformEstimate::EstimateInternal(), SpMatrix< float >::IsPosDef(), SpMatrix< float >::LogPosDefDet(), main(), TpMatrix< float >::operator()(), FullGmm::Perturb(), CompressedAffineXformStats::PrepareOneG(), rand_posdef_spmatrix(), kaldi::unittest::RandPosdefSpMatrix(), RandPosdefSpMatrix(), kaldi::RandPosdefSpMatrix(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeRt1(), OnlinePreconditioner::ReorthogonalizeXt1(), kaldi::SolveDoubleQuadraticMatrixProblem(), FullGmm::Split(), CuMatrixBase< float >::SymInvertPosDef(), kaldi::UnitTestCuTpMatrixCholesky(), kaldi::UnitTestPldaEstimation(), and PldaUnsupervisedAdaptor::UpdatePlda().

88  {
89  KALDI_ASSERT(orig.NumRows() == this->NumRows());
90  MatrixIndexT n = this->NumRows();
91  this->SetZero();
92  Real *data = this->data_, *jdata = data; // start of j'th row of matrix.
93  const Real *orig_jdata = orig.Data(); // start of j'th row of matrix.
94  for (MatrixIndexT j = 0; j < n; j++, jdata += j, orig_jdata += j) {
95  Real *kdata = data; // start of k'th row of matrix.
96  Real d(0.0);
97  for (MatrixIndexT k = 0; k < j; k++, kdata += k) {
98  Real s = cblas_Xdot(k, kdata, 1, jdata, 1);
99  // (*this)(j, k) = s = (orig(j, k) - s)/(*this)(k, k);
100  jdata[k] = s = (orig_jdata[k] - s)/kdata[k];
101  d = d + s*s;
102  }
103  // d = orig(j, j) - d;
104  d = orig_jdata[j] - d;
105 
106  if (d >= 0.0) {
107  // (*this)(j, j) = std::sqrt(d);
108  jdata[j] = std::sqrt(d);
109  } else {
110  KALDI_ERR << "Cholesky decomposition failed. Maybe matrix "
111  "is not positive definite.";
112  }
113  }
114 }
MatrixIndexT NumRows() const
float cblas_Xdot(const int N, const float *const X, const int incX, const float *const Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:98
struct rnnlm::@11::@12 n
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromMat() [1/2]

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 at line 117 of file tp-matrix.cc.

Referenced by TpMatrix< float >::CopyFromMat(), TpMatrix< float >::InvertDouble(), kaldi::UnitTestCuTpMatrixCopyFromMat(), and kaldi::UnitTestSymAddMat2().

118  {
119  if (Trans == kNoTrans) {
120  KALDI_ASSERT(this->NumRows() == M.NumRows() && M.NumRows() == M.NumCols());
121  MatrixIndexT D = this->NumRows();
122  const Real *in_i = M.Data();
123  MatrixIndexT stride = M.Stride();
124  Real *out_i = this->data_;
125  for (MatrixIndexT i = 0; i < D; i++, in_i += stride, out_i += i)
126  for (MatrixIndexT j = 0; j <= i; j++)
127  out_i[j] = in_i[j];
128  } else {
129  KALDI_ASSERT(this->NumRows() == M.NumRows() && M.NumRows() == M.NumCols());
130  MatrixIndexT D = this->NumRows();
131  const Real *in_i = M.Data();
132  MatrixIndexT stride = M.Stride();
133  Real *out_i = this->data_;
134  for (MatrixIndexT i = 0; i < D; i++, in_i++, out_i += i) {
135  for (MatrixIndexT j = 0; j <= i; j++)
136  out_i[j] = in_i[stride*j];
137  }
138  }
139 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromMat() [2/2]

void CopyFromMat ( const CuTpMatrix< Real > &  other)

This is implemented in ../cudamatrix/cu-tp-matrix.cc.

Definition at line 122 of file cu-tp-matrix.cc.

122  {
123  other.CopyToPacked(this);
124 }

◆ CopyFromTp() [1/2]

void CopyFromTp ( const TpMatrix< Real > &  other)
inline

CopyFromTp copies another triangular matrix into this one.

Definition at line 104 of file tp-matrix.h.

Referenced by FeatureTransformEstimate::EstimateInternal(), and kaldi::UnitTestTpInvert().

104  {
106  }
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)

◆ CopyFromTp() [2/2]

void CopyFromTp ( const TpMatrix< OtherReal > &  other)
inline

Definition at line 108 of file tp-matrix.h.

108  {
110  }
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)

◆ Determinant()

Real Determinant ( )

Returns the determinant of the matrix (product of diagonals)

Definition at line 71 of file tp-matrix.cc.

Referenced by TpMatrix< float >::InvertDouble().

71  {
72  double det = 1.0;
73  for (MatrixIndexT i = 0; i<this->NumRows(); i++) {
74  det *= (*this)(i, i);
75  }
76  return static_cast<Real>(det);
77 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:98

◆ Invert()

void Invert ( )

Definition at line 31 of file tp-matrix.cc.

Referenced by SpMatrix< float >::ApplyFloor(), CuMatrixBase< float >::Cholesky(), kaldi::CholeskyUnitTestTr(), Sgmm2Project::ComputeLdaTransform(), kaldi::ComputeNormalizingTransform(), TpMatrix< float >::Invert(), TpMatrix< float >::InvertDouble(), main(), TpMatrix< float >::operator()(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeRt1(), OnlinePreconditioner::ReorthogonalizeXt1(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::UnitTestPldaEstimation(), kaldi::UnitTestTpInvert(), and PldaUnsupervisedAdaptor::UpdatePlda().

31  {
32  // these are CLAPACK types
33  KaldiBlasInt result;
34  KaldiBlasInt rows = static_cast<int>(this->num_rows_);
35 
36  // clapack call
37  // NOTE: Even though "U" is for upper, lapack assumes column-wise storage
38  // of the data. We have a row-wise storage, therefore, we need to "invert"
39  clapack_Xtptri(&rows, this->data_, &result);
40 
41  if (result < 0) {
42  KALDI_ERR << "Call to CLAPACK stptri_ function failed";
43  } else if (result > 0) {
44  KALDI_ERR << "Matrix is singular";
45  }
46 }
MatrixIndexT num_rows_
void clapack_Xtptri(KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *result)
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ InvertDouble()

void InvertDouble ( )
inline

Definition at line 83 of file tp-matrix.h.

Referenced by kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), and BasisFmllrEstimate::EstimateFmllrBasis().

83  {
84  TpMatrix<double> dmat(*this);
85  dmat.Invert();
86  (*this).CopyFromTp(dmat);
87  }

◆ operator()() [1/2]

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

Definition at line 53 of file tp-matrix.h.

53  {
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  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ operator()() [2/2]

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

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

67  {
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  }
MatrixIndexT num_rows_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ operator=()

TpMatrix<Real>& operator= ( const TpMatrix< Real > &  other)
inline

Definition at line 117 of file tp-matrix.h.

117  {
119  return *this;
120  }
PackedMatrix< Real > & operator=(const PackedMatrix< Real > &other)
Definition: packed-matrix.h:68

◆ Resize()

void Resize ( MatrixIndexT  nRows,
MatrixResizeType  resize_type = kSetZero 
)
inline

Definition at line 124 of file tp-matrix.h.

Referenced by FeatureTransformEstimate::EstimateInternal().

124  {
125  PackedMatrix<Real>::Resize(nRows, resize_type);
126  }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).

◆ Swap()

void Swap ( TpMatrix< Real > *  other)

Shallow swap.

Definition at line 81 of file tp-matrix.cc.

Referenced by TpMatrix< float >::InvertDouble().

81  {
82  std::swap(this->data_, other->data_);
83  std::swap(this->num_rows_, other->num_rows_);
84 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
MatrixIndexT num_rows_

Friends And Related Function Documentation

◆ CuTpMatrix< double >

friend class CuTpMatrix< double >
friend

Definition at line 38 of file tp-matrix.h.

◆ CuTpMatrix< float >

friend class CuTpMatrix< float >
friend

Definition at line 37 of file tp-matrix.h.


The documentation for this class was generated from the following files: