packed-matrix.h
Go to the documentation of this file.
1 // matrix/packed-matrix.h
2 
3 // Copyright 2009-2013 Ondrej Glembek; Lukas Burget; Microsoft Corporation;
4 // Saarland University; Yanmin Qian;
5 // Johns Hopkins University (Author: Daniel Povey)
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 
13 // http://www.apache.org/licenses/LICENSE-2.0
14 
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #ifndef KALDI_MATRIX_PACKED_MATRIX_H_
23 #define KALDI_MATRIX_PACKED_MATRIX_H_
24 
25 #include "matrix/matrix-common.h"
26 #include <algorithm>
27 
28 namespace kaldi {
29 
31 // we need to declare the friend << operator here
32 template<typename Real>
33 std::ostream & operator <<(std::ostream & out, const PackedMatrix<Real>& M);
34 
35 
38 
40 template<typename Real> class PackedMatrix {
41  friend class CuPackedMatrix<Real>;
42  public:
43  //friend class CuPackedMatrix<Real>;
44 
45  PackedMatrix() : data_(NULL), num_rows_(0) {}
46 
47  explicit PackedMatrix(MatrixIndexT r, MatrixResizeType resize_type = kSetZero):
48  data_(NULL) { Resize(r, resize_type); }
49 
50  explicit PackedMatrix(const PackedMatrix<Real> &orig) : data_(NULL) {
51  Resize(orig.num_rows_, kUndefined);
52  CopyFromPacked(orig);
53  }
54 
55  template<typename OtherReal>
56  explicit PackedMatrix(const PackedMatrix<OtherReal> &orig) : data_(NULL) {
57  Resize(orig.NumRows(), kUndefined);
58  CopyFromPacked(orig);
59  }
60 
61  void SetZero();
62  void SetUnit();
63  void SetRandn();
64 
65  Real Trace() const;
66 
67  // Needed for inclusion in std::vector
69  Resize(other.NumRows());
70  CopyFromPacked(other);
71  return *this;
72  }
73 
75  Destroy();
76  }
77 
85  void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero);
86 
87  void AddToDiag(const Real r); // Adds r to diaginal
88 
89  void ScaleDiag(const Real alpha); // Scales diagonal by alpha.
90 
91  void SetDiag(const Real alpha); // Sets diagonal to this value.
92 
93  template<typename OtherReal>
94  void CopyFromPacked(const PackedMatrix<OtherReal> &orig);
95 
99  template<typename OtherReal>
100  void CopyFromVec(const SubVector<OtherReal> &orig);
101 
102  Real* Data() { return data_; }
103  const Real* Data() const { return data_; }
104  inline MatrixIndexT NumRows() const { return num_rows_; }
105  inline MatrixIndexT NumCols() const { return num_rows_; }
106  size_t SizeInBytes() const {
107  size_t nr = static_cast<size_t>(num_rows_);
108  return ((nr * (nr+1)) / 2) * sizeof(Real);
109  }
110 
111  //MatrixIndexT Stride() const { return stride_; }
112 
113  // This code is duplicated in child classes to avoid extra levels of calls.
115  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
116  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
117  static_cast<UnsignedMatrixIndexT>(c) <
118  static_cast<UnsignedMatrixIndexT>(num_rows_)
119  && c <= r);
120  return *(data_ + (r * (r + 1)) / 2 + c);
121  }
122 
123  // This code is duplicated in child classes to avoid extra levels of calls.
125  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
126  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
127  static_cast<UnsignedMatrixIndexT>(c) <
128  static_cast<UnsignedMatrixIndexT>(num_rows_)
129  && c <= r);
130  return *(data_ + (r * (r + 1)) / 2 + c);
131  }
132 
133  Real Max() const {
134  KALDI_ASSERT(num_rows_ > 0);
135  return * (std::max_element(data_, data_ + ((num_rows_*(num_rows_+1))/2) ));
136  }
137 
138  Real Min() const {
139  KALDI_ASSERT(num_rows_ > 0);
140  return * (std::min_element(data_, data_ + ((num_rows_*(num_rows_+1))/2) ));
141  }
142 
143  void Scale(Real c);
144 
145  friend std::ostream & operator << <> (std::ostream & out,
146  const PackedMatrix<Real> &m);
147  // Use instead of stream<<*this, if you want to add to existing contents.
148  // Will throw exception on failure.
149  void Read(std::istream &in, bool binary, bool add = false);
150 
151  void Write(std::ostream &out, bool binary) const;
152 
153  void Destroy();
154 
156  void Swap(PackedMatrix<Real> *other);
157  void Swap(Matrix<Real> *other);
158 
159 
160  protected:
161  // Will only be called from this class or derived classes.
162  void AddPacked(const Real alpha, const PackedMatrix<Real>& M);
163  Real *data_;
165  //MatrixIndexT stride_;
166  private:
171  void Init(MatrixIndexT dim);
172 
173 };
175 
176 
179 
180 template<typename Real>
181 std::ostream & operator << (std::ostream & os, const PackedMatrix<Real>& M) {
182  M.Write(os, false);
183  return os;
184 }
185 
186 template<typename Real>
187 std::istream & operator >> (std::istream &is, PackedMatrix<Real> &M) {
188  M.Read(is, false);
189  return is;
190 }
191 
193 
194 } // namespace kaldi
195 
196 #endif
197 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CopyFromVec(const SubVector< OtherReal > &orig)
CopyFromVec just interprets the vector as having the same layout as the packed matrix.
PackedMatrix(MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
Definition: packed-matrix.h:47
MatrixResizeType
Definition: matrix-common.h:37
Real Trace() const
< Set to random values of a normal distribution
void Scale(Real c)
void Read(std::istream &in, bool binary, bool add=false)
void Write(std::ostream &out, bool binary) const
const Real * Data() const
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
A class for storing matrices.
Definition: kaldi-matrix.h:823
void ScaleDiag(const Real alpha)
void SetUnit()
< Set to zero
MatrixIndexT NumRows() const
MatrixIndexT num_rows_
void SetRandn()
< Set to unit matrix.
void AddToDiag(const Real r)
MatrixIndexT NumCols() const
PackedMatrix(const PackedMatrix< OtherReal > &orig)
Definition: packed-matrix.h:56
int32 MatrixIndexT
Definition: matrix-common.h:98
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
void Swap(PackedMatrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
void SetDiag(const Real alpha)
Real operator()(MatrixIndexT r, MatrixIndexT c) const
PackedMatrix(const PackedMatrix< Real > &orig)
Definition: packed-matrix.h:50
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
PackedMatrix< Real > & operator=(const PackedMatrix< Real > &other)
Definition: packed-matrix.h:68
void Init(MatrixIndexT dim)
Init assumes the current contents of the class are is invalid (i.e.
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
size_t SizeInBytes() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Matrix for CUDA computing.
Definition: matrix-common.h:75
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).