Skip to content

Commit 6af13f8

Browse files
Merge pull request #2676 from fredrik-johansson/qr3
Fix algorithm selection in gr_mat_qr; wrap QR and LQ in ctypes
2 parents d605ace + 608d996 commit 6af13f8

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

src/gr_mat/qr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ gr_mat_qr(gr_mat_t Q, gr_mat_t R, const gr_mat_t A, gr_ctx_t ctx)
184184
{
185185
GR_MAT_TMP_INIT_SHALLOW_TRANSPOSE(QT, Q, ctx);
186186

187-
status = gr_mat_lq_gso(R, QT, QT, ctx);
187+
status = gr_mat_lq(R, QT, QT, ctx);
188188

189189
status |= gr_mat_transpose(R, R, ctx);
190190
GR_MAT_SHALLOW_TRANSPOSE(Q, QT, ctx);
@@ -195,7 +195,7 @@ gr_mat_qr(gr_mat_t Q, gr_mat_t R, const gr_mat_t A, gr_ctx_t ctx)
195195
GR_MAT_TMP_INIT_SHALLOW_TRANSPOSE(AT, A, ctx);
196196
GR_MAT_TMP_INIT_SHALLOW_TRANSPOSE(QT, Q, ctx);
197197

198-
status = gr_mat_lq_gso(R, QT, AT, ctx);
198+
status = gr_mat_lq(R, QT, AT, ctx);
199199

200200
status |= gr_mat_transpose(R, R, ctx);
201201
GR_MAT_SHALLOW_TRANSPOSE(Q, QT, ctx);

src/python/flint_ctypes.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6734,6 +6734,80 @@ def diagonalization(self):
67346734
if status & GR_DOMAIN: raise ValueError
67356735
return (D, L, R)
67366736

6737+
def qr(self):
6738+
"""
6739+
QR decomposition.
6740+
6741+
>>> A = Mat(RF)([[1,2,3],[0,0,1],[0,1,0],[2,2,1]])
6742+
>>> Q, R = A.qr()
6743+
>>> Q
6744+
[[0.4472135954999579, 0.5962847939999439, 0.5716619504750294],
6745+
[0, 0, 0.5144957554275265],
6746+
[0, 0.7453559924999299, -0.5716619504750294],
6747+
[0.8944271909999159, -0.2981423969999719, -0.2858309752375147]]
6748+
>>> R
6749+
[[2.236067977499790, 2.683281572999748, 2.236067977499790],
6750+
[0, 1.341640786499874, 1.490711984999860],
6751+
[0, 0, 1.943650631615100]]
6752+
>>> (Q * R - A).norm_max() < 1e-15
6753+
True
6754+
6755+
>>> A = Mat(QQbar)([[1,2,3],[0,0,1],[0,1,0],[2,2,1]])
6756+
>>> Q, R = A.qr()
6757+
>>> Q[1,2]
6758+
Root a = 0.514496 of 34*a^2-9
6759+
>>> Q * R - A
6760+
[[0, 0, 0],
6761+
[0, 0, 0],
6762+
[0, 0, 0],
6763+
[0, 0, 0]]
6764+
6765+
>>> A = Mat(RF, 100, 100)([[i+j+i//(1+j) for i in range(100)] for j in range(100)])
6766+
>>> Q, R = A.qr()
6767+
>>> (Q * R - A).norm_max() < 1e-13
6768+
True
6769+
6770+
"""
6771+
Cmat = self.parent()
6772+
C = Cmat._element_ring
6773+
m = self.nrows()
6774+
n = self.ncols()
6775+
Q = gr_mat(m, n, context=self.parent())
6776+
R = gr_mat(n, n, context=self.parent())
6777+
status = libgr.gr_mat_qr(Q._ref, R._ref, self._ref, C._ref)
6778+
if status:
6779+
if status & GR_UNABLE: raise NotImplementedError
6780+
if status & GR_DOMAIN: raise ValueError
6781+
return (Q, R)
6782+
6783+
def lq(self):
6784+
"""
6785+
LQ decomposition.
6786+
6787+
>>> A = Mat(RF)([[1,2,3],[0,0,1],[0,1,0],[2,2,1]]).transpose()
6788+
>>> L, Q = A.lq()
6789+
>>> L
6790+
[[2.236067977499790, 0, 0],
6791+
[2.683281572999748, 1.341640786499874, 0],
6792+
[2.236067977499790, 1.490711984999860, 1.943650631615100]]
6793+
>>> Q
6794+
[[0.4472135954999579, 0, 0, 0.8944271909999159],
6795+
[0.5962847939999439, 0, 0.7453559924999299, -0.2981423969999719],
6796+
[0.5716619504750294, 0.5144957554275265, -0.5716619504750294, -0.2858309752375147]]
6797+
>>> (L * Q - A).norm_max() < 1e-15
6798+
True
6799+
"""
6800+
Cmat = self.parent()
6801+
C = Cmat._element_ring
6802+
m = self.nrows()
6803+
n = self.ncols()
6804+
L = gr_mat(m, m, context=self.parent())
6805+
Q = gr_mat(m, n, context=self.parent())
6806+
status = libgr.gr_mat_lq(L._ref, Q._ref, self._ref, C._ref)
6807+
if status:
6808+
if status & GR_UNABLE: raise NotImplementedError
6809+
if status & GR_DOMAIN: raise ValueError
6810+
return (L, Q)
67376811

67386812
#def __getitem__(self, i):
67396813
# pass

0 commit comments

Comments
 (0)