Mercurial > hg > octave-lyh
annotate scripts/specfun/nthroot.m @ 11532:34bb8d38f19f
Add undocumented function cbrt to documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 14 Jan 2011 14:05:15 -0800 |
parents | fd0a3ac60b0e |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2004-2011 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 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5827 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5827 | 19 ## |
20 ## Original version by Paul Kienzle distributed as free software in the | |
21 ## public domain. | |
22 | |
23 ## -*- texinfo -*- | |
24 ## @deftypefn {Function File} {} nthroot (@var{x}, @var{n}) | |
25 ## | |
8494 | 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 | 28 ## |
29 ## @example | |
30 ## @group | |
31 ## nthroot (-1, 3) | |
32 ## @result{} -1 | |
33 ## (-1) ^ (1 / 3) | |
34 ## @result{} 0.50000 - 0.86603i | |
35 ## @end group | |
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. |
11532
34bb8d38f19f
Add undocumented function cbrt to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
40 ## @seealso{realsqrt, sqrt, cbrt} |
5827 | 41 ## @end deftypefn |
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 | 44 |
45 if (nargin != 2) | |
46 print_usage (); | |
47 endif | |
48 | |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
49 if (! isscalar (n)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
50 error ("nthroot: N must be a nonzero scalar"); |
5827 | 51 endif |
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)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
64 error ("nthroot: if X contains negative values, N must be an odd integer"); |
10415
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 | 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 | 74 |
75 endif | |
76 | |
77 endfunction | |
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); |