home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / f-syl.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  5KB  |  224 lines

  1. // f-syl.cc                                           -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1993, 1994, 1995 John W. Eaton
  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 2, or (at your option) any
  11. 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, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. // Written by A. S. Hodel <scotte@eng.auburn.edu>
  25.  
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29.  
  30. #include "dMatrix.h"
  31. #include "CMatrix.h"
  32. #include "dbleSCHUR.h"
  33. #include "CmplxSCHUR.h"
  34. #include "f77-uscore.h"
  35.  
  36. #include "tree-const.h"
  37. #include "user-prefs.h"
  38. #include "gripes.h"
  39. #include "error.h"
  40. #include "utils.h"
  41. #include "help.h"
  42. #include "defun-dld.h"
  43.  
  44. extern "C"
  45. {
  46.   int F77_FCN (dtrsyl) (const char*, const char*, const int*,
  47.             const int*, const int*, const double*,
  48.             const int*, const double*, const int*,
  49.             const double*, const int*, double*, int*,
  50.             long, long);
  51.  
  52.   int F77_FCN (ztrsyl) (const char*, const char*, const int*,
  53.             const int*, const int*, const Complex*,
  54.             const int*, const Complex*, const int*,
  55.             const Complex*, const int*, double*, int*,
  56.             long, long);
  57. }
  58.  
  59. DEFUN_DLD_BUILTIN ("syl", Fsyl, Ssyl, 4, 1,
  60.   "X = syl (A, B, C): solve the Sylvester equation A X + X B + C = 0")
  61. {
  62.   Octave_object retval;
  63.  
  64.   int nargin = args.length ();
  65.  
  66.   if (nargin != 3 || nargout > 1)
  67.     {
  68.       print_usage ("syl");
  69.       return retval;
  70.     }
  71.  
  72.   tree_constant arg_a = args(0);
  73.   tree_constant arg_b = args(1);
  74.   tree_constant arg_c = args(2);
  75.  
  76.   int a_nr = arg_a.rows ();
  77.   int a_nc = arg_a.columns ();
  78.  
  79.   int b_nr = arg_b.rows ();
  80.   int b_nc = arg_b.columns ();
  81.  
  82.   int c_nr = arg_c.rows ();
  83.   int c_nc = arg_c.columns ();
  84.  
  85.   int arg_a_is_empty = empty_arg ("syl", a_nr, a_nc);
  86.   int arg_b_is_empty = empty_arg ("syl", b_nr, b_nc);
  87.   int arg_c_is_empty = empty_arg ("syl", c_nr, c_nc);
  88.  
  89.   if (arg_a_is_empty > 0 && arg_b_is_empty > 0 && arg_c_is_empty > 0)
  90.     return Matrix ();
  91.   else if (arg_a_is_empty || arg_b_is_empty || arg_c_is_empty)
  92.     return retval;
  93.  
  94. // Arguments are not empty, so check for correct dimensions.
  95.  
  96.   if (a_nr != a_nc || b_nr != b_nc)
  97.     {
  98.       gripe_square_matrix_required ("syl: first two parameters:");
  99.       return retval;
  100.     }
  101.   else if (a_nr != c_nr || b_nr != c_nc)
  102.     {
  103.       gripe_nonconformant ();
  104.       return retval;
  105.     }
  106.   
  107. // Dimensions look o.k., let's solve the problem.
  108.  
  109.     if (arg_a.is_complex_type ()
  110.     || arg_b.is_complex_type ()
  111.     || arg_c.is_complex_type ())
  112.       {
  113.  
  114. // Do everything in complex arithmetic;
  115.  
  116.     ComplexMatrix ca = arg_a.complex_matrix_value ();
  117.  
  118.     if (error_state)
  119.       return retval;
  120.  
  121.     ComplexMatrix cb = arg_b.complex_matrix_value ();
  122.  
  123.     if (error_state)
  124.       return retval;
  125.  
  126.     ComplexMatrix cc = arg_c.complex_matrix_value ();
  127.  
  128.     if (error_state)
  129.       return retval;
  130.  
  131. // Compute Schur decompositions
  132.  
  133.     ComplexSCHUR as (ca, "U");
  134.     ComplexSCHUR bs (cb, "U");
  135.   
  136. // Transform cc to new coordinates.
  137.  
  138.     ComplexMatrix ua = as.unitary_matrix ();
  139.     ComplexMatrix sch_a = as.schur_matrix ();
  140.     ComplexMatrix ub = bs.unitary_matrix ();
  141.     ComplexMatrix sch_b = bs.schur_matrix ();
  142.   
  143.     ComplexMatrix cx = ua.hermitian () * cc * ub;
  144.   
  145. // Solve the sylvester equation, back-transform, and return the solution.
  146.   
  147.     double scale;
  148.     int info;
  149.     int one = 1;
  150.   
  151.     F77_FCN (ztrsyl) ("N", "N", &one, &a_nr, &b_nr,
  152.               sch_a.fortran_vec (), &a_nr,
  153.               sch_b.fortran_vec (), &b_nr,
  154.               cx.fortran_vec (), &a_nr, &scale, &info,
  155.               1L, 1L);
  156.  
  157.     cx = -ua * cx * ub.hermitian ();
  158.   
  159.     retval = cx;
  160.       }
  161.     else
  162.       {
  163.  
  164. // Do everything in real arithmetic;
  165.  
  166.     Matrix ca = arg_a.matrix_value ();
  167.  
  168.     if (error_state)
  169.       return retval;
  170.  
  171.     Matrix cb = arg_b.matrix_value ();
  172.  
  173.     if (error_state)
  174.       return retval;
  175.  
  176.     Matrix cc = arg_c.matrix_value ();
  177.  
  178.     if (error_state)
  179.       return retval;
  180.  
  181. // Compute Schur decompositions.
  182.  
  183.     SCHUR as (ca, "U");
  184.     SCHUR bs (cb, "U");
  185.   
  186. // Transform cc to new coordinates.
  187.  
  188.     Matrix ua = as.unitary_matrix ();
  189.     Matrix sch_a = as.schur_matrix ();
  190.     Matrix ub = bs.unitary_matrix ();
  191.     Matrix sch_b = bs.schur_matrix ();
  192.   
  193.     Matrix cx = ua.transpose () * cc * ub;
  194.   
  195. // Solve the sylvester equation, back-transform, and return the solution.
  196.   
  197.     double scale;
  198.     int info;
  199.     int one = 1;
  200.  
  201.     F77_FCN (dtrsyl) ("N", "N", &one, &a_nr, &b_nr,
  202.               sch_a.fortran_vec (), &a_nr, 
  203.               sch_b.fortran_vec (), &b_nr,
  204.               cx.fortran_vec (), &a_nr, &scale, &info,
  205.               1L, 1L);
  206.  
  207.     if (info)
  208.       error ("syl: trouble in dtrsyl info = %d", info);
  209.   
  210.     cx = -ua*cx*ub.transpose ();
  211.   
  212.     retval = cx;
  213.       }
  214.  
  215.   return retval;
  216. }
  217.  
  218. /*
  219. ;;; Local Variables: ***
  220. ;;; mode: C++ ***
  221. ;;; page-delimiter: "^/\\*" ***
  222. ;;; End: ***
  223. */
  224.