* of special functions or implement the interface to the bignum package. */
/*
- * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
//////////
/** default ctor. Numerically it initializes to an integer zero. */
-numeric::numeric() : basic(TINFO_numeric)
+numeric::numeric() : basic(&numeric::tinfo_static)
{
value = cln::cl_I(0);
setflag(status_flags::evaluated | status_flags::expanded);
// public
-numeric::numeric(int i) : basic(TINFO_numeric)
+numeric::numeric(int i) : basic(&numeric::tinfo_static)
{
// Not the whole int-range is available if we don't cast to long
// first. This is due to the behaviour of the cl_I-ctor, which
// emphasizes efficiency. However, if the integer is small enough
// we save space and dereferences by using an immediate type.
// (C.f. <cln/object.h>)
+ // The #if clause prevents compiler warnings on 64bit machines where the
+ // comparision is always true.
+#if cl_value_len >= 32
+ value = cln::cl_I(i);
+#else
if (i < (1L << (cl_value_len-1)) && i >= -(1L << (cl_value_len-1)))
value = cln::cl_I(i);
else
value = cln::cl_I(static_cast<long>(i));
+#endif
setflag(status_flags::evaluated | status_flags::expanded);
}
-numeric::numeric(unsigned int i) : basic(TINFO_numeric)
+numeric::numeric(unsigned int i) : basic(&numeric::tinfo_static)
{
// Not the whole uint-range is available if we don't cast to ulong
// first. This is due to the behaviour of the cl_I-ctor, which
// 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)))
+ // The #if clause prevents compiler warnings on 64bit machines where the
+ // comparision is always true.
+#if cl_value_len >= 32
+ value = cln::cl_I(i);
+#else
+ if (i < (1UL << (cl_value_len-1)))
value = cln::cl_I(i);
else
value = cln::cl_I(static_cast<unsigned long>(i));
+#endif
setflag(status_flags::evaluated | status_flags::expanded);
}
-numeric::numeric(long i) : basic(TINFO_numeric)
+numeric::numeric(long i) : basic(&numeric::tinfo_static)
{
value = cln::cl_I(i);
setflag(status_flags::evaluated | status_flags::expanded);
}
-numeric::numeric(unsigned long i) : basic(TINFO_numeric)
+numeric::numeric(unsigned long i) : basic(&numeric::tinfo_static)
{
value = cln::cl_I(i);
setflag(status_flags::evaluated | status_flags::expanded);
/** Constructor for rational numerics a/b.
*
* @exception overflow_error (division by zero) */
-numeric::numeric(long numer, long denom) : basic(TINFO_numeric)
+numeric::numeric(long numer, long denom) : basic(&numeric::tinfo_static)
{
if (!denom)
throw std::overflow_error("division by zero");
}
-numeric::numeric(double d) : basic(TINFO_numeric)
+numeric::numeric(double d) : basic(&numeric::tinfo_static)
{
// We really want to explicitly use the type cl_LF instead of the
// more general cl_F, since that would give us a cl_DF only which
/** ctor from C-style string. It also accepts complex numbers in GiNaC
* notation like "2+5*I". */
-numeric::numeric(const char *s) : basic(TINFO_numeric)
+numeric::numeric(const char *s) : basic(&numeric::tinfo_static)
{
cln::cl_N ctorval = 0;
// parse complex numbers (functional but not completely safe, unfortunately
/** Ctor from CLN types. This is for the initiated user or internal use
* only. */
-numeric::numeric(const cln::cl_N &z) : basic(TINFO_numeric)
+numeric::numeric(const cln::cl_N &z) : basic(&numeric::tinfo_static)
{
value = z;
setflag(status_flags::evaluated | status_flags::expanded);
return false;
}
+bool numeric::is_polynomial(const ex & var) const
+{
+ return true;
+}
+
int numeric::degree(const ex & s) const
{
return 0;
* results: (2+I).has(-2) -> true. But this is consistent, since we also
* would like to have (-2+I).has(2) -> true and we want to think about the
* sign as a multiplicative factor. */
-bool numeric::has(const ex &other) const
+bool numeric::has(const ex &other, unsigned options) const
{
if (!is_exactly_a<numeric>(other))
return false;
return numeric(cln::conjugate(this->value));
}
+ex numeric::real_part() const
+{
+ return numeric(cln::realpart(value));
+}
+
+ex numeric::imag_part() const
+{
+ return numeric(cln::imagpart(value));
+}
+
// protected
int numeric::compare_same_type(const basic &other) const
return numeric(cln::recip(value));
}
+/** Return the step function of a numeric. The imaginary part of it is
+ * ignored because the step function is generally considered real but
+ * a numeric may develop a small imaginary part due to rounding errors.
+ */
+numeric numeric::step() const
+{ cln::cl_R r = cln::realpart(value);
+ if(cln::zerop(r))
+ return numeric(1,2);
+ if(cln::plusp(r))
+ return 1;
+ return 0;
+}
/** Return the complex half-plane (left or right) in which the number lies.
* csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
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];
}
}
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;
}
}
+/** 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);