GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
qrp.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1994-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <algorithm>
31
32#include "Array-oct.h"
33#include "CMatrix.h"
34#include "MArray.h"
35#include "dMatrix.h"
36#include "dRowVector.h"
37#include "fCMatrix.h"
38#include "fMatrix.h"
39#include "fRowVector.h"
40#include "lapack-proto.h"
41#include "oct-locbuf.h"
42#include "qrp.h"
43
46
47// Specialization.
48
49template <>
51void
52qrp<Matrix>::init (const Matrix& a, type qr_type)
53{
55
56 F77_INT m = to_f77_int (a.rows ());
57 F77_INT n = to_f77_int (a.cols ());
58
59 F77_INT min_mn = (m < n ? m : n);
60 OCTAVE_LOCAL_BUFFER (double, tau, min_mn);
61
62 F77_INT info = 0;
63
64 Matrix afact = a;
65 if (m > n && qr_type == qr<Matrix>::std)
66 afact.resize (m, m);
67
68 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
69
70 if (m > 0)
71 {
72 // workspace query.
73 double rlwork;
74 F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.rwdata (),
75 m, jpvt.rwdata (), tau,
76 &rlwork, -1, info));
77
78 // allocate buffer and do the job.
79 F77_INT lwork = static_cast<F77_INT> (rlwork);
80 lwork = std::max (lwork, static_cast<F77_INT> (1));
81 OCTAVE_LOCAL_BUFFER (double, work, lwork);
82
83 F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.rwdata (),
84 m, jpvt.rwdata (), tau,
85 work, lwork, info));
86 }
87 else
88 {
89 for (F77_INT i = 0; i < n; i++)
90 jpvt(i) = i+1;
91 }
92
93 // Form Permutation matrix (if economy is requested, return the
94 // indices only!)
95
96 jpvt -= static_cast<F77_INT> (1);
97 m_p = PermMatrix (jpvt, true);
98
99 form (n, afact, tau, qr_type);
100}
101
102template <>
104qrp<Matrix>::qrp (const Matrix& a, type qr_type)
105 : qr<Matrix> (), m_p ()
106{
107 init (a, qr_type);
108}
109
110template <>
114{
115 Array<double> pa (m_p.col_perm_vec ());
116 RowVector pv (MArray<double> (pa) + 1.0);
117 return pv;
118}
119
120template <>
122void
124{
126
127 F77_INT m = to_f77_int (a.rows ());
128 F77_INT n = to_f77_int (a.cols ());
129
130 F77_INT min_mn = (m < n ? m : n);
131 OCTAVE_LOCAL_BUFFER (float, tau, min_mn);
132
133 F77_INT info = 0;
134
135 FloatMatrix afact = a;
136 if (m > n && qr_type == qr<FloatMatrix>::std)
137 afact.resize (m, m);
138
139 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
140
141 if (m > 0)
142 {
143 // workspace query.
144 float rlwork;
145 F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.rwdata (),
146 m, jpvt.rwdata (), tau,
147 &rlwork, -1, info));
148
149 // allocate buffer and do the job.
150 F77_INT lwork = static_cast<F77_INT> (rlwork);
151 lwork = std::max (lwork, static_cast<F77_INT> (1));
152 OCTAVE_LOCAL_BUFFER (float, work, lwork);
153
154 F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.rwdata (),
155 m, jpvt.rwdata (), tau,
156 work, lwork, info));
157 }
158 else
159 {
160 for (F77_INT i = 0; i < n; i++)
161 jpvt(i) = i+1;
162 }
163
164 // Form Permutation matrix (if economy is requested, return the
165 // indices only!)
166
167 jpvt -= static_cast<F77_INT> (1);
168 m_p = PermMatrix (jpvt, true);
169
170 form (n, afact, tau, qr_type);
171}
172
173template <>
176 : qr<FloatMatrix> (), m_p ()
177{
178 init (a, qr_type);
179}
180
181template <>
185{
186 Array<float> pa (m_p.col_perm_vec ());
187 FloatRowVector pv (MArray<float> (pa) + 1.0f);
188 return pv;
189}
190
191template <>
193void
195{
197
198 F77_INT m = to_f77_int (a.rows ());
199 F77_INT n = to_f77_int (a.cols ());
200
201 F77_INT min_mn = (m < n ? m : n);
202 OCTAVE_LOCAL_BUFFER (Complex, tau, min_mn);
203
204 F77_INT info = 0;
205
206 ComplexMatrix afact = a;
207 if (m > n && qr_type == qr<ComplexMatrix>::std)
208 afact.resize (m, m);
209
210 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
211
212 if (m > 0)
213 {
214 OCTAVE_LOCAL_BUFFER (double, rwork, 2*n);
215
216 // workspace query.
217 Complex clwork;
218 F77_XFCN (zgeqp3, ZGEQP3, (m, n,
219 F77_DBLE_CMPLX_ARG (afact.rwdata ()),
220 m, jpvt.rwdata (),
221 F77_DBLE_CMPLX_ARG (tau),
222 F77_DBLE_CMPLX_ARG (&clwork),
223 -1, rwork, info));
224
225 // allocate buffer and do the job.
226 F77_INT lwork = static_cast<F77_INT> (clwork.real ());
227 lwork = std::max (lwork, static_cast<F77_INT> (1));
228 OCTAVE_LOCAL_BUFFER (Complex, work, lwork);
229
230 F77_XFCN (zgeqp3, ZGEQP3, (m, n,
231 F77_DBLE_CMPLX_ARG (afact.rwdata ()),
232 m, jpvt.rwdata (),
233 F77_DBLE_CMPLX_ARG (tau),
234 F77_DBLE_CMPLX_ARG (work),
235 lwork, rwork, info));
236 }
237 else
238 {
239 for (F77_INT i = 0; i < n; i++)
240 jpvt(i) = i+1;
241 }
242
243 // Form Permutation matrix (if economy is requested, return the
244 // indices only!)
245
246 jpvt -= static_cast<F77_INT> (1);
247 m_p = PermMatrix (jpvt, true);
248
249 form (n, afact, tau, qr_type);
250}
251
252template <>
255 : qr<ComplexMatrix> (), m_p ()
256{
257 init (a, qr_type);
258}
259
260template <>
264{
265 Array<double> pa (m_p.col_perm_vec ());
266 RowVector pv (MArray<double> (pa) + 1.0);
267 return pv;
268}
269
270template <>
272void
274{
276
277 F77_INT m = to_f77_int (a.rows ());
278 F77_INT n = to_f77_int (a.cols ());
279
280 F77_INT min_mn = (m < n ? m : n);
281 OCTAVE_LOCAL_BUFFER (FloatComplex, tau, min_mn);
282
283 F77_INT info = 0;
284
285 FloatComplexMatrix afact = a;
286 if (m > n && qr_type == qr<FloatComplexMatrix>::std)
287 afact.resize (m, m);
288
289 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
290
291 if (m > 0)
292 {
293 OCTAVE_LOCAL_BUFFER (float, rwork, 2*n);
294
295 // workspace query.
296 FloatComplex clwork;
297 F77_XFCN (cgeqp3, CGEQP3, (m, n,
298 F77_CMPLX_ARG (afact.rwdata ()),
299 m, jpvt.rwdata (),
300 F77_CMPLX_ARG (tau),
301 F77_CMPLX_ARG (&clwork),
302 -1, rwork, info));
303
304 // allocate buffer and do the job.
305 F77_INT lwork = static_cast<F77_INT> (clwork.real ());
306 lwork = std::max (lwork, static_cast<F77_INT> (1));
307 OCTAVE_LOCAL_BUFFER (FloatComplex, work, lwork);
308
309 F77_XFCN (cgeqp3, CGEQP3, (m, n,
310 F77_CMPLX_ARG (afact.rwdata ()),
311 m, jpvt.rwdata (),
312 F77_CMPLX_ARG (tau),
313 F77_CMPLX_ARG (work),
314 lwork, rwork, info));
315 }
316 else
317 {
318 for (F77_INT i = 0; i < n; i++)
319 jpvt(i) = i+1;
320 }
321
322 // Form Permutation matrix (if economy is requested, return the
323 // indices only!)
324
325 jpvt -= static_cast<F77_INT> (1);
326 m_p = PermMatrix (jpvt, true);
327
328 form (n, afact, tau, qr_type);
329}
330
331template <>
334 : qr<FloatComplexMatrix> (), m_p ()
335{
336 init (a, qr_type);
337}
338
339template <>
343{
344 Array<float> pa (m_p.col_perm_vec ());
345 FloatRowVector pv (MArray<float> (pa) + 1.0f);
346 return pv;
347}
348
349OCTAVE_END_NAMESPACE(math)
350OCTAVE_END_NAMESPACE(octave)
N Dimensional Array with copy-on-write semantics.
Definition Array-base.h:130
octave_idx_type rows() const
Definition Array-base.h:485
octave_idx_type cols() const
Definition Array-base.h:495
T * rwdata()
Size of the specified dimension.
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
Definition CMatrix.h:191
void resize(octave_idx_type nr, octave_idx_type nc, const FloatComplex &rfv=FloatComplex(0))
Definition fCMatrix.h:199
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition fMatrix.h:156
Template for N-dimensional array classes with like-type math operators.
Definition MArray.h:61
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition dMatrix.h:156
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
Definition qr.h:38
type
Definition qr.h:46
void init(const T &, type=qr< T >::std)
RV_T Pvec() const
qrp()
Definition qrp.h:46
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define F77_DBLE_CMPLX_ARG(x)
Definition f77-fcn.h:316
#define F77_CMPLX_ARG(x)
Definition f77-fcn.h:310
#define F77_XFCN(f, F, args)
Definition f77-fcn.h:45
octave_f77_int_type F77_INT
Definition f77-fcn.h:306
#define OCTAVE_API
Definition main.in.cc:55
std::complex< double > Complex
Definition oct-cmplx.h:33
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
#define liboctave_panic_if(cond)
Definition oct-error.h:100
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition oct-locbuf.h:44