GNU Octave 10.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-2025 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.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 "lo-lapack-proto.h"
41#include "oct-locbuf.h"
42#include "qrp.h"
43
45
47
48// Specialization.
49
50template <>
52void
53qrp<Matrix>::init (const Matrix& a, type qr_type)
54{
56
57 F77_INT m = to_f77_int (a.rows ());
58 F77_INT n = to_f77_int (a.cols ());
59
60 F77_INT min_mn = (m < n ? m : n);
61 OCTAVE_LOCAL_BUFFER (double, tau, min_mn);
62
63 F77_INT info = 0;
64
65 Matrix afact = a;
66 if (m > n && qr_type == qr<Matrix>::std)
67 afact.resize (m, m);
68
69 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
70
71 if (m > 0)
72 {
73 // workspace query.
74 double rlwork;
75 F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.rwdata (),
76 m, jpvt.rwdata (), tau,
77 &rlwork, -1, info));
78
79 // allocate buffer and do the job.
80 F77_INT lwork = static_cast<F77_INT> (rlwork);
81 lwork = std::max (lwork, static_cast<F77_INT> (1));
82 OCTAVE_LOCAL_BUFFER (double, work, lwork);
83
84 F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.rwdata (),
85 m, jpvt.rwdata (), tau,
86 work, lwork, info));
87 }
88 else
89 {
90 for (F77_INT i = 0; i < n; i++)
91 jpvt(i) = i+1;
92 }
93
94 // Form Permutation matrix (if economy is requested, return the
95 // indices only!)
96
97 jpvt -= static_cast<F77_INT> (1);
98 m_p = PermMatrix (jpvt, true);
99
100 form (n, afact, tau, qr_type);
101}
102
103template <>
105qrp<Matrix>::qrp (const Matrix& a, type qr_type)
106 : qr<Matrix> (), m_p ()
107{
108 init (a, qr_type);
109}
110
111template <>
115{
116 Array<double> pa (m_p.col_perm_vec ());
117 RowVector pv (MArray<double> (pa) + 1.0);
118 return pv;
119}
120
121template <>
123void
125{
127
128 F77_INT m = to_f77_int (a.rows ());
129 F77_INT n = to_f77_int (a.cols ());
130
131 F77_INT min_mn = (m < n ? m : n);
132 OCTAVE_LOCAL_BUFFER (float, tau, min_mn);
133
134 F77_INT info = 0;
135
136 FloatMatrix afact = a;
137 if (m > n && qr_type == qr<FloatMatrix>::std)
138 afact.resize (m, m);
139
140 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
141
142 if (m > 0)
143 {
144 // workspace query.
145 float rlwork;
146 F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.rwdata (),
147 m, jpvt.rwdata (), tau,
148 &rlwork, -1, info));
149
150 // allocate buffer and do the job.
151 F77_INT lwork = static_cast<F77_INT> (rlwork);
152 lwork = std::max (lwork, static_cast<F77_INT> (1));
153 OCTAVE_LOCAL_BUFFER (float, work, lwork);
154
155 F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.rwdata (),
156 m, jpvt.rwdata (), tau,
157 work, lwork, info));
158 }
159 else
160 {
161 for (F77_INT i = 0; i < n; i++)
162 jpvt(i) = i+1;
163 }
164
165 // Form Permutation matrix (if economy is requested, return the
166 // indices only!)
167
168 jpvt -= static_cast<F77_INT> (1);
169 m_p = PermMatrix (jpvt, true);
170
171 form (n, afact, tau, qr_type);
172}
173
174template <>
177 : qr<FloatMatrix> (), m_p ()
178{
179 init (a, qr_type);
180}
181
182template <>
186{
187 Array<float> pa (m_p.col_perm_vec ());
188 FloatRowVector pv (MArray<float> (pa) + 1.0f);
189 return pv;
190}
191
192template <>
194void
196{
198
199 F77_INT m = to_f77_int (a.rows ());
200 F77_INT n = to_f77_int (a.cols ());
201
202 F77_INT min_mn = (m < n ? m : n);
203 OCTAVE_LOCAL_BUFFER (Complex, tau, min_mn);
204
205 F77_INT info = 0;
206
207 ComplexMatrix afact = a;
208 if (m > n && qr_type == qr<ComplexMatrix>::std)
209 afact.resize (m, m);
210
211 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
212
213 if (m > 0)
214 {
215 OCTAVE_LOCAL_BUFFER (double, rwork, 2*n);
216
217 // workspace query.
218 Complex clwork;
219 F77_XFCN (zgeqp3, ZGEQP3, (m, n,
220 F77_DBLE_CMPLX_ARG (afact.rwdata ()),
221 m, jpvt.rwdata (),
222 F77_DBLE_CMPLX_ARG (tau),
223 F77_DBLE_CMPLX_ARG (&clwork),
224 -1, rwork, info));
225
226 // allocate buffer and do the job.
227 F77_INT lwork = static_cast<F77_INT> (clwork.real ());
228 lwork = std::max (lwork, static_cast<F77_INT> (1));
229 OCTAVE_LOCAL_BUFFER (Complex, work, lwork);
230
231 F77_XFCN (zgeqp3, ZGEQP3, (m, n,
232 F77_DBLE_CMPLX_ARG (afact.rwdata ()),
233 m, jpvt.rwdata (),
234 F77_DBLE_CMPLX_ARG (tau),
235 F77_DBLE_CMPLX_ARG (work),
236 lwork, rwork, info));
237 }
238 else
239 {
240 for (F77_INT i = 0; i < n; i++)
241 jpvt(i) = i+1;
242 }
243
244 // Form Permutation matrix (if economy is requested, return the
245 // indices only!)
246
247 jpvt -= static_cast<F77_INT> (1);
248 m_p = PermMatrix (jpvt, true);
249
250 form (n, afact, tau, qr_type);
251}
252
253template <>
256 : qr<ComplexMatrix> (), m_p ()
257{
258 init (a, qr_type);
259}
260
261template <>
265{
266 Array<double> pa (m_p.col_perm_vec ());
267 RowVector pv (MArray<double> (pa) + 1.0);
268 return pv;
269}
270
271template <>
273void
275{
277
278 F77_INT m = to_f77_int (a.rows ());
279 F77_INT n = to_f77_int (a.cols ());
280
281 F77_INT min_mn = (m < n ? m : n);
282 OCTAVE_LOCAL_BUFFER (FloatComplex, tau, min_mn);
283
284 F77_INT info = 0;
285
286 FloatComplexMatrix afact = a;
287 if (m > n && qr_type == qr<FloatComplexMatrix>::std)
288 afact.resize (m, m);
289
290 MArray<F77_INT> jpvt (dim_vector (n, 1), 0);
291
292 if (m > 0)
293 {
294 OCTAVE_LOCAL_BUFFER (float, rwork, 2*n);
295
296 // workspace query.
297 FloatComplex clwork;
298 F77_XFCN (cgeqp3, CGEQP3, (m, n,
299 F77_CMPLX_ARG (afact.rwdata ()),
300 m, jpvt.rwdata (),
301 F77_CMPLX_ARG (tau),
302 F77_CMPLX_ARG (&clwork),
303 -1, rwork, info));
304
305 // allocate buffer and do the job.
306 F77_INT lwork = static_cast<F77_INT> (clwork.real ());
307 lwork = std::max (lwork, static_cast<F77_INT> (1));
308 OCTAVE_LOCAL_BUFFER (FloatComplex, work, lwork);
309
310 F77_XFCN (cgeqp3, CGEQP3, (m, n,
311 F77_CMPLX_ARG (afact.rwdata ()),
312 m, jpvt.rwdata (),
313 F77_CMPLX_ARG (tau),
314 F77_CMPLX_ARG (work),
315 lwork, rwork, info));
316 }
317 else
318 {
319 for (F77_INT i = 0; i < n; i++)
320 jpvt(i) = i+1;
321 }
322
323 // Form Permutation matrix (if economy is requested, return the
324 // indices only!)
325
326 jpvt -= static_cast<F77_INT> (1);
327 m_p = PermMatrix (jpvt, true);
328
329 form (n, afact, tau, qr_type);
330}
331
332template <>
335 : qr<FloatComplexMatrix> (), m_p ()
336{
337 init (a, qr_type);
338}
339
340template <>
344{
345 Array<float> pa (m_p.col_perm_vec ());
346 FloatRowVector pv (MArray<float> (pa) + 1.0f);
347 return pv;
348}
349
350OCTAVE_END_NAMESPACE(math)
351OCTAVE_END_NAMESPACE(octave)
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
octave_idx_type rows() const
Definition Array.h:463
octave_idx_type cols() const
Definition Array.h:473
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:90
Definition qr.h:39
type
Definition qr.h:47
void init(const T &, type=qr< T >::std)
RV_T Pvec() const
qrp()
Definition qrp.h:47
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 liboctave_panic_if(cond)
Definition lo-error.h:100
#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 OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition oct-locbuf.h:44