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