qrupdate-ng 1.2.0
Loading...
Searching...
No Matches
cqr1up.f90
Go to the documentation of this file.
1! Copyright (C) 2008, 2009 VZLU Prague, a.s., Czech Republic, Jaroslav Hajek <highegg@gmail.com>
2! Copyright (C) 2026 Martin Köhler <koehlerm(AT)mpi-magdeburg.mpg.de>
3!
4! This file is part of qrupdate-ng.
5!
6! qrupdate is free software; you can redistribute it and/or modify
7! it under the terms of the GNU General Public License as published by
8! the Free Software Foundation; either version 3 of the License, or
9! (at your option) any later version.
10!
11! This program is distributed in the hope that it will be useful,
12! but WITHOUT ANY WARRANTY; without even the implied warranty of
13! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14! GNU General Public License for more details.
15!
16! You should have received a copy of the GNU General Public License
17! along with this software; see the file COPYING. If not, see
18! <http://www.gnu.org/licenses/>.
19!
20!> \brief Updates a QR factorization after a rank-1 modification.
21!>
22!> \par Definition:
23! =============
24!> \verbatim
25!> subroutine cqr1up(m,n,k,Q,ldq,R,ldr,u,v,w,rw)
26!>
27!> .. Scalar Arguments ..
28!> integer m, n, k, ldq, ldr
29!> ..
30!> .. Array Arguments ..
31!> complex Q(ldq,*)
32!> complex R(ldr,*)
33!> complex u(*)
34!> complex v(*)
35!> complex w(*)
36!> real rw(*)
37!> ..
38!> \endverbatim
39!>
40!> \par Purpose:
41! =============
42!> \verbatim
43!>
44!> CQR1UP updates a QR factorization after rank-1 modification i.e.,
45!> given a m-by-k unitary Q and m-by-n upper trapezoidal R, an m-vector
46!> u and n-vector v, CQR1UP updates Q -> Q1 and R -> R1 so that
47!> Q1*R1 = Q*R + u*v', and Q1 is again unitary and R1 upper trapezoidal.
48!> (complex version)
49!> \endverbatim
50!>
51!> \param[in] m
52!> \verbatim
53!> m is INTEGER
54!> The number of rows of the matrix Q. m >= 0.
55!> \endverbatim
56!>
57!> \param[in] n
58!> \verbatim
59!> n is INTEGER
60!> The number of columns of the matrix R. n >= 0.
61!> \endverbatim
62!>
63!> \param[in] k
64!> \verbatim
65!> k is INTEGER
66!> The number of columns of Q, and rows of R. Must be
67!> either k = m (full Q) or k = n < m (economical form).
68!> \endverbatim
69!>
70!> \param[in,out] Q
71!> \verbatim
72!> Q is COMPLEX array, dimension (ldq,*)
73!> On entry, the unitary m-by-k matrix Q. On exit,
74!> the updated matrix Q1.
75!> \endverbatim
76!>
77!> \param[in] ldq
78!> \verbatim
79!> ldq is INTEGER
80!> The leading dimension of Q. ldq >= m.
81!> \endverbatim
82!>
83!> \param[in,out] R
84!> \verbatim
85!> R is COMPLEX array, dimension (ldr,*)
86!> On entry, the upper trapezoidal m-by-n matrix R. On
87!> exit, the updated matrix R1.
88!> \endverbatim
89!>
90!> \param[in] ldr
91!> \verbatim
92!> ldr is INTEGER
93!> The leading dimension of R. ldr >= k.
94!> \endverbatim
95!>
96!> \param[in,out] u
97!> \verbatim
98!> u is COMPLEX array, dimension (*)
99!> On entry, the left m-vector. On exit, if k < m,
100!> u is destroyed.
101!> \endverbatim
102!>
103!> \param[in,out] v
104!> \verbatim
105!> v is COMPLEX array, dimension (*)
106!> On entry, the right n-vector. On exit, v is
107!> destroyed.
108!> \endverbatim
109!>
110!> \param[out] w
111!> \verbatim
112!> w is COMPLEX array, dimension (*)
113!> A workspace vector of size k.
114!> \endverbatim
115!>
116!> \param[out] rw
117!> \verbatim
118!> rw is REAL array, dimension (*)
119!> A real workspace vector of size k.
120!> \endverbatim
121!>
122!> \ingroup qrdecomp
123subroutine cqr1up(m,n,k,Q,ldq,R,ldr,u,v,w,rw)
124 use iso_fortran_env
125 use qrupdate_blas
127 integer, intent(in) :: m, n, k, ldq, ldr
128 complex(real32), intent(inout) :: Q(ldq,*)
129 complex(real32), intent(inout) :: R(ldr,*)
130 complex(real32), intent(inout) :: u(*)
131 complex(real32), intent(inout) :: v(*)
132 complex(real32), intent(out) :: w(*)
133 real(real32), intent(out) :: rw(*)
134 external cch1up, cqrqh,cqhqr,cqrot,cqrtv1,caxcpy
135 external caxpy,scnrm2,slamch,csscal,crot
136 real(real32) scnrm2,slamch,ru,ruu
137 integer info,i
138 logical full
139 ! quick return if possible.
140 if (k == 0 .or. n == 0) return
141 ! check arguments.
142 info = 0
143 if (m < 0) then
144 info = 1
145 else if (n < 0) then
146 info = 2
147 else if (k /= m .and. (k /= n .or. n > m)) then
148 info = 3
149 else if (ldq < m) then
150 info = 5
151 else if (ldr < k) then
152 info = 7
153 endif
154 if (info /= 0) then
155 call qrupdate_xerror('CQR1UP',info)
156 return
157 end if
158 full = k == m
159 ru = 1.0
160 ! in the non-full case, we shall need the norm of u.
161 if (.not.full) ru = scnrm2(m,u,1)
162 ! form Q'*u. In the non-full case, form also u - Q*Q'u.
163 do i = 1,k
164 call qrupdate_cdotc(w(i), m,q(1,i),1,u,1)
165 if (.not.full) call caxpy(m,-w(i),q(1,i),1,u,1)
166 end do
167 ! generate rotations to eliminate Q'*u.
168 call cqrtv1(k,w,rw)
169 ! apply rotations to R.
170 call cqrqh(k,n,r,ldr,rw,w(2))
171 ! apply rotations to Q.
172 call cqrot('B',m,k,q,ldq,rw,w(2))
173 ! update the first row of R.
174 call caxcpy(n,w(1),v,1,r(1,1),ldr)
175 ! retriangularize R.
176 call cqhqr(k,n,r,ldr,rw,w)
177 ! apply rotations to Q.
178 call cqrot('F',m,min(k,n+1),q,ldq,rw,w)
179 ! in the full case, we're finished
180 if (full) return
181 ! compute relative residual norm
182 ruu = scnrm2(m,u,1)
183 ru = ru * slamch('e')
184 if (ruu <= ru) return
185 ! update the orthogonal basis.
186 call csscal(n,ruu,v,1)
187 call csscal(m,1e0/ruu,u,1)
188 call cch1up(n,r,ldr,v,rw)
189 do i = 1,n
190 call crot(m,q(1,i),1,u,1,rw(i),conjg(v(i)))
191 end do
192end subroutine
subroutine caxcpy(n, a, x, incx, y, incy)
Performs scaled conjugate vector addition.
Definition caxcpy.f90:96
subroutine cch1up(n, r, ldr, u, w)
Updates a Cholesky factorization after a rank-1 modification.
Definition cch1up.f90:90
subroutine qrupdate_xerror(srname, info)
Dispatches error reporting to the handler.
subroutine cqrot(dir, m, n, q, ldq, c, s)
Applies a sequence of Givens rotations from the right to a matrix.
Definition cqrot.f90:101
subroutine cqrtv1(n, u, w)
Generates Givens rotations to eliminate all but the first element of a vector.
Definition cqrtv1.f90:75
subroutine cqr1up(m, n, k, q, ldq, r, ldr, u, v, w, rw)
Updates a QR factorization after a rank-1 modification.
Definition cqr1up.f90:124
subroutine cqhqr(m, n, r, ldr, c, s)
Reduces an upper Hessenberg matrix to upper trapezoidal form.
Definition cqhqr.f90:91
subroutine cqrqh(m, n, r, ldr, c, s)
Converts an upper trapezoidal matrix to upper Hessenberg form.
Definition cqrqh.f90:89
Module for custom error handling.