givens.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 // Originally written by A. S. Hodel <scotte@eng.auburn.edu>
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include "defun-dld.h"
00030 #include "error.h"
00031 #include "oct-obj.h"
00032 
00033 DEFUN_DLD (givens, args, nargout,
00034   "-*- texinfo -*-\n\
00035 @deftypefn  {Loadable Function} {@var{g} =} givens (@var{x}, @var{y})\n\
00036 @deftypefnx {Loadable Function} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})\n\
00037 @tex\n\
00038 Return a $2\\times 2$ orthogonal matrix\n\
00039 $$\n\
00040  G = \\left[\\matrix{c & s\\cr -s'& c\\cr}\\right]\n\
00041 $$\n\
00042 such that\n\
00043 $$\n\
00044  G \\left[\\matrix{x\\cr y}\\right] = \\left[\\matrix{\\ast\\cr 0}\\right]\n\
00045 $$\n\
00046 with $x$ and $y$ scalars.\n\
00047 @end tex\n\
00048 @ifnottex\n\
00049 Return a 2 by 2 orthogonal matrix\n\
00050 @code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]} such that\n\
00051 @code{@var{g} [@var{x}; @var{y}] = [*; 0]} with @var{x} and @var{y} scalars.\n\
00052 @end ifnottex\n\
00053 \n\
00054 For example:\n\
00055 \n\
00056 @example\n\
00057 @group\n\
00058 givens (1, 1)\n\
00059      @result{}   0.70711   0.70711\n\
00060          -0.70711   0.70711\n\
00061 @end group\n\
00062 @end example\n\
00063 @end deftypefn")
00064 {
00065   octave_value_list retval;
00066 
00067   int nargin = args.length ();
00068 
00069   if (nargin != 2 || nargout > 2)
00070     {
00071       print_usage ();
00072       return retval;
00073     }
00074   else
00075     {
00076       if (args(0).is_single_type () || args(1).is_single_type ())
00077         {
00078           if (args(0).is_complex_type () || args(1).is_complex_type ())
00079             {
00080               FloatComplex cx = args(0).float_complex_value ();
00081               FloatComplex cy = args(1).float_complex_value ();
00082 
00083               if (! error_state)
00084                 {
00085                   FloatComplexMatrix result = Givens (cx, cy);
00086 
00087                   if (! error_state)
00088                     {
00089                       switch (nargout)
00090                         {
00091                         case 0:
00092                         case 1:
00093                           retval(0) = result;
00094                           break;
00095 
00096                         case 2:
00097                           retval(1) = result (0, 1);
00098                           retval(0) = result (0, 0);
00099                           break;
00100 
00101                         default:
00102                           error ("givens: invalid number of output arguments");
00103                           break;
00104                         }
00105                     }
00106                 }
00107             }
00108           else
00109             {
00110               float x = args(0).float_value ();
00111               float y = args(1).float_value ();
00112 
00113               if (! error_state)
00114                 {
00115                   FloatMatrix result = Givens (x, y);
00116 
00117                   if (! error_state)
00118                     {
00119                       switch (nargout)
00120                         {
00121                         case 0:
00122                         case 1:
00123                           retval(0) = result;
00124                           break;
00125 
00126                         case 2:
00127                           retval(1) = result (0, 1);
00128                           retval(0) = result (0, 0);
00129                           break;
00130 
00131                         default:
00132                           error ("givens: invalid number of output arguments");
00133                           break;
00134                         }
00135                     }
00136                 }
00137             }
00138         }
00139       else
00140         {
00141           if (args(0).is_complex_type () || args(1).is_complex_type ())
00142             {
00143               Complex cx = args(0).complex_value ();
00144               Complex cy = args(1).complex_value ();
00145 
00146               if (! error_state)
00147                 {
00148                   ComplexMatrix result = Givens (cx, cy);
00149 
00150                   if (! error_state)
00151                     {
00152                       switch (nargout)
00153                         {
00154                         case 0:
00155                         case 1:
00156                           retval(0) = result;
00157                           break;
00158 
00159                         case 2:
00160                           retval(1) = result (0, 1);
00161                           retval(0) = result (0, 0);
00162                           break;
00163 
00164                         default:
00165                           error ("givens: invalid number of output arguments");
00166                           break;
00167                         }
00168                     }
00169                 }
00170             }
00171           else
00172             {
00173               double x = args(0).double_value ();
00174               double y = args(1).double_value ();
00175 
00176               if (! error_state)
00177                 {
00178                   Matrix result = Givens (x, y);
00179 
00180                   if (! error_state)
00181                     {
00182                       switch (nargout)
00183                         {
00184                         case 0:
00185                         case 1:
00186                           retval(0) = result;
00187                           break;
00188 
00189                         case 2:
00190                           retval(1) = result (0, 1);
00191                           retval(0) = result (0, 0);
00192                           break;
00193 
00194                         default:
00195                           error ("givens: invalid number of output arguments");
00196                           break;
00197                         }
00198                     }
00199                 }
00200             }
00201         }
00202     }
00203 
00204   return retval;
00205 }
00206 
00207 /*
00208 
00209 %!assert (givens (1,1), [1, 1; -1, 1]/sqrt(2), 2*eps);
00210 %!assert (givens (1,0), eye(2));
00211 %!assert (givens (0,1), [0, 1; -1 0]);
00212 %!error givens(1);
00213 %!error givens()
00214 
00215 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines