#pragma once #include "mat.h" #include "det.h" #include "functions.h" #include "up_tri_mat.h" #include #include namespace cgv { namespace math { //qr decomposition returns false if matrix is singular template bool qr(const mat &a, mat &q, up_tri_mat &r) { mat rr = a; bool sing =false; unsigned n = a.nrows(); unsigned m = a.ncols(); assert(n == m); q.resize(n,n); r.resize(n); unsigned i,j,k; vec c(n), d(n); T scale,sigma,sum,tau; for (k=0;k bool qr_mgs(const mat &a, mat &q, up_tri_mat &r) { unsigned n = a.nrows(); unsigned m = a.ncols(); assert(n == m); q.resize(n,n); r.resize(n); for(unsigned j = 0; j < m; j++) { vec v = a.col(j); for(unsigned i = 0; i < j; i++) { r(i,j) = dot(q.col(i),v); v= v- r(i,j)*q.col(i); } r(j,j) = length(v); if(std::abs(r(j,j)) == 0) return false; q.set_col(j, v/r(j,j)); } return true; } // rq decomposition using tricky column and row permutations template bool rq(const mat &a, up_tri_mat &r, mat &q) { unsigned N = a.nrows(); r.resize(N); q.resize(N,N); mat p=transpose(a); p.fliplr(); p.flipud(); if(!qr(p,q,r)) return false; mat rr =r; rr.transpose(); rr.fliplr(); rr.flipud(); q.transpose(); q.fliplr(); q.flipud(); if (det(q)<0) { rr.set_col(0,-rr.col(0)); q.set_row(0,-q.row(0)); } for(unsigned i = 0; i< r.nrows();i++) for(unsigned j = i; j< r.ncols();j++) r(i,j)=rr(i,j); return true; } } }