3 * Implementation of GiNaC's initially known functions. */
6 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
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.
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.
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
33 #include "relational.h"
45 static ex abs_evalf(const ex & arg)
47 if (is_exactly_a<numeric>(arg))
48 return abs(ex_to<numeric>(arg));
50 return abs(arg).hold();
53 static ex abs_eval(const ex & arg)
55 if (is_ex_exactly_of_type(arg, numeric))
56 return abs(ex_to<numeric>(arg));
58 return abs(arg).hold();
61 REGISTER_FUNCTION(abs, eval_func(abs_eval).
62 evalf_func(abs_evalf));
69 static ex csgn_evalf(const ex & arg)
71 if (is_exactly_a<numeric>(arg))
72 return csgn(ex_to<numeric>(arg));
74 return csgn(arg).hold();
77 static ex csgn_eval(const ex & arg)
79 if (is_ex_exactly_of_type(arg, numeric))
80 return csgn(ex_to<numeric>(arg));
82 else if (is_ex_exactly_of_type(arg, mul) &&
83 is_ex_of_type(arg.op(arg.nops()-1),numeric)) {
84 numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
87 // csgn(42*x) -> csgn(x)
88 return csgn(arg/oc).hold();
90 // csgn(-42*x) -> -csgn(x)
91 return -csgn(arg/oc).hold();
93 if (oc.real().is_zero()) {
95 // csgn(42*I*x) -> csgn(I*x)
96 return csgn(I*arg/oc).hold();
98 // csgn(-42*I*x) -> -csgn(I*x)
99 return -csgn(I*arg/oc).hold();
103 return csgn(arg).hold();
106 static ex csgn_series(const ex & arg,
107 const relational & rel,
111 const ex arg_pt = arg.subs(rel);
112 if (arg_pt.info(info_flags::numeric)
113 && ex_to<numeric>(arg_pt).real().is_zero()
114 && !(options & series_options::suppress_branchcut))
115 throw (std::domain_error("csgn_series(): on imaginary axis"));
118 seq.push_back(expair(csgn(arg_pt), _ex0));
119 return pseries(rel,seq);
122 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
123 evalf_func(csgn_evalf).
124 series_func(csgn_series));
128 // Eta function: eta(x,y) == log(x*y) - log(x) - log(y).
129 // This function is closely related to the unwinding number K, sometimes found
130 // in modern literature: K(z) == (z-log(exp(z)))/(2*Pi*I).
133 static ex eta_evalf(const ex &x, const ex &y)
135 // It seems like we basically have to replicate the eval function here,
136 // since the expression might not be fully evaluated yet.
137 if (x.info(info_flags::positive) || y.info(info_flags::positive))
140 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
141 const numeric nx = ex_to<numeric>(x);
142 const numeric ny = ex_to<numeric>(y);
143 const numeric nxy = ex_to<numeric>(x*y);
145 if (nx.is_real() && nx.is_negative())
147 if (ny.is_real() && ny.is_negative())
149 if (nxy.is_real() && nxy.is_negative())
151 return evalf(I/4*Pi)*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
152 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
155 return eta(x,y).hold();
158 static ex eta_eval(const ex &x, const ex &y)
160 // trivial: eta(x,c) -> 0 if c is real and positive
161 if (x.info(info_flags::positive) || y.info(info_flags::positive))
164 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
165 // don't call eta_evalf here because it would call Pi.evalf()!
166 const numeric nx = ex_to<numeric>(x);
167 const numeric ny = ex_to<numeric>(y);
168 const numeric nxy = ex_to<numeric>(x*y);
170 if (nx.is_real() && nx.is_negative())
172 if (ny.is_real() && ny.is_negative())
174 if (nxy.is_real() && nxy.is_negative())
176 return (I/4)*Pi*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
177 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
180 return eta(x,y).hold();
183 static ex eta_series(const ex & x, const ex & y,
184 const relational & rel,
188 const ex x_pt = x.subs(rel);
189 const ex y_pt = y.subs(rel);
190 if ((x_pt.info(info_flags::numeric) && x_pt.info(info_flags::negative)) ||
191 (y_pt.info(info_flags::numeric) && y_pt.info(info_flags::negative)) ||
192 ((x_pt*y_pt).info(info_flags::numeric) && (x_pt*y_pt).info(info_flags::negative)))
193 throw (std::domain_error("eta_series(): on discontinuity"));
195 seq.push_back(expair(eta(x_pt,y_pt), _ex0));
196 return pseries(rel,seq);
199 REGISTER_FUNCTION(eta, eval_func(eta_eval).
200 evalf_func(eta_evalf).
201 series_func(eta_series).
203 set_symmetry(sy_symm(0, 1)));
210 static ex Li2_evalf(const ex & x)
212 if (is_exactly_a<numeric>(x))
213 return Li2(ex_to<numeric>(x));
215 return Li2(x).hold();
218 static ex Li2_eval(const ex & x)
220 if (x.info(info_flags::numeric)) {
225 if (x.is_equal(_ex1))
226 return power(Pi,_ex2)/_ex6;
227 // Li2(1/2) -> Pi^2/12 - log(2)^2/2
228 if (x.is_equal(_ex1_2))
229 return power(Pi,_ex2)/_ex12 + power(log(_ex2),_ex2)*_ex_1_2;
230 // Li2(-1) -> -Pi^2/12
231 if (x.is_equal(_ex_1))
232 return -power(Pi,_ex2)/_ex12;
233 // Li2(I) -> -Pi^2/48+Catalan*I
235 return power(Pi,_ex2)/_ex_48 + Catalan*I;
236 // Li2(-I) -> -Pi^2/48-Catalan*I
238 return power(Pi,_ex2)/_ex_48 - Catalan*I;
240 if (!x.info(info_flags::crational))
241 return Li2(ex_to<numeric>(x));
244 return Li2(x).hold();
247 static ex Li2_deriv(const ex & x, unsigned deriv_param)
249 GINAC_ASSERT(deriv_param==0);
251 // d/dx Li2(x) -> -log(1-x)/x
252 return -log(_ex1-x)/x;
255 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
257 const ex x_pt = x.subs(rel);
258 if (x_pt.info(info_flags::numeric)) {
259 // First special case: x==0 (derivatives have poles)
260 if (x_pt.is_zero()) {
262 // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot
263 // simply substitute x==0. The limit, however, exists: it is 1.
264 // We also know all higher derivatives' limits:
265 // (d/dx)^n Li2(x) == n!/n^2.
266 // So the primitive series expansion is
267 // Li2(x==0) == x + x^2/4 + x^3/9 + ...
269 // We first construct such a primitive series expansion manually in
270 // a dummy symbol s and then insert the argument's series expansion
271 // for s. Reexpanding the resulting series returns the desired
275 // manually construct the primitive expansion
276 for (int i=1; i<order; ++i)
277 ser += pow(s,i) / pow(numeric(i), _num2);
278 // substitute the argument's series expansion
279 ser = ser.subs(s==x.series(rel, order));
280 // maybe that was terminating, so add a proper order term
282 nseq.push_back(expair(Order(_ex1), order));
283 ser += pseries(rel, nseq);
284 // reexpanding it will collapse the series again
285 return ser.series(rel, order);
286 // NB: Of course, this still does not allow us to compute anything
287 // like sin(Li2(x)).series(x==0,2), since then this code here is
288 // not reached and the derivative of sin(Li2(x)) doesn't allow the
289 // substitution x==0. Probably limits *are* needed for the general
290 // cases. In case L'Hospital's rule is implemented for limits and
291 // basic::series() takes care of this, this whole block is probably
294 // second special case: x==1 (branch point)
295 if (x_pt.is_equal(_ex1)) {
297 // construct series manually in a dummy symbol s
300 // manually construct the primitive expansion
301 for (int i=1; i<order; ++i)
302 ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
303 // substitute the argument's series expansion
304 ser = ser.subs(s==x.series(rel, order));
305 // maybe that was terminating, so add a proper order term
307 nseq.push_back(expair(Order(_ex1), order));
308 ser += pseries(rel, nseq);
309 // reexpanding it will collapse the series again
310 return ser.series(rel, order);
312 // third special case: x real, >=1 (branch cut)
313 if (!(options & series_options::suppress_branchcut) &&
314 ex_to<numeric>(x_pt).is_real() && ex_to<numeric>(x_pt)>1) {
316 // This is the branch cut: assemble the primitive series manually
317 // and then add the corresponding complex step function.
318 const symbol &s = ex_to<symbol>(rel.lhs());
319 const ex point = rel.rhs();
322 // zeroth order term:
323 seq.push_back(expair(Li2(x_pt), _ex0));
324 // compute the intermediate terms:
325 ex replarg = series(Li2(x), s==foo, order);
326 for (unsigned i=1; i<replarg.nops()-1; ++i)
327 seq.push_back(expair((replarg.op(i)/power(s-foo,i)).series(foo==point,1,options).op(0).subs(foo==s),i));
328 // append an order term:
329 seq.push_back(expair(Order(_ex1), replarg.nops()-1));
330 return pseries(rel, seq);
333 // all other cases should be safe, by now:
334 throw do_taylor(); // caught by function::series()
337 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
338 evalf_func(Li2_evalf).
339 derivative_func(Li2_deriv).
340 series_func(Li2_series).
341 latex_name("\\mbox{Li}_2"));
347 static ex Li3_eval(const ex & x)
351 return Li3(x).hold();
354 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
355 latex_name("\\mbox{Li}_3"));
361 static ex factorial_evalf(const ex & x)
363 return factorial(x).hold();
366 static ex factorial_eval(const ex & x)
368 if (is_ex_exactly_of_type(x, numeric))
369 return factorial(ex_to<numeric>(x));
371 return factorial(x).hold();
374 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
375 evalf_func(factorial_evalf));
381 static ex binomial_evalf(const ex & x, const ex & y)
383 return binomial(x, y).hold();
386 static ex binomial_eval(const ex & x, const ex &y)
388 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
389 return binomial(ex_to<numeric>(x), ex_to<numeric>(y));
391 return binomial(x, y).hold();
394 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
395 evalf_func(binomial_evalf));
398 // Order term function (for truncated power series)
401 static ex Order_eval(const ex & x)
403 if (is_ex_exactly_of_type(x, numeric)) {
406 return Order(_ex1).hold();
409 } else if (is_ex_exactly_of_type(x, mul)) {
410 const mul &m = ex_to<mul>(x);
411 // O(c*expr) -> O(expr)
412 if (is_ex_exactly_of_type(m.op(m.nops() - 1), numeric))
413 return Order(x / m.op(m.nops() - 1)).hold();
415 return Order(x).hold();
418 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
420 // Just wrap the function into a pseries object
422 GINAC_ASSERT(is_a<symbol>(r.lhs()));
423 const symbol &s = ex_to<symbol>(r.lhs());
424 new_seq.push_back(expair(Order(_ex1), numeric(std::min(x.ldegree(s), order))));
425 return pseries(r, new_seq);
428 // Differentiation is handled in function::derivative because of its special requirements
430 REGISTER_FUNCTION(Order, eval_func(Order_eval).
431 series_func(Order_series).
432 latex_name("\\mathcal{O}"));
435 // Solve linear system
438 ex lsolve(const ex &eqns, const ex &symbols, unsigned options)
440 // solve a system of linear equations
441 if (eqns.info(info_flags::relation_equal)) {
442 if (!symbols.info(info_flags::symbol))
443 throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
444 const ex sol = lsolve(lst(eqns),lst(symbols));
446 GINAC_ASSERT(sol.nops()==1);
447 GINAC_ASSERT(is_exactly_a<relational>(sol.op(0)));
449 return sol.op(0).op(1); // return rhs of first solution
453 if (!eqns.info(info_flags::list)) {
454 throw(std::invalid_argument("lsolve(): 1st argument must be a list"));
456 for (unsigned i=0; i<eqns.nops(); i++) {
457 if (!eqns.op(i).info(info_flags::relation_equal)) {
458 throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
461 if (!symbols.info(info_flags::list)) {
462 throw(std::invalid_argument("lsolve(): 2nd argument must be a list"));
464 for (unsigned i=0; i<symbols.nops(); i++) {
465 if (!symbols.op(i).info(info_flags::symbol)) {
466 throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
470 // build matrix from equation system
471 matrix sys(eqns.nops(),symbols.nops());
472 matrix rhs(eqns.nops(),1);
473 matrix vars(symbols.nops(),1);
475 for (unsigned r=0; r<eqns.nops(); r++) {
476 const ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
478 for (unsigned c=0; c<symbols.nops(); c++) {
479 const ex co = eq.coeff(ex_to<symbol>(symbols.op(c)),1);
480 linpart -= co*symbols.op(c);
483 linpart = linpart.expand();
487 // test if system is linear and fill vars matrix
488 for (unsigned i=0; i<symbols.nops(); i++) {
489 vars(i,0) = symbols.op(i);
490 if (sys.has(symbols.op(i)))
491 throw(std::logic_error("lsolve: system is not linear"));
492 if (rhs.has(symbols.op(i)))
493 throw(std::logic_error("lsolve: system is not linear"));
498 solution = sys.solve(vars,rhs,options);
499 } catch (const std::runtime_error & e) {
500 // Probably singular matrix or otherwise overdetermined system:
501 // It is consistent to return an empty list
504 GINAC_ASSERT(solution.cols()==1);
505 GINAC_ASSERT(solution.rows()==symbols.nops());
507 // return list of equations of the form lst(var1==sol1,var2==sol2,...)
509 for (unsigned i=0; i<symbols.nops(); i++)
510 sollist.append(symbols.op(i)==solution(i,0));
515 /* Force inclusion of functions from inifcns_gamma and inifcns_zeta
516 * for static lib (so ginsh will see them). */
517 unsigned force_include_tgamma = function_index_tgamma;
518 unsigned force_include_zeta1 = function_index_zeta1;