GNU Octave  4.0.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-2015 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 Compute the Givens rotation matrix @var{G}.\n\
38 \n\
39 @tex\n\
40 The Givens matrix is a $2\\times 2$ orthogonal matrix\n\
41 $$\n\
42  G = \\left[\\matrix{c & s\\cr -s'& c\\cr}\\right]\n\
43 $$\n\
44 such that\n\
45 $$\n\
46  G \\left[\\matrix{x\\cr y}\\right] = \\left[\\matrix{\\ast\\cr 0}\\right]\n\
47 $$\n\
48 with $x$ and $y$ scalars.\n\
49 @end tex\n\
50 @ifnottex\n\
51 The Givens matrix is a 2 by 2 orthogonal matrix\n\
52 \n\
53 @code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]}\n\
54 \n\
55 such that\n\
56 \n\
57 @code{@var{g} [@var{x}; @var{y}] = [*; 0]}\n\
58 \n\
59 with @var{x} and @var{y} scalars.\n\
60 @end ifnottex\n\
61 \n\
62 If two output arguments are requested, return the factors @var{c} and\n\
63 @var{s} rather than the Givens rotation matrix.\n\
64 \n\
65 For example:\n\
66 \n\
67 @example\n\
68 @group\n\
69 givens (1, 1)\n\
70  @result{} 0.70711 0.70711\n\
71  -0.70711 0.70711\n\
72 @end group\n\
73 @end example\n\
74 @seealso{planerot}\n\
75 @end deftypefn")
76 {
77  octave_value_list retval;
78 
79  int nargin = args.length ();
80 
81  if (nargin != 2 || nargout > 2)
82  {
83  print_usage ();
84  return retval;
85  }
86  else
87  {
88  if (args(0).is_single_type () || args(1).is_single_type ())
89  {
90  if (args(0).is_complex_type () || args(1).is_complex_type ())
91  {
92  FloatComplex cx = args(0).float_complex_value ();
93  FloatComplex cy = args(1).float_complex_value ();
94 
95  if (! error_state)
96  {
97  FloatComplexMatrix result = Givens (cx, cy);
98 
99  if (! error_state)
100  {
101  switch (nargout)
102  {
103  case 0:
104  case 1:
105  retval(0) = result;
106  break;
107 
108  case 2:
109  retval(1) = result (0, 1);
110  retval(0) = result (0, 0);
111  break;
112  }
113  }
114  }
115  }
116  else
117  {
118  float x = args(0).float_value ();
119  float y = args(1).float_value ();
120 
121  if (! error_state)
122  {
123  FloatMatrix result = Givens (x, y);
124 
125  if (! error_state)
126  {
127  switch (nargout)
128  {
129  case 0:
130  case 1:
131  retval(0) = result;
132  break;
133 
134  case 2:
135  retval(1) = result (0, 1);
136  retval(0) = result (0, 0);
137  break;
138  }
139  }
140  }
141  }
142  }
143  else
144  {
145  if (args(0).is_complex_type () || args(1).is_complex_type ())
146  {
147  Complex cx = args(0).complex_value ();
148  Complex cy = args(1).complex_value ();
149 
150  if (! error_state)
151  {
152  ComplexMatrix result = Givens (cx, cy);
153 
154  if (! error_state)
155  {
156  switch (nargout)
157  {
158  case 0:
159  case 1:
160  retval(0) = result;
161  break;
162 
163  case 2:
164  retval(1) = result (0, 1);
165  retval(0) = result (0, 0);
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  }
195  }
196  }
197  }
198  }
199 
200  return retval;
201 }
202 
203 /*
204 %!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
205 %!assert (givens (1,0), eye (2))
206 %!assert (givens (0,1), [0, 1; -1 0])
207 
208 %!error givens ()
209 %!error givens (1)
210 %!error [a,b,c] = givens (1, 1)
211 */
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
octave_idx_type length(void) const
Definition: oct-obj.h:89
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
int error_state
Definition: error.cc:101
Definition: dMatrix.h:35
ComplexMatrix Givens(const Complex &x, const Complex &y)
Definition: CMatrix.cc:3570
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
std::complex< double > Complex
Definition: oct-cmplx.h:29
F77_RET_T const double * x