1######################################################################## 2## 3## Copyright (C) 2008-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {} realpow (@var{x}, @var{y}) 28## Compute the real-valued, element-by-element power operator. 29## 30## This is equivalent to @w{@code{@var{x} .^ @var{y}}}, except that 31## @code{realpow} reports an error if any return value is complex. 32## @seealso{power, reallog, realsqrt} 33## @end deftypefn 34 35function z = realpow (x, y) 36 37 if (nargin != 2) 38 print_usage (); 39 endif 40 41 z = x .^ y; 42 if (iscomplex (z)) 43 error ("realpow: produced complex result"); 44 endif 45 46endfunction 47 48 49%!assert (realpow (1:10, 0.5:0.5:5), power (1:10, 0.5:0.5:5)) 50%!assert (realpow (1:10, 0.5:0.5:5), [1:10] .^ [0.5:0.5:5]) 51%!test 52%! x = rand (10, 10); 53%! y = randn (10, 10); 54%! assert (x.^y, realpow (x,y)); 55%!assert <47775> (realpow (1i,2), -1) 56 57%!error realpow () 58%!error realpow (1) 59%!error realpow (1,2,3) 60%!error <produced complex result> realpow (-1, 1/2) 61