GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
givens.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 @example
54 @group
55 @var{G} = [ @var{c} , @var{s}
56  -@var{s}', @var{c}]
57 @end group
58 @end example
59 
60 @noindent
61 such that
62 
63 @example
64 @var{G} * [@var{x}; @var{y}] = [*; 0]
65 @end example
66 
67 @noindent
68 with @var{x} and @var{y} scalars.
69 @end ifnottex
70 
71 If two output arguments are requested, return the factors @var{c} and @var{s}
72 rather than the Givens rotation matrix.
73 
74 For example:
75 
76 @example
77 @group
78 givens (1, 1)
79  @result{} 0.70711 0.70711
80  -0.70711 0.70711
81 @end group
82 @end example
83 
84 Note: The Givens matrix represents a counterclockwise rotation of a 2-D
85 plane and can be used to introduce zeros into a matrix prior to complete
86 factorization.
87 @seealso{planerot, qr}
88 @end deftypefn */)
89 {
90  if (args.length () != 2)
91  print_usage ();
92 
94 
95  if (args(0).is_single_type () || args(1).is_single_type ())
96  {
97  if (args(0).iscomplex () || args(1).iscomplex ())
98  {
99  FloatComplex cx = args(0).float_complex_value ();
100  FloatComplex cy = args(1).float_complex_value ();
101 
102  FloatComplexMatrix result = Givens (cx, cy);
103 
104  switch (nargout)
105  {
106  case 0:
107  case 1:
108  retval = ovl (result);
109  break;
110 
111  case 2:
112  retval = ovl (result(0, 0), result(0, 1));
113  break;
114  }
115  }
116  else
117  {
118  float x = args(0).float_value ();
119  float y = args(1).float_value ();
120 
121  FloatMatrix result = Givens (x, y);
122 
123  switch (nargout)
124  {
125  case 0:
126  case 1:
127  retval = ovl (result);
128  break;
129 
130  case 2:
131  retval = ovl (result(0, 0), result(0, 1));
132  break;
133  }
134  }
135  }
136  else
137  {
138  if (args(0).iscomplex () || args(1).iscomplex ())
139  {
140  Complex cx = args(0).complex_value ();
141  Complex cy = args(1).complex_value ();
142 
143  ComplexMatrix result = Givens (cx, cy);
144 
145  switch (nargout)
146  {
147  case 0:
148  case 1:
149  retval = ovl (result);
150  break;
151 
152  case 2:
153  retval = ovl (result(0, 0), result(0, 1));
154  break;
155  }
156  }
157  else
158  {
159  double x = args(0).double_value ();
160  double y = args(1).double_value ();
161 
162  Matrix result = Givens (x, y);
163 
164  switch (nargout)
165  {
166  case 0:
167  case 1:
168  retval = ovl (result);
169  break;
170 
171  case 2:
172  retval = ovl (result(0, 0), result(0, 1));
173  break;
174  }
175  }
176  }
177 
178  return retval;
179 }
180 
181 /*
182 %!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
183 %!assert (givens (1,0), eye (2))
184 %!assert (givens (0,1), [0, 1; -1 0])
185 
186 %!error givens ()
187 %!error givens (1)
188 %!error [a,b,c] = givens (1, 1)
189 */
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition: ov.h:849
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
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:997
octave_value retval
Definition: data.cc:6246
Definition: dMatrix.h:36
With real return the complex result
Definition: data.cc:3260
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
ComplexMatrix Givens(const Complex &x, const Complex &y)
Definition: CMatrix.cc:3395
the element is set to zero In other the statement xample y
Definition: data.cc:5264
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x