GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
givens.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
37
38DEFUN (givens, args, nargout,
39 doc: /* -*- texinfo -*-
40@deftypefn {} {@var{G} =} givens (@var{x}, @var{y})
41@deftypefnx {} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})
42Compute the Givens rotation matrix @var{G}.
43
44@tex
45The Givens matrix is a $2\times 2$ orthogonal matrix
46$$
47 G = \left[\matrix{c & s\cr -s'& c\cr}\right]
48$$
49such that
50$$
51 G \left[\matrix{x\cr y}\right] = \left[\matrix{\ast\cr 0}\right]
52$$
53with $x$ and $y$ scalars.
54@end tex
55@ifnottex
56The Givens matrix is a 2-by-2 orthogonal matrix
57
58@example
59@group
60@var{G} = [ @var{c} , @var{s}
61 -@var{s}', @var{c}]
62@end group
63@end example
64
65@noindent
66such that
67
68@example
69@var{G} * [@var{x}; @var{y}] = [*; 0]
70@end example
71
72@noindent
73with @var{x} and @var{y} scalars.
74@end ifnottex
75
76If two output arguments are requested, return the factors @var{c} and @var{s}
77rather than the Givens rotation matrix.
78
79For example:
80
81@example
82@group
83givens (1, 1)
84 @result{} 0.70711 0.70711
85 -0.70711 0.70711
86@end group
87@end example
88
89Note: The Givens matrix represents a counterclockwise rotation of a 2-D
90plane and can be used to introduce zeros into a matrix prior to complete
91factorization.
92@seealso{planerot, qr}
93@end deftypefn */)
94{
95 if (args.length () != 2)
96 print_usage ();
97
98 octave_value_list retval;
99
100 if (args(0).is_single_type () || args(1).is_single_type ())
101 {
102 if (args(0).iscomplex () || args(1).iscomplex ())
103 {
104 FloatComplex cx = args(0).float_complex_value ();
105 FloatComplex cy = args(1).float_complex_value ();
106
107 FloatComplexMatrix result = Givens (cx, cy);
108
109 switch (nargout)
110 {
111 case 0:
112 case 1:
113 retval = ovl (result);
114 break;
115
116 case 2:
117 retval = ovl (result(0, 0), result(0, 1));
118 break;
119 }
120 }
121 else
122 {
123 float x = args(0).float_value ();
124 float y = args(1).float_value ();
125
126 FloatMatrix result = Givens (x, y);
127
128 switch (nargout)
129 {
130 case 0:
131 case 1:
132 retval = ovl (result);
133 break;
134
135 case 2:
136 retval = ovl (result(0, 0), result(0, 1));
137 break;
138 }
139 }
140 }
141 else
142 {
143 if (args(0).iscomplex () || args(1).iscomplex ())
144 {
145 Complex cx = args(0).complex_value ();
146 Complex cy = args(1).complex_value ();
147
148 ComplexMatrix result = Givens (cx, cy);
149
150 switch (nargout)
151 {
152 case 0:
153 case 1:
154 retval = ovl (result);
155 break;
156
157 case 2:
158 retval = ovl (result(0, 0), result(0, 1));
159 break;
160 }
161 }
162 else
163 {
164 double x = args(0).double_value ();
165 double y = args(1).double_value ();
166
167 Matrix result = Givens (x, y);
168
169 switch (nargout)
170 {
171 case 0:
172 case 1:
173 retval = ovl (result);
174 break;
175
176 case 2:
177 retval = ovl (result(0, 0), result(0, 1));
178 break;
179 }
180 }
181 }
182
183 return retval;
184}
185
186/*
187%!assert (givens (1,1), [1, 1; -1, 1] / sqrt (2), 2*eps)
188%!assert (givens (1,0), eye (2))
189%!assert (givens (0,1), [0, 1; -1 0])
190
191%!error givens ()
192%!error givens (1)
193%!error [a,b,c] = givens (1, 1)
194*/
195
196OCTAVE_END_NAMESPACE(octave)
ComplexMatrix Givens(const Complex &x, const Complex &y)
Definition CMatrix.cc:3227
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage()
Definition defun-int.h:72
#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_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217