GiNaC  1.6.2
inifcns.cpp
Go to the documentation of this file.
00001 
00005 /*
00006  *  GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 #include "inifcns.h"
00024 #include "ex.h"
00025 #include "constant.h"
00026 #include "lst.h"
00027 #include "matrix.h"
00028 #include "mul.h"
00029 #include "power.h"
00030 #include "operators.h"
00031 #include "relational.h"
00032 #include "pseries.h"
00033 #include "symbol.h"
00034 #include "symmetry.h"
00035 #include "utils.h"
00036 
00037 #include <stdexcept>
00038 #include <vector>
00039 
00040 namespace GiNaC {
00041 
00043 // complex conjugate
00045 
00046 static ex conjugate_evalf(const ex & arg)
00047 {
00048     if (is_exactly_a<numeric>(arg)) {
00049         return ex_to<numeric>(arg).conjugate();
00050     }
00051     return conjugate_function(arg).hold();
00052 }
00053 
00054 static ex conjugate_eval(const ex & arg)
00055 {
00056     return arg.conjugate();
00057 }
00058 
00059 static void conjugate_print_latex(const ex & arg, const print_context & c)
00060 {
00061     c.s << "\\bar{"; arg.print(c); c.s << "}";
00062 }
00063 
00064 static ex conjugate_conjugate(const ex & arg)
00065 {
00066     return arg;
00067 }
00068 
00069 static ex conjugate_real_part(const ex & arg)
00070 {
00071     return arg.real_part();
00072 }
00073 
00074 static ex conjugate_imag_part(const ex & arg)
00075 {
00076     return -arg.imag_part();
00077 }
00078 
00079 REGISTER_FUNCTION(conjugate_function, eval_func(conjugate_eval).
00080                                       evalf_func(conjugate_evalf).
00081                                       print_func<print_latex>(conjugate_print_latex).
00082                                       conjugate_func(conjugate_conjugate).
00083                                       real_part_func(conjugate_real_part).
00084                                       imag_part_func(conjugate_imag_part).
00085                                       set_name("conjugate","conjugate"));
00086 
00088 // real part
00090 
00091 static ex real_part_evalf(const ex & arg)
00092 {
00093     if (is_exactly_a<numeric>(arg)) {
00094         return ex_to<numeric>(arg).real();
00095     }
00096     return real_part_function(arg).hold();
00097 }
00098 
00099 static ex real_part_eval(const ex & arg)
00100 {
00101     return arg.real_part();
00102 }
00103 
00104 static void real_part_print_latex(const ex & arg, const print_context & c)
00105 {
00106     c.s << "\\Re"; arg.print(c); c.s << "";
00107 }
00108 
00109 static ex real_part_conjugate(const ex & arg)
00110 {
00111     return real_part_function(arg).hold();
00112 }
00113 
00114 static ex real_part_real_part(const ex & arg)
00115 {
00116     return real_part_function(arg).hold();
00117 }
00118 
00119 static ex real_part_imag_part(const ex & arg)
00120 {
00121     return 0;
00122 }
00123 
00124 REGISTER_FUNCTION(real_part_function, eval_func(real_part_eval).
00125                                       evalf_func(real_part_evalf).
00126                                       print_func<print_latex>(real_part_print_latex).
00127                                       conjugate_func(real_part_conjugate).
00128                                       real_part_func(real_part_real_part).
00129                                       imag_part_func(real_part_imag_part).
00130                                       set_name("real_part","real_part"));
00131 
00133 // imag part
00135 
00136 static ex imag_part_evalf(const ex & arg)
00137 {
00138     if (is_exactly_a<numeric>(arg)) {
00139         return ex_to<numeric>(arg).imag();
00140     }
00141     return imag_part_function(arg).hold();
00142 }
00143 
00144 static ex imag_part_eval(const ex & arg)
00145 {
00146     return arg.imag_part();
00147 }
00148 
00149 static void imag_part_print_latex(const ex & arg, const print_context & c)
00150 {
00151     c.s << "\\Im"; arg.print(c); c.s << "";
00152 }
00153 
00154 static ex imag_part_conjugate(const ex & arg)
00155 {
00156     return imag_part_function(arg).hold();
00157 }
00158 
00159 static ex imag_part_real_part(const ex & arg)
00160 {
00161     return imag_part_function(arg).hold();
00162 }
00163 
00164 static ex imag_part_imag_part(const ex & arg)
00165 {
00166     return 0;
00167 }
00168 
00169 REGISTER_FUNCTION(imag_part_function, eval_func(imag_part_eval).
00170                                       evalf_func(imag_part_evalf).
00171                                       print_func<print_latex>(imag_part_print_latex).
00172                                       conjugate_func(imag_part_conjugate).
00173                                       real_part_func(imag_part_real_part).
00174                                       imag_part_func(imag_part_imag_part).
00175                                       set_name("imag_part","imag_part"));
00176 
00178 // absolute value
00180 
00181 static ex abs_evalf(const ex & arg)
00182 {
00183     if (is_exactly_a<numeric>(arg))
00184         return abs(ex_to<numeric>(arg));
00185     
00186     return abs(arg).hold();
00187 }
00188 
00189 static ex abs_eval(const ex & arg)
00190 {
00191     if (is_exactly_a<numeric>(arg))
00192         return abs(ex_to<numeric>(arg));
00193 
00194     if (arg.info(info_flags::nonnegative))
00195         return arg;
00196 
00197     if (is_ex_the_function(arg, abs))
00198         return arg;
00199 
00200     return abs(arg).hold();
00201 }
00202 
00203 static void abs_print_latex(const ex & arg, const print_context & c)
00204 {
00205     c.s << "{|"; arg.print(c); c.s << "|}";
00206 }
00207 
00208 static void abs_print_csrc_float(const ex & arg, const print_context & c)
00209 {
00210     c.s << "fabs("; arg.print(c); c.s << ")";
00211 }
00212 
00213 static ex abs_conjugate(const ex & arg)
00214 {
00215     return abs(arg);
00216 }
00217 
00218 static ex abs_real_part(const ex & arg)
00219 {
00220     return abs(arg).hold();
00221 }
00222 
00223 static ex abs_imag_part(const ex& arg)
00224 {
00225     return 0;
00226 }
00227 
00228 static ex abs_power(const ex & arg, const ex & exp)
00229 {
00230     if (arg.is_equal(arg.conjugate()) && is_a<numeric>(exp) && ex_to<numeric>(exp).is_even())
00231         return power(arg, exp);
00232     else
00233         return power(abs(arg), exp).hold();
00234 }
00235 
00236 REGISTER_FUNCTION(abs, eval_func(abs_eval).
00237                        evalf_func(abs_evalf).
00238                        print_func<print_latex>(abs_print_latex).
00239                        print_func<print_csrc_float>(abs_print_csrc_float).
00240                        print_func<print_csrc_double>(abs_print_csrc_float).
00241                        conjugate_func(abs_conjugate).
00242                        real_part_func(abs_real_part).
00243                        imag_part_func(abs_imag_part).
00244                        power_func(abs_power));
00245 
00247 // Step function
00249 
00250 static ex step_evalf(const ex & arg)
00251 {
00252     if (is_exactly_a<numeric>(arg))
00253         return step(ex_to<numeric>(arg));
00254     
00255     return step(arg).hold();
00256 }
00257 
00258 static ex step_eval(const ex & arg)
00259 {
00260     if (is_exactly_a<numeric>(arg))
00261         return step(ex_to<numeric>(arg));
00262     
00263     else if (is_exactly_a<mul>(arg) &&
00264              is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
00265         numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
00266         if (oc.is_real()) {
00267             if (oc > 0)
00268                 // step(42*x) -> step(x)
00269                 return step(arg/oc).hold();
00270             else
00271                 // step(-42*x) -> step(-x)
00272                 return step(-arg/oc).hold();
00273         }
00274         if (oc.real().is_zero()) {
00275             if (oc.imag() > 0)
00276                 // step(42*I*x) -> step(I*x)
00277                 return step(I*arg/oc).hold();
00278             else
00279                 // step(-42*I*x) -> step(-I*x)
00280                 return step(-I*arg/oc).hold();
00281         }
00282     }
00283     
00284     return step(arg).hold();
00285 }
00286 
00287 static ex step_series(const ex & arg,
00288                       const relational & rel,
00289                       int order,
00290                       unsigned options)
00291 {
00292     const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
00293     if (arg_pt.info(info_flags::numeric)
00294         && ex_to<numeric>(arg_pt).real().is_zero()
00295         && !(options & series_options::suppress_branchcut))
00296         throw (std::domain_error("step_series(): on imaginary axis"));
00297     
00298     epvector seq;
00299     seq.push_back(expair(step(arg_pt), _ex0));
00300     return pseries(rel,seq);
00301 }
00302 
00303 static ex step_conjugate(const ex& arg)
00304 {
00305     return step(arg).hold();
00306 }
00307 
00308 static ex step_real_part(const ex& arg)
00309 {
00310     return step(arg).hold();
00311 }
00312 
00313 static ex step_imag_part(const ex& arg)
00314 {
00315     return 0;
00316 }
00317 
00318 REGISTER_FUNCTION(step, eval_func(step_eval).
00319                         evalf_func(step_evalf).
00320                         series_func(step_series).
00321                         conjugate_func(step_conjugate).
00322                         real_part_func(step_real_part).
00323                         imag_part_func(step_imag_part));
00324 
00326 // Complex sign
00328 
00329 static ex csgn_evalf(const ex & arg)
00330 {
00331     if (is_exactly_a<numeric>(arg))
00332         return csgn(ex_to<numeric>(arg));
00333     
00334     return csgn(arg).hold();
00335 }
00336 
00337 static ex csgn_eval(const ex & arg)
00338 {
00339     if (is_exactly_a<numeric>(arg))
00340         return csgn(ex_to<numeric>(arg));
00341     
00342     else if (is_exactly_a<mul>(arg) &&
00343              is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
00344         numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
00345         if (oc.is_real()) {
00346             if (oc > 0)
00347                 // csgn(42*x) -> csgn(x)
00348                 return csgn(arg/oc).hold();
00349             else
00350                 // csgn(-42*x) -> -csgn(x)
00351                 return -csgn(arg/oc).hold();
00352         }
00353         if (oc.real().is_zero()) {
00354             if (oc.imag() > 0)
00355                 // csgn(42*I*x) -> csgn(I*x)
00356                 return csgn(I*arg/oc).hold();
00357             else
00358                 // csgn(-42*I*x) -> -csgn(I*x)
00359                 return -csgn(I*arg/oc).hold();
00360         }
00361     }
00362     
00363     return csgn(arg).hold();
00364 }
00365 
00366 static ex csgn_series(const ex & arg,
00367                       const relational & rel,
00368                       int order,
00369                       unsigned options)
00370 {
00371     const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
00372     if (arg_pt.info(info_flags::numeric)
00373         && ex_to<numeric>(arg_pt).real().is_zero()
00374         && !(options & series_options::suppress_branchcut))
00375         throw (std::domain_error("csgn_series(): on imaginary axis"));
00376     
00377     epvector seq;
00378     seq.push_back(expair(csgn(arg_pt), _ex0));
00379     return pseries(rel,seq);
00380 }
00381 
00382 static ex csgn_conjugate(const ex& arg)
00383 {
00384     return csgn(arg).hold();
00385 }
00386 
00387 static ex csgn_real_part(const ex& arg)
00388 {
00389     return csgn(arg).hold();
00390 }
00391 
00392 static ex csgn_imag_part(const ex& arg)
00393 {
00394     return 0;
00395 }
00396 
00397 static ex csgn_power(const ex & arg, const ex & exp)
00398 {
00399     if (is_a<numeric>(exp) && exp.info(info_flags::positive) && ex_to<numeric>(exp).is_integer()) {
00400         if (ex_to<numeric>(exp).is_odd())
00401             return csgn(arg);
00402         else
00403             return power(csgn(arg), _ex2).hold();
00404     } else
00405         return power(csgn(arg), exp).hold();
00406 }
00407 
00408 
00409 REGISTER_FUNCTION(csgn, eval_func(csgn_eval).
00410                         evalf_func(csgn_evalf).
00411                         series_func(csgn_series).
00412                         conjugate_func(csgn_conjugate).
00413                         real_part_func(csgn_real_part).
00414                         imag_part_func(csgn_imag_part).
00415                         power_func(csgn_power));
00416 
00417 
00419 // Eta function: eta(x,y) == log(x*y) - log(x) - log(y).
00420 // This function is closely related to the unwinding number K, sometimes found
00421 // in modern literature: K(z) == (z-log(exp(z)))/(2*Pi*I).
00423 
00424 static ex eta_evalf(const ex &x, const ex &y)
00425 {
00426     // It seems like we basically have to replicate the eval function here,
00427     // since the expression might not be fully evaluated yet.
00428     if (x.info(info_flags::positive) || y.info(info_flags::positive))
00429         return _ex0;
00430 
00431     if (x.info(info_flags::numeric) &&  y.info(info_flags::numeric)) {
00432         const numeric nx = ex_to<numeric>(x);
00433         const numeric ny = ex_to<numeric>(y);
00434         const numeric nxy = ex_to<numeric>(x*y);
00435         int cut = 0;
00436         if (nx.is_real() && nx.is_negative())
00437             cut -= 4;
00438         if (ny.is_real() && ny.is_negative())
00439             cut -= 4;
00440         if (nxy.is_real() && nxy.is_negative())
00441             cut += 4;
00442         return evalf(I/4*Pi)*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
00443                               (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
00444     }
00445 
00446     return eta(x,y).hold();
00447 }
00448 
00449 static ex eta_eval(const ex &x, const ex &y)
00450 {
00451     // trivial:  eta(x,c) -> 0  if c is real and positive
00452     if (x.info(info_flags::positive) || y.info(info_flags::positive))
00453         return _ex0;
00454 
00455     if (x.info(info_flags::numeric) &&  y.info(info_flags::numeric)) {
00456         // don't call eta_evalf here because it would call Pi.evalf()!
00457         const numeric nx = ex_to<numeric>(x);
00458         const numeric ny = ex_to<numeric>(y);
00459         const numeric nxy = ex_to<numeric>(x*y);
00460         int cut = 0;
00461         if (nx.is_real() && nx.is_negative())
00462             cut -= 4;
00463         if (ny.is_real() && ny.is_negative())
00464             cut -= 4;
00465         if (nxy.is_real() && nxy.is_negative())
00466             cut += 4;
00467         return (I/4)*Pi*((csgn(-imag(nx))+1)*(csgn(-imag(ny))+1)*(csgn(imag(nxy))+1)-
00468                          (csgn(imag(nx))+1)*(csgn(imag(ny))+1)*(csgn(-imag(nxy))+1)+cut);
00469     }
00470     
00471     return eta(x,y).hold();
00472 }
00473 
00474 static ex eta_series(const ex & x, const ex & y,
00475                      const relational & rel,
00476                      int order,
00477                      unsigned options)
00478 {
00479     const ex x_pt = x.subs(rel, subs_options::no_pattern);
00480     const ex y_pt = y.subs(rel, subs_options::no_pattern);
00481     if ((x_pt.info(info_flags::numeric) && x_pt.info(info_flags::negative)) ||
00482         (y_pt.info(info_flags::numeric) && y_pt.info(info_flags::negative)) ||
00483         ((x_pt*y_pt).info(info_flags::numeric) && (x_pt*y_pt).info(info_flags::negative)))
00484             throw (std::domain_error("eta_series(): on discontinuity"));
00485     epvector seq;
00486     seq.push_back(expair(eta(x_pt,y_pt), _ex0));
00487     return pseries(rel,seq);
00488 }
00489 
00490 static ex eta_conjugate(const ex & x, const ex & y)
00491 {
00492     return -eta(x, y);
00493 }
00494 
00495 static ex eta_real_part(const ex & x, const ex & y)
00496 {
00497     return 0;
00498 }
00499 
00500 static ex eta_imag_part(const ex & x, const ex & y)
00501 {
00502     return -I*eta(x, y).hold();
00503 }
00504 
00505 REGISTER_FUNCTION(eta, eval_func(eta_eval).
00506                        evalf_func(eta_evalf).
00507                        series_func(eta_series).
00508                        latex_name("\\eta").
00509                        set_symmetry(sy_symm(0, 1)).
00510                        conjugate_func(eta_conjugate).
00511                        real_part_func(eta_real_part).
00512                        imag_part_func(eta_imag_part));
00513 
00514 
00516 // dilogarithm
00518 
00519 static ex Li2_evalf(const ex & x)
00520 {
00521     if (is_exactly_a<numeric>(x))
00522         return Li2(ex_to<numeric>(x));
00523     
00524     return Li2(x).hold();
00525 }
00526 
00527 static ex Li2_eval(const ex & x)
00528 {
00529     if (x.info(info_flags::numeric)) {
00530         // Li2(0) -> 0
00531         if (x.is_zero())
00532             return _ex0;
00533         // Li2(1) -> Pi^2/6
00534         if (x.is_equal(_ex1))
00535             return power(Pi,_ex2)/_ex6;
00536         // Li2(1/2) -> Pi^2/12 - log(2)^2/2
00537         if (x.is_equal(_ex1_2))
00538             return power(Pi,_ex2)/_ex12 + power(log(_ex2),_ex2)*_ex_1_2;
00539         // Li2(-1) -> -Pi^2/12
00540         if (x.is_equal(_ex_1))
00541             return -power(Pi,_ex2)/_ex12;
00542         // Li2(I) -> -Pi^2/48+Catalan*I
00543         if (x.is_equal(I))
00544             return power(Pi,_ex2)/_ex_48 + Catalan*I;
00545         // Li2(-I) -> -Pi^2/48-Catalan*I
00546         if (x.is_equal(-I))
00547             return power(Pi,_ex2)/_ex_48 - Catalan*I;
00548         // Li2(float)
00549         if (!x.info(info_flags::crational))
00550             return Li2(ex_to<numeric>(x));
00551     }
00552     
00553     return Li2(x).hold();
00554 }
00555 
00556 static ex Li2_deriv(const ex & x, unsigned deriv_param)
00557 {
00558     GINAC_ASSERT(deriv_param==0);
00559     
00560     // d/dx Li2(x) -> -log(1-x)/x
00561     return -log(_ex1-x)/x;
00562 }
00563 
00564 static ex Li2_series(const ex &x, const relational &rel, int order, unsigned options)
00565 {
00566     const ex x_pt = x.subs(rel, subs_options::no_pattern);
00567     if (x_pt.info(info_flags::numeric)) {
00568         // First special case: x==0 (derivatives have poles)
00569         if (x_pt.is_zero()) {
00570             // method:
00571             // The problem is that in d/dx Li2(x==0) == -log(1-x)/x we cannot 
00572             // simply substitute x==0.  The limit, however, exists: it is 1.
00573             // We also know all higher derivatives' limits:
00574             // (d/dx)^n Li2(x) == n!/n^2.
00575             // So the primitive series expansion is
00576             // Li2(x==0) == x + x^2/4 + x^3/9 + ...
00577             // and so on.
00578             // We first construct such a primitive series expansion manually in
00579             // a dummy symbol s and then insert the argument's series expansion
00580             // for s.  Reexpanding the resulting series returns the desired
00581             // result.
00582             const symbol s;
00583             ex ser;
00584             // manually construct the primitive expansion
00585             for (int i=1; i<order; ++i)
00586                 ser += pow(s,i) / pow(numeric(i), *_num2_p);
00587             // substitute the argument's series expansion
00588             ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
00589             // maybe that was terminating, so add a proper order term
00590             epvector nseq;
00591             nseq.push_back(expair(Order(_ex1), order));
00592             ser += pseries(rel, nseq);
00593             // reexpanding it will collapse the series again
00594             return ser.series(rel, order);
00595             // NB: Of course, this still does not allow us to compute anything
00596             // like sin(Li2(x)).series(x==0,2), since then this code here is
00597             // not reached and the derivative of sin(Li2(x)) doesn't allow the
00598             // substitution x==0.  Probably limits *are* needed for the general
00599             // cases.  In case L'Hospital's rule is implemented for limits and
00600             // basic::series() takes care of this, this whole block is probably
00601             // obsolete!
00602         }
00603         // second special case: x==1 (branch point)
00604         if (x_pt.is_equal(_ex1)) {
00605             // method:
00606             // construct series manually in a dummy symbol s
00607             const symbol s;
00608             ex ser = zeta(_ex2);
00609             // manually construct the primitive expansion
00610             for (int i=1; i<order; ++i)
00611                 ser += pow(1-s,i) * (numeric(1,i)*(I*Pi+log(s-1)) - numeric(1,i*i));
00612             // substitute the argument's series expansion
00613             ser = ser.subs(s==x.series(rel, order), subs_options::no_pattern);
00614             // maybe that was terminating, so add a proper order term
00615             epvector nseq;
00616             nseq.push_back(expair(Order(_ex1), order));
00617             ser += pseries(rel, nseq);
00618             // reexpanding it will collapse the series again
00619             return ser.series(rel, order);
00620         }
00621         // third special case: x real, >=1 (branch cut)
00622         if (!(options & series_options::suppress_branchcut) &&
00623             ex_to<numeric>(x_pt).is_real() && ex_to<numeric>(x_pt)>1) {
00624             // method:
00625             // This is the branch cut: assemble the primitive series manually
00626             // and then add the corresponding complex step function.
00627             const symbol &s = ex_to<symbol>(rel.lhs());
00628             const ex point = rel.rhs();
00629             const symbol foo;
00630             epvector seq;
00631             // zeroth order term:
00632             seq.push_back(expair(Li2(x_pt), _ex0));
00633             // compute the intermediate terms:
00634             ex replarg = series(Li2(x), s==foo, order);
00635             for (size_t i=1; i<replarg.nops()-1; ++i)
00636                 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));
00637             // append an order term:
00638             seq.push_back(expair(Order(_ex1), replarg.nops()-1));
00639             return pseries(rel, seq);
00640         }
00641     }
00642     // all other cases should be safe, by now:
00643     throw do_taylor();  // caught by function::series()
00644 }
00645 
00646 static ex Li2_conjugate(const ex & x)
00647 {
00648     // conjugate(Li2(x))==Li2(conjugate(x)) unless on the branch cuts which
00649     // run along the positive real axis beginning at 1.
00650     if (x.info(info_flags::negative)) {
00651         return Li2(x);
00652     }
00653     if (is_exactly_a<numeric>(x) &&
00654         (!x.imag_part().is_zero() || x < *_num1_p)) {
00655         return Li2(x.conjugate());
00656     }
00657     return conjugate_function(Li2(x)).hold();
00658 }
00659 
00660 REGISTER_FUNCTION(Li2, eval_func(Li2_eval).
00661                        evalf_func(Li2_evalf).
00662                        derivative_func(Li2_deriv).
00663                        series_func(Li2_series).
00664                        conjugate_func(Li2_conjugate).
00665                        latex_name("\\mathrm{Li}_2"));
00666 
00668 // trilogarithm
00670 
00671 static ex Li3_eval(const ex & x)
00672 {
00673     if (x.is_zero())
00674         return x;
00675     return Li3(x).hold();
00676 }
00677 
00678 REGISTER_FUNCTION(Li3, eval_func(Li3_eval).
00679                        latex_name("\\mathrm{Li}_3"));
00680 
00682 // Derivatives of Riemann's Zeta-function  zetaderiv(0,x)==zeta(x)
00684 
00685 static ex zetaderiv_eval(const ex & n, const ex & x)
00686 {
00687     if (n.info(info_flags::numeric)) {
00688         // zetaderiv(0,x) -> zeta(x)
00689         if (n.is_zero())
00690             return zeta(x);
00691     }
00692     
00693     return zetaderiv(n, x).hold();
00694 }
00695 
00696 static ex zetaderiv_deriv(const ex & n, const ex & x, unsigned deriv_param)
00697 {
00698     GINAC_ASSERT(deriv_param<2);
00699     
00700     if (deriv_param==0) {
00701         // d/dn zeta(n,x)
00702         throw(std::logic_error("cannot diff zetaderiv(n,x) with respect to n"));
00703     }
00704     // d/dx psi(n,x)
00705     return zetaderiv(n+1,x);
00706 }
00707 
00708 REGISTER_FUNCTION(zetaderiv, eval_func(zetaderiv_eval).
00709                              derivative_func(zetaderiv_deriv).
00710                              latex_name("\\zeta^\\prime"));
00711 
00713 // factorial
00715 
00716 static ex factorial_evalf(const ex & x)
00717 {
00718     return factorial(x).hold();
00719 }
00720 
00721 static ex factorial_eval(const ex & x)
00722 {
00723     if (is_exactly_a<numeric>(x))
00724         return factorial(ex_to<numeric>(x));
00725     else
00726         return factorial(x).hold();
00727 }
00728 
00729 static void factorial_print_dflt_latex(const ex & x, const print_context & c)
00730 {
00731     if (is_exactly_a<symbol>(x) ||
00732         is_exactly_a<constant>(x) ||
00733         is_exactly_a<function>(x)) {
00734         x.print(c); c.s << "!";
00735     } else {
00736         c.s << "("; x.print(c); c.s << ")!";
00737     }
00738 }
00739 
00740 static ex factorial_conjugate(const ex & x)
00741 {
00742     return factorial(x).hold();
00743 }
00744 
00745 static ex factorial_real_part(const ex & x)
00746 {
00747     return factorial(x).hold();
00748 }
00749 
00750 static ex factorial_imag_part(const ex & x)
00751 {
00752     return 0;
00753 }
00754 
00755 REGISTER_FUNCTION(factorial, eval_func(factorial_eval).
00756                              evalf_func(factorial_evalf).
00757                              print_func<print_dflt>(factorial_print_dflt_latex).
00758                              print_func<print_latex>(factorial_print_dflt_latex).
00759                              conjugate_func(factorial_conjugate).
00760                              real_part_func(factorial_real_part).
00761                              imag_part_func(factorial_imag_part));
00762 
00764 // binomial
00766 
00767 static ex binomial_evalf(const ex & x, const ex & y)
00768 {
00769     return binomial(x, y).hold();
00770 }
00771 
00772 static ex binomial_sym(const ex & x, const numeric & y)
00773 {
00774     if (y.is_integer()) {
00775         if (y.is_nonneg_integer()) {
00776             const unsigned N = y.to_int();
00777             if (N == 0) return _ex1;
00778             if (N == 1) return x;
00779             ex t = x.expand();
00780             for (unsigned i = 2; i <= N; ++i)
00781                 t = (t * (x + i - y - 1)).expand() / i;
00782             return t;
00783         } else
00784             return _ex0;
00785     }
00786 
00787     return binomial(x, y).hold();
00788 }
00789 
00790 static ex binomial_eval(const ex & x, const ex &y)
00791 {
00792     if (is_exactly_a<numeric>(y)) {
00793         if (is_exactly_a<numeric>(x) && ex_to<numeric>(x).is_integer())
00794             return binomial(ex_to<numeric>(x), ex_to<numeric>(y));
00795         else
00796             return binomial_sym(x, ex_to<numeric>(y));
00797     } else
00798         return binomial(x, y).hold();
00799 }
00800 
00801 // At the moment the numeric evaluation of a binomail function always
00802 // gives a real number, but if this would be implemented using the gamma
00803 // function, also complex conjugation should be changed (or rather, deleted).
00804 static ex binomial_conjugate(const ex & x, const ex & y)
00805 {
00806     return binomial(x,y).hold();
00807 }
00808 
00809 static ex binomial_real_part(const ex & x, const ex & y)
00810 {
00811     return binomial(x,y).hold();
00812 }
00813 
00814 static ex binomial_imag_part(const ex & x, const ex & y)
00815 {
00816     return 0;
00817 }
00818 
00819 REGISTER_FUNCTION(binomial, eval_func(binomial_eval).
00820                             evalf_func(binomial_evalf).
00821                             conjugate_func(binomial_conjugate).
00822                             real_part_func(binomial_real_part).
00823                             imag_part_func(binomial_imag_part));
00824 
00826 // Order term function (for truncated power series)
00828 
00829 static ex Order_eval(const ex & x)
00830 {
00831     if (is_exactly_a<numeric>(x)) {
00832         // O(c) -> O(1) or 0
00833         if (!x.is_zero())
00834             return Order(_ex1).hold();
00835         else
00836             return _ex0;
00837     } else if (is_exactly_a<mul>(x)) {
00838         const mul &m = ex_to<mul>(x);
00839         // O(c*expr) -> O(expr)
00840         if (is_exactly_a<numeric>(m.op(m.nops() - 1)))
00841             return Order(x / m.op(m.nops() - 1)).hold();
00842     }
00843     return Order(x).hold();
00844 }
00845 
00846 static ex Order_series(const ex & x, const relational & r, int order, unsigned options)
00847 {
00848     // Just wrap the function into a pseries object
00849     epvector new_seq;
00850     GINAC_ASSERT(is_a<symbol>(r.lhs()));
00851     const symbol &s = ex_to<symbol>(r.lhs());
00852     new_seq.push_back(expair(Order(_ex1), numeric(std::min(x.ldegree(s), order))));
00853     return pseries(r, new_seq);
00854 }
00855 
00856 static ex Order_conjugate(const ex & x)
00857 {
00858     return Order(x).hold();
00859 }
00860 
00861 static ex Order_real_part(const ex & x)
00862 {
00863     return Order(x).hold();
00864 }
00865 
00866 static ex Order_imag_part(const ex & x)
00867 {
00868     if(x.info(info_flags::real))
00869         return 0;
00870     return Order(x).hold();
00871 }
00872 
00873 // Differentiation is handled in function::derivative because of its special requirements
00874 
00875 REGISTER_FUNCTION(Order, eval_func(Order_eval).
00876                          series_func(Order_series).
00877                          latex_name("\\mathcal{O}").
00878                          conjugate_func(Order_conjugate).
00879                          real_part_func(Order_real_part).
00880                          imag_part_func(Order_imag_part));
00881 
00883 // Solve linear system
00885 
00886 ex lsolve(const ex &eqns, const ex &symbols, unsigned options)
00887 {
00888     // solve a system of linear equations
00889     if (eqns.info(info_flags::relation_equal)) {
00890         if (!symbols.info(info_flags::symbol))
00891             throw(std::invalid_argument("lsolve(): 2nd argument must be a symbol"));
00892         const ex sol = lsolve(lst(eqns),lst(symbols));
00893         
00894         GINAC_ASSERT(sol.nops()==1);
00895         GINAC_ASSERT(is_exactly_a<relational>(sol.op(0)));
00896         
00897         return sol.op(0).op(1); // return rhs of first solution
00898     }
00899     
00900     // syntax checks
00901     if (!eqns.info(info_flags::list)) {
00902         throw(std::invalid_argument("lsolve(): 1st argument must be a list or an equation"));
00903     }
00904     for (size_t i=0; i<eqns.nops(); i++) {
00905         if (!eqns.op(i).info(info_flags::relation_equal)) {
00906             throw(std::invalid_argument("lsolve(): 1st argument must be a list of equations"));
00907         }
00908     }
00909     if (!symbols.info(info_flags::list)) {
00910         throw(std::invalid_argument("lsolve(): 2nd argument must be a list or a symbol"));
00911     }
00912     for (size_t i=0; i<symbols.nops(); i++) {
00913         if (!symbols.op(i).info(info_flags::symbol)) {
00914             throw(std::invalid_argument("lsolve(): 2nd argument must be a list of symbols"));
00915         }
00916     }
00917     
00918     // build matrix from equation system
00919     matrix sys(eqns.nops(),symbols.nops());
00920     matrix rhs(eqns.nops(),1);
00921     matrix vars(symbols.nops(),1);
00922     
00923     for (size_t r=0; r<eqns.nops(); r++) {
00924         const ex eq = eqns.op(r).op(0)-eqns.op(r).op(1); // lhs-rhs==0
00925         ex linpart = eq;
00926         for (size_t c=0; c<symbols.nops(); c++) {
00927             const ex co = eq.coeff(ex_to<symbol>(symbols.op(c)),1);
00928             linpart -= co*symbols.op(c);
00929             sys(r,c) = co;
00930         }
00931         linpart = linpart.expand();
00932         rhs(r,0) = -linpart;
00933     }
00934     
00935     // test if system is linear and fill vars matrix
00936     for (size_t i=0; i<symbols.nops(); i++) {
00937         vars(i,0) = symbols.op(i);
00938         if (sys.has(symbols.op(i)))
00939             throw(std::logic_error("lsolve: system is not linear"));
00940         if (rhs.has(symbols.op(i)))
00941             throw(std::logic_error("lsolve: system is not linear"));
00942     }
00943     
00944     matrix solution;
00945     try {
00946         solution = sys.solve(vars,rhs,options);
00947     } catch (const std::runtime_error & e) {
00948         // Probably singular matrix or otherwise overdetermined system:
00949         // It is consistent to return an empty list
00950         return lst();
00951     }
00952     GINAC_ASSERT(solution.cols()==1);
00953     GINAC_ASSERT(solution.rows()==symbols.nops());
00954     
00955     // return list of equations of the form lst(var1==sol1,var2==sol2,...)
00956     lst sollist;
00957     for (size_t i=0; i<symbols.nops(); i++)
00958         sollist.append(symbols.op(i)==solution(i,0));
00959     
00960     return sollist;
00961 }
00962 
00964 // Find real root of f(x) numerically
00966 
00967 const numeric
00968 fsolve(const ex& f_in, const symbol& x, const numeric& x1, const numeric& x2)
00969 {
00970     if (!x1.is_real() || !x2.is_real()) {
00971         throw std::runtime_error("fsolve(): interval not bounded by real numbers");
00972     }
00973     if (x1==x2) {
00974         throw std::runtime_error("fsolve(): vanishing interval");
00975     }
00976     // xx[0] == left interval limit, xx[1] == right interval limit.
00977     // fx[0] == f(xx[0]), fx[1] == f(xx[1]).
00978     // We keep the root bracketed: xx[0]<xx[1] and fx[0]*fx[1]<0.
00979     numeric xx[2] = { x1<x2 ? x1 : x2,
00980                       x1<x2 ? x2 : x1 };
00981     ex f;
00982     if (is_a<relational>(f_in)) {
00983         f = f_in.lhs()-f_in.rhs();
00984     } else {
00985         f = f_in;
00986     }
00987     const ex fx_[2] = { f.subs(x==xx[0]).evalf(),
00988                         f.subs(x==xx[1]).evalf() };
00989     if (!is_a<numeric>(fx_[0]) || !is_a<numeric>(fx_[1])) {
00990         throw std::runtime_error("fsolve(): function does not evaluate numerically");
00991     }
00992     numeric fx[2] = { ex_to<numeric>(fx_[0]),
00993                       ex_to<numeric>(fx_[1]) };
00994     if (!fx[0].is_real() || !fx[1].is_real()) {
00995         throw std::runtime_error("fsolve(): function evaluates to complex values at interval boundaries");
00996     }
00997     if (fx[0]*fx[1]>=0) {
00998         throw std::runtime_error("fsolve(): function does not change sign at interval boundaries");
00999     }
01000 
01001     // The Newton-Raphson method has quadratic convergence!  Simply put, it
01002     // replaces x with x-f(x)/f'(x) at each step.  -f/f' is the delta:
01003     const ex ff = normal(-f/f.diff(x));
01004     int side = 0;  // Start at left interval limit.
01005     numeric xxprev;
01006     numeric fxprev;
01007     do {
01008         xxprev = xx[side];
01009         fxprev = fx[side];
01010         ex dx_ = ff.subs(x == xx[side]).evalf();
01011         if (!is_a<numeric>(dx_))
01012             throw std::runtime_error("fsolve(): function derivative does not evaluate numerically");
01013         xx[side] += ex_to<numeric>(dx_);
01014         // Now check if Newton-Raphson method shot out of the interval 
01015         bool bad_shot = (side == 0 && xx[0] < xxprev) || 
01016                 (side == 1 && xx[1] > xxprev) || xx[0] > xx[1];
01017         if (!bad_shot) {
01018             // Compute f(x) only if new x is inside the interval.
01019             // The function might be difficult to compute numerically
01020             // or even ill defined outside the interval. Also it's
01021             // a small optimization. 
01022             ex f_x = f.subs(x == xx[side]).evalf();
01023             if (!is_a<numeric>(f_x))
01024                 throw std::runtime_error("fsolve(): function does not evaluate numerically");
01025             fx[side] = ex_to<numeric>(f_x);
01026         }
01027         if (bad_shot) {
01028             // Oops, Newton-Raphson method shot out of the interval.
01029             // Restore, and try again with the other side instead!
01030             xx[side] = xxprev;
01031             fx[side] = fxprev;
01032             side = !side;
01033             xxprev = xx[side];
01034             fxprev = fx[side];
01035 
01036             ex dx_ = ff.subs(x == xx[side]).evalf();
01037             if (!is_a<numeric>(dx_))
01038                 throw std::runtime_error("fsolve(): function derivative does not evaluate numerically [2]");
01039             xx[side] += ex_to<numeric>(dx_);
01040 
01041             ex f_x = f.subs(x==xx[side]).evalf();
01042             if (!is_a<numeric>(f_x))
01043                 throw std::runtime_error("fsolve(): function does not evaluate numerically [2]");
01044             fx[side] = ex_to<numeric>(f_x);
01045         }
01046         if ((fx[side]<0 && fx[!side]<0) || (fx[side]>0 && fx[!side]>0)) {
01047             // Oops, the root isn't bracketed any more.
01048             // Restore, and perform a bisection!
01049             xx[side] = xxprev;
01050             fx[side] = fxprev;
01051 
01052             // Ah, the bisection! Bisections converge linearly. Unfortunately,
01053             // they occur pretty often when Newton-Raphson arrives at an x too
01054             // close to the result on one side of the interval and
01055             // f(x-f(x)/f'(x)) turns out to have the same sign as f(x) due to
01056             // precision errors! Recall that this function does not have a
01057             // precision goal as one of its arguments but instead relies on
01058             // x converging to a fixed point. We speed up the (safe but slow)
01059             // bisection method by mixing in a dash of the (unsafer but faster)
01060             // secant method: Instead of splitting the interval at the
01061             // arithmetic mean (bisection), we split it nearer to the root as
01062             // determined by the secant between the values xx[0] and xx[1].
01063             // Don't set the secant_weight to one because that could disturb
01064             // the convergence in some corner cases!
01065             static const double secant_weight = 0.984375;  // == 63/64 < 1
01066             numeric xxmid = (1-secant_weight)*0.5*(xx[0]+xx[1])
01067                 + secant_weight*(xx[0]+fx[0]*(xx[0]-xx[1])/(fx[1]-fx[0]));
01068             ex fxmid_ = f.subs(x == xxmid).evalf();
01069             if (!is_a<numeric>(fxmid_))
01070                 throw std::runtime_error("fsolve(): function does not evaluate numerically [3]");
01071             numeric fxmid = ex_to<numeric>(fxmid_);
01072             if (fxmid.is_zero()) {
01073                 // Luck strikes...
01074                 return xxmid;
01075             }
01076             if ((fxmid<0 && fx[side]>0) || (fxmid>0 && fx[side]<0)) {
01077                 side = !side;
01078             }
01079             xxprev = xx[side];
01080             fxprev = fx[side];
01081             xx[side] = xxmid;
01082             fx[side] = fxmid;
01083         }
01084     } while (xxprev!=xx[side]);
01085     return xxprev;
01086 }
01087 
01088 
01089 /* Force inclusion of functions from inifcns_gamma and inifcns_zeta
01090  * for static lib (so ginsh will see them). */
01091 unsigned force_include_tgamma = tgamma_SERIAL::serial;
01092 unsigned force_include_zeta1 = zeta1_SERIAL::serial;
01093 
01094 } // namespace GiNaC

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.