qrupdate-ng 1.2.0
Loading...
Searching...
No Matches
sqr1up.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 sqr1up(m,n,k,Q,ldq,R,ldr,u,v,w)
26!>
27!> .. Scalar Arguments ..
28!> integer m, n, k, ldq, ldr
29!> ..
30!> .. Array Arguments ..
31!> real Q(ldq,*)
32!> real R(ldr,*)
33!> real u(*)
34!> real v(*)
35!> real w(*)
36!> ..
37!> \endverbatim
38!>
39!> \par Purpose:
40! =============
41!> \verbatim
42!>
43!> SQR1UP updates a QR factorization after rank-1 modification i.e.,
44!> given a m-by-k orthogonal Q and m-by-n upper trapezoidal R, an
45!> m-vector u and n-vector v, SQR1UP updates Q -> Q1 and R ->
46!> R1 so that Q1*R1 = Q*R + u*v', and Q1 is again orthonormal and R1
47!> upper trapezoidal. (real version)
48!> \endverbatim
49!>
50!> \param[in] m
51!> \verbatim
52!> m is INTEGER
53!> The number of rows of the matrix Q. m >= 0.
54!> \endverbatim
55!>
56!> \param[in] n
57!> \verbatim
58!> n is INTEGER
59!> The number of columns of the matrix R. n >= 0.
60!> \endverbatim
61!>
62!> \param[in] k
63!> \verbatim
64!> k is INTEGER
65!> The number of columns of Q, and rows of R. Must be
66!> either k = m (full Q) or k = n < m (economical form).
67!> \endverbatim
68!>
69!> \param[in,out] Q
70!> \verbatim
71!> Q is REAL array, dimension (ldq,*)
72!> On entry, the orthogonal m-by-k matrix Q. On exit,
73!> the updated matrix Q1.
74!> \endverbatim
75!>
76!> \param[in] ldq
77!> \verbatim
78!> ldq is INTEGER
79!> The leading dimension of Q. ldq >= m.
80!> \endverbatim
81!>
82!> \param[in,out] R
83!> \verbatim
84!> R is REAL array, dimension (ldr,*)
85!> On entry, the upper trapezoidal m-by-n matrix R. On
86!> exit, the updated matrix R1.
87!> \endverbatim
88!>
89!> \param[in] ldr
90!> \verbatim
91!> ldr is INTEGER
92!> The leading dimension of R. ldr >= k.
93!> \endverbatim
94!>
95!> \param[in,out] u
96!> \verbatim
97!> u is REAL array, dimension (*)
98!> On entry, the left m-vector. On exit, if k < m,
99!> u is destroyed.
100!> \endverbatim
101!>
102!> \param[in,out] v
103!> \verbatim
104!> v is REAL array, dimension (*)
105!> On entry, the right n-vector. On exit, v is
106!> destroyed.
107!> \endverbatim
108!>
109!> \param[out] w
110!> \verbatim
111!> w is REAL array, dimension (*)
112!> A workspace vector of size 2*k.
113!> \endverbatim
114!>
115!> \ingroup qrdecomp
116subroutine sqr1up(m,n,k,Q,ldq,R,ldr,u,v,w)
117 use iso_fortran_env
119 integer, intent(in) :: m, n, k, ldq, ldr
120 real(real32), intent(inout) :: Q(ldq,*), R(ldr,*)
121 real(real32), intent(inout) :: u(*), v(*)
122 real(real32), intent(out) :: w(*)
123 external sqrqh,sqhqr,sqrot,sqrtv1,sch1up
124 external saxpy,sdot,snrm2,slamch,sscal,srot
125 real(real32) sdot,snrm2,slamch,ru,ruu
126 integer info,i
127 logical full
128 ! quick return if possible.
129 if (k == 0 .or. n == 0) return
130 ! check arguments.
131 info = 0
132 if (m < 0) then
133 info = 1
134 else if (n < 0) then
135 info = 2
136 else if (k /= m .and. (k /= n .or. n > m)) then
137 info = 3
138 else if (ldq < m) then
139 info = 5
140 else if (ldr < k) then
141 info = 7
142 endif
143 if (info /= 0) then
144 call qrupdate_xerror('SQR1UP',info)
145 return
146 end if
147 full = k == m
148 ! in the non-full case, we shall need the norm of u.
149 ru = 1.0
150 if (.not.full) ru = snrm2(m,u,1)
151 ! form Q'*u. In the non-full case, form also u - Q*Q'u.
152 do i = 1,k
153 w(i) = sdot(m,q(1,i),1,u,1)
154 if (.not.full) call saxpy(m,-w(i),q(1,i),1,u,1)
155 end do
156 ! generate rotations to eliminate Q'*u.
157 call sqrtv1(k,w,w(k+1))
158 ! apply rotations to R.
159 call sqrqh(k,n,r,ldr,w(k+1),w(2))
160 ! apply rotations to Q.
161 call sqrot('B',m,k,q,ldq,w(k+1),w(2))
162 ! update the first row of R.
163 call saxpy(n,w(1),v,1,r(1,1),ldr)
164 ! retriangularize R.
165 call sqhqr(k,n,r,ldr,w(k+1),w)
166 ! apply rotations to Q.
167 call sqrot('F',m,min(k,n+1),q,ldq,w(k+1),w)
168 ! in the full case, we're finished
169 if (full) return
170 ! compute relative residual norm
171 ruu = snrm2(m,u,1)
172 ru = ru * slamch('e')
173 if (ruu <= ru) return
174 ! update the orthogonal basis.
175 call sscal(n,ruu,v,1)
176 call sscal(m,1e0/ruu,u,1)
177 call sch1up(n,r,ldr,v,w(k+1))
178 do i = 1,n
179 call srot(m,q(1,i),1,u,1,w(k+i),v(i))
180 end do
181end subroutine
subroutine sch1up(n, r, ldr, u, w)
Updates a Cholesky factorization after a rank-1 modification.
Definition sch1up.f90:89
subroutine qrupdate_xerror(srname, info)
Dispatches error reporting to the handler.
subroutine sqrtv1(n, u, w)
Generates Givens rotations to eliminate all but the first element of a vector.
Definition sqrtv1.f90:74
subroutine sqrot(dir, m, n, q, ldq, c, s)
Applies a sequence of Givens rotations from the right to a matrix.
Definition sqrot.f90:101
subroutine sqhqr(m, n, r, ldr, c, s)
Reduces an upper Hessenberg matrix to upper trapezoidal form.
Definition sqhqr.f90:90
subroutine sqr1up(m, n, k, q, ldq, r, ldr, u, v, w)
Updates a QR factorization after a rank-1 modification.
Definition sqr1up.f90:117
subroutine sqrqh(m, n, r, ldr, c, s)
Converts an upper trapezoidal matrix to upper Hessenberg form.
Definition sqrqh.f90:86
Module for custom error handling.