Honeycomb  0.1
Component-Model Framework
Eigen.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 
5 #include "Honey/Misc/Lazy.h"
6 
7 namespace honey
8 {
9 
11 
20 template<class Real>
21 class Eigen
22 {
23  typedef typename Numeral<Real>::Real_ Real_;
24  typedef typename Real_::DoubleType Double_;
25  typedef typename Double_::Real Double;
26  typedef Alge_<Real> Alge;
27  typedef Alge_<Double> Alge_d;
28 public:
31 
32  Eigen() { init(); }
34  template<class T>
35  Eigen(const MatrixBase<T>& a) { init(); calc(a); }
36 
38  template<class T>
40  {
41  assert(a.rows() == a.cols(), "Matrix must be square and symmetric");
42  sdt n = a.rows();
43  _w.resize(n); _indR.resize(n); _indC.resize(n);
44  _v.resize(n, n);
45  _a = a;
46  jacobi(_a, _w, _v, _indR, _indC);
47  _vt.setDirty(true);
48  return *this;
49  }
50 
52  template<class T>
54  {
55  assert(a.rows() == a.cols(), "Matrix must be square and symmetric");
56  sdt n = a.rows();
57  _w.resize(n); _indR.resize(n); _indC.resize(n);
58  _a = a;
59  jacobi(_a, _w, optnull, _indR, _indC);
60  return *this;
61  }
62 
64  template<class B, class X>
65  void solve(const MatrixBase<B>& b, MatrixBase<X>& x) { _backSub.solve(_w, _v, _vt, b, x); }
66 
68  template<class T>
69  void inverse(MatrixBase<T>& res) { _backSub.solve(_w, _v, _vt, res); }
70 
72  const Vec& w() const { return _w; }
74  const Matrix& v() const { return _v; }
76  const Matrix& vt() const { return _vt; }
77 
78 private:
79  void init() { _vt.setEval([&](Matrix& val) { _v.transpose(val); }); }
80 
82  static void jacobi(Matrix& A, Vec& W, optional<Matrix&> V, vector<sdt>& indR, vector<sdt>& indC);
83 
84  Vec _w;
85  Matrix _v;
86  lazy<Matrix> _vt;
87  Matrix _a;
88  vector<sdt> _indR;
89  vector<sdt> _indC;
90  BackSub<Real> _backSub;
91 };
92 
93 extern template class Eigen<Float>;
94 extern template class Eigen<Double>;
95 extern template class Eigen<Quad>;
96 
97 }
Eigen(const MatrixBase< T > &a)
Calculate the eigendecomposition of symmetric matrix A.
Definition: Eigen.h:35
Algebra.
Definition: Alge.h:13
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 & vt() const
Get the row eigenvectors of the decomposition.
Definition: Eigen.h:76
void inverse(MatrixBase< T > &res)
Calculate the inverse of A, the decomposed matrix.
Definition: Eigen.h:69
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Eigen()
Definition: Eigen.h:32
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
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
const Matrix & v() const
Get the column eigenvectors of the decomposition.
Definition: Eigen.h:74
Matrix< matrix::dynamic, matrix::dynamic, Real > Matrix
Definition: Eigen.h:29
Eigen & calc(const MatrixBase< T > &a)
Calculate the eigendecomposition of symmetric matrix A. The eigenvalues/vectors are sorted from large...
Definition: Eigen.h:39
double Real
Definition: Real.h:14
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: Eigen.h:65
Eigen & calcValues(const MatrixBase< T > &a)
Calculate the eigenvalues of symmetric matrix A. This is a fast method for when the eigenvectors are ...
Definition: Eigen.h:53
Eigendecomposition. Decomposes any square (n x n) symmetric matrix into eigenvalues and eigenvectors...
Definition: Eigen.h:21
Matrix base class.
Definition: Base.h:17
Vec< matrix::dynamic, Real > Vec
Definition: Eigen.h:30
Global Honeycomb namespace.
const Vec & w() const
Get the eigenvalues of the decomposition.
Definition: Eigen.h:72
Res && transpose(Res &&res) const
transpose and store result in res. Returns res.
Definition: Base.h:314
VecS & resize(sdt dim)
Sets number of dimensions and reallocates only if different. All previous data is lost on reallocatio...
Definition: Base.h:69