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