Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR factorisation of matrices #654

Merged
merged 6 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make C++98 compliant
  • Loading branch information
unordinateur committed Jul 6, 2017
commit f9962054d9f0c46fba55abc44351a4845843b586
5 changes: 2 additions & 3 deletions glm/gtx/matrix_factorisation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#pragma once

// Dependency:
#include <algorithm>
#include "../glm.hpp"

#ifndef GLM_ENABLE_EXPERIMENTAL
Expand Down Expand Up @@ -49,15 +48,15 @@ namespace glm{
/// Given an n-by-m input matrix, q has dimensions min(n,m)-by-m, and r has dimensions n-by-min(n,m).
/// From GLM_GTX_matrix_factorisation extension.
template <length_t C, length_t R, typename T, precision P, template<length_t, length_t, typename, precision> class matType>
GLM_FUNC_DECL void qr_decompose(matType<std::min(C, R), R, T, P>& q, matType<C, std::min(C, R), T, P>& r, const matType<C, R, T, P>& in);
GLM_FUNC_DECL void qr_decompose(matType<(C < R ? C : R), R, T, P>& q, matType<C, (C < R ? C : R), T, P>& r, const matType<C, R, T, P>& in);

/// Performs RQ factorisation of a matrix.
/// Returns 2 matrices, r and q, such that r is an upper triangular matrix, the rows of q are orthonormal and span the same subspace than those of the input matrix, and r*q=in.
/// Note that in the context of RQ factorisation, the diagonal is seen as starting in the lower-right corner of the matrix, instead of the usual upper-left.
/// Given an n-by-m input matrix, r has dimensions min(n,m)-by-m, and q has dimensions n-by-min(n,m).
/// From GLM_GTX_matrix_factorisation extension.
template <length_t C, length_t R, typename T, precision P, template<length_t, length_t, typename, precision> class matType>
GLM_FUNC_DECL void rq_decompose(matType<std::min(C, R), R, T, P>& r, matType<C, std::min(C, R), T, P>& q, const matType<C, R, T, P>& in);
GLM_FUNC_DECL void rq_decompose(matType<(C < R ? C : R), R, T, P>& r, matType<C, (C < R ? C : R), T, P>& q, const matType<C, R, T, P>& in);

/// @}
}
Expand Down
10 changes: 5 additions & 5 deletions glm/gtx/matrix_factorisation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ namespace glm {
}

template <length_t C, length_t R, typename T, precision P, template<length_t, length_t, typename, precision> class matType>
GLM_FUNC_QUALIFIER void qr_decompose(matType<std::min(C, R), R, T, P>& q, matType<C, std::min(C, R), T, P>& r, const matType<C, R, T, P>& in) {
GLM_FUNC_QUALIFIER void qr_decompose(matType<(C < R ? C : R), R, T, P>& q, matType<C, (C < R ? C : R), T, P>& r, const matType<C, R, T, P>& in) {
// Uses modified Gram-Schmidt method
// Source: https://en.wikipedia.org/wiki/Gram�Schmidt_process
// And https://en.wikipedia.org/wiki/QR_decomposition

//For all the linearly independs columns of the input...
// (there can be no more linearly independents columns than there are rows.)
for (length_t i = 0; i < std::min(R, C); i++) {
for (length_t i = 0; i < (C < R ? C : R); i++) {
//Copy in Q the input's i-th column.
q[i] = in[i];

Expand All @@ -55,7 +55,7 @@ namespace glm {
}

template <length_t C, length_t R, typename T, precision P, template<length_t, length_t, typename, precision> class matType>
GLM_FUNC_QUALIFIER void rq_decompose(matType<std::min(C, R), R, T, P>& r, matType<C, std::min(C, R), T, P>& q, const matType<C, R, T, P>& in) {
GLM_FUNC_QUALIFIER void rq_decompose(matType<(C < R ? C : R), R, T, P>& r, matType<C, (C < R ? C : R), T, P>& q, const matType<C, R, T, P>& in) {
// From https://en.wikipedia.org/wiki/QR_decomposition:
// The RQ decomposition transforms a matrix A into the product of an upper triangular matrix R (also known as right-triangular) and an orthogonal matrix Q. The only difference from QR decomposition is the order of these matrices.
// QR decomposition is Gram�Schmidt orthogonalization of columns of A, started from the first column.
Expand All @@ -64,8 +64,8 @@ namespace glm {
matType<R, C, T, P> tin = transpose(in);
tin = fliplr(tin);

matType<R, std::min(C, R), T, P> tr;
matType<std::min(C, R), C, T, P> tq;
matType<R, (C < R ? C : R), T, P> tr;
matType<(C < R ? C : R), C, T, P> tq;
qr_decompose(tq, tr, tin);

tr = fliplr(tr);
Expand Down
20 changes: 10 additions & 10 deletions test/gtx/gtx_matrix_factorisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const double epsilon = 1e-10f;

template <glm::length_t C, glm::length_t R, typename T, glm::precision P, template<glm::length_t, glm::length_t, typename, glm::precision> class matType>
int test_qr(matType<C, R, T, P> m) {
matType<std::min(C, R), R, T, P> q(-999);
matType<C, std::min(C, R), T, P> r(-999);
matType<(C < R ? C : R), R, T, P> q(-999);
matType<C, (C < R ? C : R), T, P> r(-999);

glm::qr_decompose(q, r, m);

Expand All @@ -21,7 +21,7 @@ int test_qr(matType<C, R, T, P> m) {
}

//Test if the columns of q are orthonormal
for (glm::length_t i = 0; i < std::min(C, R); i++) {
for (glm::length_t i = 0; i < (C < R ? C : R); i++) {
if ((length(q[i]) - 1) > epsilon) return 2;

for (glm::length_t j = 0; j<i; j++) {
Expand All @@ -31,7 +31,7 @@ int test_qr(matType<C, R, T, P> m) {

//Test if the matrix r is upper triangular
for (glm::length_t i = 0; i < C; i++) {
for (glm::length_t j = i + 1; j < std::min(C, R); j++) {
for (glm::length_t j = i + 1; j < (C < R ? C : R); j++) {
if (r[i][j] != 0) return 4;
}
}
Expand All @@ -41,8 +41,8 @@ int test_qr(matType<C, R, T, P> m) {

template <glm::length_t C, glm::length_t R, typename T, glm::precision P, template<glm::length_t, glm::length_t, typename, glm::precision> class matType>
int test_rq(matType<C, R, T, P> m) {
matType<C, std::min(C, R), T, P> q(-999);
matType<std::min(C, R), R, T, P> r(-999);
matType<C, (C < R ? C : R), T, P> q(-999);
matType<(C < R ? C : R), R, T, P> r(-999);

glm::rq_decompose(r, q, m);

Expand All @@ -58,9 +58,9 @@ int test_rq(matType<C, R, T, P> m) {


//Test if the rows of q are orthonormal
matType<std::min(C, R), C, T, P> tq = transpose(q);
matType<(C < R ? C : R), C, T, P> tq = transpose(q);

for (glm::length_t i = 0; i < std::min(C, R); i++) {
for (glm::length_t i = 0; i < (C < R ? C : R); i++) {
if ((length(tq[i]) - 1) > epsilon) return 2;

for (glm::length_t j = 0; j<i; j++) {
Expand All @@ -69,8 +69,8 @@ int test_rq(matType<C, R, T, P> m) {
}

//Test if the matrix r is upper triangular
for (glm::length_t i = 0; i < std::min(C, R); i++) {
for (glm::length_t j = R - std::min(C, R) + i + 1; j < R; j++) {
for (glm::length_t i = 0; i < (C < R ? C : R); i++) {
for (glm::length_t j = R - (C < R ? C : R) + i + 1; j < R; j++) {
if (r[i][j] != 0) return 4;
}
}
Expand Down