📄 gmpxx.h
字号:
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_ui(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, signed long int l)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_si(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, signed long int l, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_si(f, l);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, mpf_srcptr g, double d)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_d(f, d);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
static void eval(mpf_ptr f, double d, mpf_srcptr g)
{
mpf_t temp;
mpf_init2(temp, mpf_get_prec(f));
mpf_mul(temp, g, g);
mpf_set_d(f, d);
mpf_mul(f, f, f);
mpf_add(f, f, temp);
mpf_sqrt(f, f);
mpf_clear(temp);
}
#ifdef __MPFR_H
static void eval(mpfr_ptr f, mpfr_srcptr g, mpfr_srcptr h, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_mul(f, h, h, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, mpfr_srcptr g, unsigned long int l,
mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_ui(f, l, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, unsigned long int l, mpfr_srcptr g,
mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_ui(f, l, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, mpfr_srcptr g, signed long int l,
mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_si(f, l, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, signed long int l, mpfr_srcptr g,
mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_si(f, l, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, mpfr_srcptr g, double d, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_d(f, d, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
static void eval(mpfr_ptr f, double d, mpfr_srcptr g, mp_rnd_t mode)
{
mpfr_t temp;
mpfr_init2(temp, mpfr_get_prec(f));
mpfr_mul(temp, g, g, mode);
mpfr_set_d(f, d, mode);
mpfr_mul(f, f, f, mode);
mpfr_add(f, f, temp, mode);
mpfr_sqrt(f, f, mode);
mpfr_clear(temp);
}
#endif
};
struct __gmp_sgn_function
{
static int eval(mpz_srcptr z) { return mpz_sgn(z); }
static int eval(mpq_srcptr q) { return mpq_sgn(q); }
static int eval(mpf_srcptr f) { return mpf_sgn(f); }
#ifdef __MPFR_H
static int eval(mpfr_srcptr f) { return mpfr_cmp_ui(f, 0); }
#endif
};
struct __gmp_cmp_function
{
static int eval(mpz_srcptr z, mpz_srcptr w) { return mpz_cmp(z, w); }
static int eval(mpq_srcptr q, mpq_srcptr r) { return mpq_cmp(q, r); }
static int eval(mpf_srcptr f, mpf_srcptr g) { return mpf_cmp(f, g); }
#ifdef __MPFR_H
static int eval(mpfr_srcptr f, mpfr_srcptr g) { return mpfr_cmp(f, g); }
#endif
};
struct __gmp_rand_function
{
static void eval(mpz_ptr z, gmp_randstate_t s, unsigned long int l)
{ mpz_urandomb(z, s, l); }
static void eval(mpz_ptr z, gmp_randstate_t s, mpz_srcptr w)
{ mpz_urandomm(z, s, w); }
static void eval(mpf_ptr f, gmp_randstate_t s, unsigned long int prec)
{ mpf_urandomb(f, s, prec); }
};
/**************** Auxiliary classes ****************/
/* this is the same as gmp_allocated_string in gmp-impl.h
since gmp-impl.h is not publicly available, I redefine it here
I use a different name to avoid possible clashes */
struct __gmp_alloc_cstring
{
char *str;
__gmp_alloc_cstring(char *s) { str = s; }
~__gmp_alloc_cstring() { __gmp_free_func(str, strlen(str)+1); }
};
template <class T, class U>
class __gmp_expr;
template <class T>
struct __gmp_resolve_ref
{
typedef T ref_type;
};
template <class T, class U>
struct __gmp_resolve_ref<__gmp_expr<T, U> >
{
typedef const __gmp_expr<T, U> & ref_type;
};
template <class T, class Op>
struct __gmp_unary_expr
{
typename __gmp_resolve_ref<T>::ref_type val;
__gmp_unary_expr(const T &v) : val(v) { }
private:
__gmp_unary_expr();
};
template <class T, class U, class Op>
struct __gmp_binary_expr
{
typename __gmp_resolve_ref<T>::ref_type val1;
typename __gmp_resolve_ref<U>::ref_type val2;
__gmp_binary_expr(const T &v1, const U &v2) : val1(v1), val2(v2) { }
private:
__gmp_binary_expr();
};
class __gmpz_value { };
class __gmpzref_value { };
class __gmpq_value { };
class __gmpf_value { };
template <class T, class U>
void __gmp_set_expr(mpz_ptr, const __gmp_expr<T, U> &);
template <class T, class U>
void __gmp_set_expr(mpq_ptr, const __gmp_expr<T, U> &);
template <class T, class U>
void __gmp_set_expr(mpf_ptr, const __gmp_expr<T, U> &);
/**************** Macros for in-class declarations ****************/
/* This is just repetitive code that is easier to maintain if it's written
only once */
#define __GMPZZ_DECLARE_COMPOUND_OPERATOR(fun) \
template <class T, class U> \
__gmp_expr<__gmpz_value, __gmpz_value> & fun(const __gmp_expr<T, U> &);
#define __GMPZN_DECLARE_COMPOUND_OPERATOR(fun) \
__gmp_expr & fun(signed char); \
__gmp_expr & fun(unsigned char); \
__gmp_expr & fun(signed int); \
__gmp_expr & fun(unsigned int); \
__gmp_expr & fun(signed short int); \
__gmp_expr & fun(unsigned short int); \
__gmp_expr & fun(signed long int); \
__gmp_expr & fun(unsigned long int); \
__gmp_expr & fun(float); \
__gmp_expr & fun(double); \
__gmp_expr & fun(long double);
#define __GMPZ_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPZZ_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPZN_DECLARE_COMPOUND_OPERATOR(fun)
#define __GMPZ_DECLARE_COMPOUND_OPERATOR_UI(fun) \
__gmp_expr & fun(unsigned long int);
#define __GMPZ_DECLARE_INCREMENT_OPERATOR(fun) \
inline __gmp_expr & fun(); \
inline __gmp_expr fun(int);
#define __GMPZRR_DECLARE_COMPOUND_OPERATOR(fun) \
template <class T, class U> \
__gmp_expr<__gmpz_value, __gmpzref_value> & fun(const __gmp_expr<T, U> &);
#define __GMPZRN_DECLARE_COMPOUND_OPERATOR(fun) \
__gmp_expr & fun(signed char); \
__gmp_expr & fun(unsigned char); \
__gmp_expr & fun(signed int); \
__gmp_expr & fun(unsigned int); \
__gmp_expr & fun(signed short int); \
__gmp_expr & fun(unsigned short int); \
__gmp_expr & fun(signed long int); \
__gmp_expr & fun(unsigned long int); \
__gmp_expr & fun(float); \
__gmp_expr & fun(double); \
__gmp_expr & fun(long double);
#define __GMPZR_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPZRR_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPZRN_DECLARE_COMPOUND_OPERATOR(fun)
#define __GMPZR_DECLARE_COMPOUND_OPERATOR_UI(fun) \
__gmp_expr & fun(unsigned long int);
#define __GMPZR_DECLARE_INCREMENT_OPERATOR(fun) \
inline __gmp_expr & fun(); \
inline mpz_class fun(int);
#define __GMPQQ_DECLARE_COMPOUND_OPERATOR(fun) \
template <class T, class U> \
__gmp_expr<__gmpq_value, __gmpq_value> & fun(const __gmp_expr<T, U> &);
#define __GMPQN_DECLARE_COMPOUND_OPERATOR(fun) \
__gmp_expr & fun(signed char); \
__gmp_expr & fun(unsigned char); \
__gmp_expr & fun(signed int); \
__gmp_expr & fun(unsigned int); \
__gmp_expr & fun(signed short int); \
__gmp_expr & fun(unsigned short int); \
__gmp_expr & fun(signed long int); \
__gmp_expr & fun(unsigned long int); \
__gmp_expr & fun(float); \
__gmp_expr & fun(double); \
__gmp_expr & fun(long double);
#define __GMPQ_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPQQ_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPQN_DECLARE_COMPOUND_OPERATOR(fun)
#define __GMPQ_DECLARE_COMPOUND_OPERATOR_UI(fun) \
__gmp_expr & fun(unsigned long int);
#define __GMPQ_DECLARE_INCREMENT_OPERATOR(fun) \
inline __gmp_expr & fun(); \
inline __gmp_expr fun(int);
#define __GMPFF_DECLARE_COMPOUND_OPERATOR(fun) \
template <class T, class U> \
__gmp_expr<__gmpf_value, __gmpf_value> & fun(const __gmp_expr<T, U> &);
#define __GMPFN_DECLARE_COMPOUND_OPERATOR(fun) \
__gmp_expr & fun(signed char); \
__gmp_expr & fun(unsigned char); \
__gmp_expr & fun(signed int); \
__gmp_expr & fun(unsigned int); \
__gmp_expr & fun(signed short int); \
__gmp_expr & fun(unsigned short int); \
__gmp_expr & fun(signed long int); \
__gmp_expr & fun(unsigned long int); \
__gmp_expr & fun(float); \
__gmp_expr & fun(double); \
__gmp_expr & fun(long double);
#define __GMPF_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPFF_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPFN_DECLARE_COMPOUND_OPERATOR(fun)
#define __GMPF_DECLARE_COMPOUND_OPERATOR_UI(fun) \
__gmp_expr & fun(unsigned long int);
#define __GMPF_DECLARE_INCREMENT_OPERATOR(fun) \
inline __gmp_expr & fun(); \
inline __gmp_expr fun(int);
/**************** mpz_class -- wrapper for mpz_t ****************/
template <>
class __gmp_expr<__gmpz_value, __gmpz_value>
{
private:
mpz_t mp;
public:
unsigned long int get_prec() const { return mpf_get_default_prec(); }
// constructors and destructor
__gmp_expr() { mpz_init(mp); }
__gmp_expr(const __gmp_expr &z) { mpz_init_set(mp, z.mp); }
template <class T, class U>
__gmp_expr(const __gmp_expr<T, U> &expr)
{ mpz_init(mp); __gmp_set_expr(mp, expr); }
__gmp_expr(signed char c) { mpz_init_set_si(mp, c); }
__gmp_expr(unsigned char c) { mpz_init_set_ui(mp, c); }
__gmp_expr(signed int i) { mpz_init_set_si(mp, i); }
__gmp_expr(unsigned int i) { mpz_init_set_ui(mp, i); }
__gmp_expr(signed short int s) { mpz_init_set_si(mp, s); }
__gmp_expr(unsigned short int s) { mpz_init_set_ui(mp, s); }
__gmp_expr(signed long int l) { mpz_init_set_si(mp, l); }
__gmp_expr(unsigned long int l) { mpz_init_set_ui(mp, l); }
__gmp_expr(float f) { mpz_init_set_d(mp, f); }
__gmp_expr(double d) { mpz_init_set_d(mp, d); }
// __gmp_expr(long double ld) { mpz_init_set_d(mp, ld); }
explicit __gmp_expr(const char *s)
{ mpz_init_set_str(mp, s, 0); }
__gmp_expr(const char *s, int base)
{ mpz_init_set_str(mp, s, base); }
#ifndef _WIN32_WCE
explicit __gmp_expr(const std::string &s)
{ mpz_init_set_str(mp, s.c_str(), 0); }
__gmp_expr(const std::string &s, int base)
{ mpz_init_set_str(mp, s.c_str(), base); }
#endif
explicit __gmp_expr(mpz_srcptr z) { mpz_init_set(mp, z); }
~__gmp_expr() { mpz_clear(mp); }
// assignment operators
__gmp_expr & operator=(const __gmp_expr &z)
{ mpz_set(mp, z.mp); return *this; }
template <class T, class U>
__gmp_expr<__gmpz_value, __gmpz_value> & operator=
(const __gmp_expr<T, U> &expr)
{ __gmp_set_expr(mp, expr); return *this; }
__gmp_expr & operator=(signed char c) { mpz_set_si(mp, c); return *this; }
__gmp_expr & operator=(unsigned char c) { mpz_set_ui(mp, c); return *this; }
__gmp_expr & operator=(signed int i) { mpz_set_si(mp, i); return *this; }
__gmp_expr & operator=(unsigned int i) { mpz_set_ui(mp, i); return *this; }
__gmp_expr & operator=(signed short int s)
{ mpz_set_si(mp, s); return *this; }
__gmp_expr & operator=(unsigned short int s)
{ mpz_set_ui(mp, s); return *this; }
__gmp_expr & operator=(signed long int l)
{ mpz_set_si(mp, l); return *this; }
__gmp_expr & operator=(unsigned long int l)
{ mpz_set_ui(mp, l); return *this; }
__gmp_expr & operator=(float f) { mpz_set_d(mp, f); return *this; }
__gmp_expr & operator=(double d) { mpz_set_d(mp, d); return *this; }
/*
__gmp_expr & operator=(long double ld) { mpz_set_ld(mp, ld); return *this; }
*/
__gmp_expr & operator=(const char *s)
{ mpz_set_str(mp, s, 0); return *this; }
#ifndef _WIN32_WCE
__gmp_expr & operator=(const std::string &s)
{ mpz_set_str(mp, s.c_str(), 0); return *this; }
// string input/output functions
int set_str(const std::string &s, int base)
{ return mpz_set_str(mp, s.c_str(), base); }
std::string get_str(int base = 10) const
{
__gmp_alloc_cstring temp(mpz_get_str(0, base, mp));
return std::string(temp.str);
}
#endif
// conversion functions
mpz_srcptr get_mpz_t() const { return mp; }
mpz_ptr get_mpz_t() { return mp; }
signed long int get_si() const { return mpz_get_si(mp); }
unsigned long int get_ui() const { return mpz_get_ui(mp); }
double get_d() const { return mpz_get_d(mp); } // should be long double
// bool fits_schar_p() const { return mpz_fits_schar_p(mp); }
// bool fits_uchar_p() const { return mpz_fits_uchar_p(mp); }
bool fits_sint_p() const { return mpz_fits_sint_p(mp); }
bool fits_uint_p() const { return mpz_fits_uint_p(mp); }
bool fits_sshort_p() const { return mpz_fits_sshort_p(mp); }
bool fits_ushort_p() const { return mpz_fits_ushort_p(mp); }
bool fits_slong_p() const { return mpz_fits_slong_p(mp); }
bool fits_ulong_p() const { return mpz_fits_ulong_p(mp); }
// bool fits_float_p() const { return mpz_fits_float_p(mp); }
// bool fits_double_p() const { return mpz_fits_double_p(mp); }
// bool fits_ldouble_p() const { return mpz_fits_ldouble_p(mp); }
// member operators
__GMPZ_DECLARE_COMPOUND_OPERATOR(operator+=)
__GMPZ_DECLARE_COMPOUND_OPERATOR(operator-=)
__GMPZ_DECLARE_COMPOUND_OPERATOR(operator*=)
__GMPZ_DECLARE_COMPOUND_OPERATOR(operator/=)
__GMPZ_DECLARE_COMPOUND_OPERATOR(operator%=)
__GMPZZ_DECLARE_COMPOUND_OPERATOR(operator&=)
__GMPZZ_DECLARE_COMPOUND_OPERATOR(operator|=)
__GMPZZ_DECLARE_COMPOUND_OPERATOR(opera
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -