GNU Octave  4.2.1
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-2017 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 <scotte@eng.auburn.edu>
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "ovl.h"
32 
33 DEFUN (givens, args, nargout,
34  doc: /* -*- texinfo -*-
35 @deftypefn {} {@var{G} =} givens (@var{x}, @var{y})
36 @deftypefnx {} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})
37 Compute the Givens rotation matrix @var{G}.
38 
39 @tex
40 The Givens matrix is a $2\times 2$ orthogonal matrix
41 $$
42  G = \left[\matrix{c & s\cr -s'& c\cr}\right]
43 $$
44 such that
45 $$
46  G \left[\matrix{x\cr y}\right] = \left[\matrix{\ast\cr 0}\right]
47 $$
48 with $x$ and $y$ scalars.
49 @end tex
50 @ifnottex
51 The Givens matrix is a 2 by 2 orthogonal matrix
52 
53 @code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]}
54 
55 such that
56 
57 @code{@var{g} [@var{x}; @var{y}] = [*; 0]}
58 
59 with @var{x} and @var{y} scalars.
60 @end ifnottex
61 
62 If two output arguments are requested, return the factors @var{c} and
63 @var{s} rather than the Givens rotation matrix.
64 
65 For example:
66 
67 @example
68 @group
69 givens (1, 1)
70  @result{} 0.70711 0.70711
71  -0.70711 0.70711
72 @end group
73 @end example
74 @seealso{planerot}
75 @end deftypefn */)
76 {
77  if (args.length () != 2)
78  print_usage ();
79 
81 
82  if (args(0).is_single_type () || args(1).is_single_type ())
83  {
84  if (args(0).is_complex_type () || args(1).is_complex_type ())
85  {
86  FloatComplex cx = args(0).float_complex_value ();
87  FloatComplex cy = args(1).float_complex_value ();
88 
89  FloatComplexMatrix result = Givens (cx, cy);
90 
91  switch (nargout)
92  {
93  case 0:
94  case 1:
95  retval = ovl (result);
96  break;
97 
98  case 2:
99  retval = ovl (result(0, 0), result(0, 1));
100  break;
101  }
102  }
103  else
104  {
105  float x = args(0).float_value ();
106  float y = args(1).float_value ();
107 
108  FloatMatrix result = Givens (x, y);
109 
110  switch (nargout)
111  {
112  case 0:
113  case 1:
114  retval = ovl (result);
115  break;
116 
117  case 2:
118  retval = ovl (result(0, 0), result(0, 1));
119  break;
120  }
121  }
122  }
123  else
124  {
125  if (args(0).is_complex_type () || args(1).is_complex_type ())
126  {
127  Complex cx = args(0).complex_value ();
128  Complex cy = args(1).complex_value ();
129 
130  ComplexMatrix result = Givens (cx, cy);
131 
132  switch (nargout)
133  {
134  case 0:
135  case 1:
136  retval = ovl (result);
137  break;
138 
139  case 2:
140  retval = ovl (result(0, 0), result(0, 1));
141  break;
142  }
143  }
144  else
145  {
146  double x = args(0).double_value ();
147  double y = args(1).double_value ();
148 
149  Matrix result = Givens (x, y);
150 
151  switch (nargout)
152  {
153  case 0:
154  case 1:
155  retval = ovl (result);
156  break;
157 
158  case 2:
159  retval = ovl (result(0, 0), result(0, 1));
160  break;
161  }
162  }
163  }
164 
165  return retval;
166 }
167 
168 /*
169 %!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
170 %!assert (givens (1,0), eye (2))
171 %!assert (givens (0,1), [0, 1; -1 0])
172 
173 %!error givens ()
174 %!error givens (1)
175 %!error [a,b,c] = givens (1, 1)
176 */
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
JNIEnv void * args
Definition: ov-java.cc:67
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:935
octave_value retval
Definition: data.cc:6294
Definition: dMatrix.h:37
With real return the complex result
Definition: data.cc:3375
ComplexMatrix Givens(const Complex &x, const Complex &y)
Definition: CMatrix.cc:3276
the element is set to zero In other the statement xample y
Definition: data.cc:5342
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x