summaryrefslogtreecommitdiff
path: root/kaldi_io/src/kaldi/matrix/sp-matrix.h
blob: 209d24adfd98ca3d0413897fa07284b43077560f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// matrix/sp-matrix.h

// Copyright 2009-2011   Ondrej Glembek;  Microsoft Corporation;  Lukas Burget;
//                       Saarland University;  Ariya Rastrow;  Yanmin Qian;
//                       Jan Silovsky

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//  http://www.apache.org/licenses/LICENSE-2.0

// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#ifndef KALDI_MATRIX_SP_MATRIX_H_
#define KALDI_MATRIX_SP_MATRIX_H_

#include <algorithm>
#include <vector>

#include "matrix/packed-matrix.h"

namespace kaldi {


/// \addtogroup matrix_group
/// @{
template<typename Real> class SpMatrix;


/**
 * @brief Packed symetric matrix class
*/
template<typename Real>
class SpMatrix : public PackedMatrix<Real> {
  friend class CuSpMatrix<Real>;
 public:
  // so it can use our assignment operator.
  friend class std::vector<Matrix<Real> >;

  SpMatrix(): PackedMatrix<Real>() {}

  /// Copy constructor from CUDA version of SpMatrix
  /// This is defined in ../cudamatrix/cu-sp-matrix.h
  
  explicit SpMatrix(const CuSpMatrix<Real> &cu);
 
  explicit SpMatrix(MatrixIndexT r, MatrixResizeType resize_type = kSetZero)
      : PackedMatrix<Real>(r, resize_type) {}

  SpMatrix(const SpMatrix<Real> &orig)
      : PackedMatrix<Real>(orig) {}

  template<typename OtherReal>
  explicit SpMatrix(const SpMatrix<OtherReal> &orig)
      : PackedMatrix<Real>(orig) {}

#ifdef KALDI_PARANOID
  explicit SpMatrix(const MatrixBase<Real> & orig,
                    SpCopyType copy_type = kTakeMeanAndCheck)
      : PackedMatrix<Real>(orig.NumRows(), kUndefined) {
    CopyFromMat(orig, copy_type);
  }
#else
  explicit SpMatrix(const MatrixBase<Real> & orig,
                    SpCopyType copy_type = kTakeMean)
      : PackedMatrix<Real>(orig.NumRows(), kUndefined) {
    CopyFromMat(orig, copy_type);
  }
#endif

  /// Shallow swap.
  void Swap(SpMatrix *other);

  inline void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero) {
    PackedMatrix<Real>::Resize(nRows, resize_type);
  }

  void CopyFromSp(const SpMatrix<Real> &other) {
    PackedMatrix<Real>::CopyFromPacked(other);
  }

  template<typename OtherReal>
  void CopyFromSp(const SpMatrix<OtherReal> &other) {
    PackedMatrix<Real>::CopyFromPacked(other);
  }

#ifdef KALDI_PARANOID
  void CopyFromMat(const MatrixBase<Real> &orig,
                   SpCopyType copy_type = kTakeMeanAndCheck);
#else  // different default arg if non-paranoid mode.
  void CopyFromMat(const MatrixBase<Real> &orig,
                   SpCopyType copy_type = kTakeMean);
#endif

  inline Real operator() (MatrixIndexT r, MatrixIndexT c) const {
    // if column is less than row, then swap these as matrix is stored
    // as upper-triangular...  only allowed for const matrix object.
    if (static_cast<UnsignedMatrixIndexT>(c) >
        static_cast<UnsignedMatrixIndexT>(r))
      std::swap(c, r);
    // c<=r now so don't have to check c.
    KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
                 static_cast<UnsignedMatrixIndexT>(this->num_rows_));
    return *(this->data_ + (r*(r+1)) / 2 + c);
    // Duplicating code from PackedMatrix.h
  }

  inline Real &operator() (MatrixIndexT r, MatrixIndexT c) {
    if (static_cast<UnsignedMatrixIndexT>(c) >
        static_cast<UnsignedMatrixIndexT>(r))
      std::swap(c, r);
    // c<=r now so don't have to check c.
    KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
                 static_cast<UnsignedMatrixIndexT>(this->num_rows_));
    return *(this->data_ + (r * (r + 1)) / 2 + c);
    // Duplicating code from PackedMatrix.h
  }

  using PackedMatrix<Real>::operator =;
  using PackedMatrix<Real>::Scale;

  /// matrix inverse.
  /// if inverse_needed = false, will fill matrix with garbage.
  /// (only useful if logdet wanted).
  void Invert(Real *logdet = NULL, Real *det_sign= NULL,
              bool inverse_needed = true);

  // Below routine does inversion in double precision,
  // even for single-precision object.
  void InvertDouble(Real *logdet = NULL, Real *det_sign = NULL,
                    bool inverse_needed = true);

  /// Returns maximum ratio of singular values.
  inline Real Cond() const {
    Matrix<Real> tmp(*this);
    return tmp.Cond();
  }

  /// Takes matrix to a fraction power via Svd.
  /// Will throw exception if matrix is not positive semidefinite
  /// (to within a tolerance)
  void ApplyPow(Real exponent);

  /// This is the version of SVD that we implement for symmetric positive
  /// definite matrices.  This exists for historical reasons; right now its
  /// internal implementation is the same as Eig().  It computes the eigenvalue
  /// decomposition (*this) = P * diag(s) * P^T with P orthogonal.  Will throw
  /// exception if input is not positive semidefinite to within a tolerance.
  void SymPosSemiDefEig(VectorBase<Real> *s, MatrixBase<Real> *P,
                        Real tolerance = 0.001) const;

  /// Solves the symmetric eigenvalue problem: at end we should have (*this) = P
  /// * diag(s) * P^T.  We solve the problem using the symmetric QR method.
  /// P may be NULL.
  /// Implemented in qr.cc.
  /// If you need the eigenvalues sorted, the function SortSvd declared in
  /// kaldi-matrix is suitable.
  void Eig(VectorBase<Real> *s, MatrixBase<Real> *P = NULL) const;
  
  /// This function gives you, approximately, the largest eigenvalues of the
  /// symmetric matrix and the corresponding eigenvectors.  (largest meaning,
  /// further from zero).  It does this by doing a SVD within the Krylov
  /// subspace generated by this matrix and a random vector.  This is
  /// a form of the Lanczos method with complete reorthogonalization, followed
  /// by SVD within a smaller dimension ("lanczos_dim").
  ///
  /// If *this is m by m, s should be of dimension n and P should be of
  /// dimension m by n, with n <= m.  The *columns* of P are the approximate
  /// eigenvectors; P * diag(s) * P^T would be a low-rank reconstruction of
  /// *this.  The columns of P will be orthogonal, and the elements of s will be
  /// the eigenvalues of *this projected into that subspace, but beyond that
  /// there are no exact guarantees.  (This is because the convergence of this
  /// method is statistical).  Note: it only makes sense to use this
  /// method if you are in very high dimension and n is substantially smaller
  /// than m: for example, if you want the 100 top eigenvalues of a 10k by 10k
  /// matrix.  This function calls Rand() to initialize the lanczos
  /// iterations and also for restarting.
  /// If lanczos_dim is zero, it will default to the greater of:
  /// s->Dim() + 50 or s->Dim() + s->Dim()/2, but not more than this->Dim().
  /// If lanczos_dim == this->Dim(), you might as well just call the function
  /// Eig() since the result will be the same, and Eig() would be faster; the
  /// whole point of this function is to reduce the dimension of the SVD
  /// computation.
  void TopEigs(VectorBase<Real> *s, MatrixBase<Real> *P,
               MatrixIndexT lanczos_dim = 0) const;


  
  /// Takes log of the matrix (does eigenvalue decomposition then takes
  /// log of eigenvalues and reconstructs).  Will throw of not +ve definite.
  void Log();


  // Takes exponential of the matrix (equivalent to doing eigenvalue
  // decomposition then taking exp of eigenvalues and reconstructing).
  void Exp();

  /// Returns the maximum of the absolute values of any of the
  /// eigenvalues.
  Real MaxAbsEig() const;

  void PrintEigs(const char *name) {
    Vector<Real> s((*this).NumRows());
    Matrix<Real> P((*this).NumRows(), (*this).NumCols());
    SymPosSemiDefEig(&s, &P);
    KALDI_LOG << "PrintEigs: " << name << ": " << s;
  }

  bool IsPosDef() const;  // returns true if Cholesky succeeds.
  void AddSp(const Real alpha, const SpMatrix<Real> &Ma) {
    this->AddPacked(alpha, Ma);
  }

  /// Computes log determinant but only for +ve-def matrices
  /// (it uses Cholesky).
  /// If matrix is not +ve-def, it will throw an exception
  /// was LogPDDeterminant()
  Real LogPosDefDet() const;

  Real LogDet(Real *det_sign = NULL) const;

  /// rank-one update, this <-- this + alpha v v'
  template<typename OtherReal>
  void AddVec2(const Real alpha, const VectorBase<OtherReal> &v);

  /// rank-two update, this <-- this + alpha (v w' + w v').
  void AddVecVec(const Real alpha, const VectorBase<Real> &v,
                 const VectorBase<Real> &w);

  /// Does *this = beta * *thi + alpha * diag(v) * S * diag(v)
  void AddVec2Sp(const Real alpha, const VectorBase<Real> &v,
                 const SpMatrix<Real> &S, const Real beta);
  
  /// diagonal update, this <-- this + diag(v)
  template<typename OtherReal>
  void AddDiagVec(const Real alpha, const VectorBase<OtherReal> &v);

  /// rank-N update:
  /// if (transM == kNoTrans)
  /// (*this) = beta*(*this) + alpha * M * M^T,
  /// or  (if transM == kTrans)
  ///  (*this) = beta*(*this) + alpha * M^T * M
  /// Note: beta used to default to 0.0.