[cig-commits] commit: Add a bunch of functions to muparserx

Mercurial hg at geodynamics.org
Wed Nov 9 00:47:59 PST 2011


changeset:   392:ee79b2711e13
user:        Walter Landry <wlandry at caltech.edu>
date:        Tue Nov 08 15:08:51 2011 -0800
files:       muparserx/parser/mpFuncNonCmplx.cpp muparserx/parser/mpFuncNonCmplx.h muparserx/parser/mpPackageNonCmplx.cpp
description:
Add a bunch of functions to muparserx


diff -r 53bd66104b6f -r ee79b2711e13 muparserx/parser/mpFuncNonCmplx.cpp
--- a/muparserx/parser/mpFuncNonCmplx.cpp	Tue Nov 08 15:08:32 2011 -0800
+++ b/muparserx/parser/mpFuncNonCmplx.cpp	Tue Nov 08 15:08:51 2011 -0800
@@ -37,15 +37,12 @@
 #include "mpValue.h"
 #include "mpError.h"
 
-#undef log
-#undef log2
+#include "boost/math/special_functions.hpp"
 
 MUP_NAMESPACE_START
 
   float_type log2(float_type v)  { return log(v) * 1.0/log(2.0); }
-  float_type asinh(float_type v) { return log(v + sqrt(v * v + 1)); }
-  float_type acosh(float_type v) { return log(v + sqrt(v * v - 1)); }
-  float_type atanh(float_type v) { return (0.5 * log((1 + v) / (1 - v))); }
+  float_type step(float_type v)  { return v>=0 ? 1.0 : 0.0; }
 
 #define MUP_UNARY_FUNC(CLASS, IDENT, FUNC, DESC)                     \
     CLASS::CLASS()                                                   \
@@ -80,18 +77,65 @@ MUP_NAMESPACE_START
     MUP_UNARY_FUNC(FunCosH,  "cosh",  cosh,  "hyperbolic cosine")
     MUP_UNARY_FUNC(FunTanH,  "tanh",  tanh,  "hyperbolic tangens")
     // hyperbolic arcus functions
-    MUP_UNARY_FUNC(FunASinH,  "asinh",  asinh,  "hyperbolic arcus sine")
-    MUP_UNARY_FUNC(FunACosH,  "acosh",  acosh,  "hyperbolic arcus cosine")
-    MUP_UNARY_FUNC(FunATanH,  "atanh",  atanh,  "hyperbolic arcus tangens")
+    MUP_UNARY_FUNC(FunASinH,"asinh",boost::math::asinh,
+                   "hyperbolic arcus sine")
+    MUP_UNARY_FUNC(FunACosH,"acosh",boost::math::acosh,
+                   "hyperbolic arcus cosine")
+    MUP_UNARY_FUNC(FunATanH,"atanh",boost::math::atanh,
+                   "hyperbolic arcus tangens")
     // logarithm functions
-    MUP_UNARY_FUNC(FunLog,   "log",   log10, "Logarithm base 10")
     MUP_UNARY_FUNC(FunLog10, "log10", log10, "Logarithm base 10")
     MUP_UNARY_FUNC(FunLog2,  "log2",  log2,  "Logarithm base 2")
-    MUP_UNARY_FUNC(FunLn,    "ln",    log,   "Natural logarithm")
+    MUP_UNARY_FUNC(FunLog,   "log",   log,   "Natural logarithm")
+    MUP_UNARY_FUNC(FunLog1p,"log1p",boost::math::log1p,"log1p(x) - log(1+x)")
+
+
+    MUP_UNARY_FUNC(FunErf,"erf",boost::math::erf,"error function")
+    MUP_UNARY_FUNC(FunErfc,"erfc",boost::math::erfc,
+                   "complement of the error function")
     // square root
     MUP_UNARY_FUNC(FunSqrt,  "sqrt",  sqrt,  "sqrt(x) - square root of x")
+    MUP_UNARY_FUNC(FunSqrt1pm1, "sqrt1pm1", boost::math::sqrt1pm1,
+                   "sqrt1pm1(x) - sqrt(1+x)-1")
+    MUP_UNARY_FUNC(FunCbrt,"cbrt",boost::math::cbrt,
+                   "cbrt(x) - cube root of x")
+
     MUP_UNARY_FUNC(FunExp,   "exp",   exp,   "exp(x) - e to the power of x")
+    MUP_UNARY_FUNC(FunExpm1,"expm1",boost::math::expm1,"expm1(x) - exp(x)-1")
     MUP_UNARY_FUNC(FunAbs,   "abs",   fabs,  "abs(x) - absolute value of x")
+    MUP_UNARY_FUNC(FunStep,  "step",  step,  "Heaviside step function")
+    MUP_UNARY_FUNC(FunFloor,"floor",floor,
+                   "floor(x) - largest integer not greater than x")
+    MUP_UNARY_FUNC(FunCeil,"ceil",ceil,
+                   "ceil(x) - smallest integer not less than x")
+
 #undef MUP_UNARY_FUNC
 
+#define MUP_BINARY_FUNC(CLASS, IDENT, FUNC, DESC)                     \
+    CLASS::CLASS()                                                   \
+    :ICallback(cmFUNC, _T(IDENT), 2)                                 \
+    {}                                                               \
+                                                                     \
+    void CLASS::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int)        \
+    {                                                                \
+      double a = a_pArg[0]->GetFloat();                               \
+      double b = a_pArg[1]->GetFloat();                               \
+      *ret = FUNC(a,b);                                              \
+    }                                                                \
+                                                                     \
+    const char_type* CLASS::GetDesc() const                          \
+    {                                                                \
+      return _T(DESC);                                               \
+    }                                                                \
+                                                                     \
+    IToken* CLASS::Clone() const                                     \
+    {                                                                \
+      return new CLASS(*this);                                       \
+    }
+
+    MUP_BINARY_FUNC(FunHypot,"hypot",boost::math::hypot,
+                    "hypot(x,y) - sqrt(x^2 + y^2)")
+
+#undef MUP_BINARY_FUNC
+
 MUP_NAMESPACE_END
diff -r 53bd66104b6f -r ee79b2711e13 muparserx/parser/mpFuncNonCmplx.h
--- a/muparserx/parser/mpFuncNonCmplx.h	Tue Nov 08 15:08:32 2011 -0800
+++ b/muparserx/parser/mpFuncNonCmplx.h	Tue Nov 08 15:08:51 2011 -0800
@@ -49,9 +49,9 @@ MUP_NAMESPACE_START
       virtual IToken* Clone() const;                                       \
     }; 
 
+    MUP_UNARY_FUNC_DEF(FunSin)
+    MUP_UNARY_FUNC_DEF(FunCos)
     MUP_UNARY_FUNC_DEF(FunTan)
-    MUP_UNARY_FUNC_DEF(FunCos)
-    MUP_UNARY_FUNC_DEF(FunSin)
     // arcus functions
     MUP_UNARY_FUNC_DEF(FunASin)
     MUP_UNARY_FUNC_DEF(FunACos)
@@ -65,15 +65,31 @@ MUP_NAMESPACE_START
     MUP_UNARY_FUNC_DEF(FunACosH)
     MUP_UNARY_FUNC_DEF(FunATanH)
     // logarithm functions
-    MUP_UNARY_FUNC_DEF(FunLog)
     MUP_UNARY_FUNC_DEF(FunLog10)
     MUP_UNARY_FUNC_DEF(FunLog2)
-    MUP_UNARY_FUNC_DEF(FunLn)
+    MUP_UNARY_FUNC_DEF(FunLog)
+    MUP_UNARY_FUNC_DEF(FunLog1p)
+    // error functions
+    MUP_UNARY_FUNC_DEF(FunErf)
+    MUP_UNARY_FUNC_DEF(FunErfc)
     // square root
     MUP_UNARY_FUNC_DEF(FunSqrt)
+    MUP_UNARY_FUNC_DEF(FunSqrt1pm1)
+    MUP_UNARY_FUNC_DEF(FunCbrt)
+
     MUP_UNARY_FUNC_DEF(FunExp)
+    MUP_UNARY_FUNC_DEF(FunExpm1)
     MUP_UNARY_FUNC_DEF(FunAbs)
+    MUP_UNARY_FUNC_DEF(FunStep)
+    MUP_UNARY_FUNC_DEF(FunFloor)
+    MUP_UNARY_FUNC_DEF(FunCeil)
+
+    // This is really a binary function, but the signature is the
+    // same.
+    MUP_UNARY_FUNC_DEF(FunHypot)
+
 #undef MUP_UNARY_FUNC_DEF
+
 }  // namespace mu
 
 #endif
diff -r 53bd66104b6f -r ee79b2711e13 muparserx/parser/mpPackageNonCmplx.cpp
--- a/muparserx/parser/mpPackageNonCmplx.cpp	Tue Nov 08 15:08:32 2011 -0800
+++ b/muparserx/parser/mpPackageNonCmplx.cpp	Tue Nov 08 15:08:51 2011 -0800
@@ -28,22 +28,39 @@ void PackageNonCmplx::AddToParser(Parser
   pParser->DefineFun(new FunSin());
   pParser->DefineFun(new FunCos());
   pParser->DefineFun(new FunTan());
+
+  pParser->DefineFun(new FunASin());
+  pParser->DefineFun(new FunACos());
+  pParser->DefineFun(new FunATan());
+
   pParser->DefineFun(new FunSinH());
   pParser->DefineFun(new FunCosH());
   pParser->DefineFun(new FunTanH());
-  pParser->DefineFun(new FunASin());
-  pParser->DefineFun(new FunACos());
-  pParser->DefineFun(new FunATan());
+
   pParser->DefineFun(new FunASinH());
   pParser->DefineFun(new FunACosH());
   pParser->DefineFun(new FunATanH());
-  pParser->DefineFun(new FunLog());
+
   pParser->DefineFun(new FunLog10());
   pParser->DefineFun(new FunLog2());
-  pParser->DefineFun(new FunLn());
+  pParser->DefineFun(new FunLog());
+  pParser->DefineFun(new FunLog1p());
+
+  pParser->DefineFun(new FunErf());
+  pParser->DefineFun(new FunErfc());
+
+  pParser->DefineFun(new FunSqrt());
+  pParser->DefineFun(new FunSqrt1pm1());
+  pParser->DefineFun(new FunCbrt());
+
   pParser->DefineFun(new FunExp());
-  pParser->DefineFun(new FunSqrt());
+  pParser->DefineFun(new FunExpm1());
   pParser->DefineFun(new FunAbs());
+  pParser->DefineFun(new FunStep());
+  pParser->DefineFun(new FunFloor());
+  pParser->DefineFun(new FunCeil());
+
+  pParser->DefineFun(new FunHypot());
 
   // Operator callbacks
   pParser->DefineInfixOprt(new OprtSign());



More information about the CIG-COMMITS mailing list