How do I make the diagonal elements of R in a RQ decomposition positive?

46 Views Asked by At

I am using scipy.linalg.rq()to RQ decompose: $$ M = \begin{bmatrix}353.553 & 339.645 & 277.744\\-103.528 & 23.3212 & 459.607\\0.707107 & -0.353553 & 0.612372\end{bmatrix} $$ and get the results: $$ R = \begin{bmatrix} 468.2 & -91.2 & -300.0\\ 0 & -427.2 & -200.0\\ 0 & 0 & -1.0\end{bmatrix} $$

$$ Q = \begin{bmatrix}0.41380 & 0.90915 & 0.04708\\ 0.57338 & -0.22011 & -0.78917\\ -0.70711 & 0.35355 & -0.61237 \end{bmatrix} $$

However the result should be:

enter image description here

since the elements on the diagonal of R must be positive in my case.

How can I "flip" signs of R and Q elements in Python using numpy and scipy so that the diagonal elements of R are positive?

1

There are 1 best solutions below

0
On BEST ANSWER
import numpy as np
import scipy as sp

'''
RQ decomposition with positive elements along the diagonal of R
'''
def RQ(M):
    R, Q = sp.linalg.rq(M)
    (m, m) = R.shape
    for i in range(0,m):
        if R[i,i] < 0:
            R[:,i] *= -1    
            Q[i,:] *= -1
    return [R, Q]