GNU Octave  3.8.0
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
givens.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Originally written by A. S. Hodel <[email protected]>
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "oct-obj.h"
32 
33 DEFUN (givens, args, nargout,
34  "-*- texinfo -*-\n\
35 @deftypefn {Built-in Function} {@var{g} =} givens (@var{x}, @var{y})\n\
36 @deftypefnx {Built-in Function} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})\n\
37 @tex\n\
38 Return a $2\\times 2$ orthogonal matrix\n\
39 $$\n\
40  G = \\left[\\matrix{c & s\\cr -s'& c\\cr}\\right]\n\
41 $$\n\
42 such that\n\
43 $$\n\
44  G \\left[\\matrix{x\\cr y}\\right] = \\left[\\matrix{\\ast\\cr 0}\\right]\n\
45 $$\n\
46 with $x$ and $y$ scalars.\n\
47 @end tex\n\
48 @ifnottex\n\
49 Return a 2 by 2 orthogonal matrix\n\
50 @code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]} such that\n\
51 @code{@var{g} [@var{x}; @var{y}] = [*; 0]} with @var{x} and @var{y} scalars.\n\
52 @end ifnottex\n\
53 \n\
54 For example:\n\
55 \n\
56 @example\n\
57 @group\n\
58 givens (1, 1)\n\
59  @result{} 0.70711 0.70711\n\
60  -0.70711 0.70711\n\
61 @end group\n\
62 @end example\n\
63 @end deftypefn")
64 {
65  octave_value_list retval;
66 
67  int nargin = args.length ();
68 
69  if (nargin != 2 || nargout > 2)
70  {
71  print_usage ();
72  return retval;
73  }
74  else
75  {
76  if (args(0).is_single_type () || args(1).is_single_type ())
77  {
78  if (args(0).is_complex_type () || args(1).is_complex_type ())
79  {
80  FloatComplex cx = args(0).float_complex_value ();
81  FloatComplex cy = args(1).float_complex_value ();
82 
83  if (! error_state)
84  {
85  FloatComplexMatrix result = Givens (cx, cy);
86 
87  if (! error_state)
88  {
89  switch (nargout)
90  {
91  case 0:
92  case 1:
93  retval(0) = result;
94  break;
95 
96  case 2:
97  retval(1) = result (0, 1);
98  retval(0) = result (0, 0);
99  break;
100 
101  default:
102  error ("givens: invalid number of output arguments");
103  break;
104  }
105  }
106  }
107  }
108  else
109  {
110  float x = args(0).float_value ();
111  float y = args(1).float_value ();
112 
113  if (! error_state)
114  {
115  FloatMatrix result = Givens (x, y);
116 
117  if (! error_state)
118  {
119  switch (nargout)
120  {
121  case 0:
122  case 1:
123  retval(0) = result;
124  break;
125 
126  case 2:
127  retval(1) = result (0, 1);
128  retval(0) = result (0, 0);
129  break;
130 
131  default:
132  error ("givens: invalid number of output arguments");
133  break;
134  }
135  }
136  }
137  }
138  }
139  else
140  {
141  if (args(0).is_complex_type () || args(1).is_complex_type ())
142  {
143  Complex cx = args(0).complex_value ();
144  Complex cy = args(1).complex_value ();
145 
146  if (! error_state)
147  {
148  ComplexMatrix result = Givens (cx, cy);
149 
150  if (! error_state)
151  {
152  switch (nargout)
153  {
154  case 0:
155  case 1:
156  retval(0) = result;
157  break;
158 
159  case 2:
160  retval(1) = result (0, 1);
161  retval(0) = result (0, 0);
162  break;
163 
164  default:
165  error ("givens: invalid number of output arguments");
166  break;
167  }
168  }
169  }
170  }
171  else
172  {
173  double x = args(0).double_value ();
174  double y = args(1).double_value ();
175 
176  if (! error_state)
177  {
178  Matrix result = Givens (x, y);
179 
180  if (! error_state)
181  {
182  switch (nargout)
183  {
184  case 0:
185  case 1:
186  retval(0) = result;
187  break;
188 
189  case 2:
190  retval(1) = result (0, 1);
191  retval(0) = result (0, 0);
192  break;
193 
194  default:
195  error ("givens: invalid number of output arguments");
196  break;
197  }
198  }
199  }
200  }
201  }
202  }
203 
204  return retval;
205 }
206 
207 /*
208 %!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
209 %!assert (givens (1,0), eye (2))
210 %!assert (givens (0,1), [0, 1; -1 0])
211 
212 %!error givens ()
213 %!error givens (1)
214 */