]> www.ginac.de Git - ginac.git/blobdiff - ginac/inifcns.cpp
Add step function to GiNaCs built-in functions.
[ginac.git] / ginac / inifcns.cpp
index a9ad5ce5223ec84c2e0b0d4e55813d01a8d3a07a..830b1bff41ff556816e6c398add542179ad6f7e2 100644 (file)
@@ -107,13 +107,97 @@ static ex abs_conjugate(const ex & arg)
        return abs(arg);
 }
 
+static ex abs_power(const ex & arg, const ex & exp)
+{
+       if (arg.is_equal(arg.conjugate()) && is_a<numeric>(exp) && ex_to<numeric>(exp).is_even())
+               return power(arg, exp);
+       else
+               return power(abs(arg), exp).hold();
+}
+
 REGISTER_FUNCTION(abs, eval_func(abs_eval).
                        evalf_func(abs_evalf).
                        print_func<print_latex>(abs_print_latex).
                        print_func<print_csrc_float>(abs_print_csrc_float).
                        print_func<print_csrc_double>(abs_print_csrc_float).
-                       conjugate_func(abs_conjugate));
+                       conjugate_func(abs_conjugate).
+                       power_func(abs_power));
+
+//////////
+// Step function
+//////////
+
+static ex step_evalf(const ex & arg)
+{
+       if (is_exactly_a<numeric>(arg))
+               return step(ex_to<numeric>(arg));
+       
+       return step(arg).hold();
+}
+
+static ex step_eval(const ex & arg)
+{
+       if (is_exactly_a<numeric>(arg))
+               return step(ex_to<numeric>(arg));
+       
+       else if (is_exactly_a<mul>(arg) &&
+                is_exactly_a<numeric>(arg.op(arg.nops()-1))) {
+               numeric oc = ex_to<numeric>(arg.op(arg.nops()-1));
+               if (oc.is_real()) {
+                       if (oc > 0)
+                               // step(42*x) -> step(x)
+                               return step(arg/oc).hold();
+                       else
+                               // step(-42*x) -> step(-x)
+                               return step(-arg/oc).hold();
+               }
+               if (oc.real().is_zero()) {
+                       if (oc.imag() > 0)
+                               // step(42*I*x) -> step(I*x)
+                               return step(I*arg/oc).hold();
+                       else
+                               // step(-42*I*x) -> step(-I*x)
+                               return step(-I*arg/oc).hold();
+               }
+       }
+       
+       return step(arg).hold();
+}
+
+static ex step_series(const ex & arg,
+                      const relational & rel,
+                      int order,
+                      unsigned options)
+{
+       const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
+       if (arg_pt.info(info_flags::numeric)
+           && ex_to<numeric>(arg_pt).real().is_zero()
+           && !(options & series_options::suppress_branchcut))
+               throw (std::domain_error("step_series(): on imaginary axis"));
+       
+       epvector seq;
+       seq.push_back(expair(step(arg_pt), _ex0));
+       return pseries(rel,seq);
+}
 
+static ex step_power(const ex & arg, const ex & exp)
+{
+       if (exp.info(info_flags::positive))
+               return step(arg);
+       
+       return power(step(arg), exp).hold();
+}
+
+static ex step_conjugate(const ex& arg)
+{
+       return step(arg);
+}
+
+REGISTER_FUNCTION(step, eval_func(step_eval).
+                        evalf_func(step_evalf).
+                        series_func(step_series).
+                        conjugate_func(step_conjugate).
+                        power_func(step_power));
 
 //////////
 // Complex sign
@@ -668,7 +752,7 @@ ex lsolve(const ex &eqns, const ex &symbols, unsigned options)
 //////////
 
 const numeric
-fsolve(const ex& f, const symbol& x, const numeric& x1, const numeric& x2)
+fsolve(const ex& f_in, const symbol& x, const numeric& x1, const numeric& x2)
 {
        if (!x1.is_real() || !x2.is_real()) {
                throw std::runtime_error("fsolve(): interval not bounded by real numbers");
@@ -681,6 +765,12 @@ fsolve(const ex& f, const symbol& x, const numeric& x1, const numeric& x2)
        // We keep the root bracketed: xx[0]<xx[1] and fx[0]*fx[1]<0.
        numeric xx[2] = { x1<x2 ? x1 : x2,
                          x1<x2 ? x2 : x1 };
+       ex f;
+       if (is_a<relational>(f_in)) {
+               f = f_in.lhs()-f_in.rhs();
+       } else {
+               f = f_in;
+       }
        const ex fx_[2] = { f.subs(x==xx[0]).evalf(),
                            f.subs(x==xx[1]).evalf() };
        if (!is_a<numeric>(fx_[0]) || !is_a<numeric>(fx_[1])) {
@@ -736,7 +826,7 @@ fsolve(const ex& f, const symbol& x, const numeric& x1, const numeric& x2)
                        // determined by the secant between the values xx[0] and xx[1].
                        // Don't set the secant_weight to one because that could disturb
                        // the convergence in some corner cases!
-                       static const double secant_weight = 0.96875;  // == 31/32 < 1
+                       static const double secant_weight = 0.984375;  // == 63/64 < 1
                        numeric xxmid = (1-secant_weight)*0.5*(xx[0]+xx[1])
                            + secant_weight*(xx[0]+fx[0]*(xx[0]-xx[1])/(fx[1]-fx[0]));
                        numeric fxmid = ex_to<numeric>(f.subs(x==xxmid).evalf());