* Oops, forgot to cvs add the two new files in last commit.
[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-2003 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 "operators.h"
32 #include "utils.h"
33
34 namespace GiNaC {
35
36 //////////
37 // Riemann's Zeta-function
38 //////////
39
40 static ex zeta1_evalf(const ex & x)
41 {
42         if (is_exactly_a<numeric>(x)) {
43                 try {
44                         return zeta(ex_to<numeric>(x));
45                 } catch (const dunno &e) { }
46         }
47         
48         return zeta(x).hold();
49 }
50
51 static ex zeta1_eval(const ex & x)
52 {
53         if (x.info(info_flags::numeric)) {
54                 const numeric &y = ex_to<numeric>(x);
55                 // trap integer arguments:
56                 if (y.is_integer()) {
57                         if (y.is_zero())
58                                 return _ex_1_2;
59                         if (y.is_equal(_num1))
60                                 throw(std::domain_error("zeta(1): infinity"));
61                         if (y.info(info_flags::posint)) {
62                                 if (y.info(info_flags::odd))
63                                         return zeta(x).hold();
64                                 else
65                                         return abs(bernoulli(y))*pow(Pi,y)*pow(_num2,y-_num1)/factorial(y);
66                         } else {
67                                 if (y.info(info_flags::odd))
68                                         return -bernoulli(_num1-y)/(_num1-y);
69                                 else
70                                         return _ex0;
71                         }
72                 }
73                 // zeta(float)
74                 if (y.info(info_flags::numeric) && !y.info(info_flags::crational))
75                         return zeta1_evalf(x);
76         }
77         return zeta(x).hold();
78 }
79
80 static ex zeta1_deriv(const ex & x, unsigned deriv_param)
81 {
82         GINAC_ASSERT(deriv_param==0);
83         
84         return zeta(_ex1, x);
85 }
86
87 unsigned zeta1_SERIAL::serial =
88         function::register_new(function_options("zeta").
89                                eval_func(zeta1_eval).
90                                evalf_func(zeta1_evalf).
91                                derivative_func(zeta1_deriv).
92                                latex_name("\\zeta").
93                                overloaded(2));
94
95 //////////
96 // Derivatives of Riemann's Zeta-function  zeta(0,x)==zeta(x)
97 //////////
98
99 static ex zeta2_eval(const ex & n, const ex & x)
100 {
101         if (n.info(info_flags::numeric)) {
102                 // zeta(0,x) -> zeta(x)
103                 if (n.is_zero())
104                         return zeta(x);
105         }
106         
107         return zeta(n, x).hold();
108 }
109
110 static ex zeta2_deriv(const ex & n, const ex & x, unsigned deriv_param)
111 {
112         GINAC_ASSERT(deriv_param<2);
113         
114         if (deriv_param==0) {
115                 // d/dn zeta(n,x)
116                 throw(std::logic_error("cannot diff zeta(n,x) with respect to n"));
117         }
118         // d/dx psi(n,x)
119         return zeta(n+1,x);
120 }
121
122 unsigned zeta2_SERIAL::serial =
123         function::register_new(function_options("zeta").
124                                eval_func(zeta2_eval).
125                                derivative_func(zeta2_deriv).
126                                latex_name("\\zeta").
127                                overloaded(2));
128
129 } // namespace GiNaC