3 * Implementation of GiNaC's initially known functions. */
6 * GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "operators.h"
34 #include "relational.h"
46 static ex conjugate_evalf(const ex & arg)
48 if (is_exactly_a<numeric>(arg)) {
49 return ex_to<numeric>(arg).conjugate();
51 return conjugate_function(arg).hold();
54 static ex conjugate_eval(const ex & arg)
56 return arg.conjugate();
59 static void conjugate_print_latex(const ex & arg, const print_context & c)
61 c.s << "\\bar{"; arg.print(c); c.s << "}";
64 static ex conjugate_conjugate(const ex & arg)
69 REGISTER_FUNCTION(conjugate_function, eval_func(conjugate_eval).
70 evalf_func(conjugate_evalf).
71 print_func<print_latex>(conjugate_print_latex).
72 conjugate_func(conjugate_conjugate).
73 set_name("conjugate","conjugate"));
79 static ex abs_evalf(const ex & arg)
81 if (is_exactly_a<numeric>(arg))
82 return abs(ex_to<numeric>(arg));
84 return abs(arg).hold();
87 static ex abs_eval(const ex & arg)
89 if (is_exactly_a<numeric>(arg))
90 return abs(ex_to<numeric>(arg));
92 return abs(arg).hold();
95 static void abs_print_latex(const ex & arg, const print_context & c)
97 c.s << "{|"; arg.print(c); c.s << "|}";
100 static void abs_print_csrc_float(const ex & arg, const print_context & c)
102 c.s << "fabs("; arg.print(c); c.s << ")";
105 static ex abs_conjugate(const ex & arg)
110 static ex abs_power(const ex & arg, const ex & exp)
112 if (arg.is_equal(arg.conjugate()) && is_a<numeric>(exp) && ex_to<numeric>(exp).is_even())
113 return power(arg, exp);
115 return power(abs(arg), exp).hold();
118 REGISTER_FUNCTION(abs, eval_func(abs_eval).
119 evalf_func(abs_evalf).
120 print_func<print_latex>(abs_print_latex).
121 print_func<print_csrc_float>(abs_print_csrc_float).
122 print_func<print_csrc_double>(abs_print_csrc_float).
123 conjugate_func(abs_conjugate).
124 power_func(abs_power));
130 static ex step_evalf(const ex & arg)
132 if (is_exactly_a<numeric>(arg))
133 return step(ex_to<numeric>(arg));
135 return step(arg).hold();
138 static ex step_eval(const ex & arg)
140 if (is_exactly_a<numeric>(arg))
141 return step(ex_to<numeric>(arg));
143 else if (is_exactly_a<mul>(arg) &&
144 is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
145 numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
148 // step(42*x) -> step(x)
149 return step(arg/oc).hold();
151 // step(-42*x) -> step(-x)
152 return step(-arg/oc).hold();
154 if (oc.real().is_zero()) {
156 // step(42*I*x) -> step(I*x)
157 return step(I*arg/oc).hold();
159 // step(-42*I*x) -> step(-I*x)
160 return step(-I*arg/oc).hold();
164 return step(arg).hold();
167 static ex step_series(const ex & arg,
168 const relational & rel,
172 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
173 if (arg_pt.info(info_flags::numeric)
174 && ex_to<numeric>(arg_pt).real().is_zero()
175 && !(options & series_options::suppress_branchcut))
176 throw (std::domain_error("step_series(): on imaginary axis"));
179 seq.push_back(expair(step(arg_pt), _ex0));
180 return pseries(rel,seq);
183 static ex step_power(const ex & arg, const ex & exp)
185 if (exp.info(info_flags::positive))
188 return power(step(arg), exp).hold();
191 static ex step_conjugate(const ex& arg)
196 REGISTER_FUNCTION(step, eval_func(step_eval).
197 evalf_func(step_evalf).
198 series_func(step_series).
199 conjugate_func(step_conjugate).
200 power_func(step_power));
206 static ex csgn_evalf(const ex & arg)
208 if (is_exactly_a<numeric>(arg))
209 return csgn(ex_to<numeric>(arg));
211 return csgn(arg).hold();
214 static ex csgn_eval(const ex & arg)
216 if (is_exactly_a<numeric>(arg))
217 return csgn(ex_to<numeric>(arg));
219 else if (is_exactly_a<mul>(arg) &&
220 is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
221 numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
224 // csgn(42*x) -> csgn(x)
225 return csgn(arg/oc).hold();
227 // csgn(-42*x) -> -csgn(x)
228 return -csgn(arg/oc).hold();
230 if (oc.real().is_zero()) {
232 // csgn(42*I*x) -> csgn(I*x)
233 return csgn(I*arg/oc).hold();
235 // csgn(-42*I*x) -> -csgn(I*x)
236 return -csgn(I*arg/oc).hold();
240 return csgn(arg).hold();
243 static ex csgn_series(const ex & arg,
244 const relational & rel,
248 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
249 if (arg_pt.info(info_flags::numeric)
250 && ex_to<numeric>(arg_pt).real().is_zero()
251 && !(options & series_options::suppress_branchcut))
252 throw (std::domain_error("csgn_series(): on imaginary axis"));
255 seq.push_back(expair(csgn(arg_pt), _ex0));
256 return pseries(rel,seq);
259 static ex csgn_conjugate(const ex& arg)
264 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
265 evalf_func(csgn_evalf).
266 series_func(csgn_series).
267 conjugate_func(csgn_conjugate));
271 // Eta function: eta(x,y) == log(x*y) - log(x) - log(y).
272 // This function is closely related to the unwinding number K, sometimes found
273 // in modern literature: K(z) == (z-log(exp(z)))/(2*Pi*I).
276 static ex eta_evalf(const ex &x, const ex &y)
278 // It seems like we basically have to replicate the eval function here,
279 // since the expression might not be fully evaluated yet.
280 if (x.info(info_flags::positive) || y.info(info_flags::positive))
283 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
284 const numeric nx = ex_to<numeric>(x);
285 const numeric ny = ex_to<numeric>(y);
286 const numeric nxy = ex_to<numeric>(x*y);
288 if (nx.is_real() && nx.is_negative())
290 if (ny.is_real() && ny.is_negative())
292 if (nxy.is_real() && nxy.is_negative())
294 return evalf(I/4*Pi)*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
295 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
298 return eta(x,y).hold();
301 static ex eta_eval(const ex &x, const ex &y)
303 // trivial: eta(x,c) -> 0 if c is real and positive
304 if (x.info(info_flags::positive) || y.info(info_flags::positive))
307 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
308 // don't call eta_evalf here because it would call Pi.evalf()!
309 const numeric nx = ex_to<numeric>(x);
310 const numeric ny = ex_to<numeric>(y);
311 const numeric nxy = ex_to<numeric>(x*y);
313 if (nx.is_real() && nx.is_negative())
315 if (ny.is_real() && ny.is_negative())
317 if (nxy.is_real() && nxy.is_negative())
319 return (I/4)*Pi*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
320 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
323 return eta(x,y).hold();
326 static ex eta_series(const ex & x, const ex & y,
327 const relational & rel,
331 const ex x_pt = x.subs(rel, subs_options::no_pattern);
332 const ex y_pt = y.subs(rel, subs_options::no_pattern);
333 if ((x_pt.info(info_flags::numeric) && x_pt.info(info_flags::negative)) ||
334 (y_pt.info(info_flags::numeric) && y_pt.info(info_flags::negative)) ||
335 ((x_pt*y_pt).info(info_flags::numeric) && (x_pt*y_pt).info(info_flags::negative)))
336 throw (std::domain_error("eta_series(): on discontinuity"));
338 seq.push_back(expair(eta(x_pt,y_pt), _ex0));
339 return pseries(rel,seq);
342 static ex eta_conjugate(const ex & x, const ex & y)
347 REGISTER_FUNCTION(eta, eval_func(eta_eval).
348 evalf_func(eta_evalf).
349 series_func(eta_series).
351 set_symmetry(sy_symm(0, 1)).
352 conjugate_func(eta_conjugate));
359 static ex Li2_evalf(const ex & x)
361 if (is_exactly_a<numeric>(x))
362 return Li2(ex_to<numeric>(x));
364 return Li2(x).hold();
367 static ex Li2_eval(const ex & x)
369 if (x.info(info_flags::numeric)) {
374 if (x.is_equal(_ex1))
375 return power(Pi,_ex2)/_ex6;
376 // Li2(1/2) -> Pi^2/12 - log(2)^2/2
377 if (x.is_equal(_ex1_2))
378 return power(Pi,_ex2)/_ex12 + power(log(_ex2),_ex2)*_ex_1_2;
379 // Li2(-1) -> -Pi^2/12
380 if (x.is_equal(_ex_1))
381 return -power(Pi,_ex2)/_ex12;
382 // Li2(I) -> -Pi^2/48+Catalan*I
384 return power(Pi,_ex2)/_ex_48 + Catalan*I;
385 // Li2(-I) -> -Pi^2/48-Catalan*I
387 return power(Pi,_ex2)/_ex_48 - Catalan*I;
389 if (!x.info(info_flags::crational))
390 return Li2(ex_to<numeric>(x));
393 return Li2(x).hold();
396 static ex Li2_deriv(const ex & x, unsigned deriv_param)
398 GINAC_ASSERT(deriv_param==0);
400 // d/dx Li2(x) -> -log(1-x)/x
401 return -log(_ex1-x)/x;
404 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
406 const ex x_pt = x.subs(rel, subs_options::no_pattern);
407 if (x_pt.info(info_flags::numeric)) {
408 // First special case: x==0 (derivatives have poles)
409 if (x_pt.is_zero()) {
411 // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot
412 // simply substitute x==0. The limit, however, exists: it is 1.
413 // We also know all higher derivatives' limits:
414 // (d/dx)^n Li2(x) == n!/n^2.
415 // So the primitive series expansion is
416 // Li2(x==0) == x + x^2/4 + x^3/9 + ...
418 // We first construct such a primitive series expansion manually in
419 // a dummy symbol s and then insert the argument's series expansion
420 // for s. Reexpanding the resulting series returns the desired
424 // manually construct the primitive expansion
425 for (int i=1; i<order; ++i)
426 ser += pow(s,i) / pow(numeric(i), *_num2_p);
427 // substitute the argument's series expansion
428 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
429 // maybe that was terminating, so add a proper order term
431 nseq.push_back(expair(Order(_ex1), order));
432 ser += pseries(rel, nseq);
433 // reexpanding it will collapse the series again
434 return ser.series(rel, order);
435 // NB: Of course, this still does not allow us to compute anything
436 // like sin(Li2(x)).series(x==0,2), since then this code here is
437 // not reached and the derivative of sin(Li2(x)) doesn't allow the
438 // substitution x==0. Probably limits *are* needed for the general
439 // cases. In case L'Hospital's rule is implemented for limits and
440 // basic::series() takes care of this, this whole block is probably
443 // second special case: x==1 (branch point)
444 if (x_pt.is_equal(_ex1)) {
446 // construct series manually in a dummy symbol s
449 // manually construct the primitive expansion
450 for (int i=1; i<order; ++i)
451 ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
452 // substitute the argument's series expansion
453 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
454 // maybe that was terminating, so add a proper order term
456 nseq.push_back(expair(Order(_ex1), order));
457 ser += pseries(rel, nseq);
458 // reexpanding it will collapse the series again
459 return ser.series(rel, order);
461 // third special case: x real, >=1 (branch cut)
462 if (!(options & series_options::suppress_branchcut) &&
463 ex_to<numeric>(x_pt).is_real() && ex_to<numeric>(x_pt)>1) {
465 // This is the branch cut: assemble the primitive series manually
466 // and then add the corresponding complex step function.
467 const symbol &s = ex_to<symbol>(rel.lhs());
468 const ex point = rel.rhs();
471 // zeroth order term:
472 seq.push_back(expair(Li2(x_pt), _ex0));
473 // compute the intermediate terms:
474 ex replarg = series(Li2(x), s==foo, order);
475 for (size_t i=1; i<replarg.nops()-1; ++i)
476 seq.push_back(expair((replarg.op(i)/power(s-foo,i)).series(foo==point,1,options).op(0).subs(foo==s, subs_options::no_pattern),i));
477 // append an order term:
478 seq.push_back(expair(Order(_ex1), replarg.nops()-1));
479 return pseries(rel, seq);
482 // all other cases should be safe, by now:
483 throw do_taylor(); // caught by function::series()
486 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
487 evalf_func(Li2_evalf).
488 derivative_func(Li2_deriv).
489 series_func(Li2_series).
490 latex_name("\\mbox{Li}_2"));
496 static ex Li3_eval(const ex & x)
500 return Li3(x).hold();
503 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
504 latex_name("\\mbox{Li}_3"));
507 // Derivatives of Riemann's Zeta-function zetaderiv(0,x)==zeta(x)
510 static ex zetaderiv_eval(const ex & n, const ex & x)
512 if (n.info(info_flags::numeric)) {
513 // zetaderiv(0,x) -> zeta(x)
518 return zetaderiv(n, x).hold();
521 static ex zetaderiv_deriv(const ex & n, const ex & x, unsigned deriv_param)
523 GINAC_ASSERT(deriv_param<2);
525 if (deriv_param==0) {
527 throw(std::logic_error("cannot diff zetaderiv(n,x) with respect to n"));
530 return zetaderiv(n+1,x);
533 REGISTER_FUNCTION(zetaderiv, eval_func(zetaderiv_eval).
534 derivative_func(zetaderiv_deriv).
535 latex_name("\\zeta^\\prime"));
541 static ex factorial_evalf(const ex & x)
543 return factorial(x).hold();
546 static ex factorial_eval(const ex & x)
548 if (is_exactly_a<numeric>(x))
549 return factorial(ex_to<numeric>(x));
551 return factorial(x).hold();
554 static void factorial_print_dflt_latex(const ex & x, const print_context & c)
556 if (is_exactly_a<symbol>(x) ||
557 is_exactly_a<constant>(x) ||
558 is_exactly_a<function>(x)) {
559 x.print(c); c.s << "!";
561 c.s << "("; x.print(c); c.s << ")!";
565 static ex factorial_conjugate(const ex & x)
570 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
571 evalf_func(factorial_evalf).
572 print_func<print_dflt>(factorial_print_dflt_latex).
573 print_func<print_latex>(factorial_print_dflt_latex).
574 conjugate_func(factorial_conjugate));
580 static ex binomial_evalf(const ex & x, const ex & y)
582 return binomial(x, y).hold();
585 static ex binomial_sym(const ex & x, const numeric & y)
587 if (y.is_integer()) {
588 if (y.is_nonneg_integer()) {
589 const unsigned N = y.to_int();
590 if (N == 0) return _ex0;
591 if (N == 1) return x;
593 for (unsigned i = 2; i <= N; ++i)
594 t = (t * (x + i - y - 1)).expand() / i;
600 return binomial(x, y).hold();
603 static ex binomial_eval(const ex & x, const ex &y)
605 if (is_exactly_a<numeric>(y)) {
606 if (is_exactly_a<numeric>(x) && ex_to<numeric>(x).is_integer())
607 return binomial(ex_to<numeric>(x), ex_to<numeric>(y));
609 return binomial_sym(x, ex_to<numeric>(y));
611 return binomial(x, y).hold();
614 // At the moment the numeric evaluation of a binomail function always
615 // gives a real number, but if this would be implemented using the gamma
616 // function, also complex conjugation should be changed (or rather, deleted).
617 static ex binomial_conjugate(const ex & x, const ex & y)
619 return binomial(x,y);
622 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
623 evalf_func(binomial_evalf).
624 conjugate_func(binomial_conjugate));
627 // Order term function (for truncated power series)
630 static ex Order_eval(const ex & x)
632 if (is_exactly_a<numeric>(x)) {
635 return Order(_ex1).hold();
638 } else if (is_exactly_a<mul>(x)) {
639 const mul &m = ex_to<mul>(x);
640 // O(c*expr) -> O(expr)
641 if (is_exactly_a<numeric>(m.op(m.nops() - 1)))
642 return Order(x / m.op(m.nops() - 1)).hold();
644 return Order(x).hold();
647 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
649 // Just wrap the function into a pseries object
651 GINAC_ASSERT(is_a<symbol>(r.lhs()));
652 const symbol &s = ex_to<symbol>(r.lhs());
653 new_seq.push_back(expair(Order(_ex1), numeric(std::min(x.ldegree(s), order))));
654 return pseries(r, new_seq);
657 static ex Order_conjugate(const ex & x)
662 // Differentiation is handled in function::derivative because of its special requirements
664 REGISTER_FUNCTION(Order, eval_func(Order_eval).
665 series_func(Order_series).
666 latex_name("\\mathcal{O}").
667 conjugate_func(Order_conjugate));
670 // Solve linear system
673 ex lsolve(const ex &eqns, const ex &symbols, unsigned options)
675 // solve a system of linear equations
676 if (eqns.info(info_flags::relation_equal)) {
677 if (!symbols.info(info_flags::symbol))
678 throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
679 const ex sol = lsolve(lst(eqns),lst(symbols));
681 GINAC_ASSERT(sol.nops()==1);
682 GINAC_ASSERT(is_exactly_a<relational>(sol.op(0)));
684 return sol.op(0).op(1); // return rhs of first solution
688 if (!eqns.info(info_flags::list)) {
689 throw(std::invalid_argument("lsolve(): 1st argument must be a list"));
691 for (size_t i=0; i<eqns.nops(); i++) {
692 if (!eqns.op(i).info(info_flags::relation_equal)) {
693 throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
696 if (!symbols.info(info_flags::list)) {
697 throw(std::invalid_argument("lsolve(): 2nd argument must be a list"));
699 for (size_t i=0; i<symbols.nops(); i++) {
700 if (!symbols.op(i).info(info_flags::symbol)) {
701 throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
705 // build matrix from equation system
706 matrix sys(eqns.nops(),symbols.nops());
707 matrix rhs(eqns.nops(),1);
708 matrix vars(symbols.nops(),1);
710 for (size_t r=0; r<eqns.nops(); r++) {
711 const ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
713 for (size_t c=0; c<symbols.nops(); c++) {
714 const ex co = eq.coeff(ex_to<symbol>(symbols.op(c)),1);
715 linpart -= co*symbols.op(c);
718 linpart = linpart.expand();
722 // test if system is linear and fill vars matrix
723 for (size_t i=0; i<symbols.nops(); i++) {
724 vars(i,0) = symbols.op(i);
725 if (sys.has(symbols.op(i)))
726 throw(std::logic_error("lsolve: system is not linear"));
727 if (rhs.has(symbols.op(i)))
728 throw(std::logic_error("lsolve: system is not linear"));
733 solution = sys.solve(vars,rhs,options);
734 } catch (const std::runtime_error & e) {
735 // Probably singular matrix or otherwise overdetermined system:
736 // It is consistent to return an empty list
739 GINAC_ASSERT(solution.cols()==1);
740 GINAC_ASSERT(solution.rows()==symbols.nops());
742 // return list of equations of the form lst(var1==sol1,var2==sol2,...)
744 for (size_t i=0; i<symbols.nops(); i++)
745 sollist.append(symbols.op(i)==solution(i,0));
751 // Find real root of f(x) numerically
755 fsolve(const ex& f_in, const symbol& x, const numeric& x1, const numeric& x2)
757 if (!x1.is_real() || !x2.is_real()) {
758 throw std::runtime_error("fsolve(): interval not bounded by real numbers");
761 throw std::runtime_error("fsolve(): vanishing interval");
763 // xx[0] == left interval limit, xx[1] == right interval limit.
764 // fx[0] == f(xx[0]), fx[1] == f(xx[1]).
765 // We keep the root bracketed: xx[0]<xx[1] and fx[0]*fx[1]<0.
766 numeric xx[2] = { x1<x2 ? x1 : x2,
769 if (is_a<relational>(f_in)) {
770 f = f_in.lhs()-f_in.rhs();
774 const ex fx_[2] = { f.subs(x==xx[0]).evalf(),
775 f.subs(x==xx[1]).evalf() };
776 if (!is_a<numeric>(fx_[0]) || !is_a<numeric>(fx_[1])) {
777 throw std::runtime_error("fsolve(): function does not evaluate numerically");
779 numeric fx[2] = { ex_to<numeric>(fx_[0]),
780 ex_to<numeric>(fx_[1]) };
781 if (!fx[0].is_real() || !fx[1].is_real()) {
782 throw std::runtime_error("fsolve(): function evaluates to complex values at interval boundaries");
784 if (fx[0]*fx[1]>=0) {
785 throw std::runtime_error("fsolve(): function does not change sign at interval boundaries");
788 // The Newton-Raphson method has quadratic convergence! Simply put, it
789 // replaces x with x-f(x)/f'(x) at each step. -f/f' is the delta:
790 const ex ff = normal(-f/f.diff(x));
791 int side = 0; // Start at left interval limit.
797 xx[side] += ex_to<numeric>(ff.subs(x==xx[side]).evalf());
798 fx[side] = ex_to<numeric>(f.subs(x==xx[side]).evalf());
799 if ((side==0 && xx[0]<xxprev) || (side==1 && xx[1]>xxprev) || xx[0]>xx[1]) {
800 // Oops, Newton-Raphson method shot out of the interval.
801 // Restore, and try again with the other side instead!
807 xx[side] += ex_to<numeric>(ff.subs(x==xx[side]).evalf());
808 fx[side] = ex_to<numeric>(f.subs(x==xx[side]).evalf());
810 if ((fx[side]<0 && fx[!side]<0) || (fx[side]>0 && fx[!side]>0)) {
811 // Oops, the root isn't bracketed any more.
812 // Restore, and perform a bisection!
816 // Ah, the bisection! Bisections converge linearly. Unfortunately,
817 // they occur pretty often when Newton-Raphson arrives at an x too
818 // close to the result on one side of the interval and
819 // f(x-f(x)/f'(x)) turns out to have the same sign as f(x) due to
820 // precision errors! Recall that this function does not have a
821 // precision goal as one of its arguments but instead relies on
822 // x converging to a fixed point. We speed up the (safe but slow)
823 // bisection method by mixing in a dash of the (unsafer but faster)
824 // secant method: Instead of splitting the interval at the
825 // arithmetic mean (bisection), we split it nearer to the root as
826 // determined by the secant between the values xx[0] and xx[1].
827 // Don't set the secant_weight to one because that could disturb
828 // the convergence in some corner cases!
829 static const double secant_weight = 0.984375; // == 63/64 < 1
830 numeric xxmid = (1-secant_weight)*0.5*(xx[0]+xx[1])
831 + secant_weight*(xx[0]+fx[0]*(xx[0]-xx[1])/(fx[1]-fx[0]));
832 numeric fxmid = ex_to<numeric>(f.subs(x==xxmid).evalf());
833 if (fxmid.is_zero()) {
837 if ((fxmid<0 && fx[side]>0) || (fxmid>0 && fx[side]<0)) {
845 } while (xxprev!=xx[side]);
850 /* Force inclusion of functions from inifcns_gamma and inifcns_zeta
851 * for static lib (so ginsh will see them). */
852 unsigned force_include_tgamma = tgamma_SERIAL::serial;
853 unsigned force_include_zeta1 = zeta1_SERIAL::serial;