3 * Implementation of GiNaC's initially known functions. */
6 * GiNaC Copyright (C) 1999-2001 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"
44 static ex abs_evalf(const ex & arg)
47 TYPECHECK(arg,numeric)
48 END_TYPECHECK(abs(arg))
50 return abs(ex_to<numeric>(arg));
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)
72 TYPECHECK(arg,numeric)
73 END_TYPECHECK(csgn(arg))
75 return csgn(ex_to<numeric>(arg));
78 static ex csgn_eval(const ex & arg)
80 if (is_ex_exactly_of_type(arg, numeric))
81 return csgn(ex_to<numeric>(arg));
83 else if (is_ex_of_type(arg, mul) &&
84 is_ex_of_type(arg.op(arg.nops()-1),numeric)) {
85 numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
88 // csgn(42*x) -> csgn(x)
89 return csgn(arg/oc).hold();
91 // csgn(-42*x) -> -csgn(x)
92 return -csgn(arg/oc).hold();
94 if (oc.real().is_zero()) {
96 // csgn(42*I*x) -> csgn(I*x)
97 return csgn(I*arg/oc).hold();
99 // csgn(-42*I*x) -> -csgn(I*x)
100 return -csgn(I*arg/oc).hold();
104 return csgn(arg).hold();
107 static ex csgn_series(const ex & arg,
108 const relational & rel,
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"));
119 seq.push_back(expair(csgn(arg_pt), _ex0()));
120 return pseries(rel,seq);
123 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
124 evalf_func(csgn_evalf).
125 series_func(csgn_series));
129 // Eta function: log(x*y) == log(x) + log(y) + eta(x,y).
132 static ex eta_evalf(const ex & x, const ex & y)
137 END_TYPECHECK(eta(x,y))
139 numeric xim = imag(ex_to<numeric>(x));
140 numeric yim = imag(ex_to<numeric>(y));
141 numeric xyim = imag(ex_to<numeric>(x*y));
142 return evalf(I/4*Pi)*((csgn(-xim)+1)*(csgn(-yim)+1)*(csgn(xyim)+1)-(csgn(xim)+1)*(csgn(yim)+1)*(csgn(-xyim)+1));
145 static ex eta_eval(const ex & x, const ex & y)
147 if (is_ex_exactly_of_type(x, numeric) &&
148 is_ex_exactly_of_type(y, numeric)) {
149 // don't call eta_evalf here because it would call Pi.evalf()!
150 numeric xim = imag(ex_to<numeric>(x));
151 numeric yim = imag(ex_to<numeric>(y));
152 numeric xyim = imag(ex_to<numeric>(x*y));
153 return (I/4)*Pi*((csgn(-xim)+1)*(csgn(-yim)+1)*(csgn(xyim)+1)-(csgn(xim)+1)*(csgn(yim)+1)*(csgn(-xyim)+1));
156 return eta(x,y).hold();
159 static ex eta_series(const ex & arg1,
161 const relational & rel,
165 const ex arg1_pt = arg1.subs(rel);
166 const ex arg2_pt = arg2.subs(rel);
167 if (ex_to<numeric>(arg1_pt).imag().is_zero() ||
168 ex_to<numeric>(arg2_pt).imag().is_zero() ||
169 ex_to<numeric>(arg1_pt*arg2_pt).imag().is_zero()) {
170 throw (std::domain_error("eta_series(): on discontinuity"));
173 seq.push_back(expair(eta(arg1_pt,arg2_pt), _ex0()));
174 return pseries(rel,seq);
177 REGISTER_FUNCTION(eta, eval_func(eta_eval).
178 evalf_func(eta_evalf).
179 series_func(eta_series).
180 latex_name("\\eta"));
187 static ex Li2_evalf(const ex & x)
191 END_TYPECHECK(Li2(x))
193 return Li2(ex_to<numeric>(x)); // -> numeric Li2(numeric)
196 static ex Li2_eval(const ex & x)
198 if (x.info(info_flags::numeric)) {
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
213 return power(Pi,_ex2())/_ex_48() + Catalan*I;
214 // Li2(-I) -> -Pi^2/48-Catalan*I
216 return power(Pi,_ex2())/_ex_48() - Catalan*I;
218 if (!x.info(info_flags::crational))
222 return Li2(x).hold();
225 static ex Li2_deriv(const ex & x, unsigned deriv_param)
227 GINAC_ASSERT(deriv_param==0);
229 // d/dx Li2(x) -> -log(1-x)/x
233 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
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()) {
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 + ...
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
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
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
272 // second special case: x==1 (branch point)
273 if (x_pt == _ex1()) {
275 // construct series manually in a dummy symbol s
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
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);
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) {
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();
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);
311 // all other cases should be safe, by now:
312 throw do_taylor(); // caught by function::series()
315 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
316 evalf_func(Li2_evalf).
317 derivative_func(Li2_deriv).
318 series_func(Li2_series).
319 latex_name("\\mbox{Li}_2"));
325 static ex Li3_eval(const ex & x)
329 return Li3(x).hold();
332 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
333 latex_name("\\mbox{Li}_3"));
339 static ex factorial_evalf(const ex & x)
341 return factorial(x).hold();
344 static ex factorial_eval(const ex & x)
346 if (is_ex_exactly_of_type(x, numeric))
347 return factorial(ex_to<numeric>(x));
349 return factorial(x).hold();
352 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
353 evalf_func(factorial_evalf));
359 static ex binomial_evalf(const ex & x, const ex & y)
361 return binomial(x, y).hold();
364 static ex binomial_eval(const ex & x, const ex &y)
366 if (is_ex_exactly_of_type(x, numeric) && is_ex_exactly_of_type(y, numeric))
367 return binomial(ex_to<numeric>(x), ex_to<numeric>(y));
369 return binomial(x, y).hold();
372 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
373 evalf_func(binomial_evalf));
376 // Order term function (for truncated power series)
379 static ex Order_eval(const ex & x)
381 if (is_ex_exactly_of_type(x, numeric)) {
384 return Order(_ex1()).hold();
387 } else if (is_ex_exactly_of_type(x, mul)) {
388 mul *m = static_cast<mul *>(x.bp);
389 // O(c*expr) -> O(expr)
390 if (is_ex_exactly_of_type(m->op(m->nops() - 1), numeric))
391 return Order(x / m->op(m->nops() - 1)).hold();
393 return Order(x).hold();
396 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
398 // Just wrap the function into a pseries object
400 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
401 const symbol *s = static_cast<symbol *>(r.lhs().bp);
402 new_seq.push_back(expair(Order(_ex1()), numeric(std::min(x.ldegree(*s), order))));
403 return pseries(r, new_seq);
406 // Differentiation is handled in function::derivative because of its special requirements
408 REGISTER_FUNCTION(Order, eval_func(Order_eval).
409 series_func(Order_series).
410 latex_name("\\mathcal{O}"));
413 // Inert partial differentiation operator
416 ex Derivative_eval(const ex & f, const ex & l)
418 if (!is_ex_of_type(f, function))
419 throw(std::invalid_argument("Derivative(): 1st argument must be a function"));
420 if (!is_ex_of_type(l, lst))
421 throw(std::invalid_argument("Derivative(): 2nd argument must be a list"));
424 // Perform differentiations if possible
425 const function &fcn = ex_to<function>(f);
426 if (fcn.registered_functions()[fcn.get_serial()].has_derivative() && l.nops() > 0) {
428 // The function actually seems to have a derivative, let's calculate it
429 ex d = fcn.pderivative(ex_to_numeric(l.op(0)).to_int());
431 // If this was the last differentiation, return the result
435 // Otherwise recursively continue as long as the derivative is still
437 if (is_ex_of_type(d, function)) {
438 lst l_copy = ex_to<lst>(l);
439 l_copy.remove_first();
440 return Derivative(d, l_copy);
444 return Derivative(f, l).hold();
447 REGISTER_FUNCTION(Derivative, eval_func(Derivative_eval).
448 latex_name("\\mathrm{D}"));
451 // Solve linear system
454 ex lsolve(const ex &eqns, const ex &symbols)
456 // solve a system of linear equations
457 if (eqns.info(info_flags::relation_equal)) {
458 if (!symbols.info(info_flags::symbol))
459 throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
460 ex sol=lsolve(lst(eqns),lst(symbols));
462 GINAC_ASSERT(sol.nops()==1);
463 GINAC_ASSERT(is_ex_exactly_of_type(sol.op(0),relational));
465 return sol.op(0).op(1); // return rhs of first solution
469 if (!eqns.info(info_flags::list)) {
470 throw(std::invalid_argument("lsolve(): 1st argument must be a list"));
472 for (unsigned i=0; i<eqns.nops(); i++) {
473 if (!eqns.op(i).info(info_flags::relation_equal)) {
474 throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
477 if (!symbols.info(info_flags::list)) {
478 throw(std::invalid_argument("lsolve(): 2nd argument must be a list"));
480 for (unsigned i=0; i<symbols.nops(); i++) {
481 if (!symbols.op(i).info(info_flags::symbol)) {
482 throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
486 // build matrix from equation system
487 matrix sys(eqns.nops(),symbols.nops());
488 matrix rhs(eqns.nops(),1);
489 matrix vars(symbols.nops(),1);
491 for (unsigned r=0; r<eqns.nops(); r++) {
492 ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
494 for (unsigned c=0; c<symbols.nops(); c++) {
495 ex co = eq.coeff(ex_to<symbol>(symbols.op(c)),1);
496 linpart -= co*symbols.op(c);
499 linpart = linpart.expand();
503 // test if system is linear and fill vars matrix
504 for (unsigned i=0; i<symbols.nops(); i++) {
505 vars(i,0) = symbols.op(i);
506 if (sys.has(symbols.op(i)))
507 throw(std::logic_error("lsolve: system is not linear"));
508 if (rhs.has(symbols.op(i)))
509 throw(std::logic_error("lsolve: system is not linear"));
514 solution = sys.solve(vars,rhs);
515 } catch (const std::runtime_error & e) {
516 // Probably singular matrix or otherwise overdetermined system:
517 // It is consistent to return an empty list
520 GINAC_ASSERT(solution.cols()==1);
521 GINAC_ASSERT(solution.rows()==symbols.nops());
523 // return list of equations of the form lst(var1==sol1,var2==sol2,...)
525 for (unsigned i=0; i<symbols.nops(); i++)
526 sollist.append(symbols.op(i)==solution(i,0));
531 /* Force inclusion of functions from inifcns_gamma and inifcns_zeta
532 * for static lib (so ginsh will see them). */
533 unsigned force_include_tgamma = function_index_tgamma;
534 unsigned force_include_zeta1 = function_index_zeta1;