Honeycomb  0.1
Component-Model Framework
Qrd.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 
6 namespace honey
7 {
8 
10 
20 template<class Real>
21 class Qrd
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  typedef BackSub<Real> BackSub;
29 
30 public:
32 
33  enum class Mode
34  {
35  full,
36  reduced
37  };
38 
39  Qrd() {}
41  template<class T>
42  Qrd(const MatrixBase<T>& a, Mode mode = Mode::reduced) { calc(a, mode); }
43 
45  template<class T>
47  {
48  sdt m = a.rows(), n = a.cols();
49  assert(m >= n);
50  _h = a;
51  _q.resize(m, mode == Mode::full ? m : n);
52  _r.resize(n, n);
53  householder(_h, _q, _r);
54  return *this;
55  }
56 
58  bool isFullRank() const { return BackSub::isFullRank(_r); }
59 
61  template<class B, class X>
62  void solve(const MatrixBase<B>& b, MatrixBase<X>& x)
63  {
64  sdt m = _q.rows(), n = _r.rows();
65  bool full = m == _q.cols();
66  if (full) _q.block(0,0,m,n).transposeMul(b, _y);
67  else _q.transposeMul(b, _y);
68  BackSub::solve(_r, _y, x);
69  }
70 
72  const Matrix& q() const { return _q; }
74  const Matrix& r() const { return _r; }
76  const Matrix& h() const { return _h; }
77 
78 private:
80  static void householder(Matrix& a, Matrix& q, Matrix& r);
81 
82  Matrix _q;
83  Matrix _r;
84  Matrix _h;
85  Matrix _y;
86 };
87 
88 extern template class Qrd<Float>;
89 extern template class Qrd<Double>;
90 extern template class Qrd<Quad>;
91 
92 }
static bool isFullRank(const MatrixBase< T > &a)
Check if a triangular/trapezoidal matrix has full rank (ie. all vectors are linearly independent; no ...
Definition: BackSub.h:25
Algebra.
Definition: Alge.h:13
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
bool isFullRank() const
Check if the decomposed matrix has full rank (ie. all vectors are linearly independent; no vector is ...
Definition: Qrd.h:58
Res && transposeMul(const T &rhs, Res &&res) const
Stores result in and returns res.
Definition: Base.h:341
Matrix< matrix::dynamic, matrix::dynamic, Real > Matrix
Definition: Qrd.h:31
compute reduced QRD
const Matrix & q() const
Get Q of the decomposition.
Definition: Qrd.h:72
const Matrix & h() const
Get the Householder column vectors that define the reflections. H is a (m x n) lower trapazoidal matr...
Definition: Qrd.h:76
QR Decomposition. Can be used to solve least squares problems.
Definition: Qrd.h:21
Mode
Definition: Qrd.h:33
Qrd()
Definition: Qrd.h:39
const Matrix & r() const
Get R of the decomposition.
Definition: Qrd.h:74
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
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: Qrd.h:62
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
compute full QRD
static void solve(const Matrix &r, const MatrixBase< B > &b, MatrixBase< X > &x)
Solve where R is an upper triangular/trapezoidal matrix. R and B row sizes must match. R must have full rank.
Definition: BackSub.h:35
double Real
Definition: Real.h:14
Qrd & calc(const MatrixBase< T > &a, Mode mode=Mode::reduced)
Calculate the QRD of matrix A.
Definition: Qrd.h:46
Matrix base class.
Definition: Base.h:17
Global Honeycomb namespace.
Qrd(const MatrixBase< T > &a, Mode mode=Mode::reduced)
Calculate the QRD of matrix A.
Definition: Qrd.h:42