philsupertramp/game-math
Loading...
Searching...
No Matches
qr.h
Go to the documentation of this file.
1#pragma once
2#include "../../Matrix.h"
3#include "../utils.h"
4
5
12std::pair<Matrix<double>, Matrix<double>> qr(const Matrix<double>& A) {
13 size_t num_rows = A.rows();
14 size_t num_cols = A.columns();
15
16 auto Q = zeros(num_rows, num_rows);
17 size_t col = 0;
18
19 for(size_t i_a = 0; i_a < num_cols; i_a++) {
20 auto a = A.GetSlice(0, num_rows - 1, i_a, i_a);
21 auto u = a;
22 for(size_t i = 0; i < col; i++) {
23 auto q = Q.GetSlice(0, num_rows - 1, i, i);
24 auto proj = (q.Transpose() * a)(0, 0) * q;
25 u -= proj;
26 }
27 auto e = 1.0 / norm(u) * u;
28 Q.SetSlice(0, e.rows() - 1, col, col, e);
29 col += 1;
30 }
31 return std::make_pair(Q, Q.Transpose() * A);
32}
33
Definition: Matrix.h:42
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
Matrix GetSlice(size_t rowStart) const
Definition: Matrix.h:609
double norm(const Matrix< double > &in)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
std::pair< Matrix< double >, Matrix< double > > qr(const Matrix< double > &A)
Definition: qr.h:12