qrupdate-ng 1.2.0
Loading...
Searching...
No Matches
sqrinc.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 inserting a new column.
21!>
22!> \par Definition:
23! =============
24!> \verbatim
25!> subroutine sqrinc(m,n,k,Q,ldq,R,ldr,j,x,w)
26!>
27!> .. Scalar Arguments ..
28!> integer m, n, k, ldq, ldr, j
29!> ..
30!> .. Array Arguments ..
31!> real Q(ldq,*)
32!> real R(ldr,*)
33!> real x(*)
34!> real w(*)
35!> ..
36!> \endverbatim
37!>
38!> \par Purpose:
39! =============
40!> \verbatim
41!>
42!> SQRINC updates a QR factorization after inserting a new column. i.e.,
43!> given an m-by-k orthogonal matrix Q, an m-by-n upper trapezoidal
44!> matrix R and index j in the range 1:n+1, SQRINC updates the
45!> matrix Q -> Q1 and R -> R1 so that Q1 is again orthogonal, R1 upper
46!> trapezoidal, and Q1*R1 = [A(:,1:j-1); x; A(:,j:n)], where A = Q*R.
47!> (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!> basis dimension will increase).
68!> \endverbatim
69!>
70!> \param[in,out] Q
71!> \verbatim
72!> Q is REAL array, dimension (ldq,*)
73!> On entry, the orthogonal 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 REAL array, dimension (ldr,*)
86!> On entry, the original matrix R. On exit, the
87!> updated matrix R1.
88!> \endverbatim
89!>
90!> \param[in] ldr
91!> \verbatim
92!> ldr is INTEGER
93!> The leading dimension of R. ldr >= min(m,n+1).
94!> \endverbatim
95!>
96!> \param[in] j
97!> \verbatim
98!> j is INTEGER
99!> The position of the new column in R1. 1 <= j <= n+1.
100!> \endverbatim
101!>
102!> \param[in] x
103!> \verbatim
104!> x is REAL array, dimension (*)
105!> The column being inserted.
106!> \endverbatim
107!>
108!> \param[out] w
109!> \verbatim
110!> w is REAL array, dimension (*)
111!> A workspace vector of size k.
112!> \endverbatim
113!>
114!> \ingroup qrdecomp
115subroutine sqrinc(m,n,k,Q,ldq,R,ldr,j,x,w)
116 use iso_fortran_env
118 integer, intent(in) :: m, n, k, ldq, ldr, j
119 real(real32), intent(inout) :: Q(ldq,*), R(ldr,*)
120 real(real32), intent(in) :: x(*)
121 real(real32), intent(out) :: w(*)
122 external sqrtv1,sqrqh,sqrot
123 external scopy,sdot,saxpy,sscal,snrm2,sgqvec
124 real(real32) sdot,snrm2,rx
125 integer info,i,k1
126 logical full
127 ! quick return if possible.
128 if (m == 0) return
129 ! check arguments.
130 info = 0
131 if (m < 0) then
132 info = 1
133 else if (n < 0) then
134 info = 2
135 else if (k /= m .and. (k /= n .or. n >= m)) then
136 info = 3
137 else if (ldq < m) then
138 info = 5
139 else if (ldr < min(m,k+1)) then
140 info = 7
141 else if (j < 1 .or. j > n+1) then
142 info = 8
143 end if
144 if (info /= 0) then
145 call qrupdate_xerror('SQRINC',info)
146 return
147 end if
148 full = k == m
149 ! insert empty column at j-th position.
150 do i = n,j,-1
151 call scopy(k,r(1,i),1,r(1,i+1),1)
152 end do
153 ! insert Q'*u into R. In the nonfull case, form also u-Q*Q'*u.
154 if (full) then
155 k1 = k
156 do i = 1,k
157 r(i,j) = sdot(m,q(1,i),1,x,1)
158 end do
159 else
160 k1 = k + 1
161 ! zero last row of R
162 do i = 1,n+1
163 r(k1,i) = 0e0
164 end do
165 call scopy(m,x,1,q(1,k1),1)
166 do i = 1,k
167 r(i,j) = sdot(m,q(1,i),1,q(1,k1),1)
168 call saxpy(m,-r(i,j),q(1,i),1,q(1,k1),1)
169 end do
170 ! get norm of the inserted column
171 rx = snrm2(m,q(1,k1),1)
172 r(k1,j) = rx
173 if (rx == 0e0) then
174 ! in the rare case when rx is exact zero, we still need to provide
175 ! a valid orthogonal unit vector. The details are boring, so handle
176 ! that elsewhere.
177 call sgqvec(m,k,q,ldq,q(1,k1))
178 else
179 ! otherwise, just normalize the added column.
180 call sscal(m,1e0/rx,q(1,k1),1)
181 end if
182 end if
183 ! maybe we're finished.
184 if (j > k) return
185 ! eliminate the spike.
186 call sqrtv1(k1+1-j,r(j,j),w)
187 ! apply rotations to R(j:k,j:n).
188 if (j <= n) call sqrqh(k1+1-j,n+1-j,r(j,j+1),ldr,w,r(j+1,j))
189 ! apply rotations to Q(:,j:k).
190 call sqrot('B',m,k1+1-j,q(1,j),ldq,w,r(j+1,j))
191 ! zero spike.
192 do i = j+1,k1
193 r(i,j) = 0e0
194 end do
195end subroutine
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 sgqvec(m, n, q, ldq, u)
Generates a unit vector orthogonal to the column space of a unitary matrix.
Definition sgqvec.f90:83
subroutine sqrinc(m, n, k, q, ldq, r, ldr, j, x, w)
Updates a QR factorization after inserting a new column.
Definition sqrinc.f90:116
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.