1## Copyright (C) 2004, 2006, 2007, 2009 Paul Kienzle
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
19## Original version by Paul Kienzle distributed as free software in the
23## @deftypefn {Function File} {} nthroot (@var{x}, @var{n})
25## Compute the n-th root of @var{x}, returning real results for real
26## components of @var{x}. For example
33## @result{} 0.50000 - 0.86603i
39function y = nthroot (x, m)
55 idx = (mod (m, 2) == 1 & imag (x) == 0 & x < 0);
58 y(idx) = -(-x(idx)).^(1./m(idx));
61 ## If result is all real, make sure it looks real
62 if (all (imag (y) == 0))
68%!assert(nthroot(-1,[3,-3]), [-1,-1],eps);
69%!assert(nthroot([-1,1],[3.1,-3]), [-1,1].^(1./[3.1,-3]));
70%!assert(nthroot([-1+1i,-1-1i],3), [-1+1i,-1-1i].^(1/3));