home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / CmplxSVD.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  4KB  |  179 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "CmplxSVD.h"
  32. #include "f77-fcn.h"
  33. #include "lo-error.h"
  34.  
  35. extern "C"
  36. {
  37.   int F77_FCN (zgesvd, ZGESVD) (const char*, const char*, const int&,
  38.                 const int&, Complex*, const int&,
  39.                 double*, Complex*, const int&,
  40.                 Complex*, const int&, Complex*,
  41.                 const int&, double*, int&, long,
  42.                 long);
  43. }
  44.  
  45. ComplexMatrix
  46. ComplexSVD::left_singular_matrix (void) const
  47. {
  48.   if (type_computed == SVD::sigma_only)
  49.     {
  50.       (*current_liboctave_error_handler)
  51.     ("ComplexSVD: U not computed because type == SVD::sigma_only");
  52.       return ComplexMatrix ();
  53.     }
  54.   else
  55.     return left_sm;
  56. }
  57.  
  58. ComplexMatrix
  59. ComplexSVD::right_singular_matrix (void) const
  60. {
  61.   if (type_computed == SVD::sigma_only)
  62.     {
  63.       (*current_liboctave_error_handler)
  64.     ("ComplexSVD: V not computed because type == SVD::sigma_only");
  65.       return ComplexMatrix ();
  66.     }
  67.   else
  68.     return right_sm;
  69. }
  70.  
  71. int
  72. ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type)
  73. {
  74.   int info;
  75.  
  76.   int m = a.rows ();
  77.   int n = a.cols ();
  78.  
  79.   ComplexMatrix atmp = a;
  80.   Complex *tmp_data = atmp.fortran_vec ();
  81.  
  82.   int min_mn = m < n ? m : n;
  83.   int max_mn = m > n ? m : n;
  84.  
  85.   char jobu = 'A';
  86.   char jobv = 'A';
  87.  
  88.   int ncol_u = m;
  89.   int nrow_vt = n;
  90.   int nrow_s = m;
  91.   int ncol_s = n;
  92.  
  93.   switch (svd_type)
  94.     {
  95.     case SVD::economy:
  96.       jobu = jobv = 'S';
  97.       ncol_u = nrow_vt = nrow_s = ncol_s = min_mn;
  98.       break;
  99.  
  100.     case SVD::sigma_only:
  101.  
  102.       // Note:  for this case, both jobu and jobv should be 'N', but
  103.       // there seems to be a bug in dgesvd from Lapack V2.0.  To
  104.       // demonstrate the bug, set both jobu and jobv to 'N' and find
  105.       // the singular values of [eye(3), eye(3)].  The result is
  106.       // [-sqrt(2), -sqrt(2), -sqrt(2)].
  107.       //
  108.       // For Lapack 3.0, this problem seems to be fixed.
  109.  
  110.       jobu = 'N';
  111.       jobv = 'N';
  112.       ncol_u = nrow_vt = 1;
  113.       break;
  114.  
  115.     default:
  116.       break;
  117.     }
  118.  
  119.   type_computed = svd_type;
  120.  
  121.   if (! (jobu == 'N' || jobu == 'O'))
  122.     left_sm.resize (m, ncol_u);
  123.  
  124.   Complex *u = left_sm.fortran_vec ();
  125.  
  126.   sigma.resize (nrow_s, ncol_s);
  127.   double *s_vec = sigma.fortran_vec ();
  128.  
  129.   if (! (jobv == 'N' || jobv == 'O'))
  130.     right_sm.resize (nrow_vt, n);
  131.  
  132.   Complex *vt = right_sm.fortran_vec ();
  133.  
  134.   int lrwork = 5*max_mn;
  135.  
  136.   Array<double> rwork (lrwork);
  137.  
  138.   // Ask ZGESVD what the dimension of WORK should be.
  139.  
  140.   int lwork = -1;
  141.  
  142.   Array<Complex> work (1);
  143.  
  144.   F77_XFCN (zgesvd, ZGESVD, (&jobu, &jobv, m, n, tmp_data, m, s_vec,
  145.                  u, m, vt, nrow_vt, work.fortran_vec (),
  146.                  lwork, rwork.fortran_vec (), info, 1L,
  147.                  1L));
  148.  
  149.   if (f77_exception_encountered)
  150.     (*current_liboctave_error_handler) ("unrecoverable error in zgesvd");
  151.   else
  152.     {
  153.       lwork = static_cast<int> (work(0).real ());
  154.       work.resize (lwork);
  155.  
  156.       F77_XFCN (zgesvd, ZGESVD, (&jobu, &jobv, m, n, tmp_data, m,
  157.                  s_vec, u, m, vt, nrow_vt,
  158.                  work.fortran_vec (), lwork,
  159.                  rwork.fortran_vec (),
  160.                  info, 1L, 1L));
  161.  
  162.       if (f77_exception_encountered)
  163.     (*current_liboctave_error_handler) ("unrecoverable error in zgesvd");
  164.       else
  165.     {
  166.       if (! (jobv == 'N' || jobv == 'O'))
  167.         right_sm = right_sm.hermitian ();
  168.     }
  169.     }
  170.  
  171.   return info;
  172. }
  173.  
  174. /*
  175. ;;; Local Variables: ***
  176. ;;; mode: C++ ***
  177. ;;; End: ***
  178. */
  179.