]> www.ginac.de Git - ginac.git/commitdiff
- added the beta function to GiNaC
authorRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Thu, 2 Dec 1999 22:28:00 +0000 (22:28 +0000)
committerRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Thu, 2 Dec 1999 22:28:00 +0000 (22:28 +0000)
- threw out ginsh's beta function
- added a check for first polygamma function

check/inifcns_consist.cpp
ginac/inifcns.h
ginac/inifcns_gamma.cpp
ginsh/ginsh.1
ginsh/ginsh_parser.yy

index 024dad79917382849d191f334fca34c00b9f0ebb..25e3419732bc00124f32db6667443d7448616faf 100644 (file)
@@ -169,8 +169,8 @@ static unsigned inifcns_consist_trans(void)
     return result;
 }
 
     return result;
 }
 
-/* Simple tests on the Gamma combinatorial function.  We stuff in arguments
- * where the result exists in closed form and check if it's ok. */
+/* Simple tests on the Gamma function.  We stuff in arguments where the results
+ * exists in closed form and check if it's ok. */
 static unsigned inifcns_consist_gamma(void)
 {
     unsigned result = 0;
 static unsigned inifcns_consist_gamma(void)
 {
     unsigned result = 0;
@@ -214,6 +214,27 @@ static unsigned inifcns_consist_gamma(void)
     return result;
 }
 
     return result;
 }
 
+/* Simple tests on the Psi-function (aka polygamma-function).  We stuff in
+   arguments where the result exists in closed form and check if it's ok. */
+static unsigned inifcns_consist_psi(void)
+{
+    unsigned result = 0;
+    symbol x;
+    ex e;
+    
+    // We check psi(1) and psi(1/2) implicitly by calculating the curious
+    // little identity gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) == 2*log(2).
+    e += (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1));
+    e -= (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1,2));
+    if (e!=2*log(2)) {
+        clog << "gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) erroneously returned "
+             << e << " instead of 2*log(2)" << endl;
+        ++result;
+    }
+    
+    return result;
+}
+
 /* Simple tests on the Riemann Zeta function.  We stuff in arguments where the
  * result exists in closed form and check if it's ok.  Of course, this checks
  * the Bernoulli numbers as a side effect. */
 /* Simple tests on the Riemann Zeta function.  We stuff in arguments where the
  * result exists in closed form and check if it's ok.  Of course, this checks
  * the Bernoulli numbers as a side effect. */
@@ -253,6 +274,7 @@ unsigned inifcns_consist(void)
     result += inifcns_consist_cos();
     result += inifcns_consist_trans();
     result += inifcns_consist_gamma();
     result += inifcns_consist_cos();
     result += inifcns_consist_trans();
     result += inifcns_consist_gamma();
+    result += inifcns_consist_psi();
     result += inifcns_consist_zeta();
 
     if ( !result ) {
     result += inifcns_consist_zeta();
 
     if ( !result ) {
index 84e0dbcbaa1d126111466e49a313fc6e433a120b..a5a527000cdbebbf4b69673ef3682ffd0b3373ba 100644 (file)
@@ -88,7 +88,11 @@ DECLARE_FUNCTION_1P(zeta)
 /** Gamma-function. */
 DECLARE_FUNCTION_1P(gamma)
 
 /** Gamma-function. */
 DECLARE_FUNCTION_1P(gamma)
 
+/** Beta-function. */
+DECLARE_FUNCTION_2P(beta)
+
 /** Psi-function (aka polygamma-function). */
 /** Psi-function (aka polygamma-function). */
+// overloading @ work: we cannot use the macros
 extern const unsigned function_index_psi1;
 inline function psi(ex const & p1) {
     return function(function_index_psi1, p1);
 extern const unsigned function_index_psi1;
 inline function psi(ex const & p1) {
     return function(function_index_psi1, p1);
index 2fd58cce517fe04d66c8bf158aa95878de6635ea..0d59eb306b67dd2612d55f3d648296d6f4f11700 100644 (file)
@@ -1,7 +1,7 @@
 /** @file inifcns_gamma.cpp
  *
 /** @file inifcns_gamma.cpp
  *
- *  Implementation of Gamma-function, Polygamma-functions, and some related
- *  stuff. */
+ *  Implementation of Gamma-function, Beta-function, Polygamma-functions, and
+ *  some related stuff. */
 
 /*
  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
 
 /*
  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
@@ -53,7 +53,7 @@ static ex gamma_eval(ex const & x)
             if (x.info(info_flags::posint)) {
                 return factorial(ex_to_numeric(x).sub(numONE()));
             } else {
             if (x.info(info_flags::posint)) {
                 return factorial(ex_to_numeric(x).sub(numONE()));
             } else {
-                return numZERO();  // Infinity. Throw? What?
+                throw (std::domain_error("gamma_eval(): simple pole"));
             }
         }
         // trap half integer arguments:
             }
         }
         // trap half integer arguments:
@@ -106,6 +106,77 @@ static ex gamma_series(ex const & x, symbol const & s, ex const & point, int ord
 
 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
 
 
 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
 
+//////////
+// Beta-function
+//////////
+
+static ex beta_eval(ex const & x, ex const & y)
+{
+    if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
+        numeric nx(ex_to_numeric(x));
+        numeric ny(ex_to_numeric(y));
+        // treat all problematic x and y that may not be passed into gamma,
+        // because they would throw there although beta(x,y) is well-defined:
+        if (nx.is_real() && nx.is_integer() &&
+            ny.is_real() && ny.is_integer()) {
+            if (nx.is_negative()) {
+                if (nx<=-ny)
+                    return numMINUSONE().power(ny)*beta(1-x-y, y);
+                else
+                    throw (std::domain_error("beta_eval(): simple pole"));
+            }
+            if (ny.is_negative()) {
+                if (ny<=-nx)
+                    return numMINUSONE().power(nx)*beta(1-y-x, x);
+                else
+                    throw (std::domain_error("beta_eval(): simple pole"));
+            }
+            return gamma(x)*gamma(y)/gamma(x+y);
+        }
+        // no problem in numerator, but denominator has pole:
+        if ((nx+ny).is_real() &&
+            (nx+ny).is_integer() &&
+            !(nx+ny).is_positive())
+            return exZERO();
+        return gamma(x)*gamma(y)/gamma(x+y);
+    }
+    return beta(x,y).hold();
+}
+
+static ex beta_evalf(ex const & x, ex const & y)
+{
+    BEGIN_TYPECHECK
+        TYPECHECK(x,numeric)
+        TYPECHECK(y,numeric)
+    END_TYPECHECK(beta(x,y))
+    
+    return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
+        / gamma(ex_to_numeric(x+y));
+}
+
+static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
+{
+    GINAC_ASSERT(diff_param<2);
+    ex retval;
+    
+    if (diff_param==0)  // d/dx beta(x,y)
+        retval = (psi(x)-psi(x+y))*beta(x,y);
+    if (diff_param==1)  // d/dy beta(x,y)
+        retval = (psi(y)-psi(x+y))*beta(x,y);
+    return retval;
+}
+
+static ex beta_series(ex const & x, ex const & y, symbol const & s, ex const & point, int order)
+{
+       if (x.is_equal(s) && point.is_zero()) {
+               ex e = 1 / s - EulerGamma + s * (pow(Pi, 2) / 12 + pow(EulerGamma, 2) / 2) + Order(pow(s, 2));
+               return e.series(s, point, order);
+       } else
+               throw(std::logic_error("don't know the series expansion of this particular beta function"));
+}
+
+REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
+
 //////////
 // Psi-function (aka polygamma-function)
 //////////
 //////////
 // Psi-function (aka polygamma-function)
 //////////
index 32b0cec6456afe0645e12e93394417bdda5312d4..cf3b945c84ed158d03f4438b2924b7d24aa9faa6 100644 (file)
@@ -216,9 +216,6 @@ detail here. Please refer to the GiNaC documentation.
 .PP
 .RS
 \" GINSH_FCN_HELP_START
 .PP
 .RS
 \" GINSH_FCN_HELP_START
-.BI beta( expression ", " expression )
-\- beta function
-.br
 .BI charpoly( matrix ", " symbol )
 \- characteristic polynomial of a matrix
 .br
 .BI charpoly( matrix ", " symbol )
 \- characteristic polynomial of a matrix
 .br
index c49e7c0dd630190bb941642f5b38490a348758f2..2e02b6cc1c7fdb171ecac700c4ad31eb1ca1a732 100644 (file)
@@ -242,7 +242,6 @@ static void push(const ex &e)
  *  Built-in functions
  */
 
  *  Built-in functions
  */
 
-static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
 static ex f_denom(const exprseq &e) {return e[0].denom();}
 static ex f_eval1(const exprseq &e) {return e[0].eval();}
 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
 static ex f_denom(const exprseq &e) {return e[0].denom();}
 static ex f_eval1(const exprseq &e) {return e[0].eval();}
 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
@@ -483,7 +482,6 @@ struct fcn_init {
 };
 
 static const fcn_init builtin_fcns[] = {
 };
 
 static const fcn_init builtin_fcns[] = {
-       {"beta", fcn_desc(f_beta, 2)},
        {"charpoly", fcn_desc(f_charpoly, 2)},
        {"coeff", fcn_desc(f_coeff, 3)},
        {"collect", fcn_desc(f_collect, 2)},
        {"charpoly", fcn_desc(f_charpoly, 2)},
        {"coeff", fcn_desc(f_coeff, 3)},
        {"collect", fcn_desc(f_collect, 2)},
@@ -759,8 +757,10 @@ int main(int argc, char **argv)
        insert_fcn_help("atan", "inverse tangent function");
        insert_fcn_help("atan2", "inverse tangent function with two arguments");
        insert_fcn_help("atanh", "inverse hyperbolic tangent function");
        insert_fcn_help("atan", "inverse tangent function");
        insert_fcn_help("atan2", "inverse tangent function with two arguments");
        insert_fcn_help("atanh", "inverse hyperbolic tangent function");
+       insert_fcn_help("beta", "beta function");
        insert_fcn_help("cos", "cosine function");
        insert_fcn_help("cosh", "hyperbolic cosine function");
        insert_fcn_help("cos", "cosine function");
        insert_fcn_help("cosh", "hyperbolic cosine function");
+       insert_fcn_help("psi", "polygamma function");
        insert_fcn_help("sin", "sine function");
        insert_fcn_help("sinh", "hyperbolic sine function");
        insert_fcn_help("tan", "tangent function");
        insert_fcn_help("sin", "sine function");
        insert_fcn_help("sinh", "hyperbolic sine function");
        insert_fcn_help("tan", "tangent function");