octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[CHANGESET] reallog, realpow and realsqrt functions


From: David Bateman
Subject: [CHANGESET] reallog, realpow and realsqrt functions
Date: Fri, 21 Mar 2008 18:43:46 +0100
User-agent: Thunderbird 2.0.0.12 (X11/20080306)

Here is a patch that adds these three rather trivial functions that
return an error if the return value is complex. Funnily to be matlab
compatible "realpow(1i,2)" is allowed. These are matlab core functions,
though I wonder how much they are really used..

D.
# HG changeset patch
# User David Bateman <address@hidden>
# Date 1206112141 -3600
# Node ID 30680ac6c22593cc6976eb6b3f649e89e7c08a16
# Parent  c4d7313942644b03f11b4e3b72c78543ff2de2bb
Add the reallog, realsqrt and realpow functions

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@ 2008-03-20  David Bateman  <address@hidden
+2008-03-21  David Bateman  <address@hidden>
+
+       * specfun/reallog.m, specfun/realpow.m, specfun/realsqrt.m: New
+       functions.
+
 2008-03-20  David Bateman  <address@hidden>
 
        * general/tril.m, general/triu.m: Fail if input is a structure.
diff --git a/scripts/specfun/Makefile.in b/scripts/specfun/Makefile.in
--- a/scripts/specfun/Makefile.in
+++ b/scripts/specfun/Makefile.in
@@ -35,7 +35,7 @@ INSTALL_DATA = @INSTALL_DATA@
 
 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))
 
diff --git a/scripts/specfun/reallog.m b/scripts/specfun/reallog.m
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))
diff --git a/scripts/specfun/realpow.m b/scripts/specfun/realpow.m
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
+## @address@hidden .^ @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))
diff --git a/scripts/specfun/realsqrt.m b/scripts/specfun/realsqrt.m
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))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]