I didn't post it earlier, but here's the code that creates arbitrarily shaped QR-type decompositions. The code is on a Githuib gist here but I'm putting the full code here because there's always a risk github removes the gist functionality or it in some way is damaged.
You can read the comment code to try to understand exactly what this function does, but I'd say it's easier just to run it and look at the output matrices to understand what happens. Note that if 'flipped' is set to True then while the function will still return the decomposed components in the order Q,R, it will be the case that A = RQ.
"""
Arbitrary shape QR style decompositions
Parameters
----------
A : array_like
Input matrix to be decompoosed.
corner : String
The 'dense' corner of the triangular matrix. E.g. top-right in a standard
upper triangular matrix. One of 'bl','tr','br','tl' meaning bottom left,
top right, etc.
flipped : Boolean
Determines if the decomposition should be Orthogonal Triangular, or, if
True, Triangualr Orthogonal.
Returns
-------
Q : array_like
Orthogonal Matrix.
R : array_like
Triangular Matrix.
"""
if flipped:
A = A.T
if 't' in corner:
A = np.flip(A,axis=1)
Q,R = np.linalg.qr(A)
Q = Q.T
R = R.T
if 'r' in corner:
Q = np.flip(Q,axis=0)
R = np.flip(R,axis=1)
if 't' in corner:
R = np.flip(R,axis=0)
else:
if 'l' in corner:
Q,R = np.linalg.qr(np.flip(A,axis=1))
R = np.flip(R,axis=1)
else:
Q,R = np.linalg.qr(A)
if 'b' in corner:
Q = np.flip(Q,axis=1)
R = np.flip(R,axis=0)
return Q,R
No comments:
Post a Comment