]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- Clifford traces of many gammas are a lot faster now (especially with gamma5)
[ginac.git] / ginac / inifcns.cpp
1 /** @file inifcns.cpp
2  *
3  *  Implementation of GiNaC's initially known functions. */
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 "ex.h"
28 #include "constant.h"
29 #include "lst.h"
30 #include "matrix.h"
31 #include "mul.h"
32 #include "ncmul.h"
33 #include "numeric.h"
34 #include "power.h"
35 #include "relational.h"
36 #include "pseries.h"
37 #include "symbol.h"
38 #include "utils.h"
39
40 namespace GiNaC {
41
42 //////////
43 // absolute value
44 //////////
45
46 static ex abs_evalf(const ex & arg)
47 {
48         BEGIN_TYPECHECK
49                 TYPECHECK(arg,numeric)
50         END_TYPECHECK(abs(arg))
51         
52         return abs(ex_to_numeric(arg));
53 }
54
55 static ex abs_eval(const ex & arg)
56 {
57         if (is_ex_exactly_of_type(arg, numeric))
58                 return abs(ex_to_numeric(arg));
59         else
60                 return abs(arg).hold();
61 }
62
63 REGISTER_FUNCTION(abs, eval_func(abs_eval).
64                        evalf_func(abs_evalf));
65
66
67 //////////
68 // Complex sign
69 //////////
70
71 static ex csgn_evalf(const ex & arg)
72 {
73         BEGIN_TYPECHECK
74                 TYPECHECK(arg,numeric)
75         END_TYPECHECK(csgn(arg))
76         
77         return csgn(ex_to_numeric(arg));
78 }
79
80 static ex csgn_eval(const ex & arg)
81 {
82         if (is_ex_exactly_of_type(arg, numeric))
83                 return csgn(ex_to_numeric(arg));
84         
85         else if (is_ex_of_type(arg, mul) &&
86                  is_ex_of_type(arg.op(arg.nops()-1),numeric)) {
87                 numeric oc = ex_to_numeric(arg.op(arg.nops()-1));
88                 if (oc.is_real()) {
89                         if (oc > 0)
90                                 // csgn(42*x) -> csgn(x)
91                                 return csgn(arg/oc).hold();
92                         else
93                                 // csgn(-42*x) -> -csgn(x)
94                                 return -csgn(arg/oc).hold();
95                 }
96                 if (oc.real().is_zero()) {
97                         if (oc.imag() > 0)
98                                 // csgn(42*I*x) -> csgn(I*x)
99                                 return csgn(I*arg/oc).hold();
100                         else
101                                 // csgn(-42*I*x) -> -csgn(I*x)
102                                 return -csgn(I*arg/oc).hold();
103                 }
104         }
105         
106         return csgn(arg).hold();
107 }
108
109 static ex csgn_series(const ex & arg,
110                       const relational & rel,
111                       int order,
112                       unsigned options)
113 {
114         const ex arg_pt = arg.subs(rel);
115         if (arg_pt.info(info_flags::numeric)
116             && ex_to_numeric(arg_pt).real().is_zero()
117             && !(options & series_options::suppress_branchcut))
118                 throw (std::domain_error("csgn_series(): on imaginary axis"));
119         
120         epvector seq;
121         seq.push_back(expair(csgn(arg_pt), _ex0()));
122         return pseries(rel,seq);
123 }
124
125 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
126                         evalf_func(csgn_evalf).
127                         series_func(csgn_series));
128
129
130 //////////
131 // Eta function: log(x*y) == log(x) + log(y) + eta(x,y).
132 //////////
133
134 static ex eta_evalf(const ex & x, const ex & y)
135 {
136         BEGIN_TYPECHECK
137                 TYPECHECK(x,numeric)
138                 TYPECHECK(y,numeric)
139         END_TYPECHECK(eta(x,y))
140                 
141         numeric xim = imag(ex_to_numeric(x));
142         numeric yim = imag(ex_to_numeric(y));
143         numeric xyim = imag(ex_to_numeric(x*y));
144         return evalf(I/4*Pi)*((csgn(-xim)+1)*(csgn(-yim)+1)*(csgn(xyim)+1)-(csgn(xim)+1)*(csgn(yim)+1)*(csgn(-xyim)+1));
145 }
146
147 static ex eta_eval(const ex & x, const ex & y)
148 {
149         if (is_ex_exactly_of_type(x, numeric) &&
150                 is_ex_exactly_of_type(y, numeric)) {
151                 // don't call eta_evalf here because it would call Pi.evalf()!
152                 numeric xim = imag(ex_to_numeric(x));
153                 numeric yim = imag(ex_to_numeric(y));
154                 numeric xyim = imag(ex_to_numeric(x*y));
155                 return (I/4)*Pi*((csgn(-xim)+1)*(csgn(-yim)+1)*(csgn(xyim)+1)-(csgn(xim)+1)*(csgn(yim)+1)*(csgn(-xyim)+1));
156         }
157         
158         return eta(x,y).hold();
159 }
160
161 static ex eta_series(const ex & arg1,
162                      const ex & arg2,
163                      const relational & rel,
164                      int order,
165                      unsigned options)
166 {
167         const ex arg1_pt = arg1.subs(rel);
168         const ex arg2_pt = arg2.subs(rel);
169         if (ex_to_numeric(arg1_pt).imag().is_zero() ||
170                 ex_to_numeric(arg2_pt).imag().is_zero() ||
171                 ex_to_numeric(arg1_pt*arg2_pt).imag().is_zero()) {
172                 throw (std::domain_error("eta_series(): on discontinuity"));
173         }
174         epvector seq;
175         seq.push_back(expair(eta(arg1_pt,arg2_pt), _ex0()));
176         return pseries(rel,seq);
177 }
178
179 REGISTER_FUNCTION(eta, eval_func(eta_eval).
180                        evalf_func(eta_evalf).
181                        series_func(eta_series).
182                        latex_name("\\eta"));
183
184
185 //////////
186 // dilogarithm
187 //////////
188
189 static ex Li2_evalf(const ex & x)
190 {
191         BEGIN_TYPECHECK
192                 TYPECHECK(x,numeric)
193         END_TYPECHECK(Li2(x))
194         
195         return Li2(ex_to_numeric(x));  // -> numeric Li2(numeric)
196 }
197
198 static ex Li2_eval(const ex & x)
199 {
200         if (x.info(info_flags::numeric)) {
201                 // Li2(0) -> 0
202                 if (x.is_zero())
203                         return _ex0();
204                 // Li2(1) -> Pi^2/6
205                 if (x.is_equal(_ex1()))
206                         return power(Pi,_ex2())/_ex6();
207                 // Li2(1/2) -> Pi^2/12 - log(2)^2/2
208                 if (x.is_equal(_ex1_2()))
209                         return power(Pi,_ex2())/_ex12() + power(log(_ex2()),_ex2())*_ex_1_2();
210                 // Li2(-1) -> -Pi^2/12
211                 if (x.is_equal(_ex_1()))
212                         return -power(Pi,_ex2())/_ex12();
213                 // Li2(I) -> -Pi^2/48+Catalan*I
214                 if (x.is_equal(I))
215                         return power(Pi,_ex2())/_ex_48() + Catalan*I;
216                 // Li2(-I) -> -Pi^2/48-Catalan*I
217                 if (x.is_equal(-I))
218                         return power(Pi,_ex2())/_ex_48() - Catalan*I;
219                 // Li2(float)
220                 if (!x.info(info_flags::crational))
221                         return Li2_evalf(x);
222         }
223         
224         return Li2(x).hold();
225 }
226
227 static ex Li2_deriv(const ex & x, unsigned deriv_param)
228 {
229         GINAC_ASSERT(deriv_param==0);
230         
231         // d/dx Li2(x) -> -log(1-x)/x
232         return -log(1-x)/x;
233 }
234
235 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
236 {
237         const ex x_pt = x.subs(rel);
238         if (x_pt.info(info_flags::numeric)) {
239                 // First special case: x==0 (derivatives have poles)
240                 if (x_pt.is_zero()) {
241                         // method:
242                         // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot 
243                         // simply substitute x==0.  The limit, however, exists: it is 1.
244                         // We also know all higher derivatives' limits:
245                         // (d/dx)^n Li2(x) == n!/n^2.
246                         // So the primitive series expansion is
247                         // Li2(x==0) == x + x^2/4 + x^3/9 + ...
248                         // and so on.
249                         // We first construct such a primitive series expansion manually in
250                         // a dummy symbol s and then insert the argument's series expansion
251                         // for s.  Reexpanding the resulting series returns the desired
252                         // result.
253                         const symbol s;
254                         ex ser;
255                         // manually construct the primitive expansion
256                         for (int i=1; i<order; ++i)
257                                 ser += pow(s,i) / pow(numeric(i), _num2());
258                         // substitute the argument's series expansion
259                         ser = ser.subs(s==x.series(rel, order));
260                         // maybe that was terminating, so add a proper order term
261                         epvector nseq;
262                         nseq.push_back(expair(Order(_ex1()), order));
263                         ser += pseries(rel, nseq);
264                         // reexpanding it will collapse the series again
265                         return ser.series(rel, order);
266                         // NB: Of course, this still does not allow us to compute anything
267                         // like sin(Li2(x)).series(x==0,2), since then this code here is
268                         // not reached and the derivative of sin(Li2(x)) doesn't allow the
269                         // substitution x==0.  Probably limits *are* needed for the general
270                         // cases.  In case L'Hospital's rule is implemented for limits and
271                         // basic::series() takes care of this, this whole block is probably
272                         // obsolete!
273                 }
274                 // second special case: x==1 (branch point)
275                 if (x_pt == _ex1()) {
276                         // method:
277                         // construct series manually in a dummy symbol s
278                         const symbol s;
279                         ex ser = zeta(2);
280                         // manually construct the primitive expansion
281                         for (int i=1; i<order; ++i)
282                                 ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
283                         // substitute the argument's series expansion
284                         ser = ser.subs(s==x.series(rel, order));
285                         // maybe that was terminating, so add a proper order term
286                         epvector nseq;
287                         nseq.push_back(expair(Order(_ex1()), order));
288                         ser += pseries(rel, nseq);
289                         // reexpanding it will collapse the series again
290                         return ser.series(rel, order);
291                 }
292                 // third special case: x real, >=1 (branch cut)
293                 if (!(options & series_options::suppress_branchcut) &&
294                         ex_to_numeric(x_pt).is_real() && ex_to_numeric(x_pt)>1) {
295                         // method:
296                         // This is the branch cut: assemble the primitive series manually
297                         // and then add the corresponding complex step function.
298                         const symbol *s = static_cast<symbol *>(rel.lhs().bp);
299                         const ex point = rel.rhs();
300                         const symbol foo;
301                         epvector seq;
302                         // zeroth order term:
303                         seq.push_back(expair(Li2(x_pt), _ex0()));
304                         // compute the intermediate terms:
305                         ex replarg = series(Li2(x), *s==foo, order);
306                         for (unsigned i=1; i<replarg.nops()-1; ++i)
307                                 seq.push_back(expair((replarg.op(i)/power(*s-foo,i)).series(foo==point,1,options).op(0).subs(foo==*s),i));
308                         // append an order term:
309                         seq.push_back(expair(Order(_ex1()), replarg.nops()-1));
310                         return pseries(rel, seq);
311                 }
312         }
313         // all other cases should be safe, by now:
314         throw do_taylor();  // caught by function::series()
315 }
316
317 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
318                        evalf_func(Li2_evalf).
319                        derivative_func(Li2_deriv).
320                        series_func(Li2_series).
321                        latex_name("\\mbox{Li}_2"));
322
323 //////////
324 // trilogarithm
325 //////////
326
327 static ex Li3_eval(const ex & x)
328 {
329         if (x.is_zero())
330                 return x;
331         return Li3(x).hold();
332 }
333
334 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
335                        latex_name("\\mbox{Li}_3"));
336
337 //////////
338 // factorial
339 //////////
340
341 static ex factorial_evalf(const ex & x)
342 {
343         return factorial(x).hold();
344 }
345
346 static ex factorial_eval(const ex & x)
347 {
348         if (is_ex_exactly_of_type(x, numeric))
349                 return factorial(ex_to_numeric(x));
350         else
351                 return factorial(x).hold();
352 }
353
354 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
355                              evalf_func(factorial_evalf));
356
357 //////////
358 // binomial
359 //////////
360
361 static ex binomial_evalf(const ex & x, const ex & y)
362 {
363         return binomial(x, y).hold();
364 }
365
366 static ex binomial_eval(const ex & x, const ex &y)
367 {
368         if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
369                 return binomial(ex_to_numeric(x), ex_to_numeric(y));
370         else
371                 return binomial(x, y).hold();
372 }
373
374 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
375                             evalf_func(binomial_evalf));
376
377 //////////
378 // Order term function (for truncated power series)
379 //////////
380
381 static ex Order_eval(const ex & x)
382 {
383         if (is_ex_exactly_of_type(x, numeric)) {
384                 // O(c) -> O(1) or 0
385                 if (!x.is_zero())
386                         return Order(_ex1()).hold();
387                 else
388                         return _ex0();
389         } else if (is_ex_exactly_of_type(x, mul)) {
390                 mul *m = static_cast<mul *>(x.bp);
391                 // O(c*expr) -> O(expr)
392                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric))
393                         return Order(x / m->op(m->nops() - 1)).hold();
394         }
395         return Order(x).hold();
396 }
397
398 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
399 {
400         // Just wrap the function into a pseries object
401         epvector new_seq;
402         GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
403         const symbol *s = static_cast<symbol *>(r.lhs().bp);
404         new_seq.push_back(expair(Order(_ex1()), numeric(std::min(x.ldegree(*s), order))));
405         return pseries(r, new_seq);
406 }
407
408 // Differentiation is handled in function::derivative because of its special requirements
409
410 REGISTER_FUNCTION(Order, eval_func(Order_eval).
411                          series_func(Order_series).
412                          latex_name("\\mathcal{O}"));
413
414 //////////
415 // Inert partial differentiation operator
416 //////////
417
418 static ex Derivative_eval(const ex & f, const ex & l)
419 {
420         if (!is_ex_exactly_of_type(f, function)) {
421                 throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
422         }
423         if (!is_ex_exactly_of_type(l, lst)) {
424                 throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
425         }
426         return Derivative(f, l).hold();
427 }
428
429 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval));
430
431 //////////
432 // Solve linear system
433 //////////
434
435 ex lsolve(const ex &eqns, const ex &symbols)
436 {
437         // solve a system of linear equations
438         if (eqns.info(info_flags::relation_equal)) {
439                 if (!symbols.info(info_flags::symbol))
440                         throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
441                 ex sol=lsolve(lst(eqns),lst(symbols));
442                 
443                 GINAC_ASSERT(sol.nops()==1);
444                 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
445                 
446                 return sol.op(0).op(1); // return rhs of first solution
447         }
448         
449         // syntax checks
450         if (!eqns.info(info_flags::list)) {
451                 throw(std::invalid_argument("lsolve(): 1st argument must be a list"));
452         }
453         for (unsigned i=0; i<eqns.nops(); i++) {
454                 if (!eqns.op(i).info(info_flags::relation_equal)) {
455                         throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
456                 }
457         }
458         if (!symbols.info(info_flags::list)) {
459                 throw(std::invalid_argument("lsolve(): 2nd argument must be a list"));
460         }
461         for (unsigned i=0; i<symbols.nops(); i++) {
462                 if (!symbols.op(i).info(info_flags::symbol)) {
463                         throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
464                 }
465         }
466         
467         // build matrix from equation system
468         matrix sys(eqns.nops(),symbols.nops());
469         matrix rhs(eqns.nops(),1);
470         matrix vars(symbols.nops(),1);
471         
472         for (unsigned r=0; r<eqns.nops(); r++) {
473                 ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
474                 ex linpart = eq;
475                 for (unsigned c=0; c<symbols.nops(); c++) {
476                         ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
477                         linpart -= co*symbols.op(c);
478                         sys.set(r,c,co);
479                 }
480                 linpart = linpart.expand();
481                 rhs.set(r,0,-linpart);
482         }
483         
484         // test if system is linear and fill vars matrix
485         for (unsigned i=0; i<symbols.nops(); i++) {
486                 vars.set(i,0,symbols.op(i));
487                 if (sys.has(symbols.op(i)))
488                         throw(std::logic_error("lsolve: system is not linear"));
489                 if (rhs.has(symbols.op(i)))
490                         throw(std::logic_error("lsolve: system is not linear"));
491         }
492         
493         matrix solution;
494         try {
495                 solution = sys.solve(vars,rhs);
496         } catch (const std::runtime_error & e) {
497                 // Probably singular matrix or otherwise overdetermined system:
498                 // It is consistent to return an empty list
499                 return lst();
500         }    
501         GINAC_ASSERT(solution.cols()==1);
502         GINAC_ASSERT(solution.rows()==symbols.nops());
503         
504         // return list of equations of the form lst(var1==sol1,var2==sol2,...)
505         lst sollist;
506         for (unsigned i=0; i<symbols.nops(); i++)
507                 sollist.append(symbols.op(i)==solution(i,0));
508         
509         return sollist;
510 }
511
512 /** non-commutative power. */
513 ex ncpower(const ex &basis, unsigned exponent)
514 {
515         if (exponent==0) {
516                 return _ex1();
517         }
518
519         exvector v;
520         v.reserve(exponent);
521         for (unsigned i=0; i<exponent; ++i) {
522                 v.push_back(basis);
523         }
524
525         return ncmul(v,1);
526 }
527
528 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
529  *  for static lib (so ginsh will see them). */
530 unsigned force_include_tgamma = function_index_tgamma;
531 unsigned force_include_zeta1 = function_index_zeta1;
532
533 } // namespace GiNaC