annotate scripts/specfun/nthroot.m @ 10821:693e22af08ae

Grammarcheck documentation of m-files Add newlines between @item fields for readability.
author Rik <octave@nomad.inbox5.com>
date Mon, 26 Jul 2010 21:25:36 -0700
parents fbd7843974fa
children 1740012184f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8494
diff changeset
1 ## Copyright (C) 2004, 2006, 2007, 2009 Paul Kienzle
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
2 ## Copyright (C) 2010 VZLU Prague
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
3 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
5 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
9 ## your option) any later version.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
10 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5827
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
19 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
20 ## Original version by Paul Kienzle distributed as free software in the
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
21 ## public domain.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
22
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
23 ## -*- texinfo -*-
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
24 ## @deftypefn {Function File} {} nthroot (@var{x}, @var{n})
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
25 ##
8494
836618fee9d6 [docs] add hyphen
Brian Gough <bjg@gnu.org>
parents: 7017
diff changeset
26 ## Compute the n-th root of @var{x}, returning real results for real
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
27 ## components of @var{x}. For example:
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
28 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
29 ## @example
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
30 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
31 ## nthroot (-1, 3)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
32 ## @result{} -1
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
33 ## (-1) ^ (1 / 3)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
34 ## @result{} 0.50000 - 0.86603i
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
35 ## @end group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
36 ## @end example
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
37 ##
10711
fbd7843974fa Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents: 10415
diff changeset
38 ## @var{n} must be a scalar. If @var{n} is not an even integer and @var{X} has
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
39 ## negative entries, an error is produced.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
40 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
41 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
42
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
43 function y = nthroot (x, n)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
44
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45 if (nargin != 2)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
46 print_usage ();
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
47 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
48
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
49 if (! isscalar (n))
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
50 error ("nthroot: n must be a nonzero scalar");
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
51 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
52
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
53 if (n == 3)
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
54 y = cbrt (x);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
55 elseif (n == -3)
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
56 y = 1 ./ cbrt (x);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
57 elseif (n < 0)
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
58 y = 1 ./ nthroot (x, -n);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
59 else
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
60 ## Compute using power.
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
61 if (n == round (n) && mod (n, 2) == 1)
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
62 y = abs (x) .^ (1/n) .* sign (x);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
63 elseif (any (x(:) < 0))
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
64 error ("nthroot: if x contains negative values, n must be an odd integer");
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
65 else
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
66 y = x .^ (1/n);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
67 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
68
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
69 if (finite (n) && n > 0 && n == round (n))
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
70 ## Correction.
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
71 y = ((n-1)*y + x ./ (y.^(n-1))) / n;
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
72 y = merge (finite (y), y, x);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
73 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
74
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
75 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
76
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
77 endfunction
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
78
10415
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
79 %!assert(nthroot(-32,5), -2);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
80 %!assert(nthroot(81,4), 3);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
81 %!assert(nthroot(Inf,4), Inf);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
82 %!assert(nthroot(-Inf,7), -Inf);
976e76b77fa0 rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
83 %!assert(nthroot(-Inf,-7), 0);