]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
* Simplify bernoulli. The orginal was somewhat overoptimized.
[ginac.git] / ginac / numeric.cpp
index 963bc74dcff8d5fde3f46e2564da8a82c8f20690..965368625a9343dfe59ee2f82aa7803f66561a55 100644 (file)
@@ -107,7 +107,7 @@ numeric::numeric(unsigned int i) : basic(TINFO_numeric)
        // emphasizes efficiency.  However, if the integer is small enough
        // we save space and dereferences by using an immediate type.
        // (C.f. <cln/object.h>)
-       if (i < (1U << (cl_value_len-1)))
+       if (i < (1UL << (cl_value_len-1)))
                value = cln::cl_I(i);
        else
                value = cln::cl_I(static_cast<unsigned long>(i));
@@ -631,15 +631,22 @@ bool numeric::has(const ex &other) const
        const numeric &o = ex_to<numeric>(other);
        if (this->is_equal(o) || this->is_equal(-o))
                return true;
-       if (o.imag().is_zero())  // e.g. scan for 3 in -3*I
-               return (this->real().is_equal(o) || this->imag().is_equal(o) ||
-                       this->real().is_equal(-o) || this->imag().is_equal(-o));
+       if (o.imag().is_zero()) {   // e.g. scan for 3 in -3*I
+               if (!this->real().is_equal(*_num0_p))
+                       if (this->real().is_equal(o) || this->real().is_equal(-o))
+                               return true;
+               if (!this->imag().is_equal(*_num0_p))
+                       if (this->imag().is_equal(o) || this->imag().is_equal(-o))
+                               return true;
+               return false;
+       }
        else {
                if (o.is_equal(I))  // e.g scan for I in 42*I
                        return !this->is_real();
                if (o.real().is_zero())  // e.g. scan for 2*I in 2*I+1
-                       return (this->real().has(o*I) || this->imag().has(o*I) ||
-                               this->real().has(-o*I) || this->imag().has(-o*I));
+                       if (!this->imag().is_equal(*_num0_p))
+                               if (this->imag().is_equal(o*I) || this->imag().is_equal(-o*I))
+                                       return true;
        }
        return false;
 }
@@ -1701,20 +1708,20 @@ const numeric bernoulli(const numeric &nn)
        results.reserve(n/2);
        for (unsigned p=next_r; p<=n;  p+=2) {
                cln::cl_I  c = 1;  // seed for binonmial coefficients
-               cln::cl_RA b = cln::cl_RA(1-p)/2;
-               const unsigned p3 = p+3;
-               const unsigned pm = p-2;
-               unsigned i, k, p_2;
-               // test if intermediate unsigned int can be represented by immediate
-               // objects by CLN (i.e. < 2^29 for 32 Bit machines, see <cln/object.h>)
+               cln::cl_RA b = cln::cl_RA(p-1)/-2;
+               // The CLN manual says: "The conversion from `unsigned int' works only
+               // if the argument is < 2^29" (This is for 32 Bit machines. More
+               // generally, cl_value_len is the limiting exponent of 2. We must make
+               // sure that no intermediates are created which exceed this value. The
+               // largest intermediate is (p+3-2*k)*(p/2-k+1) <= (p^2+p)/2.
                if (p < (1UL<<cl_value_len/2)) {
-                       for (i=2, k=1, p_2=p/2; i<=pm; i+=2, ++k, --p_2) {
-                               c = cln::exquo(c * ((p3-i) * p_2), (i-1)*k);
+                       for (unsigned k=1; k<=p/2-1; ++k) {
+                               c = cln::exquo(c * ((p+3-2*k) * (p/2-k+1)), (2*k-1)*k);
                                b = b + c*results[k-1];
                        }
                } else {
-                       for (i=2, k=1, p_2=p/2; i<=pm; i+=2, ++k, --p_2) {
-                               c = cln::exquo((c * (p3-i)) * p_2, cln::cl_I(i-1)*k);
+                       for (unsigned k=1; k<=p/2-1; ++k) {
+                               c = cln::exquo((c * (p+3-2*k)) * (p/2-k+1), cln::cl_I(2*k-1)*k);
                                b = b + c*results[k-1];
                        }
                }
@@ -1992,14 +1999,25 @@ _numeric_digits::_numeric_digits()
                throw(std::runtime_error("I told you not to do instantiate me!"));
        too_late = true;
        cln::default_float_format = cln::float_format(17);
+
+       // add callbacks for built-in functions
+       // like ... add_callback(Li_lookuptable);
 }
 
 
 /** Assign a native long to global Digits object. */
 _numeric_digits& _numeric_digits::operator=(long prec)
 {
+       long digitsdiff = prec - digits;
        digits = prec;
-       cln::default_float_format = cln::float_format(prec); 
+       cln::default_float_format = cln::float_format(prec);
+
+       // call registered callbacks
+       std::vector<digits_changed_callback>::const_iterator it = callbacklist.begin(), end = callbacklist.end();
+       for (; it != end; ++it) {
+               (*it)(digitsdiff);
+       }
+
        return *this;
 }
 
@@ -2019,6 +2037,13 @@ void _numeric_digits::print(std::ostream &os) const
 }
 
 
+/** Add a new callback function. */
+void _numeric_digits::add_callback(digits_changed_callback callback)
+{
+       callbacklist.push_back(callback);
+}
+
+
 std::ostream& operator<<(std::ostream &os, const _numeric_digits &e)
 {
        e.print(os);