Mercurial > hg > octave-lyh
annotate scripts/specfun/nthroot.m @ 15806:01d4f742d75d
doc: Re-organize and improve Java Interface documentation.
* doc/interpreter/java.txi: Add usejava, isjava functions, debug_java,
java_matrix_autoconversion, java_unsigned_autoconversion to docs.
Re-organize to put functions in logical order. Add new "Dialog Box Functions"
node.
* doc/interpreter/system.txi: Remove usejava from list of system functions.
* libinterp/octave-value/ov-java.cc(FjavaObject, FjavaMethod,
Fjava_unsigned_autoconversion): Improve docstrings.
* scripts/java/javaArray.m: Tweak docstring.
* scripts/java/listdlg.m: Wrap long lines in docstring. Improve
code example in docstring.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 17 Dec 2012 10:23:43 -0800 |
parents | f3d52523cde1 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
1 ## Copyright (C) 2004-2012 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}) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11532
diff
changeset
|
25 ## |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11532
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 | 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 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11532
diff
changeset
|
37 ## |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12628
diff
changeset
|
38 ## @var{x} must have all real entries. @var{n} must be a scalar. |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
39 ## If @var{n} is an even integer and @var{X} has negative entries, an |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
40 ## error is produced. |
11532
34bb8d38f19f
Add undocumented function cbrt to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
41 ## @seealso{realsqrt, sqrt, cbrt} |
5827 | 42 ## @end deftypefn |
43 | |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
44 function y = nthroot (x, n) |
5827 | 45 |
46 if (nargin != 2) | |
47 print_usage (); | |
48 endif | |
49 | |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 if (any (iscomplex (x(:)))) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 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
|
52 endif |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 if (! isscalar (n) || n == 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
|
55 error ("nthroot: N must be a nonzero scalar"); |
5827 | 56 endif |
57 | |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
58 if (n == 3) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 y = cbrt (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 elseif (n == -3) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
61 y = 1 ./ cbrt (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
62 elseif (n < 0) |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
63 y = 1 ./ nthroot (x, -n); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
64 else |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
65 ## Compute using power. |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 if (n == fix (n) && mod (n, 2) == 1) |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
67 y = abs (x) .^ (1/n) .* sign (x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
68 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
|
69 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
|
70 else |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
71 y = x .^ (1/n); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
72 endif |
5827 | 73 |
12628
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 if (finite (n) && n > 0 && n == fix (n)) |
10415
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
75 ## Correction. |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
76 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
|
77 y = merge (finite (y), y, x); |
976e76b77fa0
rewrite nth_root, move to specfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
78 endif |
5827 | 79 |
80 endif | |
81 | |
82 endfunction | |
83 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %!assert (nthroot (-32,5), -2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %!assert (nthroot (81,4), 3); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 %!assert (nthroot (Inf,4), Inf); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!assert (nthroot (-Inf,7), -Inf); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!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
|
90 |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 %% Test input validation |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 %!error (nthroot ()) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %!error (nthroot (1)) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 %!error (nthroot (1,2,3)) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %!error (nthroot (1+j,2)) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 %!error (nthroot (1,[1 2])) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 %!error (nthroot (1,0)) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 %!error (nthroot (-1,2)) |
619fbc98a7eb
nthroot.m: Fix input validation to disallow complex values (bug #33135)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 |