- moved is_of_type and friend macros into utils.h so they can be phased
[ginac.git] / ginac / inifcns_zeta.cpp
1 /** @file inifcns_zeta.cpp
2  *
3  *  Implementation of the Zeta-function and some related stuff. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <vector>
24 #include <stdexcept>
25
26 #include "inifcns.h"
27 #include "constant.h"
28 #include "numeric.h"
29 #include "power.h"
30 #include "symbol.h"
31 #include "utils.h"
32
33 namespace GiNaC {
34
35 //////////
36 // Riemann's Zeta-function
37 //////////
38
39 static ex zeta1_evalf(const ex & x)
40 {
41         if (is_exactly_a<numeric>(x)) {
42                 try {
43                         return zeta(ex_to<numeric>(x));
44                 } catch (const dunno &e) { }
45         }
46         
47         return zeta(x).hold();
48 }
49
50 static ex zeta1_eval(const ex & x)
51 {
52         if (x.info(info_flags::numeric)) {
53                 numeric y = ex_to<numeric>(x);
54                 // trap integer arguments:
55                 if (y.is_integer()) {
56                         if (y.is_zero())
57                                 return _ex_1_2();
58                         if (x.is_equal(_ex1()))
59                                 throw(std::domain_error("zeta(1): infinity"));
60                         if (x.info(info_flags::posint)) {
61                                 if (x.info(info_flags::odd))
62                                         return zeta(x).hold();
63                                 else
64                                         return abs(bernoulli(y))*pow(Pi,x)*pow(_num2(),y-_num1())/factorial(y);
65                         } else {
66                                 if (x.info(info_flags::odd))
67                                         return -bernoulli(_num1()-y)/(_num1()-y);
68                                 else
69                                         return _num0();
70                         }
71                 }
72                 // zeta(float)
73                 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
74                         return zeta1_evalf(x);
75         }
76         return zeta(x).hold();
77 }
78
79 static ex zeta1_deriv(const ex & x, unsigned deriv_param)
80 {
81         GINAC_ASSERT(deriv_param==0);
82         
83         return zeta(_ex1(), x);
84 }
85
86 const unsigned function_index_zeta1 =
87         function::register_new(function_options("zeta").
88                                eval_func(zeta1_eval).
89                                evalf_func(zeta1_evalf).
90                                derivative_func(zeta1_deriv).
91                                latex_name("\\zeta").
92                                overloaded(2));
93
94 //////////
95 // Derivatives of Riemann's Zeta-function  zeta(0,x)==zeta(x)
96 //////////
97
98 static ex zeta2_eval(const ex & n, const ex & x)
99 {
100         if (n.info(info_flags::numeric)) {
101                 // zeta(0,x) -> zeta(x)
102                 if (n.is_zero())
103                         return zeta(x);
104         }
105         
106         return zeta(n, x).hold();
107 }
108
109 static ex zeta2_deriv(const ex & n, const ex & x, unsigned deriv_param)
110 {
111         GINAC_ASSERT(deriv_param<2);
112         
113         if (deriv_param==0) {
114                 // d/dn zeta(n,x)
115                 throw(std::logic_error("cannot diff zeta(n,x) with respect to n"));
116         }
117         // d/dx psi(n,x)
118         return zeta(n+1,x);
119 }
120
121 const unsigned function_index_zeta2 =
122         function::register_new(function_options("zeta").
123                                eval_func(zeta2_eval).
124                                derivative_func(zeta2_deriv).
125                                latex_name("\\zeta").
126                                overloaded(2));
127
128 } // namespace GiNaC