All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 61 of file matrix-common.h.

Constructor & Destructor Documentation

TpMatrix ( )
inline

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

40 : PackedMatrix<Real>() {}
TpMatrix ( MatrixIndexT  r,
MatrixResizeType  resize_type = kSetZero 
)
inlineexplicit

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

42  : PackedMatrix<Real>(r, resize_type) {}
TpMatrix ( const TpMatrix< Real > &  orig)
inline

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

43 : PackedMatrix<Real>(orig) {}
TpMatrix ( const CuTpMatrix< Real > &  cu)
explicit

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

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

References CuPackedMatrix< Real >::NumRows().

112  {
113  this->Resize(cu.NumRows());
114  this->CopyFromMat(cu);
115 }
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:118
TpMatrix ( const TpMatrix< OtherReal > &  orig)
inlineexplicit

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

51  : PackedMatrix<Real>(orig) {}

Member Function Documentation

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)
void Cholesky ( const SpMatrix< Real > &  orig)

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

References kaldi::cblas_Xdot(), rnnlm::d, PackedMatrix< Real >::Data(), data_, rnnlm::j, KALDI_ASSERT, KALDI_WARN, rnnlm::n, and PackedMatrix< Real >::NumRows().

Referenced by SpMatrix< Real >::ApplyFloor(), CuMatrixBase< Real >::Cholesky(), kaldi::CholeskyUnitTestTr(), IvectorExtractorStats::CommitStatsForW(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), Sgmm2Project::ComputeLdaTransform(), kaldi::ComputeNormalizingTransform(), FastNnetCombiner::ComputePreconditioner(), LdaEstimate::Estimate(), BasisFmllrEstimate::EstimateFmllrBasis(), FeatureTransformEstimate::EstimateInternal(), SpMatrix< Real >::IsPosDef(), SpMatrix< Real >::LogPosDefDet(), main(), FullGmm::Perturb(), CompressedAffineXformStats::PrepareOneG(), rand_posdef_spmatrix(), kaldi::unittest::RandPosdefSpMatrix(), RandPosdefSpMatrix(), kaldi::RandPosdefSpMatrix(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeXt1(), OnlinePreconditioner::ReorthogonalizeXt1(), kaldi::SolveDoubleQuadraticMatrixProblem(), FullGmm::Split(), CuMatrixBase< Real >::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_WARN << "Cholesky decomposition failed. Maybe matrix "
111  "is not positive definite. Throwing error";
112  throw std::runtime_error("Cholesky decomposition failed.");
113  }
114  }
115 }
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:96
struct rnnlm::@11::@12 n
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 118 of file tp-matrix.cc.

References MatrixBase< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

Referenced by kaldi::UnitTestCuTpMatrixCopyFromMat(), and kaldi::UnitTestSymAddMat2().

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

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

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

References CuPackedMatrix< Real >::CopyToPacked().

120  {
121  other.CopyToPacked(this);
122 }
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 Fmpe::ComputeC(), FeatureTransformEstimate::EstimateInternal(), and kaldi::UnitTestTpInvert().

104  {
106  }
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void CopyFromTp ( const TpMatrix< OtherReal > &  other)
inline

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

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

Returns the determinant of the matrix (product of diagonals)

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

References rnnlm::i.

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:96
void Invert ( )

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

References kaldi::clapack_Xtptri(), data_, and KALDI_ERR.

Referenced by SpMatrix< Real >::ApplyFloor(), CuMatrixBase< Real >::Cholesky(), kaldi::CholeskyUnitTestTr(), Sgmm2Project::ComputeLdaTransform(), kaldi::ComputeNormalizingTransform(), FastNnetCombiner::ComputePreconditioner(), TpMatrix< BaseFloat >::InvertDouble(), main(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeXt1(), 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 }
void clapack_Xtptri(KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *result)
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT num_rows_
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  }
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:169
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:169
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
void Resize ( MatrixIndexT  nRows,
MatrixResizeType  resize_type = kSetZero 
)
inline

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

Referenced by Fmpe::ComputeC(), FastNnetCombiner::ComputePreconditioner(), and 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).
void Swap ( TpMatrix< Real > *  other)

Shallow swap.

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

References data_, PackedMatrix< Real >::data_, PackedMatrix< Real >::num_rows_, and kaldi::swap().

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

friend class CuTpMatrix< double >
friend

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

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: