Honeycomb  0.1
Component-Model Framework
Svd.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
6 
7 namespace honey
8 {
9 
11 
23 template<class Real>
24 class Svd
25 {
26  typedef typename Numeral<Real>::Real_ Real_;
27  typedef typename Real_::DoubleType Double_;
28  typedef typename Double_::Real Double;
29  typedef Alge_<Real> Alge;
30  typedef Alge_<Double> Alge_d;
31 public:
35 
36  enum class Mode
37  {
38  full,
39  reduced
40  };
41 
42  Svd() {}
44  template<class T>
45  Svd(const MatrixBase<T>& a, Mode mode = Mode::reduced) { calc(a, mode); }
46 
48  template<class T>
50  {
51  sdt m = a.rows(), n = a.cols();
52  _w.resize(Alge::min(m, n));
53  _wd.resize(_w.size());
54 
55  if (m >= n)
56  {
57  _vt.resize(n, n);
58  if (mode == Mode::full) a.transpose(_ut.resize(m,m).block(0,0,n,m));
59  else a.transpose(_ut);
60  jacobi(_ut, _w, _wd, _vt, _rand);
61  _ut.transpose(_u);
62  }
63  else
64  {
65  //m < n
66  _u.resize(m, m);
67  if (mode == Mode::full) _vt.resize(n,n).block(0,0,m,n) = a;
68  else _vt = a;
69  jacobi(_vt, _w, _wd, _u, _rand);
70  _u.transposeInPlace();
71  }
72 
73  return *this;
74  }
75 
77  template<class T>
79  {
80  sdt m = a.rows(), n = a.cols();
81  _w.resize(Alge::min(m, n));
82  _wd.resize(_w.size());
83 
84  if (m >= n)
85  {
86  a.transpose(_ut);
87  jacobi(_ut, _w, _wd, optnull, _rand);
88  }
89  else
90  {
91  _vt = a;
92  jacobi(_vt, _w, _wd, optnull, _rand);
93  }
94 
95  return *this;
96  }
97 
99  template<class B, class X>
100  void solve(const MatrixBase<B>& b, MatrixBase<X>& x) { _backSub.solve(_w, _u, _vt, b, x); }
101 
103  template<class T>
104  void inverse(MatrixBase<T>& res) { _backSub.solve(_w, _u, _vt, res); }
105 
107  const Vec& w() const { return _w; }
109  const Matrix& u() const { return _u; }
111  const Matrix& vt() const { return _vt; }
112 
113 private:
115  static void jacobi(Matrix& At, Vec& w, Vec_d& wd, optional<Matrix&> Vt, RandomGen& rand);
116 
117  Vec _w;
118  Vec_d _wd;
119  Matrix _u;
120  Matrix _ut;
121  Matrix _vt;
122  Chacha _rand;
123  BackSub<Real> _backSub;
124 };
125 
126 extern template class Svd<Float>;
127 extern template class Svd<Double>;
128 extern template class Svd<Quad>;
129 
130 }
void solve(const MatrixBase< B > &b, MatrixBase< X > &x)
Solve the linear system where A is the decomposed matrix. A and B row sizes must match...
Definition: Svd.h:100
compute reduced SVD
Random number generator interface.
Definition: Gen.h:11
Svd(const MatrixBase< T > &a, Mode mode=Mode::reduced)
Calculate the SVD of matrix A.
Definition: Svd.h:45
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
const Matrix & u() const
Get the left singular column vectors of the decomposition.
Definition: Svd.h:109
matrix::Block< MatrixS, Rows, Cols > block(sdt row, sdt col, sdt rows=-1, sdt cols=-1)
Get block at offset (row,col) with size (Rows, Cols). If Rows or Cols is fixed then rows or cols may ...
Definition: Base.h:252
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
const Matrix & vt() const
Get the right singular row vectors of the decomposition.
Definition: Svd.h:111
Vec< matrix::dynamic, Double > Vec_d
Definition: Svd.h:33
compute full SVD
static std::common_type< Num, Num2 >::type min(Num a, Num2 b)
Get the minimum of two numbers.
Definition: Alge.h:89
ChaCha8, a cryptographically secure pseudo random number generator.
Definition: Chacha.h:41
Singular Value Decomposition. Can be used to calculate the pseudo-inverse of any matrix or solve leas...
Definition: Base.h:13
Mode
Definition: Svd.h:36
MatrixS & resize(sdt rows, sdt cols)
Sets number of rows/columns and reallocates only if the size has changed (rows*cols). All previous data is lost on reallocation. Returns self.
Definition: Base.h:277
Matrix< matrix::dynamic, matrix::dynamic, Real > Matrix
Definition: Svd.h:32
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
Svd & calcValues(const MatrixBase< T > &a)
Calculate the singular values of matrix A. This is a fast method for when the left and right singular...
Definition: Svd.h:78
double Real
Definition: Real.h:14
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
Svd & calc(const MatrixBase< T > &a, Mode mode=Mode::reduced)
Calculate the SVD of matrix A.
Definition: Svd.h:49
Vec< matrix::dynamic, Real > Vec
Definition: Svd.h:34
void inverse(MatrixBase< T > &res)
Calculate the inverse of A, the decomposed matrix.
Definition: Svd.h:104
Matrix base class.
Definition: Base.h:17
void transposeInPlace()
transpose and store in this matrix, only valid for square matrices
Definition: Base.h:328
const Vec & w() const
Get the singular values of the decomposition.
Definition: Svd.h:107
Svd()
Definition: Svd.h:42
Global Honeycomb namespace.
Res && transpose(Res &&res) const
transpose and store result in res. Returns res.
Definition: Base.h:314
Back substitute to solve a linear system.
Definition: BackSub.h:11
VecS & resize(sdt dim)
Sets number of dimensions and reallocates only if different. All previous data is lost on reallocatio...
Definition: Base.h:69