# HG changeset patch # User Jaroslav Hajek # Date 1268748992 -3600 # Node ID 2a8b1db1e2ca81b011bf59dbb3e362689a0041e3 # Parent 271c5262975b0c5847a7604895a2185a136e9f52 implement built-in cbrt diff -r 271c5262975b -r 2a8b1db1e2ca configure.ac --- a/configure.ac Tue Mar 16 12:29:49 2010 +0100 +++ b/configure.ac Tue Mar 16 15:16:32 2010 +0100 @@ -1690,7 +1690,7 @@ ### Check for nonstandard but common math functions that we need. AC_CHECK_FUNCS(acosh acoshf asinh asinhf atanh atanhf erf erff erfc erfcf exp2f log2 log2f) -AC_CHECK_FUNCS(hypotf _hypotf) +AC_CHECK_FUNCS(hypotf _hypotf cbrt cbrtf) ### Checks for OS specific cruft. diff -r 271c5262975b -r 2a8b1db1e2ca liboctave/ChangeLog --- a/liboctave/ChangeLog Tue Mar 16 12:29:49 2010 +0100 +++ b/liboctave/ChangeLog Tue Mar 16 15:16:32 2010 +0100 @@ -1,3 +1,9 @@ +2010-03-16 Jaroslav Hajek + + * lo-specfun.cc: Substitute cbrt and cbrtf if needed. + * lo-specfun.h: Declare cbrt and cbrtf if needed. + * configure.ac: Check for cbrt and cbrtf. + 2010-03-15 John W. Eaton * oct-glob.cc (octave_glob): Tag calls to glob and globfree with diff -r 271c5262975b -r 2a8b1db1e2ca liboctave/lo-specfun.cc --- a/liboctave/lo-specfun.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/liboctave/lo-specfun.cc Tue Mar 16 15:16:32 2010 +0100 @@ -560,6 +560,22 @@ return retval; } +#if !defined (HAVE_CBRT) +double cbrt (double x) +{ + static const double one_third = 0.3333333333333333333; + if (xfinite (x)) + { + // Use pow. + double y = std::pow (std::abs (x), one_third) * signum (x); + // Correct for better accuracy. + return (x / (y*y) + y + y) / 3; + } + else + return x; +} +#endif + #if !defined (HAVE_LOG1PF) float log1pf (float x) @@ -603,6 +619,22 @@ return retval; } +#if !defined (HAVE_CBRTF) +float cbrtf (float x) +{ + static const float one_third = 0.3333333333333333333f; + if (xfinite (x)) + { + // Use pow. + float y = std::pow (std::abs (x), one_third) * signum (x); + // Correct for better accuracy. + return (x / (y*y) + y + y) / 3; + } + else + return x; +} +#endif + static inline Complex zbesj (const Complex& z, double alpha, int kode, octave_idx_type& ierr); diff -r 271c5262975b -r 2a8b1db1e2ca liboctave/lo-specfun.h --- a/liboctave/lo-specfun.h Tue Mar 16 12:29:49 2010 +0100 +++ b/liboctave/lo-specfun.h Tue Mar 16 15:16:32 2010 +0100 @@ -101,6 +101,14 @@ #endif extern OCTAVE_API FloatComplex log1p (const FloatComplex& x); +#if !defined (HAVE_CBRT) +extern OCTAVE_API double cbrt (double x); +#endif + +#if !defined (HAVE_CBRTF) +extern OCTAVE_API float cbrtf (float x); +#endif + extern OCTAVE_API double xgamma (double x); extern OCTAVE_API double xlgamma (double x); extern OCTAVE_API Complex rc_lgamma (double x); diff -r 271c5262975b -r 2a8b1db1e2ca src/ChangeLog --- a/src/ChangeLog Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ChangeLog Tue Mar 16 15:16:32 2010 +0100 @@ -1,3 +1,13 @@ +2010-03-16 Jaroslav Hajek + + * ov-base.h (unary_mapper_t::umap_cbrt): New enum member. + * ov.h (octave_value::cbrt): New forwarder method. + * ov-scalar.cc (octave_scalar::map): Handle cbrt. + * ov-float.cc (octave_float_scalar::map): Ditto. + * ov-re-mat.cc (octave_matrix::map): Ditto. + * ov-flt-re-mat.cc (octave_float_matrix::map): Ditto. + * ov-re-sparse.cc (octave_sparse_matrix::map): Ditto. + 2010-03-15 John W. Eaton * oct-parse.yy, lex.ll: Undefine GNULIB_NAMESPACE immediately diff -r 271c5262975b -r 2a8b1db1e2ca src/mappers.cc --- a/src/mappers.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/mappers.cc Tue Mar 16 15:16:32 2010 +0100 @@ -351,6 +351,35 @@ */ +DEFUN (cbrt, args, , + "-*- texinfo -*-\n\ +@deftypefn {Mapping Function} {} cbrt (@var{x})\n\ +Return the real cube root of @var{x}. Unlike @code{@var{x}^(1/3)},\n\ +the result will be negative if @var{x} is negative.\n\ +@end deftypefn") +{ + octave_value retval; + if (args.length () == 1) + retval = args(0).cbrt (); + else + print_usage (); + + return retval; +} + +/* + +%!assert (cbrt (64), 4) +%!assert (cbrt (-125), -5) +%!assert (cbrt (0), 0) +%!assert (cbrt (Inf), Inf) +%!assert (cbrt (-Inf), -Inf) +%!assert (cbrt (NaN), NaN) +%!assert (cbrt (2^300), 2^100) +%!assert (cbrt (125*2^300), 5*2^100) + +*/ + DEFUN (ceil, args, , "-*- texinfo -*-\n\ @deftypefn {Mapping Function} {} ceil (@var{x})\n\ diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-base.cc --- a/src/ov-base.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-base.cc Tue Mar 16 15:16:32 2010 +0100 @@ -1150,6 +1150,7 @@ "asinh", "atan", "atanh", + "cbrt", "ceil", "conj", "cos", diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-base.h --- a/src/ov-base.h Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-base.h Tue Mar 16 15:16:32 2010 +0100 @@ -658,6 +658,7 @@ umap_asinh, umap_atan, umap_atanh, + umap_cbrt, umap_ceil, umap_conj, umap_cos, diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-float.cc --- a/src/ov-float.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-float.cc Tue Mar 16 15:16:32 2010 +0100 @@ -290,6 +290,7 @@ SCALAR_MAPPER (erfcx, ::erfcx); SCALAR_MAPPER (gamma, xgamma); SCALAR_MAPPER (lgamma, rc_lgamma); + SCALAR_MAPPER (cbrt, ::cbrtf); SCALAR_MAPPER (ceil, ::ceilf); SCALAR_MAPPER (cos, ::cosf); SCALAR_MAPPER (cosh, ::coshf); diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-flt-re-mat.cc --- a/src/ov-flt-re-mat.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-flt-re-mat.cc Tue Mar 16 15:16:32 2010 +0100 @@ -770,6 +770,7 @@ ARRAY_MAPPER (erfcx, float, ::erfcx); ARRAY_MAPPER (gamma, float, xgamma); RC_ARRAY_MAPPER (lgamma, FloatComplex, rc_lgamma); + ARRAY_MAPPER (cbrt, float, ::cbrtf); ARRAY_MAPPER (ceil, float, ::ceilf); ARRAY_MAPPER (cos, float, ::cosf); ARRAY_MAPPER (cosh, float, ::coshf); diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-re-mat.cc --- a/src/ov-re-mat.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-re-mat.cc Tue Mar 16 15:16:32 2010 +0100 @@ -893,6 +893,7 @@ ARRAY_MAPPER (erfcx, double, ::erfcx); ARRAY_MAPPER (gamma, double, xgamma); RC_ARRAY_MAPPER (lgamma, Complex, rc_lgamma); + ARRAY_MAPPER (cbrt, double, ::cbrt); ARRAY_MAPPER (ceil, double, ::ceil); ARRAY_MAPPER (cos, double, ::cos); ARRAY_MAPPER (cosh, double, ::cosh); diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-re-sparse.cc --- a/src/ov-re-sparse.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-re-sparse.cc Tue Mar 16 15:16:32 2010 +0100 @@ -913,6 +913,7 @@ ARRAY_MAPPER (erfc, double, ::erfc); ARRAY_MAPPER (gamma, double, xgamma); ARRAY_MAPPER (lgamma, Complex, rc_lgamma); + ARRAY_MAPPER (cbrt, double, ::cbrt); ARRAY_MAPPER (ceil, double, ::ceil); ARRAY_MAPPER (cos, double, ::cos); ARRAY_MAPPER (cosh, double, ::cosh); diff -r 271c5262975b -r 2a8b1db1e2ca src/ov-scalar.cc --- a/src/ov-scalar.cc Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov-scalar.cc Tue Mar 16 15:16:32 2010 +0100 @@ -306,6 +306,7 @@ SCALAR_MAPPER (erfcx, ::erfcx); SCALAR_MAPPER (gamma, xgamma); SCALAR_MAPPER (lgamma, rc_lgamma); + SCALAR_MAPPER (cbrt, ::cbrt); SCALAR_MAPPER (ceil, ::ceil); SCALAR_MAPPER (cos, ::cos); SCALAR_MAPPER (cosh, ::cosh); diff -r 271c5262975b -r 2a8b1db1e2ca src/ov.h --- a/src/ov.h Tue Mar 16 12:29:49 2010 +0100 +++ b/src/ov.h Tue Mar 16 15:16:32 2010 +0100 @@ -1082,6 +1082,7 @@ MAPPER_FORWARD (asinh) MAPPER_FORWARD (atan) MAPPER_FORWARD (atanh) + MAPPER_FORWARD (cbrt) MAPPER_FORWARD (ceil) MAPPER_FORWARD (conj) MAPPER_FORWARD (cos)