GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qrp.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 John W. Eatonn
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <cassert>
29 
30 #include "CMatrix.h"
31 #include "dMatrix.h"
32 #include "dRowVector.h"
33 #include "fCMatrix.h"
34 #include "fMatrix.h"
35 #include "fRowVector.h"
36 #include "lo-error.h"
37 #include "lo-lapack-proto.h"
38 #include "oct-locbuf.h"
39 #include "qrp.h"
40 
41 namespace octave
42 {
43  namespace math
44  {
45  // Specialization.
46 
47  template <>
48  void
50  {
51  assert (qr_type != qr<Matrix>::raw);
52 
53  octave_idx_type m = a.rows ();
54  octave_idx_type n = a.cols ();
55 
56  octave_idx_type min_mn = m < n ? m : n;
57  OCTAVE_LOCAL_BUFFER (double, tau, min_mn);
58 
59  octave_idx_type info = 0;
60 
61  Matrix afact = a;
62  if (m > n && qr_type == qr<Matrix>::std)
63  afact.resize (m, m);
64 
65  MArray<octave_idx_type> jpvt (dim_vector (n, 1), 0);
66 
67  if (m > 0)
68  {
69  // workspace query.
70  double rlwork;
71  F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.fortran_vec (),
72  m, jpvt.fortran_vec (), tau,
73  &rlwork, -1, info));
74 
75  // allocate buffer and do the job.
76  octave_idx_type lwork = rlwork;
77  lwork = std::max (lwork, static_cast<octave_idx_type> (1));
78  OCTAVE_LOCAL_BUFFER (double, work, lwork);
79  F77_XFCN (dgeqp3, DGEQP3, (m, n, afact.fortran_vec (),
80  m, jpvt.fortran_vec (), tau,
81  work, lwork, info));
82  }
83  else
84  for (octave_idx_type i = 0; i < n; i++) jpvt(i) = i+1;
85 
86  // Form Permutation matrix (if economy is requested, return the
87  // indices only!)
88 
89  jpvt -= static_cast<octave_idx_type> (1);
90  p = PermMatrix (jpvt, true);
91 
92  form (n, afact, tau, qr_type);
93  }
94 
95  template <>
97  : qr<Matrix> (), p ()
98  {
99  init (a, qr_type);
100  }
101 
102  template <>
103  RowVector
104  qrp<Matrix>::Pvec (void) const
105  {
106  Array<double> pa (p.col_perm_vec ());
107  RowVector pv (MArray<double> (pa) + 1.0);
108  return pv;
109  }
110 
111  template <>
112  void
114  {
115  assert (qr_type != qr<FloatMatrix>::raw);
116 
117  octave_idx_type m = a.rows ();
118  octave_idx_type n = a.cols ();
119 
120  octave_idx_type min_mn = m < n ? m : n;
121  OCTAVE_LOCAL_BUFFER (float, tau, min_mn);
122 
123  octave_idx_type info = 0;
124 
125  FloatMatrix afact = a;
126  if (m > n && qr_type == qr<FloatMatrix>::std)
127  afact.resize (m, m);
128 
129  MArray<octave_idx_type> jpvt (dim_vector (n, 1), 0);
130 
131  if (m > 0)
132  {
133  // workspace query.
134  float rlwork;
135  F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.fortran_vec (),
136  m, jpvt.fortran_vec (), tau,
137  &rlwork, -1, info));
138 
139  // allocate buffer and do the job.
140  octave_idx_type lwork = rlwork;
141  lwork = std::max (lwork, static_cast<octave_idx_type> (1));
142  OCTAVE_LOCAL_BUFFER (float, work, lwork);
143  F77_XFCN (sgeqp3, SGEQP3, (m, n, afact.fortran_vec (),
144  m, jpvt.fortran_vec (), tau,
145  work, lwork, info));
146  }
147  else
148  for (octave_idx_type i = 0; i < n; i++) jpvt(i) = i+1;
149 
150  // Form Permutation matrix (if economy is requested, return the
151  // indices only!)
152 
153  jpvt -= static_cast<octave_idx_type> (1);
154  p = PermMatrix (jpvt, true);
155 
156  form (n, afact, tau, qr_type);
157  }
158 
159  template <>
161  : qr<FloatMatrix> (), p ()
162  {
163  init (a, qr_type);
164  }
165 
166  template <>
169  {
170  Array<float> pa (p.col_perm_vec ());
171  FloatRowVector pv (MArray<float> (pa) + 1.0f);
172  return pv;
173  }
174 
175  template <>
176  void
178  {
179  assert (qr_type != qr<ComplexMatrix>::raw);
180 
181  octave_idx_type m = a.rows ();
182  octave_idx_type n = a.cols ();
183 
184  octave_idx_type min_mn = m < n ? m : n;
185  OCTAVE_LOCAL_BUFFER (Complex, tau, min_mn);
186 
187  octave_idx_type info = 0;
188 
189  ComplexMatrix afact = a;
190  if (m > n && qr_type == qr<ComplexMatrix>::std)
191  afact.resize (m, m);
192 
193  MArray<octave_idx_type> jpvt (dim_vector (n, 1), 0);
194 
195  if (m > 0)
196  {
197  OCTAVE_LOCAL_BUFFER (double, rwork, 2*n);
198 
199  // workspace query.
200  Complex clwork;
201  F77_XFCN (zgeqp3, ZGEQP3, (m, n, F77_DBLE_CMPLX_ARG (afact.fortran_vec ()),
202  m, jpvt.fortran_vec (), F77_DBLE_CMPLX_ARG (tau),
203  F77_DBLE_CMPLX_ARG (&clwork), -1, rwork, info));
204 
205  // allocate buffer and do the job.
206  octave_idx_type lwork = clwork.real ();
207  lwork = std::max (lwork, static_cast<octave_idx_type> (1));
208  OCTAVE_LOCAL_BUFFER (Complex, work, lwork);
209  F77_XFCN (zgeqp3, ZGEQP3, (m, n, F77_DBLE_CMPLX_ARG (afact.fortran_vec ()),
210  m, jpvt.fortran_vec (), F77_DBLE_CMPLX_ARG (tau),
211  F77_DBLE_CMPLX_ARG (work), lwork, rwork, info));
212  }
213  else
214  for (octave_idx_type i = 0; i < n; i++) jpvt(i) = i+1;
215 
216  // Form Permutation matrix (if economy is requested, return the
217  // indices only!)
218 
219  jpvt -= static_cast<octave_idx_type> (1);
220  p = PermMatrix (jpvt, true);
221 
222  form (n, afact, tau, qr_type);
223  }
224 
225  template <>
227  : qr<ComplexMatrix> (), p ()
228  {
229  init (a, qr_type);
230  }
231 
232  template <>
233  RowVector
235  {
236  Array<double> pa (p.col_perm_vec ());
237  RowVector pv (MArray<double> (pa) + 1.0);
238  return pv;
239  }
240 
241  template <>
242  void
244  {
245  assert (qr_type != qr<FloatComplexMatrix>::raw);
246 
247  octave_idx_type m = a.rows ();
248  octave_idx_type n = a.cols ();
249 
250  octave_idx_type min_mn = m < n ? m : n;
251  OCTAVE_LOCAL_BUFFER (FloatComplex, tau, min_mn);
252 
253  octave_idx_type info = 0;
254 
255  FloatComplexMatrix afact = a;
256  if (m > n && qr_type == qr<FloatComplexMatrix>::std)
257  afact.resize (m, m);
258 
259  MArray<octave_idx_type> jpvt (dim_vector (n, 1), 0);
260 
261  if (m > 0)
262  {
263  OCTAVE_LOCAL_BUFFER (float, rwork, 2*n);
264 
265  // workspace query.
266  FloatComplex clwork;
267  F77_XFCN (cgeqp3, CGEQP3, (m, n, F77_CMPLX_ARG (afact.fortran_vec ()),
268  m, jpvt.fortran_vec (), F77_CMPLX_ARG (tau),
269  F77_CMPLX_ARG (&clwork), -1, rwork, info));
270 
271  // allocate buffer and do the job.
272  octave_idx_type lwork = clwork.real ();
273  lwork = std::max (lwork, static_cast<octave_idx_type> (1));
274  OCTAVE_LOCAL_BUFFER (FloatComplex, work, lwork);
275  F77_XFCN (cgeqp3, CGEQP3, (m, n, F77_CMPLX_ARG (afact.fortran_vec ()),
276  m, jpvt.fortran_vec (), F77_CMPLX_ARG (tau),
277  F77_CMPLX_ARG (work), lwork, rwork, info));
278  }
279  else
280  for (octave_idx_type i = 0; i < n; i++) jpvt(i) = i+1;
281 
282  // Form Permutation matrix (if economy is requested, return the
283  // indices only!)
284 
285  jpvt -= static_cast<octave_idx_type> (1);
286  p = PermMatrix (jpvt, true);
287 
288  form (n, afact, tau, qr_type);
289  }
290 
291  template <>
293  : qr<FloatComplexMatrix> (), p ()
294  {
295  init (a, qr_type);
296  }
297 
298  template <>
301  {
302  Array<float> pa (p.col_perm_vec ());
303  FloatRowVector pv (MArray<float> (pa) + 1.0f);
304  return pv;
305  }
306  }
307 }
void resize(octave_idx_type nr, octave_idx_type nc, const FloatComplex &rfv=FloatComplex(0))
Definition: fCMatrix.h:189
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition: fMatrix.h:149
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
qrp(void)
Definition: qrp.h:46
static octave::math::qr< T >::type qr_type(int nargin, int nargout)
Definition: qr.cc:51
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:145
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
OCTAVE_EXPORT octave_value_list while another program execution is suspended until the graphics object the function returns immediately In the second form
Definition: graphics.cc:11575
#define F77_DBLE_CMPLX_ARG(x)
Definition: f77-fcn.h:345
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:52
RowVector Pvec(void) const
Definition: qrp.cc:104
octave_idx_type rows(void) const
Definition: Array.h:401
void init(const T &, type=qr< T >::std)
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
idx type
Definition: ov.cc:3129
Definition: dMatrix.h:37
void init(const Matrix &a, type qr_type)
Definition: qrp.cc:49
qrp(const Matrix &a, type qr_type)
Definition: qrp.cc:96
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
#define F77_CMPLX_ARG(x)
Definition: f77-fcn.h:339
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
Definition: CMatrix.h:184
Definition: mx-defs.h:77
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:200
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31
const T * fortran_vec(void) const
Definition: Array.h:584
octave_idx_type cols(void) const
Definition: Array.h:409
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87