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_conjugate(const ex& arg)
188 REGISTER_FUNCTION(step, eval_func(step_eval).
189 evalf_func(step_evalf).
190 series_func(step_series).
191 conjugate_func(step_conjugate));
197 static ex csgn_evalf(const ex & arg)
199 if (is_exactly_a<numeric>(arg))
200 return csgn(ex_to<numeric>(arg));
202 return csgn(arg).hold();
205 static ex csgn_eval(const ex & arg)
207 if (is_exactly_a<numeric>(arg))
208 return csgn(ex_to<numeric>(arg));
210 else if (is_exactly_a<mul>(arg) &&
211 is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
212 numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
215 // csgn(42*x) -> csgn(x)
216 return csgn(arg/oc).hold();
218 // csgn(-42*x) -> -csgn(x)
219 return -csgn(arg/oc).hold();
221 if (oc.real().is_zero()) {
223 // csgn(42*I*x) -> csgn(I*x)
224 return csgn(I*arg/oc).hold();
226 // csgn(-42*I*x) -> -csgn(I*x)
227 return -csgn(I*arg/oc).hold();
231 return csgn(arg).hold();
234 static ex csgn_series(const ex & arg,
235 const relational & rel,
239 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
240 if (arg_pt.info(info_flags::numeric)
241 && ex_to<numeric>(arg_pt).real().is_zero()
242 && !(options & series_options::suppress_branchcut))
243 throw (std::domain_error("csgn_series(): on imaginary axis"));
246 seq.push_back(expair(csgn(arg_pt), _ex0));
247 return pseries(rel,seq);
250 static ex csgn_conjugate(const ex& arg)
255 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
256 evalf_func(csgn_evalf).
257 series_func(csgn_series).
258 conjugate_func(csgn_conjugate));
262 // Eta function: eta(x,y) == log(x*y) - log(x) - log(y).
263 // This function is closely related to the unwinding number K, sometimes found
264 // in modern literature: K(z) == (z-log(exp(z)))/(2*Pi*I).
267 static ex eta_evalf(const ex &x, const ex &y)
269 // It seems like we basically have to replicate the eval function here,
270 // since the expression might not be fully evaluated yet.
271 if (x.info(info_flags::positive) || y.info(info_flags::positive))
274 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
275 const numeric nx = ex_to<numeric>(x);
276 const numeric ny = ex_to<numeric>(y);
277 const numeric nxy = ex_to<numeric>(x*y);
279 if (nx.is_real() && nx.is_negative())
281 if (ny.is_real() && ny.is_negative())
283 if (nxy.is_real() && nxy.is_negative())
285 return evalf(I/4*Pi)*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
286 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
289 return eta(x,y).hold();
292 static ex eta_eval(const ex &x, const ex &y)
294 // trivial: eta(x,c) -> 0 if c is real and positive
295 if (x.info(info_flags::positive) || y.info(info_flags::positive))
298 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
299 // don't call eta_evalf here because it would call Pi.evalf()!
300 const numeric nx = ex_to<numeric>(x);
301 const numeric ny = ex_to<numeric>(y);
302 const numeric nxy = ex_to<numeric>(x*y);
304 if (nx.is_real() && nx.is_negative())
306 if (ny.is_real() && ny.is_negative())
308 if (nxy.is_real() && nxy.is_negative())
310 return (I/4)*Pi*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
311 (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
314 return eta(x,y).hold();
317 static ex eta_series(const ex & x, const ex & y,
318 const relational & rel,
322 const ex x_pt = x.subs(rel, subs_options::no_pattern);
323 const ex y_pt = y.subs(rel, subs_options::no_pattern);
324 if ((x_pt.info(info_flags::numeric) && x_pt.info(info_flags::negative)) ||
325 (y_pt.info(info_flags::numeric) && y_pt.info(info_flags::negative)) ||
326 ((x_pt*y_pt).info(info_flags::numeric) && (x_pt*y_pt).info(info_flags::negative)))
327 throw (std::domain_error("eta_series(): on discontinuity"));
329 seq.push_back(expair(eta(x_pt,y_pt), _ex0));
330 return pseries(rel,seq);
333 static ex eta_conjugate(const ex & x, const ex & y)
338 REGISTER_FUNCTION(eta, eval_func(eta_eval).
339 evalf_func(eta_evalf).
340 series_func(eta_series).
342 set_symmetry(sy_symm(0, 1)).
343 conjugate_func(eta_conjugate));
350 static ex Li2_evalf(const ex & x)
352 if (is_exactly_a<numeric>(x))
353 return Li2(ex_to<numeric>(x));
355 return Li2(x).hold();
358 static ex Li2_eval(const ex & x)
360 if (x.info(info_flags::numeric)) {
365 if (x.is_equal(_ex1))
366 return power(Pi,_ex2)/_ex6;
367 // Li2(1/2) -> Pi^2/12 - log(2)^2/2
368 if (x.is_equal(_ex1_2))
369 return power(Pi,_ex2)/_ex12 + power(log(_ex2),_ex2)*_ex_1_2;
370 // Li2(-1) -> -Pi^2/12
371 if (x.is_equal(_ex_1))
372 return -power(Pi,_ex2)/_ex12;
373 // Li2(I) -> -Pi^2/48+Catalan*I
375 return power(Pi,_ex2)/_ex_48 + Catalan*I;
376 // Li2(-I) -> -Pi^2/48-Catalan*I
378 return power(Pi,_ex2)/_ex_48 - Catalan*I;
380 if (!x.info(info_flags::crational))
381 return Li2(ex_to<numeric>(x));
384 return Li2(x).hold();
387 static ex Li2_deriv(const ex & x, unsigned deriv_param)
389 GINAC_ASSERT(deriv_param==0);
391 // d/dx Li2(x) -> -log(1-x)/x
392 return -log(_ex1-x)/x;
395 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
397 const ex x_pt = x.subs(rel, subs_options::no_pattern);
398 if (x_pt.info(info_flags::numeric)) {
399 // First special case: x==0 (derivatives have poles)
400 if (x_pt.is_zero()) {
402 // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot
403 // simply substitute x==0. The limit, however, exists: it is 1.
404 // We also know all higher derivatives' limits:
405 // (d/dx)^n Li2(x) == n!/n^2.
406 // So the primitive series expansion is
407 // Li2(x==0) == x + x^2/4 + x^3/9 + ...
409 // We first construct such a primitive series expansion manually in
410 // a dummy symbol s and then insert the argument's series expansion
411 // for s. Reexpanding the resulting series returns the desired
415 // manually construct the primitive expansion
416 for (int i=1; i<order; ++i)
417 ser += pow(s,i) / pow(numeric(i), *_num2_p);
418 // substitute the argument's series expansion
419 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
420 // maybe that was terminating, so add a proper order term
422 nseq.push_back(expair(Order(_ex1), order));
423 ser += pseries(rel, nseq);
424 // reexpanding it will collapse the series again
425 return ser.series(rel, order);
426 // NB: Of course, this still does not allow us to compute anything
427 // like sin(Li2(x)).series(x==0,2), since then this code here is
428 // not reached and the derivative of sin(Li2(x)) doesn't allow the
429 // substitution x==0. Probably limits *are* needed for the general
430 // cases. In case L'Hospital's rule is implemented for limits and
431 // basic::series() takes care of this, this whole block is probably
434 // second special case: x==1 (branch point)
435 if (x_pt.is_equal(_ex1)) {
437 // construct series manually in a dummy symbol s
440 // manually construct the primitive expansion
441 for (int i=1; i<order; ++i)
442 ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
443 // substitute the argument's series expansion
444 ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
445 // maybe that was terminating, so add a proper order term
447 nseq.push_back(expair(Order(_ex1), order));
448 ser += pseries(rel, nseq);
449 // reexpanding it will collapse the series again
450 return ser.series(rel, order);
452 // third special case: x real, >=1 (branch cut)
453 if (!(options & series_options::suppress_branchcut) &&
454 ex_to<numeric>(x_pt).is_real() && ex_to<numeric>(x_pt)>1) {
456 // This is the branch cut: assemble the primitive series manually
457 // and then add the corresponding complex step function.
458 const symbol &s = ex_to<symbol>(rel.lhs());
459 const ex point = rel.rhs();
462 // zeroth order term:
463 seq.push_back(expair(Li2(x_pt), _ex0));
464 // compute the intermediate terms:
465 ex replarg = series(Li2(x), s==foo, order);
466 for (size_t i=1; i<replarg.nops()-1; ++i)
467 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));
468 // append an order term:
469 seq.push_back(expair(Order(_ex1), replarg.nops()-1));
470 return pseries(rel, seq);
473 // all other cases should be safe, by now:
474 throw do_taylor(); // caught by function::series()
477 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
478 evalf_func(Li2_evalf).
479 derivative_func(Li2_deriv).
480 series_func(Li2_series).
481 latex_name("\\mbox{Li}_2"));
487 static ex Li3_eval(const ex & x)
491 return Li3(x).hold();
494 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
495 latex_name("\\mbox{Li}_3"));
498 // Derivatives of Riemann's Zeta-function zetaderiv(0,x)==zeta(x)
501 static ex zetaderiv_eval(const ex & n, const ex & x)
503 if (n.info(info_flags::numeric)) {
504 // zetaderiv(0,x) -> zeta(x)
509 return zetaderiv(n, x).hold();
512 static ex zetaderiv_deriv(const ex & n, const ex & x, unsigned deriv_param)
514 GINAC_ASSERT(deriv_param<2);
516 if (deriv_param==0) {
518 throw(std::logic_error("cannot diff zetaderiv(n,x) with respect to n"));
521 return zetaderiv(n+1,x);
524 REGISTER_FUNCTION(zetaderiv, eval_func(zetaderiv_eval).
525 derivative_func(zetaderiv_deriv).
526 latex_name("\\zeta^\\prime"));
532 static ex factorial_evalf(const ex & x)
534 return factorial(x).hold();
537 static ex factorial_eval(const ex & x)
539 if (is_exactly_a<numeric>(x))
540 return factorial(ex_to<numeric>(x));
542 return factorial(x).hold();
545 static void factorial_print_dflt_latex(const ex & x, const print_context & c)
547 if (is_exactly_a<symbol>(x) ||
548 is_exactly_a<constant>(x) ||
549 is_exactly_a<function>(x)) {
550 x.print(c); c.s << "!";
552 c.s << "("; x.print(c); c.s << ")!";
556 static ex factorial_conjugate(const ex & x)
561 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
562 evalf_func(factorial_evalf).
563 print_func<print_dflt>(factorial_print_dflt_latex).
564 print_func<print_latex>(factorial_print_dflt_latex).
565 conjugate_func(factorial_conjugate));
571 static ex binomial_evalf(const ex & x, const ex & y)
573 return binomial(x, y).hold();
576 static ex binomial_sym(const ex & x, const numeric & y)
578 if (y.is_integer()) {
579 if (y.is_nonneg_integer()) {
580 const unsigned N = y.to_int();
581 if (N == 0) return _ex0;
582 if (N == 1) return x;
584 for (unsigned i = 2; i <= N; ++i)
585 t = (t * (x + i - y - 1)).expand() / i;
591 return binomial(x, y).hold();
594 static ex binomial_eval(const ex & x, const ex &y)
596 if (is_exactly_a<numeric>(y)) {
597 if (is_exactly_a<numeric>(x) && ex_to<numeric>(x).is_integer())
598 return binomial(ex_to<numeric>(x), ex_to<numeric>(y));
600 return binomial_sym(x, ex_to<numeric>(y));
602 return binomial(x, y).hold();
605 // At the moment the numeric evaluation of a binomail function always
606 // gives a real number, but if this would be implemented using the gamma
607 // function, also complex conjugation should be changed (or rather, deleted).
608 static ex binomial_conjugate(const ex & x, const ex & y)
610 return binomial(x,y);
613 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
614 evalf_func(binomial_evalf).
615 conjugate_func(binomial_conjugate));
618 // Order term function (for truncated power series)
621 static ex Order_eval(const ex & x)
623 if (is_exactly_a<numeric>(x)) {
626 return Order(_ex1).hold();
629 } else if (is_exactly_a<mul>(x)) {
630 const mul &m = ex_to<mul>(x);
631 // O(c*expr) -> O(expr)
632 if (is_exactly_a<numeric>(m.op(m.nops() - 1)))
633 return Order(x / m.op(m.nops() - 1)).hold();
635 return Order(x).hold();
638 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
640 // Just wrap the function into a pseries object
642 GINAC_ASSERT(is_a<symbol>(r.lhs()));
643 const symbol &s = ex_to<symbol>(r.lhs());
644 new_seq.push_back(expair(Order(_ex1), numeric(std::min(x.ldegree(s), order))));
645 return pseries(r, new_seq);
648 static ex Order_conjugate(const ex & x)
653 // Differentiation is handled in function::derivative because of its special requirements
655 REGISTER_FUNCTION(Order, eval_func(Order_eval).
656 series_func(Order_series).
657 latex_name("\\mathcal{O}").
658 conjugate_func(Order_conjugate));
661 // Solve linear system
664 ex lsolve(const ex &eqns, const ex &symbols, unsigned options)
666 // solve a system of linear equations
667 if (eqns.info(info_flags::relation_equal)) {
668 if (!symbols.info(info_flags::symbol))
669 throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
670 const ex sol = lsolve(lst(eqns),lst(symbols));
672 GINAC_ASSERT(sol.nops()==1);
673 GINAC_ASSERT(is_exactly_a<relational>(sol.op(0)));
675 return sol.op(0).op(1); // return rhs of first solution
679 if (!eqns.info(info_flags::list)) {
680 throw(std::invalid_argument("lsolve(): 1st argument must be a list"));
682 for (size_t i=0; i<eqns.nops(); i++) {
683 if (!eqns.op(i).info(info_flags::relation_equal)) {
684 throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
687 if (!symbols.info(info_flags::list)) {
688 throw(std::invalid_argument("lsolve(): 2nd argument must be a list"));
690 for (size_t i=0; i<symbols.nops(); i++) {
691 if (!symbols.op(i).info(info_flags::symbol)) {
692 throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
696 // build matrix from equation system
697 matrix sys(eqns.nops(),symbols.nops());
698 matrix rhs(eqns.nops(),1);
699 matrix vars(symbols.nops(),1);
701 for (size_t r=0; r<eqns.nops(); r++) {
702 const ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
704 for (size_t c=0; c<symbols.nops(); c++) {
705 const ex co = eq.coeff(ex_to<symbol>(symbols.op(c)),1);
706 linpart -= co*symbols.op(c);
709 linpart = linpart.expand();
713 // test if system is linear and fill vars matrix
714 for (size_t i=0; i<symbols.nops(); i++) {
715 vars(i,0) = symbols.op(i);
716 if (sys.has(symbols.op(i)))
717 throw(std::logic_error("lsolve: system is not linear"));
718 if (rhs.has(symbols.op(i)))
719 throw(std::logic_error("lsolve: system is not linear"));
724 solution = sys.solve(vars,rhs,options);
725 } catch (const std::runtime_error & e) {
726 // Probably singular matrix or otherwise overdetermined system:
727 // It is consistent to return an empty list
730 GINAC_ASSERT(solution.cols()==1);
731 GINAC_ASSERT(solution.rows()==symbols.nops());
733 // return list of equations of the form lst(var1==sol1,var2==sol2,...)
735 for (size_t i=0; i<symbols.nops(); i++)
736 sollist.append(symbols.op(i)==solution(i,0));
742 // Find real root of f(x) numerically
746 fsolve(const ex& f_in, const symbol& x, const numeric& x1, const numeric& x2)
748 if (!x1.is_real() || !x2.is_real()) {
749 throw std::runtime_error("fsolve(): interval not bounded by real numbers");
752 throw std::runtime_error("fsolve(): vanishing interval");
754 // xx[0] == left interval limit, xx[1] == right interval limit.
755 // fx[0] == f(xx[0]), fx[1] == f(xx[1]).
756 // We keep the root bracketed: xx[0]<xx[1] and fx[0]*fx[1]<0.
757 numeric xx[2] = { x1<x2 ? x1 : x2,
760 if (is_a<relational>(f_in)) {
761 f = f_in.lhs()-f_in.rhs();
765 const ex fx_[2] = { f.subs(x==xx[0]).evalf(),
766 f.subs(x==xx[1]).evalf() };
767 if (!is_a<numeric>(fx_[0]) || !is_a<numeric>(fx_[1])) {
768 throw std::runtime_error("fsolve(): function does not evaluate numerically");
770 numeric fx[2] = { ex_to<numeric>(fx_[0]),
771 ex_to<numeric>(fx_[1]) };
772 if (!fx[0].is_real() || !fx[1].is_real()) {
773 throw std::runtime_error("fsolve(): function evaluates to complex values at interval boundaries");
775 if (fx[0]*fx[1]>=0) {
776 throw std::runtime_error("fsolve(): function does not change sign at interval boundaries");
779 // The Newton-Raphson method has quadratic convergence! Simply put, it
780 // replaces x with x-f(x)/f'(x) at each step. -f/f' is the delta:
781 const ex ff = normal(-f/f.diff(x));
782 int side = 0; // Start at left interval limit.
788 xx[side] += ex_to<numeric>(ff.subs(x==xx[side]).evalf());
789 fx[side] = ex_to<numeric>(f.subs(x==xx[side]).evalf());
790 if ((side==0 && xx[0]<xxprev) || (side==1 && xx[1]>xxprev) || xx[0]>xx[1]) {
791 // Oops, Newton-Raphson method shot out of the interval.
792 // Restore, and try again with the other side instead!
798 xx[side] += ex_to<numeric>(ff.subs(x==xx[side]).evalf());
799 fx[side] = ex_to<numeric>(f.subs(x==xx[side]).evalf());
801 if ((fx[side]<0 && fx[!side]<0) || (fx[side]>0 && fx[!side]>0)) {
802 // Oops, the root isn't bracketed any more.
803 // Restore, and perform a bisection!
807 // Ah, the bisection! Bisections converge linearly. Unfortunately,
808 // they occur pretty often when Newton-Raphson arrives at an x too
809 // close to the result on one side of the interval and
810 // f(x-f(x)/f'(x)) turns out to have the same sign as f(x) due to
811 // precision errors! Recall that this function does not have a
812 // precision goal as one of its arguments but instead relies on
813 // x converging to a fixed point. We speed up the (safe but slow)
814 // bisection method by mixing in a dash of the (unsafer but faster)
815 // secant method: Instead of splitting the interval at the
816 // arithmetic mean (bisection), we split it nearer to the root as
817 // determined by the secant between the values xx[0] and xx[1].
818 // Don't set the secant_weight to one because that could disturb
819 // the convergence in some corner cases!
820 static const double secant_weight = 0.984375; // == 63/64 < 1
821 numeric xxmid = (1-secant_weight)*0.5*(xx[0]+xx[1])
822 + secant_weight*(xx[0]+fx[0]*(xx[0]-xx[1])/(fx[1]-fx[0]));
823 numeric fxmid = ex_to<numeric>(f.subs(x==xxmid).evalf());
824 if (fxmid.is_zero()) {
828 if ((fxmid<0 && fx[side]>0) || (fxmid>0 && fx[side]<0)) {
836 } while (xxprev!=xx[side]);
841 /* Force inclusion of functions from inifcns_gamma and inifcns_zeta
842 * for static lib (so ginsh will see them). */
843 unsigned force_include_tgamma = tgamma_SERIAL::serial;
844 unsigned force_include_zeta1 = zeta1_SERIAL::serial;