]> www.ginac.de Git - ginac.git/blobdiff - ginac/inifcns_zeta.cpp
* Make zeta1_eval() more lightweight by copying less stuff.
[ginac.git] / ginac / inifcns_zeta.cpp
index 6829d5654910c8a577c9be74ee07e23c082e3ad5..a7b956ae71764ee870844b23a24e66e8cfe9df9e 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of the Zeta-function and some related stuff. */
 
 /*
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include <stdexcept>
 
 #include "inifcns.h"
-#include "ex.h"
 #include "constant.h"
 #include "numeric.h"
 #include "power.h"
 #include "symbol.h"
+#include "utils.h"
 
-#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
 
 //////////
 // Riemann's Zeta-function
 //////////
 
-static ex zeta1_evalf(ex const & x)
+static ex zeta1_evalf(const ex & x)
 {
-    BEGIN_TYPECHECK
-        TYPECHECK(x,numeric)
-    END_TYPECHECK(zeta(x))
-        
-    return zeta(ex_to_numeric(x));
+       if (is_exactly_a<numeric>(x)) {
+               try {
+                       return zeta(ex_to<numeric>(x));
+               } catch (const dunno &e) { }
+       }
+       
+       return zeta(x).hold();
 }
 
-static ex zeta1_eval(ex const & x)
+static ex zeta1_eval(const ex & x)
 {
-    if (x.info(info_flags::numeric)) {
-        numeric y = ex_to_numeric(x);
-        // trap integer arguments:
-        if (y.is_integer()) {
-            if (y.is_zero())
-                return -exHALF();
-            if (x.is_equal(exONE()))
-                throw(std::domain_error("zeta(1): infinity"));
-            if (x.info(info_flags::posint)) {
-                if (x.info(info_flags::odd))
-                    return zeta(x).hold();
-                else
-                    return abs(bernoulli(y))*pow(Pi,x)*numTWO().power(y-numONE())/factorial(y);
-            } else {
-                if (x.info(info_flags::odd))
-                    return -bernoulli(numONE()-y)/(numONE()-y);
-                else
-                    return numZERO();
-            }
-        }
-    }
-    return zeta(x).hold();
+       if (x.info(info_flags::numeric)) {
+               const numeric &y = ex_to<numeric>(x);
+               // trap integer arguments:
+               if (y.is_integer()) {
+                       if (y.is_zero())
+                               return _ex_1_2;
+                       if (y.is_equal(_num1))
+                               throw(std::domain_error("zeta(1): infinity"));
+                       if (y.info(info_flags::posint)) {
+                               if (y.info(info_flags::odd))
+                                       return zeta(x).hold();
+                               else
+                                       return abs(bernoulli(y))*pow(Pi,y)*pow(_num2,y-_num1)/factorial(y);
+                       } else {
+                               if (y.info(info_flags::odd))
+                                       return -bernoulli(_num1-y)/(_num1-y);
+                               else
+                                       return _ex0;
+                       }
+               }
+               // zeta(float)
+               if (y.info(info_flags::numeric) && !y.info(info_flags::crational))
+                       return zeta1_evalf(x);
+       }
+       return zeta(x).hold();
 }
 
-static ex zeta1_diff(ex const & x, unsigned diff_param)
+static ex zeta1_deriv(const ex & x, unsigned deriv_param)
 {
-    GINAC_ASSERT(diff_param==0);
-    
-    return zeta(exONE(), x);
+       GINAC_ASSERT(deriv_param==0);
+       
+       return zeta(_ex1, x);
 }
 
-const unsigned function_index_zeta1 = function::register_new("zeta", zeta1_eval, zeta1_evalf, zeta1_diff, NULL);
+const unsigned function_index_zeta1 =
+       function::register_new(function_options("zeta").
+                              eval_func(zeta1_eval).
+                              evalf_func(zeta1_evalf).
+                              derivative_func(zeta1_deriv).
+                              latex_name("\\zeta").
+                              overloaded(2));
 
 //////////
 // Derivatives of Riemann's Zeta-function  zeta(0,x)==zeta(x)
 //////////
 
-static ex zeta2_eval(ex const & n, ex const & x)
+static ex zeta2_eval(const ex & n, const ex & x)
 {
-    if (n.info(info_flags::numeric)) {
-        // zeta(0,x) -> zeta(x)
-        if (n.is_zero())
-            return zeta(x);
-    }
-    
-    return zeta(n, x).hold();
+       if (n.info(info_flags::numeric)) {
+               // zeta(0,x) -> zeta(x)
+               if (n.is_zero())
+                       return zeta(x);
+       }
+       
+       return zeta(n, x).hold();
 }
 
-static ex zeta2_diff(ex const & n, ex const & x, unsigned diff_param)
+static ex zeta2_deriv(const ex & n, const ex & x, unsigned deriv_param)
 {
-    GINAC_ASSERT(diff_param<2);
-    
-    if (diff_param==0) {
-        // d/dn zeta(n,x)
-        throw(std::logic_error("cannot diff zeta(n,x) with respect to n"));
-    }
-    // d/dx psi(n,x)
-    return zeta(n+1,x);
+       GINAC_ASSERT(deriv_param<2);
+       
+       if (deriv_param==0) {
+               // d/dn zeta(n,x)
+               throw(std::logic_error("cannot diff zeta(n,x) with respect to n"));
+       }
+       // d/dx psi(n,x)
+       return zeta(n+1,x);
 }
 
-const unsigned function_index_zeta2 = function::register_new("zeta", zeta2_eval, NULL, zeta2_diff, NULL);
+const unsigned function_index_zeta2 =
+       function::register_new(function_options("zeta").
+                              eval_func(zeta2_eval).
+                              derivative_func(zeta2_deriv).
+                              latex_name("\\zeta").
+                              overloaded(2));
 
-#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE