| 3 |
* Makes the interface to the underlying bignum package available. */ |
* Makes the interface to the underlying bignum package available. */ |
| 4 |
|
|
| 5 |
/* |
/* |
| 6 |
* GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany |
* GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany |
| 7 |
* |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
| 17 |
* |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
*/ |
*/ |
| 22 |
|
|
| 23 |
#ifndef __GINAC_NUMERIC_H__ |
#ifndef __GINAC_NUMERIC_H__ |
| 26 |
#include "basic.h" |
#include "basic.h" |
| 27 |
#include "ex.h" |
#include "ex.h" |
| 28 |
|
|
| 29 |
#include <cln/number.h> |
#include <stdexcept> |
| 30 |
// forward decln of cln::cl_N, since cln/complex_class.h is not included: |
#include <vector> |
| 31 |
namespace cln { class cl_N; } |
|
| 32 |
|
#include <cln/complex.h> |
| 33 |
|
|
| 34 |
#if defined(G__CINTVERSION) && !defined(__MAKECINT__) |
#if defined(G__CINTVERSION) && !defined(__MAKECINT__) |
| 35 |
// Cint @$#$! doesn't like forward declaring classes used for casting operators |
// Cint @$#$! doesn't like forward declaring classes used for casting operators |
| 40 |
|
|
| 41 |
namespace GiNaC { |
namespace GiNaC { |
| 42 |
|
|
| 43 |
#define HASHVALUE_NUMERIC 0x80000001U |
/** Function pointer to implement callbacks in the case 'Digits' gets changed. |
| 44 |
|
* Main purpose of such callbacks is to adjust look-up tables of certain |
| 45 |
|
* functions to the new precision. Parameter contains the signed difference |
| 46 |
|
* between new Digits and old Digits. */ |
| 47 |
|
typedef void (* digits_changed_callback)(long); |
| 48 |
|
|
| 49 |
/** This class is used to instantiate a global singleton object Digits |
/** This class is used to instantiate a global singleton object Digits |
| 50 |
* which behaves just like Maple's Digits. We need an object rather |
* which behaves just like Maple's Digits. We need an object rather |
| 63 |
_numeric_digits& operator=(long prec); |
_numeric_digits& operator=(long prec); |
| 64 |
operator long(); |
operator long(); |
| 65 |
void print(std::ostream &os) const; |
void print(std::ostream &os) const; |
| 66 |
|
void add_callback(digits_changed_callback callback); |
| 67 |
// member variables |
// member variables |
| 68 |
private: |
private: |
| 69 |
long digits; ///< Number of decimal digits |
long digits; ///< Number of decimal digits |
| 70 |
static bool too_late; ///< Already one object present |
static bool too_late; ///< Already one object present |
| 71 |
|
// Holds a list of functions that get called when digits is changed. |
| 72 |
|
std::vector<digits_changed_callback> callbacklist; |
| 73 |
}; |
}; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
/** Exception class thrown when a singularity is encountered. */ |
| 77 |
|
class pole_error : public std::domain_error { |
| 78 |
|
public: |
| 79 |
|
explicit pole_error(const std::string& what_arg, int degree); |
| 80 |
|
int degree() const; |
| 81 |
|
private: |
| 82 |
|
int deg; |
| 83 |
|
}; |
| 84 |
|
|
| 85 |
|
|
| 86 |
/** This class is a wrapper around CLN-numbers within the GiNaC class |
/** This class is a wrapper around CLN-numbers within the GiNaC class |
| 87 |
* hierarchy. Objects of this type may directly be created by the user.*/ |
* hierarchy. Objects of this type may directly be created by the user.*/ |
| 88 |
class numeric : public basic |
class numeric : public basic |
| 91 |
|
|
| 92 |
// member functions |
// member functions |
| 93 |
|
|
| 94 |
// other ctors |
// other constructors |
| 95 |
public: |
public: |
| 96 |
explicit numeric(int i); |
numeric(int i); |
| 97 |
explicit numeric(unsigned int i); |
numeric(unsigned int i); |
| 98 |
explicit numeric(long i); |
numeric(long i); |
| 99 |
explicit numeric(unsigned long i); |
numeric(unsigned long i); |
| 100 |
explicit numeric(long numer, long denom); |
numeric(long numer, long denom); |
| 101 |
explicit numeric(double d); |
numeric(double d); |
| 102 |
explicit numeric(const char *); |
numeric(const char *); |
| 103 |
|
|
| 104 |
// functions overriding virtual functions from bases classes |
// functions overriding virtual functions from base classes |
| 105 |
public: |
public: |
| 106 |
void print(const print_context & c, unsigned level = 0) const; |
unsigned precedence() const {return 30;} |
|
unsigned precedence(void) const {return 30;} |
|
| 107 |
bool info(unsigned inf) const; |
bool info(unsigned inf) const; |
| 108 |
bool has(const ex &other) const; |
bool is_polynomial(const ex & var) const; |
| 109 |
|
int degree(const ex & s) const; |
| 110 |
|
int ldegree(const ex & s) const; |
| 111 |
|
ex coeff(const ex & s, int n = 1) const; |
| 112 |
|
bool has(const ex &other, unsigned options = 0) const; |
| 113 |
ex eval(int level = 0) const; |
ex eval(int level = 0) const; |
| 114 |
ex evalf(int level = 0) const; |
ex evalf(int level = 0) const; |
| 115 |
ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const; |
ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons |
| 116 |
ex to_rational(lst &repl_lst) const; |
ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const; |
| 117 |
numeric integer_content(void) const; |
ex to_rational(exmap & repl) const; |
| 118 |
|
ex to_polynomial(exmap & repl) const; |
| 119 |
|
numeric integer_content() const; |
| 120 |
ex smod(const numeric &xi) const; |
ex smod(const numeric &xi) const; |
| 121 |
numeric max_coefficient(void) const; |
numeric max_coefficient() const; |
| 122 |
|
ex conjugate() const; |
| 123 |
protected: |
protected: |
| 124 |
/** Implementation of ex::diff for a numeric always returns 0. |
/** Implementation of ex::diff for a numeric always returns 0. |
| 125 |
* @see ex::diff */ |
* @see ex::diff */ |
| 126 |
ex derivative(const symbol &s) const { return _ex0(); } |
ex derivative(const symbol &s) const { return 0; } |
| 127 |
bool is_equal_same_type(const basic &other) const; |
bool is_equal_same_type(const basic &other) const; |
| 128 |
unsigned calchash(void) const; |
unsigned calchash() const; |
| 129 |
|
|
| 130 |
// new virtual functions which can be overridden by derived classes |
// new virtual functions which can be overridden by derived classes |
| 131 |
// (none) |
// (none) |
| 148 |
const numeric & operator=(unsigned long i); |
const numeric & operator=(unsigned long i); |
| 149 |
const numeric & operator=(double d); |
const numeric & operator=(double d); |
| 150 |
const numeric & operator=(const char *s); |
const numeric & operator=(const char *s); |
| 151 |
const numeric inverse(void) const; |
const numeric inverse() const; |
| 152 |
int csgn(void) const; |
numeric step() const; |
| 153 |
|
int csgn() const; |
| 154 |
int compare(const numeric &other) const; |
int compare(const numeric &other) const; |
| 155 |
bool is_equal(const numeric &other) const; |
bool is_equal(const numeric &other) const; |
| 156 |
bool is_zero(void) const; |
bool is_zero() const; |
| 157 |
bool is_positive(void) const; |
bool is_positive() const; |
| 158 |
bool is_negative(void) const; |
bool is_negative() const; |
| 159 |
bool is_integer(void) const; |
bool is_integer() const; |
| 160 |
bool is_pos_integer(void) const; |
bool is_pos_integer() const; |
| 161 |
bool is_nonneg_integer(void) const; |
bool is_nonneg_integer() const; |
| 162 |
bool is_even(void) const; |
bool is_even() const; |
| 163 |
bool is_odd(void) const; |
bool is_odd() const; |
| 164 |
bool is_prime(void) const; |
bool is_prime() const; |
| 165 |
bool is_rational(void) const; |
bool is_rational() const; |
| 166 |
bool is_real(void) const; |
bool is_real() const; |
| 167 |
bool is_cinteger(void) const; |
bool is_cinteger() const; |
| 168 |
bool is_crational(void) const; |
bool is_crational() const; |
| 169 |
bool operator==(const numeric &other) const; |
bool operator==(const numeric &other) const; |
| 170 |
bool operator!=(const numeric &other) const; |
bool operator!=(const numeric &other) const; |
| 171 |
bool operator<(const numeric &other) const; |
bool operator<(const numeric &other) const; |
| 172 |
bool operator<=(const numeric &other) const; |
bool operator<=(const numeric &other) const; |
| 173 |
bool operator>(const numeric &other) const; |
bool operator>(const numeric &other) const; |
| 174 |
bool operator>=(const numeric &other) const; |
bool operator>=(const numeric &other) const; |
| 175 |
int to_int(void) const; |
int to_int() const; |
| 176 |
long to_long(void) const; |
long to_long() const; |
| 177 |
double to_double(void) const; |
double to_double() const; |
| 178 |
cln::cl_N to_cl_N(void) const; |
cln::cl_N to_cl_N() const; |
| 179 |
const numeric real(void) const; |
const numeric real() const; |
| 180 |
const numeric imag(void) const; |
const numeric imag() const; |
| 181 |
const numeric numer(void) const; |
const numeric numer() const; |
| 182 |
const numeric denom(void) const; |
const numeric denom() const; |
| 183 |
int int_length(void) const; |
int int_length() const; |
| 184 |
// converting routines for interfacing with CLN: |
// converting routines for interfacing with CLN: |
| 185 |
numeric(const cln::cl_N &z); |
numeric(const cln::cl_N &z); |
| 186 |
|
|
| 187 |
|
protected: |
| 188 |
|
void print_numeric(const print_context & c, const char *par_open, const char *par_close, const char *imag_sym, const char *mul_sym, unsigned level) const; |
| 189 |
|
void do_print(const print_context & c, unsigned level) const; |
| 190 |
|
void do_print_latex(const print_latex & c, unsigned level) const; |
| 191 |
|
void do_print_csrc(const print_csrc & c, unsigned level) const; |
| 192 |
|
void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const; |
| 193 |
|
void do_print_tree(const print_tree & c, unsigned level) const; |
| 194 |
|
void do_print_python_repr(const print_python_repr & c, unsigned level) const; |
| 195 |
|
|
| 196 |
// member variables |
// member variables |
| 197 |
|
|
| 198 |
protected: |
protected: |
| 199 |
cln::cl_number value; |
cln::cl_N value; |
| 200 |
}; |
}; |
| 201 |
|
|
| 202 |
|
|
| 203 |
// global constants |
// global constants |
| 204 |
|
|
| 205 |
extern const numeric I; |
extern const numeric I; |
| 206 |
extern _numeric_digits Digits; |
extern _numeric_digits Digits; |
| 207 |
|
|
|
// deprecated macro, for internal use only |
|
|
#define is_a_numeric_hash(x) ((x)&0x80000000U) |
|
|
|
|
| 208 |
// global functions |
// global functions |
| 209 |
|
|
| 210 |
const numeric exp(const numeric &x); |
//-- to go ... |
|
const numeric log(const numeric &x); |
|
|
const numeric sin(const numeric &x); |
|
|
const numeric cos(const numeric &x); |
|
|
const numeric tan(const numeric &x); |
|
|
const numeric asin(const numeric &x); |
|
|
const numeric acos(const numeric &x); |
|
|
const numeric atan(const numeric &x); |
|
|
const numeric atan(const numeric &y, const numeric &x); |
|
|
const numeric sinh(const numeric &x); |
|
|
const numeric cosh(const numeric &x); |
|
|
const numeric tanh(const numeric &x); |
|
|
const numeric asinh(const numeric &x); |
|
|
const numeric acosh(const numeric &x); |
|
|
const numeric atanh(const numeric &x); |
|
| 211 |
const numeric Li2(const numeric &x); |
const numeric Li2(const numeric &x); |
| 212 |
const numeric zeta(const numeric &x); |
const numeric zeta(const numeric &x); |
|
const numeric lgamma(const numeric &x); |
|
|
const numeric tgamma(const numeric &x); |
|
|
const numeric psi(const numeric &x); |
|
|
const numeric psi(const numeric &n, const numeric &x); |
|
|
const numeric factorial(const numeric &n); |
|
| 213 |
const numeric doublefactorial(const numeric &n); |
const numeric doublefactorial(const numeric &n); |
|
const numeric binomial(const numeric &n, const numeric &k); |
|
| 214 |
const numeric bernoulli(const numeric &n); |
const numeric bernoulli(const numeric &n); |
| 215 |
const numeric fibonacci(const numeric &n); |
const numeric fibonacci(const numeric &n); |
| 216 |
const numeric abs(const numeric &x); |
//-- |
| 217 |
const numeric isqrt(const numeric &x); |
const numeric isqrt(const numeric &x); |
| 218 |
const numeric sqrt(const numeric &x); |
const numeric sqrt(const numeric &x); |
|
const numeric abs(const numeric &x); |
|
| 219 |
const numeric mod(const numeric &a, const numeric &b); |
const numeric mod(const numeric &a, const numeric &b); |
| 220 |
const numeric smod(const numeric &a, const numeric &b); |
const numeric smod(const numeric &a, const numeric &b); |
| 221 |
const numeric irem(const numeric &a, const numeric &b); |
const numeric irem(const numeric &a, const numeric &b); |
| 232 |
inline const numeric inverse(const numeric &x) |
inline const numeric inverse(const numeric &x) |
| 233 |
{ return x.inverse(); } |
{ return x.inverse(); } |
| 234 |
|
|
|
inline int csgn(const numeric &x) |
|
|
{ return x.csgn(); } |
|
|
|
|
| 235 |
inline bool is_zero(const numeric &x) |
inline bool is_zero(const numeric &x) |
| 236 |
{ return x.is_zero(); } |
{ return x.is_zero(); } |
| 237 |
|
|
| 291 |
|
|
| 292 |
// numeric evaluation functions for class constant objects: |
// numeric evaluation functions for class constant objects: |
| 293 |
|
|
| 294 |
ex PiEvalf(void); |
ex PiEvalf(); |
| 295 |
ex EulerEvalf(void); |
ex EulerEvalf(); |
| 296 |
ex CatalanEvalf(void); |
ex CatalanEvalf(); |
|
|
|
| 297 |
|
|
|
// utility functions |
|
|
inline const numeric &ex_to_numeric(const ex &e) |
|
|
{ |
|
|
return static_cast<const numeric &>(*e.bp); |
|
|
} |
|
| 298 |
|
|
| 299 |
} // namespace GiNaC |
} // namespace GiNaC |
| 300 |
|
|