GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
svd.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2024 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 <cassert>
31 
32 #include <algorithm>
33 #include <unordered_map>
34 
35 #include "CMatrix.h"
36 #include "dDiagMatrix.h"
37 #include "dMatrix.h"
38 #include "fCMatrix.h"
39 #include "fDiagMatrix.h"
40 #include "fMatrix.h"
41 #include "lo-error.h"
42 #include "lo-lapack-proto.h"
43 #include "svd.h"
44 
45 // class to compute optimal work space size (lwork) for DGEJSV and SGEJSV
46 template<typename T>
47 class
48 gejsv_lwork
49 {
50 public:
51 
52  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE_DELETE (gejsv_lwork)
53 
54  // Unfortunately, dgejsv and sgejsv do not provide estimation of 'lwork'.
55  // Thus, we have to estimate it according to corresponding LAPACK
56  // documentation and related source codes (e.g. cgejsv).
57  // In LAPACKE (C interface to LAPACK), the memory handling code in
58  // LAPACKE_dgejsv() (lapacke_dgejsv.c, last visit 2019-02-17) uses
59  // the minimum required working space. In contrast, here the optimal
60  // working space size is computed, at the cost of much longer code.
61 
62  static F77_INT optimal (char& joba, char& jobu, char& jobv,
63  F77_INT m, F77_INT n);
64 
65 private:
66  typedef typename T::element_type P;
67 
68  // functions could be called from GEJSV
69  static F77_INT geqp3_lwork (F77_INT m, F77_INT n,
70  P *a, F77_INT lda,
71  F77_INT *jpvt, P *tau, P *work,
72  F77_INT lwork, F77_INT& info);
73 
74  static F77_INT geqrf_lwork (F77_INT m, F77_INT n,
75  P *a, F77_INT lda,
76  P *tau, P *work,
77  F77_INT lwork, F77_INT& info);
78 
79  static F77_INT gelqf_lwork (F77_INT m, F77_INT n,
80  P *a, F77_INT lda,
81  P *tau, P *work,
82  F77_INT lwork, F77_INT& info);
83 
84  static F77_INT ormlq_lwork (char& side, char& trans,
86  P *a, F77_INT lda,
87  P *tau, P *c, F77_INT ldc,
88  P *work, F77_INT lwork, F77_INT& info);
89 
90  static F77_INT ormqr_lwork (char& side, char& trans,
92  P *a, F77_INT lda,
93  P *tau, P *c, F77_INT ldc,
94  P *work, F77_INT lwork, F77_INT& info);
95 };
96 
97 #define GEJSV_REAL_QP3_LWORK(f, F) \
98  F77_XFCN (f, F, (m, n, a, lda, jpvt, tau, work, lwork, info))
99 
100 #define GEJSV_REAL_QR_LWORK(f, F) \
101  F77_XFCN (f, F, (m, n, a, lda, tau, work, lwork, info))
102 
103 #define GEJSV_REAL_ORM_LWORK(f, F) \
104  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&side, 1), \
105  F77_CONST_CHAR_ARG2 (&trans, 1), \
106  m, n, k, a, lda, tau, \
107  c, ldc, work, lwork, info \
108  F77_CHAR_ARG_LEN (1) \
109  F77_CHAR_ARG_LEN (1)))
110 
111 // For Matrix
112 template<>
113 F77_INT
114 gejsv_lwork<Matrix>::geqp3_lwork (F77_INT m, F77_INT n,
115  P *a, F77_INT lda,
116  F77_INT *jpvt, P *tau, P *work,
117  F77_INT lwork, F77_INT& info)
118 {
119  GEJSV_REAL_QP3_LWORK (dgeqp3, DGEQP3);
120  return static_cast<F77_INT> (work[0]);
121 }
122 
123 template<>
124 F77_INT
125 gejsv_lwork<Matrix>::geqrf_lwork (F77_INT m, F77_INT n,
126  P *a, F77_INT lda,
127  P *tau, P *work,
128  F77_INT lwork, F77_INT& info)
129 {
130  GEJSV_REAL_QR_LWORK (dgeqrf, DGEQRF);
131  return static_cast<F77_INT> (work[0]);
132 }
133 
134 template<>
135 F77_INT
136 gejsv_lwork<Matrix>::gelqf_lwork (F77_INT m, F77_INT n,
137  P *a, F77_INT lda,
138  P *tau, P *work,
139  F77_INT lwork, F77_INT& info)
140 {
141  GEJSV_REAL_QR_LWORK (dgelqf, DGELQF);
142  return static_cast<F77_INT> (work[0]);
143 }
144 
145 template<>
146 F77_INT
147 gejsv_lwork<Matrix>::ormlq_lwork (char& side, char& trans,
148  F77_INT m, F77_INT n, F77_INT k,
149  P *a, F77_INT lda,
150  P *tau, P *c, F77_INT ldc,
151  P *work, F77_INT lwork, F77_INT& info)
152 {
153  GEJSV_REAL_ORM_LWORK (dormlq, DORMLQ);
154  return static_cast<F77_INT> (work[0]);
155 }
156 
157 template<>
158 F77_INT
159 gejsv_lwork<Matrix>::ormqr_lwork (char& side, char& trans,
160  F77_INT m, F77_INT n, F77_INT k,
161  P *a, F77_INT lda,
162  P *tau, P *c, F77_INT ldc,
163  P *work, F77_INT lwork, F77_INT& info)
164 {
165  GEJSV_REAL_ORM_LWORK (dormqr, DORMQR);
166  return static_cast<F77_INT> (work[0]);
167 }
168 
169 // For FloatMatrix
170 template<>
171 F77_INT
172 gejsv_lwork<FloatMatrix>::geqp3_lwork (F77_INT m, F77_INT n,
173  P *a, F77_INT lda,
174  F77_INT *jpvt, P *tau, P *work,
175  F77_INT lwork, F77_INT& info)
176 {
177  GEJSV_REAL_QP3_LWORK (sgeqp3, SGEQP3);
178  return static_cast<F77_INT> (work[0]);
179 }
180 
181 template<>
182 F77_INT
183 gejsv_lwork<FloatMatrix>::geqrf_lwork (F77_INT m, F77_INT n,
184  P *a, F77_INT lda,
185  P *tau, P *work,
186  F77_INT lwork, F77_INT& info)
187 {
188  GEJSV_REAL_QR_LWORK (sgeqrf, SGEQRF);
189  return static_cast<F77_INT> (work[0]);
190 }
191 
192 template<>
193 F77_INT
194 gejsv_lwork<FloatMatrix>::gelqf_lwork (F77_INT m, F77_INT n,
195  P *a, F77_INT lda,
196  P *tau, P *work,
197  F77_INT lwork, F77_INT& info)
198 {
199  GEJSV_REAL_QR_LWORK (sgelqf, SGELQF);
200  return static_cast<F77_INT> (work[0]);
201 }
202 
203 template<>
204 F77_INT
205 gejsv_lwork<FloatMatrix>::ormlq_lwork (char& side, char& trans,
206  F77_INT m, F77_INT n, F77_INT k,
207  P *a, F77_INT lda,
208  P *tau, P *c, F77_INT ldc,
209  P *work, F77_INT lwork, F77_INT& info)
210 {
211  GEJSV_REAL_ORM_LWORK (sormlq, SORMLQ);
212  return static_cast<F77_INT> (work[0]);
213 }
214 
215 template<>
216 F77_INT
217 gejsv_lwork<FloatMatrix>::ormqr_lwork (char& side, char& trans,
218  F77_INT m, F77_INT n, F77_INT k,
219  P *a, F77_INT lda,
220  P *tau, P *c, F77_INT ldc,
221  P *work, F77_INT lwork, F77_INT& info)
222 {
223  GEJSV_REAL_ORM_LWORK (sormqr, SORMQR);
224  return static_cast<F77_INT> (work[0]);
225 }
226 
227 #undef GEJSV_REAL_QP3_LWORK
228 #undef GEJSV_REAL_QR_LWORK
229 #undef GEJSV_REAL_ORM_LWORK
230 
231 template<typename T>
232 F77_INT
233 gejsv_lwork<T>::optimal (char& joba, char& jobu, char& jobv,
234  F77_INT m, F77_INT n)
235 {
236  F77_INT lwork = -1;
237  std::vector<P> work (2); // dummy work space
238 
239  // variables that mimic running environment of gejsv
240  F77_INT lda = std::max<F77_INT> (m, 1);
241  F77_INT ierr = 0;
242  char side = 'L';
243  char trans = 'N';
244  std::vector<P> mat_a (1);
245  P *a = mat_a.data (); // dummy input matrix
246  std::vector<F77_INT> vec_jpvt = {0};
247  P *tau = work.data ();
248  P *u = work.data ();
249  P *v = work.data ();
250 
251  bool need_lsvec = jobu == 'U' || jobu == 'F';
252  bool need_rsvec = jobv == 'V' || jobv == 'J';
253 
254  F77_INT lw_pocon = 3 * n; // for [s,d]pocon
255  F77_INT lw_geqp3 = geqp3_lwork (m, n, a, lda, vec_jpvt.data (),
256  tau, work.data (), -1, ierr);
257  F77_INT lw_geqrf = geqrf_lwork (m, n, a, lda,
258  tau, work.data (), -1, ierr);
259 
260  if (! (need_lsvec || need_rsvec) )
261  {
262  // only SIGMA is needed
263  if (! (joba == 'E' || joba == 'G') )
264  lwork = std::max<F77_INT> ({2*m + n, n + lw_geqp3, n + lw_geqrf, 7});
265  else
266  lwork = std::max<F77_INT> ({2*m + n, n + lw_geqp3, n + lw_geqrf,
267  n + n*n + lw_pocon, 7});
268  }
269  else if (need_rsvec && ! need_lsvec)
270  {
271  // SIGMA and the right singular vectors are needed
272  F77_INT lw_gelqf = gelqf_lwork (n, n, a, lda,
273  tau, work.data (), -1, ierr);
274  trans = 'T';
275  F77_INT lw_ormlq = ormlq_lwork (side, trans, n, n, n, a, lda,
276  tau, v, n, work.data (), -1, ierr);
277  lwork = std::max<F77_INT> ({2*m + n, n + lw_geqp3, n + lw_pocon,
278  n + lw_gelqf, 2*n + lw_geqrf, n + lw_ormlq});
279  }
280  else if (need_lsvec && ! need_rsvec)
281  {
282  // SIGMA and the left singular vectors are needed
283  F77_INT n1 = (jobu == 'U') ? n : m; // size of U is m x n1
284  F77_INT lw_ormqr = ormqr_lwork (side, trans, m, n1, n, a, lda,
285  tau, u, m, work.data (), -1, ierr);
286  lwork = std::max<F77_INT> ({2*m + n, n + lw_geqp3, n + lw_pocon,
287  2*n + lw_geqrf, n + lw_ormqr});
288  }
289  else // full SVD is needed
290  {
291  if (jobv == 'V')
292  lwork = std::max (2*m + n, 6*n + 2*n*n);
293  else if (jobv == 'J')
294  lwork = std::max<F77_INT> ({2*m + n, 4*n + n*n, 2*n + n*n + 6});
295 
296  F77_INT n1 = (jobu == 'U') ? n : m; // size of U is m x n1
297  F77_INT lw_ormqr = ormqr_lwork (side, trans, m, n1, n, a, lda,
298  tau, u, m, work.data (), -1, ierr);
299  lwork = std::max (lwork, n + lw_ormqr);
300  }
301 
302  return lwork;
303 }
304 
306 
308 
309 template <typename T>
310 T
312 {
313  if (m_type == svd::Type::sigma_only)
314  (*current_liboctave_error_handler)
315  ("svd: U not computed because type == svd::sigma_only");
316 
317  return m_left_sm;
318 }
319 
320 template <typename T>
321 T
323 {
324  if (m_type == svd::Type::sigma_only)
325  (*current_liboctave_error_handler)
326  ("svd: V not computed because type == svd::sigma_only");
327 
328  return m_right_sm;
329 }
330 
331 // GESVD specializations
332 
333 #define GESVD_REAL_STEP(f, F) \
334  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&jobu, 1), \
335  F77_CONST_CHAR_ARG2 (&jobv, 1), \
336  m, n, tmp_data, m1, s_vec, u, m1, vt, \
337  nrow_vt1, work.data (), lwork, info \
338  F77_CHAR_ARG_LEN (1) \
339  F77_CHAR_ARG_LEN (1)))
340 
341 #define GESVD_COMPLEX_STEP(f, F, CMPLX_ARG) \
342  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&jobu, 1), \
343  F77_CONST_CHAR_ARG2 (&jobv, 1), \
344  m, n, CMPLX_ARG (tmp_data), \
345  m1, s_vec, CMPLX_ARG (u), m1, \
346  CMPLX_ARG (vt), nrow_vt1, \
347  CMPLX_ARG (work.data ()), \
348  lwork, rwork.data (), info \
349  F77_CHAR_ARG_LEN (1) \
350  F77_CHAR_ARG_LEN (1)))
351 
352 // DGESVD
353 template<>
354 OCTAVE_API void
355 svd<Matrix>::gesvd (char& jobu, char& jobv, F77_INT m, F77_INT n,
356  double *tmp_data, F77_INT m1, double *s_vec,
357  double *u, double *vt, F77_INT nrow_vt1,
358  std::vector<double>& work, F77_INT& lwork,
359  F77_INT& info)
360 {
361  GESVD_REAL_STEP (dgesvd, DGESVD);
362 
363  lwork = static_cast<F77_INT> (work[0]);
364  work.reserve (lwork);
365 
366  GESVD_REAL_STEP (dgesvd, DGESVD);
367 }
368 
369 // SGESVD
370 template<>
371 OCTAVE_API void
372 svd<FloatMatrix>::gesvd (char& jobu, char& jobv, F77_INT m, F77_INT n,
373  float *tmp_data, F77_INT m1, float *s_vec,
374  float *u, float *vt, F77_INT nrow_vt1,
375  std::vector<float>& work, F77_INT& lwork,
376  F77_INT& info)
377 {
378  GESVD_REAL_STEP (sgesvd, SGESVD);
379 
380  lwork = static_cast<F77_INT> (work[0]);
381  work.reserve (lwork);
382 
383  GESVD_REAL_STEP (sgesvd, SGESVD);
384 }
385 
386 // ZGESVD
387 template<>
388 OCTAVE_API void
389 svd<ComplexMatrix>::gesvd (char& jobu, char& jobv, F77_INT m, F77_INT n,
390  Complex *tmp_data, F77_INT m1, double *s_vec,
391  Complex *u, Complex *vt, F77_INT nrow_vt1,
392  std::vector<Complex>& work, F77_INT& lwork,
393  F77_INT& info)
394 {
395  std::vector<double> rwork (5 * std::max (m, n));
396 
397  GESVD_COMPLEX_STEP (zgesvd, ZGESVD, F77_DBLE_CMPLX_ARG);
398 
399  lwork = static_cast<F77_INT> (work[0].real ());
400  work.reserve (lwork);
401 
402  GESVD_COMPLEX_STEP (zgesvd, ZGESVD, F77_DBLE_CMPLX_ARG);
403 }
404 
405 // CGESVD
406 template<>
407 OCTAVE_API void
408 svd<FloatComplexMatrix>::gesvd (char& jobu, char& jobv, F77_INT m,
409  F77_INT n, FloatComplex *tmp_data,
410  F77_INT m1, float *s_vec, FloatComplex *u,
411  FloatComplex *vt, F77_INT nrow_vt1,
412  std::vector<FloatComplex>& work,
413  F77_INT& lwork, F77_INT& info)
414 {
415  std::vector<float> rwork (5 * std::max (m, n));
416 
417  GESVD_COMPLEX_STEP (cgesvd, CGESVD, F77_CMPLX_ARG);
418 
419  lwork = static_cast<F77_INT> (work[0].real ());
420  work.reserve (lwork);
421 
422  GESVD_COMPLEX_STEP (cgesvd, CGESVD, F77_CMPLX_ARG);
423 }
424 
425 #undef GESVD_REAL_STEP
426 #undef GESVD_COMPLEX_STEP
427 
428 // GESDD specializations
429 
430 #define GESDD_REAL_STEP(f, F) \
431  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&jobz, 1), \
432  m, n, tmp_data, m1, s_vec, u, m1, vt, nrow_vt1, \
433  work.data (), lwork, iwork, info \
434  F77_CHAR_ARG_LEN (1)))
435 
436 #define GESDD_COMPLEX_STEP(f, F, CMPLX_ARG) \
437  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&jobz, 1), m, n, \
438  CMPLX_ARG (tmp_data), m1, \
439  s_vec, CMPLX_ARG (u), m1, \
440  CMPLX_ARG (vt), nrow_vt1, \
441  CMPLX_ARG (work.data ()), lwork, \
442  rwork.data (), iwork, info \
443  F77_CHAR_ARG_LEN (1)))
444 
445 // DGESDD
446 template<>
447 OCTAVE_API void
448 svd<Matrix>::gesdd (char& jobz, F77_INT m, F77_INT n, double *tmp_data,
449  F77_INT m1, double *s_vec, double *u, double *vt,
450  F77_INT nrow_vt1, std::vector<double>& work,
451  F77_INT& lwork, F77_INT *iwork, F77_INT& info)
452 {
453  GESDD_REAL_STEP (dgesdd, DGESDD);
454 
455  lwork = static_cast<F77_INT> (work[0]);
456  work.reserve (lwork);
457 
458  GESDD_REAL_STEP (dgesdd, DGESDD);
459 }
460 
461 // SGESDD
462 template<>
463 OCTAVE_API void
464 svd<FloatMatrix>::gesdd (char& jobz, F77_INT m, F77_INT n, float *tmp_data,
465  F77_INT m1, float *s_vec, float *u, float *vt,
466  F77_INT nrow_vt1, std::vector<float>& work,
467  F77_INT& lwork, F77_INT *iwork, F77_INT& info)
468 {
469  GESDD_REAL_STEP (sgesdd, SGESDD);
470 
471  lwork = static_cast<F77_INT> (work[0]);
472  work.reserve (lwork);
473 
474  GESDD_REAL_STEP (sgesdd, SGESDD);
475 }
476 
477 // ZGESDD
478 template<>
479 OCTAVE_API void
481  Complex *tmp_data, F77_INT m1, double *s_vec,
482  Complex *u, Complex *vt, F77_INT nrow_vt1,
483  std::vector<Complex>& work, F77_INT& lwork,
484  F77_INT *iwork, F77_INT& info)
485 {
486 
487  F77_INT min_mn = std::min (m, n);
488  F77_INT max_mn = std::max (m, n);
489 
490  F77_INT lrwork;
491  if (jobz == 'N')
492  lrwork = 7*min_mn;
493  else
494  lrwork = min_mn * std::max (5*min_mn+5, 2*max_mn+2*min_mn+1);
495 
496  std::vector<double> rwork (lrwork);
497 
498  GESDD_COMPLEX_STEP (zgesdd, ZGESDD, F77_DBLE_CMPLX_ARG);
499 
500  lwork = static_cast<F77_INT> (work[0].real ());
501  work.reserve (lwork);
502 
503  GESDD_COMPLEX_STEP (zgesdd, ZGESDD, F77_DBLE_CMPLX_ARG);
504 }
505 
506 // CGESDD
507 template<>
508 OCTAVE_API void
510  FloatComplex *tmp_data, F77_INT m1,
511  float *s_vec, FloatComplex *u,
512  FloatComplex *vt, F77_INT nrow_vt1,
513  std::vector<FloatComplex>& work,
514  F77_INT& lwork, F77_INT *iwork,
515  F77_INT& info)
516 {
517  F77_INT min_mn = std::min (m, n);
518  F77_INT max_mn = std::max (m, n);
519 
520  F77_INT lrwork;
521  if (jobz == 'N')
522  lrwork = 7*min_mn;
523  else
524  lrwork = min_mn * std::max (5*min_mn+5, 2*max_mn+2*min_mn+1);
525  std::vector<float> rwork (lrwork);
526 
527  GESDD_COMPLEX_STEP (cgesdd, CGESDD, F77_CMPLX_ARG);
528 
529  lwork = static_cast<F77_INT> (work[0].real ());
530  work.reserve (lwork);
531 
532  GESDD_COMPLEX_STEP (cgesdd, CGESDD, F77_CMPLX_ARG);
533 }
534 
535 #undef GESDD_REAL_STEP
536 #undef GESDD_COMPLEX_STEP
537 
538 // GEJSV specializations
539 
540 #define GEJSV_REAL_STEP(f, F) \
541  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&joba, 1), \
542  F77_CONST_CHAR_ARG2 (&jobu, 1), \
543  F77_CONST_CHAR_ARG2 (&jobv, 1), \
544  F77_CONST_CHAR_ARG2 (&jobr, 1), \
545  F77_CONST_CHAR_ARG2 (&jobt, 1), \
546  F77_CONST_CHAR_ARG2 (&jobp, 1), \
547  m, n, tmp_data, m1, s_vec, u, m1, v, nrow_v1, \
548  work.data (), lwork, iwork.data (), info \
549  F77_CHAR_ARG_LEN (1) \
550  F77_CHAR_ARG_LEN (1) \
551  F77_CHAR_ARG_LEN (1) \
552  F77_CHAR_ARG_LEN (1) \
553  F77_CHAR_ARG_LEN (1) \
554  F77_CHAR_ARG_LEN (1)))
555 
556 #define GEJSV_COMPLEX_STEP(f, F, CMPLX_ARG) \
557  F77_XFCN (f, F, (F77_CONST_CHAR_ARG2 (&joba, 1), \
558  F77_CONST_CHAR_ARG2 (&jobu, 1), \
559  F77_CONST_CHAR_ARG2 (&jobv, 1), \
560  F77_CONST_CHAR_ARG2 (&jobr, 1), \
561  F77_CONST_CHAR_ARG2 (&jobt, 1), \
562  F77_CONST_CHAR_ARG2 (&jobp, 1), \
563  m, n, CMPLX_ARG (tmp_data), m1, \
564  s_vec, CMPLX_ARG (u), m1, \
565  CMPLX_ARG (v), nrow_v1, \
566  CMPLX_ARG (work.data ()), lwork, \
567  rwork.data (), lrwork, iwork.data (), info \
568  F77_CHAR_ARG_LEN (1) \
569  F77_CHAR_ARG_LEN (1) \
570  F77_CHAR_ARG_LEN (1) \
571  F77_CHAR_ARG_LEN (1) \
572  F77_CHAR_ARG_LEN (1) \
573  F77_CHAR_ARG_LEN (1)))
574 
575 // DGEJSV
576 template<>
577 void
578 svd<Matrix>::gejsv (char& joba, char& jobu, char& jobv,
579  char& jobr, char& jobt, char& jobp,
580  F77_INT m, F77_INT n,
581  P *tmp_data, F77_INT m1, DM_P *s_vec, P *u,
582  P *v, F77_INT nrow_v1, std::vector<P>& work,
583  F77_INT& lwork, std::vector<F77_INT>& iwork,
584  F77_INT& info)
585 {
586  lwork = gejsv_lwork<Matrix>::optimal (joba, jobu, jobv, m, n);
587  work.reserve (lwork);
588 
589  GEJSV_REAL_STEP (dgejsv, DGEJSV);
590 }
591 
592 // SGEJSV
593 template<>
594 void
595 svd<FloatMatrix>::gejsv (char& joba, char& jobu, char& jobv,
596  char& jobr, char& jobt, char& jobp,
597  F77_INT m, F77_INT n,
598  P *tmp_data, F77_INT m1, DM_P *s_vec, P *u,
599  P *v, F77_INT nrow_v1, std::vector<P>& work,
600  F77_INT& lwork, std::vector<F77_INT>& iwork,
601  F77_INT& info)
602 {
603  lwork = gejsv_lwork<FloatMatrix>::optimal (joba, jobu, jobv, m, n);
604  work.reserve (lwork);
605 
606  GEJSV_REAL_STEP (sgejsv, SGEJSV);
607 }
608 
609 // ZGEJSV
610 template<>
611 void
612 svd<ComplexMatrix>::gejsv (char& joba, char& jobu, char& jobv,
613  char& jobr, char& jobt, char& jobp,
614  F77_INT m, F77_INT n,
615  P *tmp_data, F77_INT m1, DM_P *s_vec, P *u,
616  P *v, F77_INT nrow_v1, std::vector<P>& work,
617  F77_INT& lwork, std::vector<F77_INT>& iwork,
618  F77_INT& info)
619 {
620  F77_INT lrwork = -1; // work space size query
621  std::vector<double> rwork (1);
622  work.reserve (2);
623 
624  GEJSV_COMPLEX_STEP (zgejsv, ZGEJSV, F77_DBLE_CMPLX_ARG);
625 
626  lwork = static_cast<F77_INT> (work[0].real ());
627  work.reserve (lwork);
628 
629  lrwork = static_cast<F77_INT> (rwork[0]);
630  rwork.reserve (lrwork);
631 
632  F77_INT liwork = static_cast<F77_INT> (iwork[0]);
633  iwork.reserve (liwork);
634 
635  GEJSV_COMPLEX_STEP (zgejsv, ZGEJSV, F77_DBLE_CMPLX_ARG);
636 }
637 
638 // CGEJSV
639 template<>
640 void
641 svd<FloatComplexMatrix>::gejsv (char& joba, char& jobu, char& jobv,
642  char& jobr, char& jobt, char& jobp,
643  F77_INT m, F77_INT n, P *tmp_data,
644  F77_INT m1, DM_P *s_vec, P *u, P *v,
645  F77_INT nrow_v1, std::vector<P>& work,
646  F77_INT& lwork,
647  std::vector<F77_INT>& iwork, F77_INT& info)
648 {
649  F77_INT lrwork = -1; // work space size query
650  std::vector<float> rwork (1);
651  work.reserve (2);
652 
653  GEJSV_COMPLEX_STEP (cgejsv, CGEJSV, F77_CMPLX_ARG);
654 
655  lwork = static_cast<F77_INT> (work[0].real ());
656  work.reserve (lwork);
657 
658  lrwork = static_cast<F77_INT> (rwork[0]);
659  rwork.reserve (lrwork);
660 
661  F77_INT liwork = static_cast<F77_INT> (iwork[0]);
662  iwork.reserve (liwork);
663 
664  GEJSV_COMPLEX_STEP (cgejsv, CGEJSV, F77_CMPLX_ARG);
665 }
666 
667 #undef GEJSV_REAL_STEP
668 #undef GEJSV_COMPLEX_STEP
669 
670 template<typename T>
671 svd<T>::svd (const T& a, svd::Type type, svd::Driver driver)
672  : m_type (type), m_driver (driver), m_left_sm (), m_sigma (),
673  m_right_sm ()
674 {
675  F77_INT info;
676 
677  F77_INT m = to_f77_int (a.rows ());
678  F77_INT n = to_f77_int (a.cols ());
679 
680  if (m == 0 || n == 0)
681  {
682  switch (m_type)
683  {
684  case svd::Type::std:
685  m_left_sm = T (m, m, 0);
686  for (F77_INT i = 0; i < m; i++)
687  m_left_sm.xelem (i, i) = 1;
688  m_sigma = DM_T (m, n);
689  m_right_sm = T (n, n, 0);
690  for (F77_INT i = 0; i < n; i++)
691  m_right_sm.xelem (i, i) = 1;
692  break;
693 
694  case svd::Type::economy:
695  m_left_sm = T (m, 0, 0);
696  m_sigma = DM_T (0, 0);
697  m_right_sm = T (n, 0, 0);
698  break;
699 
701  default:
702  m_sigma = DM_T (0, 1);
703  break;
704  }
705  return;
706  }
707 
708  T atmp = a;
709  P *tmp_data = atmp.fortran_vec ();
710 
711  F77_INT min_mn = (m < n ? m : n);
712 
713  char jobu = 'A';
714  char jobv = 'A';
715 
716  F77_INT ncol_u = m;
717  F77_INT nrow_vt = n;
718  F77_INT nrow_s = m;
719  F77_INT ncol_s = n;
720 
721  switch (m_type)
722  {
723  case svd::Type::economy:
724  jobu = jobv = 'S';
725  ncol_u = nrow_vt = nrow_s = ncol_s = min_mn;
726  break;
727 
729 
730  // Note: for this case, both jobu and jobv should be 'N', but there
731  // seems to be a bug in dgesvd from Lapack V2.0. To demonstrate the
732  // bug, set both jobu and jobv to 'N' and find the singular values of
733  // [eye(3), eye(3)]. The result is [-sqrt(2), -sqrt(2), -sqrt(2)].
734  //
735  // For Lapack 3.0, this problem seems to be fixed.
736 
737  jobu = jobv = 'N';
738  ncol_u = nrow_vt = 1;
739  break;
740 
741  default:
742  break;
743  }
744 
745  if (! (jobu == 'N' || jobu == 'O'))
746  m_left_sm.resize (m, ncol_u);
747 
748  P *u = m_left_sm.fortran_vec ();
749 
750  m_sigma.resize (nrow_s, ncol_s);
751  DM_P *s_vec = m_sigma.fortran_vec ();
752 
753  if (! (jobv == 'N' || jobv == 'O'))
754  {
755  if (m_driver == svd::Driver::GEJSV)
756  m_right_sm.resize (n, nrow_vt);
757  else
758  m_right_sm.resize (nrow_vt, n);
759  }
760 
761  P *vt = m_right_sm.fortran_vec ();
762 
763  // Query _GESVD for the correct dimension of WORK.
764 
765  F77_INT lwork = -1;
766 
767  std::vector<P> work (1);
768 
769  const F77_INT f77_int_one = static_cast<F77_INT> (1);
770  F77_INT m1 = std::max (m, f77_int_one);
771  F77_INT nrow_vt1 = std::max (nrow_vt, f77_int_one);
772 
773  if (m_driver == svd::Driver::GESVD)
774  gesvd (jobu, jobv, m, n, tmp_data, m1, s_vec, u, vt, nrow_vt1,
775  work, lwork, info);
776  else if (m_driver == svd::Driver::GESDD)
777  {
778  assert (jobu == jobv);
779  char jobz = jobu;
780 
781  std::vector<F77_INT> iwork (8 * std::min (m, n));
782 
783  gesdd (jobz, m, n, tmp_data, m1, s_vec, u, vt, nrow_vt1,
784  work, lwork, iwork.data (), info);
785  }
786  else if (m_driver == svd::Driver::GEJSV)
787  {
788  bool transposed = false;
789  if (n > m)
790  {
791  // GEJSV only accepts m >= n, thus we need to transpose here
792  transposed = true;
793 
794  std::swap (m, n);
795  m1 = std::max (m, f77_int_one);
796  nrow_vt1 = std::max (n, f77_int_one); // we have m > n
797  if (m_type == svd::Type::sigma_only)
798  nrow_vt1 = 1;
799  std::swap (jobu, jobv);
800 
801  atmp = atmp.hermitian ();
802  tmp_data = atmp.fortran_vec ();
803 
804  // Swap pointers of U and V.
805  u = m_right_sm.fortran_vec ();
806  vt = m_left_sm.fortran_vec ();
807  }
808 
809  // translate jobu and jobv from gesvd to gejsv.
810  std::unordered_map<char, std::string> job_svd2jsv;
811  job_svd2jsv['A'] = "FJ";
812  job_svd2jsv['S'] = "UV";
813  job_svd2jsv['O'] = "WW";
814  job_svd2jsv['N'] = "NN";
815  jobu = job_svd2jsv[jobu][0];
816  jobv = job_svd2jsv[jobv][1];
817 
818  char joba = 'F'; // 'F': most conservative
819  char jobr = 'R'; // 'R' is recommended.
820  char jobt = 'N'; // or 'T', but that requires U and V appear together
821  char jobp = 'N'; // use 'P' if denormal is poorly implemented.
822 
823  std::vector<F77_INT> iwork (std::max<F77_INT> (m + 3*n, 1));
824 
825  gejsv (joba, jobu, jobv, jobr, jobt, jobp, m, n, tmp_data, m1,
826  s_vec, u, vt, nrow_vt1, work, lwork, iwork, info);
827 
828  if (iwork[2] == 1)
829  (*current_liboctave_warning_with_id_handler)
830  ("Octave:convergence", "svd: (driver: GEJSV) "
831  "Denormal occurred, possible loss of accuracy.");
832 
833  if (info < 0)
834  (*current_liboctave_error_handler)
835  ("svd: (driver: GEJSV) Illegal argument at #%d",
836  static_cast<int> (-info));
837  else if (info > 0)
838  (*current_liboctave_warning_with_id_handler)
839  ("Octave:convergence", "svd: (driver: GEJSV) "
840  "Fail to converge within max sweeps, "
841  "possible inaccurate result.");
842 
843  if (transposed) // put things that need to transpose back here
844  std::swap (m, n);
845  }
846  else
847  (*current_liboctave_error_handler) ("svd: unknown driver");
848 
849  // LAPACK can return -0 which is a small problem (bug #55710).
850  for (octave_idx_type i = 0; i < m_sigma.diag_length (); i++)
851  {
852  if (! m_sigma.dgxelem (i))
853  m_sigma.dgxelem (i) = DM_P (0);
854  }
855 
856  // GESVD and GESDD return VT instead of V, GEJSV return V.
857  if (! (jobv == 'N' || jobv == 'O') && (m_driver != svd::Driver::GEJSV))
858  m_right_sm = m_right_sm.hermitian ();
859 }
860 
861 // Instantiations we need.
862 
863 template class svd<Matrix>;
864 
865 template class svd<FloatMatrix>;
866 
867 template class svd<ComplexMatrix>;
868 
869 template class svd<FloatComplexMatrix>;
870 
871 OCTAVE_END_NAMESPACE(math)
872 OCTAVE_END_NAMESPACE(octave)
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
Definition: svd.h:41
T::real_diag_matrix_type DM_T
Definition: svd.h:44
Type
Definition: svd.h:47
T right_singular_matrix() const
Definition: svd.cc:322
Driver
Definition: svd.h:54
T left_singular_matrix() const
Definition: svd.cc:311
svd()
Definition: svd.h:60
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
octave_f77_int_type F77_INT
Definition: f77-fcn.h:306
#define GESVD_REAL_STEP(f, F)
Definition: svd.cc:333
#define GESDD_COMPLEX_STEP(f, F, CMPLX_ARG)
Definition: svd.cc:436
#define GEJSV_REAL_STEP(f, F)
Definition: svd.cc:540
#define GEJSV_REAL_ORM_LWORK(f, F)
Definition: svd.cc:103
#define GEJSV_REAL_QR_LWORK(f, F)
Definition: svd.cc:100
#define GEJSV_COMPLEX_STEP(f, F, CMPLX_ARG)
Definition: svd.cc:556
#define GEJSV_REAL_QP3_LWORK(f, F)
Definition: svd.cc:97
#define GESDD_REAL_STEP(f, F)
Definition: svd.cc:430
#define GESVD_COMPLEX_STEP(f, F, CMPLX_ARG)
Definition: svd.cc:341
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:41
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT F77_INT & ierr
#define OCTAVE_API
Definition: main.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34