GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // Originally written by A. S. Hodel <scotte@eng.auburn.edu>
27 
28 #if defined (HAVE_CONFIG_H)
29 # include "config.h"
30 #endif
31 
32 #include "defun.h"
33 #include "error.h"
34 #include "ovl.h"
35 
36 DEFUN (givens, args, nargout,
37  doc: /* -*- texinfo -*-
38 @deftypefn {} {@var{G} =} givens (@var{x}, @var{y})
39 @deftypefnx {} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})
40 Compute the Givens rotation matrix @var{G}.
41 
42 @tex
43 The Givens matrix is a $2\times 2$ orthogonal matrix
44 $$
45  G = \left[\matrix{c & s\cr -s'& c\cr}\right]
46 $$
47 such that
48 $$
49  G \left[\matrix{x\cr y}\right] = \left[\matrix{\ast\cr 0}\right]
50 $$
51 with $x$ and $y$ scalars.
52 @end tex
53 @ifnottex
54 The Givens matrix is a 2-by-2 orthogonal matrix
55 
56 @example
57 @group
58 @var{G} = [ @var{c} , @var{s}
59  -@var{s}', @var{c}]
60 @end group
61 @end example
62 
63 @noindent
64 such that
65 
66 @example
67 @var{G} * [@var{x}; @var{y}] = [*; 0]
68 @end example
69 
70 @noindent
71 with @var{x} and @var{y} scalars.
72 @end ifnottex
73 
74 If two output arguments are requested, return the factors @var{c} and @var{s}
75 rather than the Givens rotation matrix.
76 
77 For example:
78 
79 @example
80 @group
81 givens (1, 1)
82  @result{} 0.70711 0.70711
83  -0.70711 0.70711
84 @end group
85 @end example
86 
87 Note: The Givens matrix represents a counterclockwise rotation of a 2-D
88 plane and can be used to introduce zeros into a matrix prior to complete
89 factorization.
90 @seealso{planerot, qr}
91 @end deftypefn */)
92 {
93  if (args.length () != 2)
94  print_usage ();
95 
97 
98  if (args(0).is_single_type () || args(1).is_single_type ())
99  {
100  if (args(0).iscomplex () || args(1).iscomplex ())
101  {
102  FloatComplex cx = args(0).float_complex_value ();
103  FloatComplex cy = args(1).float_complex_value ();
104 
105  FloatComplexMatrix result = Givens (cx, cy);
106 
107  switch (nargout)
108  {
109  case 0:
110  case 1:
111  retval = ovl (result);
112  break;
113 
114  case 2:
115  retval = ovl (result(0, 0), result(0, 1));
116  break;
117  }
118  }
119  else
120  {
121  float x = args(0).float_value ();
122  float y = args(1).float_value ();
123 
124  FloatMatrix result = Givens (x, y);
125 
126  switch (nargout)
127  {
128  case 0:
129  case 1:
130  retval = ovl (result);
131  break;
132 
133  case 2:
134  retval = ovl (result(0, 0), result(0, 1));
135  break;
136  }
137  }
138  }
139  else
140  {
141  if (args(0).iscomplex () || args(1).iscomplex ())
142  {
143  Complex cx = args(0).complex_value ();
144  Complex cy = args(1).complex_value ();
145 
146  ComplexMatrix result = Givens (cx, cy);
147 
148  switch (nargout)
149  {
150  case 0:
151  case 1:
152  retval = ovl (result);
153  break;
154 
155  case 2:
156  retval = ovl (result(0, 0), result(0, 1));
157  break;
158  }
159  }
160  else
161  {
162  double x = args(0).double_value ();
163  double y = args(1).double_value ();
164 
165  Matrix result = Givens (x, y);
166 
167  switch (nargout)
168  {
169  case 0:
170  case 1:
171  retval = ovl (result);
172  break;
173 
174  case 2:
175  retval = ovl (result(0, 0), result(0, 1));
176  break;
177  }
178  }
179  }
180 
181  return retval;
182 }
183 
184 /*
185 %!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
186 %!assert (givens (1,0), eye (2))
187 %!assert (givens (0,1), [0, 1; -1 0])
188 
189 %!error givens ()
190 %!error givens (1)
191 %!error [a,b,c] = givens (1, 1)
192 */
ComplexMatrix Givens(const Complex &x, const Complex &y)
Definition: CMatrix.cc:3202
Definition: dMatrix.h:42
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
F77_RET_T const F77_DBLE * x
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211