Mercurial > hg > octave-lyh
changeset 7621:4682dda22527
Add the reallog, realsqrt and realpow functions
author | David Bateman <dbateman@free.fr> |
---|---|
date | Fri, 21 Mar 2008 16:09:01 +0100 |
parents | 36594d5bbe13 |
children | c195bd0a5c64 |
files | scripts/ChangeLog scripts/specfun/Makefile.in scripts/specfun/reallog.m scripts/specfun/realpow.m scripts/specfun/realsqrt.m |
diffstat | 5 files changed, 131 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2008-03-21 David Bateman <dbateman@free.fr> + + * specfun/reallog.m, specfun/realpow.m, specfun/realsqrt.m: New + functions. + 2008-03-20 David Bateman <dbateman@free.fr> * general/tril.m, general/triu.m: Fail if input is a structure.
--- a/scripts/specfun/Makefile.in +++ b/scripts/specfun/Makefile.in @@ -35,7 +35,7 @@ SOURCES = bessel.m beta.m betai.m betaln.m erfinv.m factor.m \ factorial.m gammai.m isprime.m legendre.m log2.m nchoosek.m \ - perms.m pow2.m primes.m + perms.m pow2.m primes.m reallog.m realpow.m realsqrt.m DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES))
new file mode 100644 --- /dev/null +++ b/scripts/specfun/reallog.m @@ -0,0 +1,40 @@ +## Copyright (C) 2008 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} realsqrt (@var{x}) +## Return the real natural logarithm of @var{x}. If any element results in the +## return value being complex @code{reallog} produces an error. +## @seealso{log, realsqrt, realpow} +## @end deftypefn + +function y = reallog (x) + if (nargin != 1) + print_usage (); + elseif (iscomplex (x) || any (x(:) < 0)) + error ("reallog: produced complex result"); + else + y = log (x); + endif +endfunction + +%!assert (log(1:5),reallog(1:5)) +%!test +%! x = rand (10,10); +%! assert (log(x),reallog(x)) +%!error (reallog(-1))
new file mode 100644 --- /dev/null +++ b/scripts/specfun/realpow.m @@ -0,0 +1,45 @@ +## Copyright (C) 2008 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} realpow (@var{x}, @var{y}) +## Return the element by element power operator. This is equivalent to +## @code{@var{x} .^ @var{y}), except that if the return value +## is complex @code{realpow} produces an error. +## @seealso{log, reallog, realsqrt} +## @end deftypefn + +function z = realpow (x, y) + if (nargin != 2) + print_usage (); + else + z = x .^ y; + if (iscomplex (z)) + error ("realpow: produced complex result"); + endif + endif +endfunction + +%!assert (power (1:10, 0.5:0.5:5), realpow (1:10, 0.5:0.5:5)) +%!assert ([1:10] .^ [0.5:0.5:5], realpow (1:10, 0.5:0.5:5)) +%!test +%! x = rand (10,10); +%! y = randn (10,10); +%! assert (x.^y,realpow(x,y)) +%!assert (realpow(1i,2),-1) +%!error (realpow(-1, 1/2))
new file mode 100644 --- /dev/null +++ b/scripts/specfun/realsqrt.m @@ -0,0 +1,40 @@ +## Copyright (C) 2008 David Bateman +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} realsqrt (@var{x}) +## Return the real sqrt of @var{x}. If any element results in the +## return value being complex @code{realsqrt} produces an error. +## @seealso{sqrt, reallog, realpow} +## @end deftypefn + +function y = realsqrt (x) + if (nargin != 1) + print_usage (); + elseif (iscomplex (x) || any (x(:) < 0)) + error ("realsqrt: produced complex result"); + else + y = sqrt (x); + endif +endfunction + +%!assert (sqrt(1:5),realsqrt(1:5)) +%!test +%! x = rand (10,10); +%! assert (sqrt(x),realsqrt(x)) +%!error (realsqrt(-1))