]> www.ginac.de Git - ginac.git/blob - ginac/inifcns.cpp
- Introduced derivative of Li2.
[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 & x, const relational & rel, int order)
111 {
112     const ex x_pt = x.subs(rel);
113     if (x_pt.info(info_flags::numeric)) {
114         if (ex_to_numeric(x_pt).real().is_zero())
115             throw (std::domain_error("csgn_series(): on imaginary axis"));
116         epvector seq;
117         seq.push_back(expair(csgn(x_pt), _ex0()));
118         return pseries(rel,seq);
119     }
120     epvector seq;
121     seq.push_back(expair(csgn(x_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 // dilogarithm
131 //////////
132
133 static ex Li2_eval(const ex & x)
134 {
135     // Li2(0) -> 0
136     if (x.is_zero())
137         return x;
138     // Li2(1) -> Pi^2/6
139     if (x.is_equal(_ex1()))
140         return power(Pi,_ex2())/_ex6();
141     // Li2(1/2) -> Pi^2/12 - log(2)^2/2
142     if (x.is_equal(_ex1_2()))
143         return power(Pi,_ex2())/_ex12() + power(log(_ex2()),_ex2())*_ex_1_2();
144     // Li2(-1) -> -Pi^2/12
145     if (x.is_equal(_ex_1()))
146         return -power(Pi,_ex2())/_ex12();
147     // Li2(I) -> -Pi^2/48+Catalan*I
148     if (x.is_equal(I))
149         return power(Pi,_ex2())/_ex_48() + Catalan*I;
150     // Li2(-I) -> -Pi^2/48-Catalan*I
151     if (x.is_equal(-I))
152         return power(Pi,_ex2())/_ex_48() - Catalan*I;
153     return Li2(x).hold();
154 }
155
156 static ex Li2_deriv(const ex & x, unsigned deriv_param)
157 {
158     GINAC_ASSERT(deriv_param==0);
159     
160     // d/dx Li2(x) -> -log(1-x)/x
161     return -log(1-x)/x;
162 }
163
164 static ex Li2_series(const ex &x, const relational &rel, int order)
165 {
166     const ex x_pt = x.subs(rel);
167     if (!x_pt.is_zero() && !x_pt.is_equal(_ex1()))
168         throw do_taylor();  // caught by function::series()
169     // First case: x==0 (derivatives have poles)
170     if (x_pt.is_zero()) {
171         // method:
172         // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot 
173         // simply substitute x==0.  The limit, however, exists: it is 1.  We
174         // also know all higher derivatives' limits: (d/dx)^n Li2(x) == n!/n^2.
175         // So the primitive series expansion is Li2(x==0) == x + x^2/4 + x^3/9
176         // and so on.
177         // We first construct such a primitive series expansion manually in
178         // a dummy symbol s and then insert the argument's series expansion
179         // for s.  Reexpanding the resulting series returns the desired result.
180         const symbol s;
181         ex ser;
182         // construct manually the primitive expansion
183         for (int i=1; i<order; ++i)
184             ser += pow(s,i)/pow(numeric(i),numeric(2));
185         // substitute the argument's series expansion
186         ser = ser.subs(s==x.series(rel,order));
187         // maybe that was terminanting, so add a proper order term
188         epvector nseq;
189         nseq.push_back(expair(Order(_ex1()), numeric(order)));
190         ser += pseries(rel, nseq);
191         // reexpand will collapse the series again
192         ser = ser.series(rel,order);
193         return ser;
194     }
195     // second problematic case: x real, >=1 (branch cut)
196     return pseries();
197     // TODO: Li2_series should do something around branch point?
198     // Careful: may involve logs!
199 }
200
201 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
202                        derivative_func(Li2_deriv).
203                        series_func(Li2_series));
204
205 //////////
206 // trilogarithm
207 //////////
208
209 static ex Li3_eval(const ex & x)
210 {
211     if (x.is_zero())
212         return x;
213     return Li3(x).hold();
214 }
215
216 REGISTER_FUNCTION(Li3, eval_func(Li3_eval));
217
218 //////////
219 // factorial
220 //////////
221
222 static ex factorial_evalf(const ex & x)
223 {
224     return factorial(x).hold();
225 }
226
227 static ex factorial_eval(const ex & x)
228 {
229     if (is_ex_exactly_of_type(x, numeric))
230         return factorial(ex_to_numeric(x));
231     else
232         return factorial(x).hold();
233 }
234
235 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
236                              evalf_func(factorial_evalf));
237
238 //////////
239 // binomial
240 //////////
241
242 static ex binomial_evalf(const ex & x, const ex & y)
243 {
244     return binomial(x, y).hold();
245 }
246
247 static ex binomial_eval(const ex & x, const ex &y)
248 {
249     if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
250         return binomial(ex_to_numeric(x), ex_to_numeric(y));
251     else
252         return binomial(x, y).hold();
253 }
254
255 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
256                             evalf_func(binomial_evalf));
257
258 //////////
259 // Order term function (for truncated power series)
260 //////////
261
262 static ex Order_eval(const ex & x)
263 {
264         if (is_ex_exactly_of_type(x, numeric)) {
265
266                 // O(c)=O(1)
267                 return Order(_ex1()).hold();
268
269         } else if (is_ex_exactly_of_type(x, mul)) {
270
271                 mul *m = static_cast<mul *>(x.bp);
272                 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric)) {
273
274                         // O(c*expr)=O(expr)
275                         return Order(x / m->op(m->nops() - 1)).hold();
276                 }
277         }
278         return Order(x).hold();
279 }
280
281 static ex Order_series(const ex & x, const relational & r, int order)
282 {
283         // Just wrap the function into a pseries object
284         epvector new_seq;
285     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
286     const symbol *s = static_cast<symbol *>(r.lhs().bp);
287         new_seq.push_back(expair(Order(_ex1()), numeric(min(x.ldegree(*s), order))));
288         return pseries(r, new_seq);
289 }
290
291 // Differentiation is handled in function::derivative because of its special requirements
292
293 REGISTER_FUNCTION(Order, eval_func(Order_eval).
294                          series_func(Order_series));
295
296 //////////
297 // Inert partial differentiation operator
298 //////////
299
300 static ex Derivative_eval(const ex & f, const ex & l)
301 {
302         if (!is_ex_exactly_of_type(f, function)) {
303         throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
304         }
305     if (!is_ex_exactly_of_type(l, lst)) {
306         throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
307     }
308         return Derivative(f, l).hold();
309 }
310
311 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval));
312
313 //////////
314 // Solve linear system
315 //////////
316
317 ex lsolve(const ex &eqns, const ex &symbols)
318 {
319     // solve a system of linear equations
320     if (eqns.info(info_flags::relation_equal)) {
321         if (!symbols.info(info_flags::symbol)) {
322             throw(std::invalid_argument("lsolve: 2nd argument must be a symbol"));
323         }
324         ex sol=lsolve(lst(eqns),lst(symbols));
325         
326         GINAC_ASSERT(sol.nops()==1);
327         GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
328         
329         return sol.op(0).op(1); // return rhs of first solution
330     }
331     
332     // syntax checks
333     if (!eqns.info(info_flags::list)) {
334         throw(std::invalid_argument("lsolve: 1st argument must be a list"));
335     }
336     for (unsigned i=0; i<eqns.nops(); i++) {
337         if (!eqns.op(i).info(info_flags::relation_equal)) {
338             throw(std::invalid_argument("lsolve: 1st argument must be a list of equations"));
339         }
340     }
341     if (!symbols.info(info_flags::list)) {
342         throw(std::invalid_argument("lsolve: 2nd argument must be a list"));
343     }
344     for (unsigned i=0; i<symbols.nops(); i++) {
345         if (!symbols.op(i).info(info_flags::symbol)) {
346             throw(std::invalid_argument("lsolve: 2nd argument must be a list of symbols"));
347         }
348     }
349     
350     // build matrix from equation system
351     matrix sys(eqns.nops(),symbols.nops());
352     matrix rhs(eqns.nops(),1);
353     matrix vars(symbols.nops(),1);
354     
355     for (unsigned r=0; r<eqns.nops(); r++) {
356         ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
357         ex linpart = eq;
358         for (unsigned c=0; c<symbols.nops(); c++) {
359             ex co = eq.coeff(ex_to_symbol(symbols.op(c)),1);
360             linpart -= co*symbols.op(c);
361             sys.set(r,c,co);
362         }
363         linpart=linpart.expand();
364         rhs.set(r,0,-linpart);
365     }
366     
367     // test if system is linear and fill vars matrix
368     for (unsigned i=0; i<symbols.nops(); i++) {
369         vars.set(i,0,symbols.op(i));
370         if (sys.has(symbols.op(i)))
371             throw(std::logic_error("lsolve: system is not linear"));
372         if (rhs.has(symbols.op(i)))
373             throw(std::logic_error("lsolve: system is not linear"));
374     }
375     
376     //matrix solution=sys.solve(rhs);
377     matrix solution;
378     try {
379         solution = sys.fraction_free_elim(vars,rhs);
380     } catch (const runtime_error & e) {
381         // probably singular matrix (or other error)
382         // return empty solution list
383         // cerr << e.what() << endl;
384         return lst();
385     }
386     
387     // return a list of equations
388     if (solution.cols()!=1) {
389         throw(std::runtime_error("lsolve: strange number of columns returned from matrix::solve"));
390     }
391     if (solution.rows()!=symbols.nops()) {
392         cout << "symbols.nops()=" << symbols.nops() << endl;
393         cout << "solution.rows()=" << solution.rows() << endl;
394         throw(std::runtime_error("lsolve: strange number of rows returned from matrix::solve"));
395     }
396     
397     // return list of the form lst(var1==sol1,var2==sol2,...)
398     lst sollist;
399     for (unsigned i=0; i<symbols.nops(); i++) {
400         sollist.append(symbols.op(i)==solution(i,0));
401     }
402     
403     return sollist;
404 }
405
406 /** non-commutative power. */
407 ex ncpower(const ex &basis, unsigned exponent)
408 {
409     if (exponent==0) {
410         return _ex1();
411     }
412
413     exvector v;
414     v.reserve(exponent);
415     for (unsigned i=0; i<exponent; ++i) {
416         v.push_back(basis);
417     }
418
419     return ncmul(v,1);
420 }
421
422 /** Force inclusion of functions from initcns_gamma and inifcns_zeta
423  *  for static lib (so ginsh will see them). */
424 unsigned force_include_tgamma = function_index_tgamma;
425 unsigned force_include_zeta1 = function_index_zeta1;
426
427 #ifndef NO_NAMESPACE_GINAC
428 } // namespace GiNaC
429 #endif // ndef NO_NAMESPACE_GINAC