Mercurial > hg > octave-jordi
annotate scripts/specfun/nthroot.m @ 20851:516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 12 Dec 2015 07:31:00 -0800 |
parents | 9fc020886ae9 |
children |
rev | line source |
---|---|
19696
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19291
diff
changeset
|
1 ## Copyright (C) 2004-2015 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 -*- | |
20851
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19832
diff
changeset
|
24 ## @deftypefn {} {} nthroot (@var{x}, @var{n}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11532
diff
changeset
|
25 ## |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
26 ## Compute the real (non-complex) @var{n}-th root of @var{x}. |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
27 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
28 ## @var{x} must have all real entries and @var{n} must be a scalar. |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## If @var{n} is an even integer and @var{x} has negative entries then |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## @code{nthroot} aborts and issues an error. |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 ## |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## Example: |
5827 | 33 ## |
34 ## @example | |
35 ## @group | |
36 ## nthroot (-1, 3) | |
37 ## @result{} -1 | |
38 ## (-1) ^ (1 / 3) | |
39 ## @result{} 0.50000 - 0.86603i | |
40 ## @end group | |
41 ## @end example | |
11532
34bb8d38f19f
Add undocumented function cbrt to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 ## @seealso{realsqrt, sqrt, cbrt} |
5827 | 43 ## @end deftypefn |
44 | |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
45 function y = nthroot (x, n) |
5827 | 46 |
47 if (nargin != 2) | |
48 print_usage (); | |
49 endif | |
50 | |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 if (iscomplex (x)) |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 error ("nthroot: X must not contain complex values"); |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 endif |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 if (! isreal (n) || ! isscalar (n) || n == 0) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 error ("nthroot: N must be a real nonzero scalar"); |
5827 | 57 endif |
58 | |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 if (n == 3) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 y = cbrt (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
61 elseif (n == -3) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
62 y = 1 ./ cbrt (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
63 elseif (n < 0) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
64 y = 1 ./ nthroot (x, -n); |
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 ## Compute using power. |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 integer_n = n == fix (n); |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
68 if (integer_n && mod (n, 2) == 1) |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
69 y = abs (x) .^ (1/n) .* sign (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
70 elseif (any (x(:) < 0)) |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 error ("nthroot: N must be an odd integer if X contains negative values"); |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
72 else |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
73 y = x .^ (1/n); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
74 endif |
5827 | 75 |
19116
a4c226a963c5
Deprecate finite in favor of isfinite.
Rik <rik@octave.org>
parents:
19101
diff
changeset
|
76 if (integer_n && n > 0 && isfinite (n)) |
19291
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
77 if (isscalar (y) && y == 0) |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
78 ## Don't apply correction which leads to division by zero (bug #43492) |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
79 else |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
80 ## FIXME: What is this correction for? |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
81 y = ((n-1)*y + x ./ (y.^(n-1))) / n; |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
82 y = merge (isfinite (y), y, x); |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
83 endif |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
84 endif |
5827 | 85 endif |
86 | |
87 endfunction | |
88 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 %!assert (nthroot (-32, 5), -2) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 %!assert (nthroot (81, 4), 3) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
92 %!assert (nthroot (Inf, 4), Inf) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 %!assert (nthroot (-Inf, 7), -Inf) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
94 %!assert (nthroot (-Inf, -7), 0) |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 |
19291
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
96 ## Bug #43492. This should not generate a division by zero warning |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
97 %!test |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
98 %! warnmsg = lastwarn (); |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
99 %! assert (nthroot (0, 2), 0); |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
100 %! assert (lastwarn (), warnmsg); |
238522618904
nthroot.m: Fix division by zero warning when input x is 0 (bug #43492).
Rik <rik@octave.org>
parents:
19116
diff
changeset
|
101 |
19832
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19696
diff
changeset
|
102 ## Test input validation |
19101
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 %!error nthroot () |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 %!error nthroot (1) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 %!error nthroot (1,2,3) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 %!error <X must not contain complex values> nthroot (1+j, 2) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 %!error <N must be a real nonzero scalar> nthroot (1, i) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 %!error <N must be a real nonzero scalar> nthroot (1, [1 2]) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 %!error <N must be a real nonzero scalar> nthroot (1, 0) |
bb20384acf7b
doc: Overhaul documentation for some functions in specfun/ dir.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 %!error <N must be an odd integer> nthroot (-1, 2) |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 |