changeset 19169: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 9163a6e9b096
children a6d44158bc6d
files libinterp/corefcn/givens.cc scripts/linear-algebra/planerot.m
diffstat 2 files changed, 31 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/givens.cc	Sat Sep 27 12:47:37 2014 -0700
+++ b/libinterp/corefcn/givens.cc	Sat Sep 27 13:41:57 2014 -0700
@@ -32,10 +32,12 @@
 
 DEFUN (givens, args, nargout,
        "-*- texinfo -*-\n\
-@deftypefn  {Built-in Function} {@var{g} =} givens (@var{x}, @var{y})\n\
+@deftypefn  {Built-in Function} {@var{G} =} givens (@var{x}, @var{y})\n\
 @deftypefnx {Built-in Function} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})\n\
+Compute the Givens rotation matrix @var{G}.\n\
+\n\
 @tex\n\
-Return a $2\\times 2$ orthogonal matrix\n\
+The Givens matrix is a $2\\times 2$ orthogonal matrix\n\
 $$\n\
  G = \\left[\\matrix{c & s\\cr -s'& c\\cr}\\right]\n\
 $$\n\
@@ -46,11 +48,20 @@
 with $x$ and $y$ scalars.\n\
 @end tex\n\
 @ifnottex\n\
-Return a 2 by 2 orthogonal matrix\n\
-@code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]} such that\n\
-@code{@var{g} [@var{x}; @var{y}] = [*; 0]} with @var{x} and @var{y} scalars.\n\
+The Givens matrix is a 2 by 2 orthogonal matrix\n\
+\n\
+@code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]}\n\
+\n\
+such that\n\
+\n\
+@code{@var{g} [@var{x}; @var{y}] = [*; 0]}\n\
+\n\
+with @var{x} and @var{y} scalars.\n\
 @end ifnottex\n\
 \n\
+If two output arguments are requested, return the factors @var{c} and\n\
+@var{s} rather than the Givens rotation matrix.\n\
+\n\
 For example:\n\
 \n\
 @example\n\
@@ -60,6 +71,7 @@
        -0.70711   0.70711\n\
 @end group\n\
 @end example\n\
+@seealso{planerot}\n\
 @end deftypefn")
 {
   octave_value_list retval;
@@ -97,10 +109,6 @@
                           retval(1) = result (0, 1);
                           retval(0) = result (0, 0);
                           break;
-
-                        default:
-                          error ("givens: invalid number of output arguments");
-                          break;
                         }
                     }
                 }
@@ -127,10 +135,6 @@
                           retval(1) = result (0, 1);
                           retval(0) = result (0, 0);
                           break;
-
-                        default:
-                          error ("givens: invalid number of output arguments");
-                          break;
                         }
                     }
                 }
@@ -160,10 +164,6 @@
                           retval(1) = result (0, 1);
                           retval(0) = result (0, 0);
                           break;
-
-                        default:
-                          error ("givens: invalid number of output arguments");
-                          break;
                         }
                     }
                 }
@@ -190,10 +190,6 @@
                           retval(1) = result (0, 1);
                           retval(0) = result (0, 0);
                           break;
-
-                        default:
-                          error ("givens: invalid number of output arguments");
-                          break;
                         }
                     }
                 }
@@ -211,4 +207,5 @@
 
 %!error givens ()
 %!error givens (1)
+%!error [a,b,c] = givens (1, 1)
 */
--- a/scripts/linear-algebra/planerot.m	Sat Sep 27 12:47:37 2014 -0700
+++ b/scripts/linear-algebra/planerot.m	Sat Sep 27 13:41:57 2014 -0700
@@ -17,8 +17,8 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {[@var{g}, @var{y}] =} planerot (@var{x})
-## Given a two-element column vector, returns the
+## @deftypefn {Function File} {[@var{G}, @var{y}] =} planerot (@var{x})
+## Given a two-element column vector, return the
 ## @tex
 ## $2 \times 2$ orthogonal matrix
 ## @end tex
@@ -31,8 +31,16 @@
 ## @end deftypefn
 
 function [G, y] = planerot (x)
+
+  if (nargin != 1)
+    print_usage ();
+  elseif (! (isvector (x) && numel (x) == 2))
+    error ("planerot: X must be a 2-element vector");
+  endif
+
   G = givens (x(1), x(2));
   y = G * x(:);
+
 endfunction
 
 
@@ -42,7 +50,8 @@
 %! assert (g, [x(1) x(2); -x(2) x(1)] / sqrt (x(1)^2 + x(2)^2), 2e-8);
 %! assert (y(2), 0, 2e-8);
 
-%!error planerot ([0])
-%!error planerot ([0 0 0])
 %!error planerot ()
+%!error planerot (1,2)
+%!error <X must be a 2-element vector> planerot (ones (2,2))
+%!error <X must be a 2-element vector> planerot ([0 0 0])