Mercurial > hg > octave-nkf
comparison scripts/linear-algebra/planerot.m @ 19361:1faae07afbd8
Overhaul givens and planerot functions.
* givens.cc (Fgivens): Redo docstring. Remove useless default to detect
nargout > 2 in switch statements.
* planerot.m: Redo docstring. Add input validation. Add BIST tests for input validation.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 27 Sep 2014 13:41:57 -0700 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
19360:9163a6e9b096 | 19361:1faae07afbd8 |
---|---|
15 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
16 ## along with Octave; see the file COPYING. If not, see | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 17 ## <http://www.gnu.org/licenses/>. |
18 | 18 |
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {[@var{g}, @var{y}] =} planerot (@var{x}) | 20 ## @deftypefn {Function File} {[@var{G}, @var{y}] =} planerot (@var{x}) |
21 ## Given a two-element column vector, returns the | 21 ## Given a two-element column vector, return the |
22 ## @tex | 22 ## @tex |
23 ## $2 \times 2$ orthogonal matrix | 23 ## $2 \times 2$ orthogonal matrix |
24 ## @end tex | 24 ## @end tex |
25 ## @ifnottex | 25 ## @ifnottex |
26 ## 2 by 2 orthogonal matrix | 26 ## 2 by 2 orthogonal matrix |
29 ## @code{@var{y} = @var{g} * @var{x}} and @code{@var{y}(2) = 0}. | 29 ## @code{@var{y} = @var{g} * @var{x}} and @code{@var{y}(2) = 0}. |
30 ## @seealso{givens} | 30 ## @seealso{givens} |
31 ## @end deftypefn | 31 ## @end deftypefn |
32 | 32 |
33 function [G, y] = planerot (x) | 33 function [G, y] = planerot (x) |
34 | |
35 if (nargin != 1) | |
36 print_usage (); | |
37 elseif (! (isvector (x) && numel (x) == 2)) | |
38 error ("planerot: X must be a 2-element vector"); | |
39 endif | |
40 | |
34 G = givens (x(1), x(2)); | 41 G = givens (x(1), x(2)); |
35 y = G * x(:); | 42 y = G * x(:); |
43 | |
36 endfunction | 44 endfunction |
37 | 45 |
38 | 46 |
39 %!test | 47 %!test |
40 %! x = [3 4]; | 48 %! x = [3 4]; |
41 %! [g y] = planerot (x); | 49 %! [g y] = planerot (x); |
42 %! assert (g, [x(1) x(2); -x(2) x(1)] / sqrt (x(1)^2 + x(2)^2), 2e-8); | 50 %! assert (g, [x(1) x(2); -x(2) x(1)] / sqrt (x(1)^2 + x(2)^2), 2e-8); |
43 %! assert (y(2), 0, 2e-8); | 51 %! assert (y(2), 0, 2e-8); |
44 | 52 |
45 %!error planerot ([0]) | |
46 %!error planerot ([0 0 0]) | |
47 %!error planerot () | 53 %!error planerot () |
54 %!error planerot (1,2) | |
55 %!error <X must be a 2-element vector> planerot (ones (2,2)) | |
56 %!error <X must be a 2-element vector> planerot ([0 0 0]) | |
48 | 57 |