kaldi-matrix.h
Go to the documentation of this file.
1 // matrix/kaldi-matrix.h
2 
3 // Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation; Lukas Burget;
4 // Saarland University; Petr Schwarz; Yanmin Qian;
5 // Karel Vesely; Go Vivace Inc.; Haihua Xu
6 // 2017 Shiyin Kang
7 // 2019 Yiwen Shao
8 
9 // See ../../COPYING for clarification regarding multiple authors
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
19 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
20 // MERCHANTABLITY OR NON-INFRINGEMENT.
21 // See the Apache 2 License for the specific language governing permissions and
22 // limitations under the License.
23 
24 #ifndef KALDI_MATRIX_KALDI_MATRIX_H_
25 #define KALDI_MATRIX_KALDI_MATRIX_H_ 1
26 
27 #include <algorithm>
28 
29 #include "matrix/matrix-common.h"
30 
31 namespace kaldi {
32 
34 
37 template<typename Real>
38 Real TraceMatMat(const MatrixBase<Real> &A, const MatrixBase<Real> &B,
41 
44 
48 template<typename Real>
49 class MatrixBase {
50  public:
51  // so this child can access protected members of other instances.
52  friend class Matrix<Real>;
53  // friend declarations for CUDA matrices (see ../cudamatrix/)
54  friend class CuMatrixBase<Real>;
55  friend class CuMatrix<Real>;
56  friend class CuSubMatrix<Real>;
57  friend class CuPackedMatrix<Real>;
58  friend class PackedMatrix<Real>;
59  friend class SparseMatrix<Real>;
60  friend class SparseMatrix<float>;
61  friend class SparseMatrix<double>;
62 
64  inline MatrixIndexT NumRows() const { return num_rows_; }
65 
67  inline MatrixIndexT NumCols() const { return num_cols_; }
68 
70  inline MatrixIndexT Stride() const { return stride_; }
71 
73  size_t SizeInBytes() const {
74  return static_cast<size_t>(num_rows_) * static_cast<size_t>(stride_) *
75  sizeof(Real);
76  }
77 
79  inline const Real* Data() const {
80  return data_;
81  }
82 
84  inline Real* Data() { return data_; }
85 
87  inline Real* RowData(MatrixIndexT i) {
88  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
89  static_cast<UnsignedMatrixIndexT>(num_rows_));
90  return data_ + i * stride_;
91  }
92 
94  inline const Real* RowData(MatrixIndexT i) const {
95  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
96  static_cast<UnsignedMatrixIndexT>(num_rows_));
97  return data_ + i * stride_;
98  }
99 
102  inline Real& operator() (MatrixIndexT r, MatrixIndexT c) {
103  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
104  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
105  static_cast<UnsignedMatrixIndexT>(c) <
106  static_cast<UnsignedMatrixIndexT>(num_cols_));
107  return *(data_ + r * stride_ + c);
108  }
111  Real &Index (MatrixIndexT r, MatrixIndexT c) { return (*this)(r, c); }
112 
115  inline const Real operator() (MatrixIndexT r, MatrixIndexT c) const {
116  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
117  static_cast<UnsignedMatrixIndexT>(num_rows_) &&
118  static_cast<UnsignedMatrixIndexT>(c) <
119  static_cast<UnsignedMatrixIndexT>(num_cols_));
120  return *(data_ + r * stride_ + c);
121  }
122 
123  /* Basic setting-to-special values functions. */
124 
126  void SetZero();
128  void Set(Real);
130  void SetUnit();
132  void SetRandn();
134  void SetRandUniform();
135 
136  /* Copying functions. These do not resize the matrix! */
137 
138 
140  template<typename OtherReal>
141  void CopyFromMat(const MatrixBase<OtherReal> & M,
142  MatrixTransposeType trans = kNoTrans);
143 
145  void CopyFromMat(const CompressedMatrix &M);
146 
148  template<typename OtherReal>
149  void CopyFromSp(const SpMatrix<OtherReal> &M);
150 
152  template<typename OtherReal>
153  void CopyFromTp(const TpMatrix<OtherReal> &M,
154  MatrixTransposeType trans = kNoTrans);
155 
157  template<typename OtherReal>
158  void CopyFromMat(const CuMatrixBase<OtherReal> &M,
159  MatrixTransposeType trans = kNoTrans);
160 
165  void CopyRowsFromVec(const VectorBase<Real> &v);
166 
168  void CopyRowsFromVec(const CuVectorBase<Real> &v);
169 
170  template<typename OtherReal>
171  void CopyRowsFromVec(const VectorBase<OtherReal> &v);
172 
176  void CopyColsFromVec(const VectorBase<Real> &v);
177 
179  void CopyColFromVec(const VectorBase<Real> &v, const MatrixIndexT col);
181  void CopyRowFromVec(const VectorBase<Real> &v, const MatrixIndexT row);
183  void CopyDiagFromVec(const VectorBase<Real> &v);
184 
185  /* Accessing of sub-parts of the matrix. */
186 
188  inline const SubVector<Real> Row(MatrixIndexT i) const {
189  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
190  static_cast<UnsignedMatrixIndexT>(num_rows_));
191  return SubVector<Real>(data_ + (i * stride_), NumCols());
192  }
193 
196  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
197  static_cast<UnsignedMatrixIndexT>(num_rows_));
198  return SubVector<Real>(data_ + (i * stride_), NumCols());
199  }
200 
202  inline SubMatrix<Real> Range(const MatrixIndexT row_offset,
203  const MatrixIndexT num_rows,
204  const MatrixIndexT col_offset,
205  const MatrixIndexT num_cols) const {
206  return SubMatrix<Real>(*this, row_offset, num_rows,
207  col_offset, num_cols);
208  }
209  inline SubMatrix<Real> RowRange(const MatrixIndexT row_offset,
210  const MatrixIndexT num_rows) const {
211  return SubMatrix<Real>(*this, row_offset, num_rows, 0, num_cols_);
212  }
213  inline SubMatrix<Real> ColRange(const MatrixIndexT col_offset,
214  const MatrixIndexT num_cols) const {
215  return SubMatrix<Real>(*this, 0, num_rows_, col_offset, num_cols);
216  }
217 
218  /* Various special functions. */
220  Real Sum() const;
222  Real Trace(bool check_square = true) const;
223  // If check_square = true, will crash if matrix is not square.
224 
226  Real Max() const;
228  Real Min() const;
229 
231  void MulElements(const MatrixBase<Real> &A);
232 
234  void DivElements(const MatrixBase<Real> &A);
235 
237  void Scale(Real alpha);
238 
240  void Max(const MatrixBase<Real> &A);
242  void Min(const MatrixBase<Real> &A);
243 
246  void MulColsVec(const VectorBase<Real> &scale);
247 
250  void MulRowsVec(const VectorBase<Real> &scale);
251 
255  void MulRowsGroupMat(const MatrixBase<Real> &src);
256 
258  Real LogDet(Real *det_sign = NULL) const;
259 
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);
271 
273  void InvertElements();
274 
278  void Transpose();
279 
284  void CopyCols(const MatrixBase<Real> &src,
285  const MatrixIndexT *indices);
286 
291  void CopyRows(const MatrixBase<Real> &src,
292  const MatrixIndexT *indices);
293 
299  void AddCols(const MatrixBase<Real> &src,
300  const MatrixIndexT *indices);
301 
306  void CopyRows(const Real *const *src);
307 
312  void CopyToRows(Real *const *dst) const;
313 
317  void AddRows(Real alpha,
318  const MatrixBase<Real> &src,
319  const MatrixIndexT *indexes);
320 
324  void AddRows(Real alpha, const Real *const *src);
325 
331  void AddToRows(Real alpha, Real *const *dst) const;
332 
337  void AddToRows(Real alpha,
338  const MatrixIndexT *indexes,
339  MatrixBase<Real> *dst) const;
340 
341  inline void ApplyPow(Real power) {
342  this -> Pow(*this, power);
343  }
344 
345 
346  inline void ApplyPowAbs(Real power, bool include_sign=false) {
347  this -> PowAbs(*this, power, include_sign);
348  }
349 
350  inline void ApplyHeaviside() {
351  this -> Heaviside(*this);
352  }
353 
354  inline void ApplyFloor(Real floor_val) {
355  this -> Floor(*this, floor_val);
356  }
357 
358  inline void ApplyCeiling(Real ceiling_val) {
359  this -> Ceiling(*this, ceiling_val);
360  }
361 
362  inline void ApplyExp() {
363  this -> Exp(*this);
364  }
365 
366  inline void ApplyExpSpecial() {
367  this -> ExpSpecial(*this);
368  }
369 
370  inline void ApplyExpLimited(Real lower_limit, Real upper_limit) {
371  this -> ExpLimited(*this, lower_limit, upper_limit);
372  }
373 
374  inline void ApplyLog() {
375  this -> Log(*this);
376  }
377 
392  void Eig(MatrixBase<Real> *P,
393  VectorBase<Real> *eigs_real,
394  VectorBase<Real> *eigs_imag) const;
395 
403  bool Power(Real pow);
404 
417  MatrixBase<Real> *Vt); // Destroys calling matrix.
418 
424  MatrixBase<Real> *Vt) const;
426  void Svd(VectorBase<Real> *s) const { Svd(s, NULL, NULL); }
427 
428 
430  Real MinSingularValue() const {
431  Vector<Real> tmp(std::min(NumRows(), NumCols()));
432  Svd(&tmp);
433  return tmp.Min();
434  }
435 
436  void TestUninitialized() const; // This function is designed so that if any element
437  // if the matrix is uninitialized memory, valgrind will complain.
438 
441  Real Cond() const;
442 
444  bool IsSymmetric(Real cutoff = 1.0e-05) const; // replace magic number
445 
447  bool IsDiagonal(Real cutoff = 1.0e-05) const; // replace magic number
448 
453  bool IsUnit(Real cutoff = 1.0e-05) const; // replace magic number
454 
456  bool IsZero(Real cutoff = 1.0e-05) const; // replace magic number
457 
460  Real FrobeniusNorm() const;
461 
464  bool ApproxEqual(const MatrixBase<Real> &other, float tol = 0.01) const;
465 
467  bool Equal(const MatrixBase<Real> &other) const;
468 
470  Real LargestAbsElem() const; // largest absolute value.
471 
477  Real LogSumExp(Real prune = -1.0) const;
478 
481  Real ApplySoftMax();
482 
484  void Sigmoid(const MatrixBase<Real> &src);
485 
490  void Heaviside(const MatrixBase<Real> &src);
491 
492  void Exp(const MatrixBase<Real> &src);
493 
494  void Pow(const MatrixBase<Real> &src, Real power);
495 
496  void Log(const MatrixBase<Real> &src);
497 
504  void PowAbs(const MatrixBase<Real> &src, Real power, bool include_sign=false);
505 
506  void Floor(const MatrixBase<Real> &src, Real floor_val);
507 
508  void Ceiling(const MatrixBase<Real> &src, Real ceiling_val);
509 
513  void ExpSpecial(const MatrixBase<Real> &src);
514 
519  void ExpLimited(const MatrixBase<Real> &src, Real lower_limit, Real upper_limit);
520 
522  void SoftHinge(const MatrixBase<Real> &src);
523 
526  void GroupPnorm(const MatrixBase<Real> &src, Real power);
527 
534  void GroupPnormDeriv(const MatrixBase<Real> &input, const MatrixBase<Real> &output,
535  Real power);
536 
539  void GroupMax(const MatrixBase<Real> &src);
540 
548  void GroupMaxDeriv(const MatrixBase<Real> &input, const MatrixBase<Real> &output);
549 
551  void Tanh(const MatrixBase<Real> &src);
552 
553  // Function used in backpropagating derivatives of the sigmoid function:
554  // element-by-element, set *this = diff * value * (1.0 - value).
555  void DiffSigmoid(const MatrixBase<Real> &value,
556  const MatrixBase<Real> &diff);
557 
558  // Function used in backpropagating derivatives of the tanh function:
559  // element-by-element, set *this = diff * (1.0 - value^2).
560  void DiffTanh(const MatrixBase<Real> &value,
561  const MatrixBase<Real> &diff);
562 
575  Real check_thresh = 0.001);
576 
577  // There are some weird issue with template friend function in a class
578  // template in Windows version of nvcc. This is simple an ugly walkaround.
579 #if defined(__NVCC__) && defined(_MSC_VER)
580  template<typename Real>
581 #endif
582  friend Real kaldi::TraceMatMat<Real>(const MatrixBase<Real> &A,
583  const MatrixBase<Real> &B, MatrixTransposeType trans); // tr (A B)
584 
585  // so it can get around const restrictions on the pointer to data_.
586  friend class SubMatrix<Real>;
587 
589  void Add(const Real alpha);
590 
592  void AddToDiag(const Real alpha);
593 
595  template<typename OtherReal>
596  void AddVecVec(const Real alpha, const VectorBase<OtherReal> &a,
597  const VectorBase<OtherReal> &b);
598 
600  template<typename OtherReal>
601  void AddVecToRows(const Real alpha, const VectorBase<OtherReal> &v);
602 
604  template<typename OtherReal>
605  void AddVecToCols(const Real alpha, const VectorBase<OtherReal> &v);
606 
608  void AddMat(const Real alpha, const MatrixBase<Real> &M,
609  MatrixTransposeType transA = kNoTrans);
610 
612  void AddSmat(Real alpha, const SparseMatrix<Real> &A,
613  MatrixTransposeType trans = kNoTrans);
614 
617  void AddSmatMat(Real alpha, const SparseMatrix<Real> &A,
618  MatrixTransposeType transA, const MatrixBase<Real> &B,
619  Real beta);
620 
625  void AddMatSmat(Real alpha, const MatrixBase<Real> &A,
626  const SparseMatrix<Real> &B, MatrixTransposeType transB,
627  Real beta);
628 
632  void SymAddMat2(const Real alpha, const MatrixBase<Real> &M,
633  MatrixTransposeType transA, Real beta);
634 
637  void AddDiagVecMat(const Real alpha, const VectorBase<Real> &v,
638  const MatrixBase<Real> &M, MatrixTransposeType transM,
639  Real beta = 1.0);
640 
643  void AddMatDiagVec(const Real alpha,
644  const MatrixBase<Real> &M, MatrixTransposeType transM,
645  VectorBase<Real> &v,
646  Real beta = 1.0);
647 
649  void AddMatMatElements(const Real alpha,
650  const MatrixBase<Real>& A,
651  const MatrixBase<Real>& B,
652  const Real beta);
653 
655  template<typename OtherReal>
656  void AddSp(const Real alpha, const SpMatrix<OtherReal> &S);
657 
658  void AddMatMat(const Real alpha,
659  const MatrixBase<Real>& A, MatrixTransposeType transA,
660  const MatrixBase<Real>& B, MatrixTransposeType transB,
661  const Real beta);
662 
664  void SetMatMatDivMat(const MatrixBase<Real>& A,
665  const MatrixBase<Real>& B,
666  const MatrixBase<Real>& C);
667 
670  void AddMatSmat(const Real alpha,
671  const MatrixBase<Real>& A, MatrixTransposeType transA,
672  const MatrixBase<Real>& B, MatrixTransposeType transB,
673  const Real beta);
674 
677  void AddSmatMat(const Real alpha,
678  const MatrixBase<Real>& A, MatrixTransposeType transA,
679  const MatrixBase<Real>& B, MatrixTransposeType transB,
680  const Real beta);
681 
683  void AddMatMatMat(const Real alpha,
684  const MatrixBase<Real>& A, MatrixTransposeType transA,
685  const MatrixBase<Real>& B, MatrixTransposeType transB,
686  const MatrixBase<Real>& C, MatrixTransposeType transC,
687  const Real beta);
688 
690  // This and the routines below are really
691  // stubs that need to be made more efficient.
692  void AddSpMat(const Real alpha,
693  const SpMatrix<Real>& A,
694  const MatrixBase<Real>& B, MatrixTransposeType transB,
695  const Real beta) {
696  Matrix<Real> M(A);
697  return AddMatMat(alpha, M, kNoTrans, B, transB, beta);
698  }
700  void AddTpMat(const Real alpha,
701  const TpMatrix<Real>& A, MatrixTransposeType transA,
702  const MatrixBase<Real>& B, MatrixTransposeType transB,
703  const Real beta) {
704  Matrix<Real> M(A);
705  return AddMatMat(alpha, M, transA, B, transB, beta);
706  }
708  void AddMatSp(const Real alpha,
709  const MatrixBase<Real>& A, MatrixTransposeType transA,
710  const SpMatrix<Real>& B,
711  const Real beta) {
712  Matrix<Real> M(B);
713  return AddMatMat(alpha, A, transA, M, kNoTrans, beta);
714  }
716  void AddSpMatSp(const Real alpha,
717  const SpMatrix<Real> &A,
718  const MatrixBase<Real>& B, MatrixTransposeType transB,
719  const SpMatrix<Real>& C,
720  const Real beta) {
721  Matrix<Real> M(A), N(C);
722  return AddMatMatMat(alpha, M, kNoTrans, B, transB, N, kNoTrans, beta);
723  }
725  void AddMatTp(const Real alpha,
726  const MatrixBase<Real>& A, MatrixTransposeType transA,
727  const TpMatrix<Real>& B, MatrixTransposeType transB,
728  const Real beta) {
729  Matrix<Real> M(B);
730  return AddMatMat(alpha, A, transA, M, transB, beta);
731  }
732 
734  void AddTpTp(const Real alpha,
735  const TpMatrix<Real>& A, MatrixTransposeType transA,
736  const TpMatrix<Real>& B, MatrixTransposeType transB,
737  const Real beta) {
738  Matrix<Real> M(A), N(B);
739  return AddMatMat(alpha, M, transA, N, transB, beta);
740  }
741 
743  // This one is more efficient, not like the others above.
744  void AddSpSp(const Real alpha,
745  const SpMatrix<Real>& A, const SpMatrix<Real>& B,
746  const Real beta);
747 
749  void CopyLowerToUpper();
750 
752  void CopyUpperToLower();
753 
758  void OrthogonalizeRows();
759 
762  // Will throw exception on failure.
763  void Read(std::istream & in, bool binary, bool add = false);
765  void Write(std::ostream & out, bool binary) const;
766 
767  // Below is internal methods for Svd, user does not have to know about this.
768 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD)
769  // protected:
770  // Should be protected but used directly in testing routine.
771  // destroys *this!
773  MatrixBase<Real> *Vt);
774 #else
775  protected:
776  // destroys *this!
777  bool JamaSvd(VectorBase<Real> *s, MatrixBase<Real> *U,
778  MatrixBase<Real> *V);
779 
780 #endif
781  protected:
782 
784  explicit MatrixBase(Real *data, MatrixIndexT cols, MatrixIndexT rows, MatrixIndexT stride) :
785  data_(data), num_cols_(cols), num_rows_(rows), stride_(stride) {
787  }
788 
791  explicit MatrixBase(): data_(NULL) {
793  }
794 
795  // Make sure pointers to MatrixBase cannot be deleted.
797 
803  inline Real* Data_workaround() const {
804  return data_;
805  }
806 
808  Real* data_;
809 
814 
817  private:
819 };
820 
822 template<typename Real>
823 class Matrix : public MatrixBase<Real> {
824  public:
825 
827  Matrix();
828 
830  Matrix(const MatrixIndexT r, const MatrixIndexT c,
831  MatrixResizeType resize_type = kSetZero,
832  MatrixStrideType stride_type = kDefaultStride):
833  MatrixBase<Real>() { Resize(r, c, resize_type, stride_type); }
834 
837  template<typename OtherReal>
838  explicit Matrix(const CuMatrixBase<OtherReal> &cu,
839  MatrixTransposeType trans = kNoTrans);
840 
841 
843  void Swap(Matrix<Real> *other);
844 
846  void Swap(CuMatrix<Real> *mat);
847 
850  explicit Matrix(const MatrixBase<Real> & M,
851  MatrixTransposeType trans = kNoTrans);
852 
854  Matrix(const Matrix<Real> & M); // (cannot make explicit)
855 
857  template<typename OtherReal>
858  explicit Matrix(const MatrixBase<OtherReal> & M,
859  MatrixTransposeType trans = kNoTrans);
860 
863  template<typename OtherReal>
864  explicit Matrix(const SpMatrix<OtherReal> & M) : MatrixBase<Real>() {
865  Resize(M.NumRows(), M.NumRows(), kUndefined);
866  this->CopyFromSp(M);
867  }
868 
870  explicit Matrix(const CompressedMatrix &C);
871 
873  template <typename OtherReal>
874  explicit Matrix(const TpMatrix<OtherReal> & M,
875  MatrixTransposeType trans = kNoTrans) : MatrixBase<Real>() {
876  if (trans == kNoTrans) {
877  Resize(M.NumRows(), M.NumCols(), kUndefined);
878  this->CopyFromTp(M);
879  } else {
880  Resize(M.NumCols(), M.NumRows(), kUndefined);
881  this->CopyFromTp(M, kTrans);
882  }
883  }
884 
886  // Unlike one in base, allows resizing.
887  void Read(std::istream & in, bool binary, bool add = false);
888 
890  void RemoveRow(MatrixIndexT i);
891 
894  void Transpose();
895 
897  ~Matrix() { Destroy(); }
898 
911  void Resize(const MatrixIndexT r,
912  const MatrixIndexT c,
913  MatrixResizeType resize_type = kSetZero,
914  MatrixStrideType stride_type = kDefaultStride);
915 
917  Matrix<Real> &operator = (const MatrixBase<Real> &other) {
918  if (MatrixBase<Real>::NumRows() != other.NumRows() ||
919  MatrixBase<Real>::NumCols() != other.NumCols())
920  Resize(other.NumRows(), other.NumCols(), kUndefined);
922  return *this;
923  }
924 
926  Matrix<Real> &operator = (const Matrix<Real> &other) {
927  if (MatrixBase<Real>::NumRows() != other.NumRows() ||
928  MatrixBase<Real>::NumCols() != other.NumCols())
929  Resize(other.NumRows(), other.NumCols(), kUndefined);
931  return *this;
932  }
933 
934 
935  private:
937  void Destroy();
938 
943  void Init(const MatrixIndexT r,
944  const MatrixIndexT c,
945  const MatrixStrideType stride_type);
946 
947 };
949 
952 
955 struct HtkHeader {
961  int16 mSampleSize;
963  uint16 mSampleKind;
964 };
965 
966 // Read HTK formatted features from file into matrix.
967 template<typename Real>
968 bool ReadHtk(std::istream &is, Matrix<Real> *M, HtkHeader *header_ptr);
969 
970 // Write (HTK format) features to file from matrix.
971 template<typename Real>
972 bool WriteHtk(std::ostream &os, const MatrixBase<Real> &M, HtkHeader htk_hdr);
973 
974 // Write (CMUSphinx format) features to file from matrix.
975 template<typename Real>
976 bool WriteSphinx(std::ostream &os, const MatrixBase<Real> &M);
977 
979 
987 template<typename Real>
988 class SubMatrix : public MatrixBase<Real> {
989  public:
990  // Initialize a SubMatrix from part of a matrix; this is
991  // a bit like A(b:c, d:e) in Matlab.
992  // This initializer is against the proper semantics of "const", since
993  // SubMatrix can change its contents. It would be hard to implement
994  // a "const-safe" version of this class.
995  SubMatrix(const MatrixBase<Real>& T,
996  const MatrixIndexT ro, // row offset, 0 < ro < NumRows()
997  const MatrixIndexT r, // number of rows, r > 0
998  const MatrixIndexT co, // column offset, 0 < co < NumCols()
999  const MatrixIndexT c); // number of columns, c > 0
1000 
1001  // This initializer is mostly intended for use in CuMatrix and related
1002  // classes. Be careful!
1003  SubMatrix(Real *data,
1004  MatrixIndexT num_rows,
1005  MatrixIndexT num_cols,
1006  MatrixIndexT stride);
1007 
1009 
1012  SubMatrix<Real> (const SubMatrix &other):
1013  MatrixBase<Real> (other.data_, other.num_cols_, other.num_rows_,
1014  other.stride_) {}
1015 
1016  private:
1018  SubMatrix<Real> &operator = (const SubMatrix<Real> &other);
1019 };
1021 
1024 
1025 // Some declarations. These are traces of products.
1026 
1027 
1028 template<typename Real>
1030  const MatrixBase<Real> &B, Real tol = 0.01) {
1031  return A.ApproxEqual(B, tol);
1032 }
1033 
1034 template<typename Real>
1035 inline void AssertEqual(const MatrixBase<Real> &A, const MatrixBase<Real> &B,
1036  float tol = 0.01) {
1037  KALDI_ASSERT(A.ApproxEqual(B, tol));
1038 }
1039 
1041 template <typename Real>
1042 double TraceMat(const MatrixBase<Real> &A) { return A.Trace(); }
1043 
1044 
1046 template <typename Real>
1048  const MatrixBase<Real> &B, MatrixTransposeType transB,
1049  const MatrixBase<Real> &C, MatrixTransposeType transC);
1050 
1052 template <typename Real>
1054  const MatrixBase<Real> &B, MatrixTransposeType transB,
1055  const MatrixBase<Real> &C, MatrixTransposeType transC,
1056  const MatrixBase<Real> &D, MatrixTransposeType transD);
1057 
1059 
1060 
1063 
1064 
1072 template<typename Real> void SortSvd(VectorBase<Real> *s, MatrixBase<Real> *U,
1073  MatrixBase<Real>* Vt = NULL,
1074  bool sort_on_absolute_value = true);
1075 
1082 template<typename Real>
1084  MatrixBase<Real> *D);
1085 
1090 template<typename Real>
1091 bool AttemptComplexPower(Real *x_re, Real *x_im, Real power);
1092 
1093 
1094 
1096 
1099 template<typename Real>
1100 std::ostream & operator << (std::ostream & Out, const MatrixBase<Real> & M);
1101 
1102 template<typename Real>
1103 std::istream & operator >> (std::istream & In, MatrixBase<Real> & M);
1104 
1105 // The Matrix read allows resizing, so we override the MatrixBase one.
1106 template<typename Real>
1107 std::istream & operator >> (std::istream & In, Matrix<Real> & M);
1108 
1109 
1110 template<typename Real>
1111 bool SameDim(const MatrixBase<Real> &M, const MatrixBase<Real> &N) {
1112  return (M.NumRows() == N.NumRows() && M.NumCols() == N.NumCols());
1113 }
1114 
1116 
1117 
1118 } // namespace kaldi
1119 
1120 
1121 
1122 // we need to include the implementation and some
1123 // template specializations.
1124 #include "matrix/kaldi-matrix-inl.h"
1125 
1126 
1127 #endif // KALDI_MATRIX_KALDI_MATRIX_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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)
double real
Packed symetric matrix class.
Definition: matrix-common.h:62
void Write(std::ostream &out, bool binary) const
write to stream.
Real LargestAbsElem() const
largest absolute value.
int16 mSampleSize
Sample size.
Definition: kaldi-matrix.h:961
Real * Data()
Gives pointer to raw data (non-const).
Definition: kaldi-matrix.h:84
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.
MatrixResizeType
Definition: matrix-common.h:37
const Real * RowData(MatrixIndexT i) const
Returns pointer to data for one row (const)
Definition: kaldi-matrix.h:94
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
Definition: kaldi-matrix.h:816
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.
Definition: kaldi-matrix.h:426
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.
Definition: kaldi-matrix.h:700
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
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. ...
Definition: kaldi-matrix.h:49
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:79
void Exp(const MatrixBase< Real > &src)
Real MinSingularValue() const
Returns smallest singular value.
Definition: kaldi-matrix.h:430
Matrix(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Basic constructor.
Definition: kaldi-matrix.h:830
Real * data_
data memory area
Definition: kaldi-matrix.h:808
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)
Definition: kaldi-matrix.h:87
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)
kaldi::int32 int32
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&#39;th row&#39;s j&#39;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.
Definition: kaldi-matrix.h:803
Matrix(const SpMatrix< OtherReal > &M)
Copy constructor taking SpMatrix...
Definition: kaldi-matrix.h:864
A class for storing matrices.
Definition: kaldi-matrix.h:823
void Read(std::istream &in, bool binary, bool add=false)
stream read.
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
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)
int32 mSamplePeriod
Sample period.
Definition: kaldi-matrix.h:959
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...
Definition: kaldi-matrix.h:874
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
Definition: kaldi-matrix.h:213
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.
Definition: kaldi-matrix.h:70
int32 MatrixIndexT
Definition: matrix-common.h:98
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.
Definition: kaldi-matrix.h:734
void Log(const MatrixBase< Real > &src)
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
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.
Definition: kaldi-matrix.h:195
MatrixIndexT num_rows_
< Number of columns
Definition: kaldi-matrix.h:813
MatrixBase(Real *data, MatrixIndexT cols, MatrixIndexT rows, MatrixIndexT stride)
Initializer, callable only from child.
Definition: kaldi-matrix.h:784
Real & Index(MatrixIndexT r, MatrixIndexT c)
Indexing operator, provided for ease of debugging (gdb doesn&#39;t work with parenthesis operator)...
Definition: kaldi-matrix.h:111
void AddVecToRows(const Real alpha, const VectorBase< OtherReal > &v)
[each row of *this] += alpha * v
MatrixStrideType
Definition: matrix-common.h:44
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.
Definition: kaldi-matrix.h:708
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)
Definition: kaldi-error.h:206
uint16 mSampleKind
Sample kind.
Definition: kaldi-matrix.h:963
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.
Definition: matrix-common.h:63
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
Definition: kaldi-matrix.h:812
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.
Definition: matrix-common.h:70
Real Sum() const
Returns sum of all elements in matrix.
void SetZero()
Sets matrix to zero.
void ApplyCeiling(Real ceiling_val)
Definition: kaldi-matrix.h:358
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)
Definition: kaldi-utils.h:137
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.
Definition: matrix-common.h:69
void InvertElements()
Inverts all the elements of the matrix.
MatrixBase()
Initializer, callable only from child.
Definition: kaldi-matrix.h:791
void DiffSigmoid(const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
SubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
Definition: kaldi-matrix.h:209
A class representing a vector.
Definition: kaldi-vector.h:406
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.
Definition: kaldi-matrix.h:716
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)
Definition: kaldi-error.h:185
Real & operator()(MatrixIndexT r, MatrixIndexT c)
Indexing operator, non-const (only checks sizes if compiled with -DKALDI_PARANOID) ...
Definition: kaldi-matrix.h:102
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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.
Definition: kaldi-matrix.h:73
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.
Definition: matrix-common.h:75
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
MatrixTransposeType
Definition: matrix-common.h:32
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
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.
Definition: kaldi-matrix.h:202
bool Equal(const MatrixBase< Real > &other) const
Tests for exact equality. It&#39;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].
int32 mNSamples
Number of samples.
Definition: kaldi-matrix.h:957
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
Definition: kaldi-matrix.cc:38
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.
Definition: kaldi-matrix.h:725
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.
Definition: kaldi-vector.h:41
void ApplyFloor(Real floor_val)
Definition: kaldi-matrix.h:354
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)
Definition: kaldi-matrix.h:341
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
~Matrix()
Distructor to free matrices.
Definition: kaldi-matrix.h:897
void ApplyExpLimited(Real lower_limit, Real upper_limit)
Definition: kaldi-matrix.h:370
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.
Definition: kaldi-matrix.h:692
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Sub-matrix representation.
Definition: kaldi-matrix.h:988
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
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.
Definition: matrix-common.h:72
void ApplyPowAbs(Real power, bool include_sign=false)
Definition: kaldi-matrix.h:346
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".