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