qrupdate-ng 1.2.0
Loading...
Searching...
No Matches
cqrinc.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 cqrinc(m,n,k,Q,ldq,R,ldr,j,x,rw)
26!>
27!> .. Scalar Arguments ..
28!> integer m, n, k, ldq, ldr, j
29!> ..
30!> .. Array Arguments ..
31!> complex Q(ldq,*)
32!> complex R(ldr,*)
33!> complex x(*)
34!> real rw(*)
35!> ..
36!> \endverbatim
37!>
38!> \par Purpose:
39! =============
40!> \verbatim
41!>
42!> CQRINC updates a QR factorization after inserting a new column. i.e.,
43!> given an m-by-k unitary matrix Q, an m-by-n upper trapezoidal matrix
44!> R and index j in the range 1:n+1, CQRINC updates the matrix
45!> Q -> Q1 and R -> R1 so that Q1 is again unitary, R1 upper
46!> trapezoidal, and Q1*R1 = [A(:,1:j-1); x; A(:,j:n)], where A = Q*R.
47!> (complex 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 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 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 COMPLEX array, dimension (*)
105!> The column being inserted.
106!> \endverbatim
107!>
108!> \param[out] rw
109!> \verbatim
110!> rw is REAL array, dimension (*)
111!> A real workspace vector of size k.
112!> \endverbatim
113!>
114!> \ingroup qrdecomp
115subroutine cqrinc(m,n,k,Q,ldq,R,ldr,j,x,rw)
116 use iso_fortran_env
117 use qrupdate_blas
119 integer, intent(in) :: m, n, k, ldq, ldr, j
120 complex(real32), intent(inout) :: Q(ldq,*), R(ldr,*)
121 complex(real32), intent(in) :: x(*)
122 real(real32), intent(out) :: rw(*)
123 external cgqvec, cqrtv1,cqrqh,cqrot
124 external ccopy,caxpy,csscal,scnrm2
125 real(real32) scnrm2,rx
126 integer info,i,k1
127 logical full
128 ! quick return if possible.
129 if (m == 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 < min(m,k+1)) then
141 info = 7
142 else if (j < 1 .or. j > n+1) then
143 info = 8
144 end if
145 if (info /= 0) then
146 call qrupdate_xerror('CQRINC',info)
147 return
148 end if
149 full = k == m
150 ! insert empty column at j-th position
151 do i = n,j,-1
152 call ccopy(k,r(1,i),1,r(1,i+1),1)
153 end do
154 ! insert Q'*u into R. In the nonfull case, form also u-Q*Q'*u.
155 if (full) then
156 k1 = k
157 do i = 1,k
158 call qrupdate_cdotc(r(i,j), m,q(1,i),1,x,1)
159 end do
160 else
161 k1 = k + 1
162 ! zero last row of R
163 do i = 1,n+1
164 r(k1,i) = 0e0
165 end do
166 call ccopy(m,x,1,q(1,k1),1)
167 do i = 1,k
168 call qrupdate_cdotc(r(i,j), m,q(1,i),1,q(1,k1),1)
169 call caxpy(m,-r(i,j),q(1,i),1,q(1,k1),1)
170 end do
171 ! get norm of the inserted column
172 rx = scnrm2(m,q(1,k1),1)
173 r(k1,j) = rx
174 if (rx == 0e0) then
175 ! in the rare case when rx is exact zero, we still need to provide
176 ! a valid orthogonal unit vector. The details are boring, so handle
177 ! that elsewhere.
178 call cgqvec(m,k,q,ldq,q(1,k1))
179 else
180 ! otherwise, just normalize the added column.
181 call csscal(m,1e0/rx,q(1,k1),1)
182 end if
183 end if
184 ! maybe we're finished.
185 if (j > k) return
186 ! eliminate the spike.
187 call cqrtv1(k1+1-j,r(j,j),rw)
188 ! apply rotations to R(j:k,j:n).
189 if (j <= n) call cqrqh(k1+1-j,n+1-j,r(j,j+1),ldr,rw,r(j+1,j))
190 ! apply rotations to Q(:,j:k).
191 call cqrot('B',m,k1+1-j,q(1,j),ldq,rw,r(j+1,j))
192 ! zero spike.
193 do i = j+1,k1
194 r(i,j) = 0e0
195 end do
196end subroutine
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 cqrinc(m, n, k, q, ldq, r, ldr, j, x, rw)
Updates a QR factorization after inserting a new column.
Definition cqrinc.f90:116
subroutine cgqvec(m, n, q, ldq, u)
Generates a unit vector orthogonal to the column space of a unitary matrix.
Definition cgqvec.f90:83
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.