00001
00002 #include <gmp.h>
00003 #include <R.h>
00004 #include <Rdefines.h>
00005
00006 #include <string>
00007
00018 class biginteger
00019 {
00023 mpz_t value;
00024
00028 bool na;
00029
00030 public:
00034 biginteger() {mpz_init(value); setValue();}
00035
00039 biginteger(void* raw);
00040
00045 biginteger(const mpz_t& value_) : na(false) {mpz_init_set(value, value_);}
00046
00050 biginteger(int value_) : na(false) {mpz_init_set_ui(value, value_);}
00051
00055 biginteger(double value_) : na(false) {mpz_init_set_d(value, value_);}
00056
00060 biginteger(const std::string& value_) : na(false) {mpz_init_set_str(value, value_.c_str(), 0);}
00061
00065 biginteger(const biginteger& rhs) : na(rhs.na) {mpz_init_set(value, rhs.value);}
00066
00070 biginteger& operator=(const biginteger& rhs) {mpz_set(value, rhs.value); na = rhs.na;}
00071
00075 ~biginteger() {mpz_clear(value);}
00076
00077
00081 void setValue() {mpz_set_ui(value, 0); na = true;}
00082
00086 void setValue(mpz_t value_) {mpz_set(value, value_); na = false;}
00087
00093 const mpz_t& getValueTemp() const {return value;}
00094
00098 bool isNA() const {return na;}
00099
00103 std::string str() const;
00104
00109 long as_long() const {return mpz_get_ui(value);}
00110
00114 double as_double() const {return mpz_get_d(value);}
00115
00126 int as_raw(void* raw) const;
00127
00132 size_t raw_size() const;
00133
00137 void swap(biginteger& other);
00138
00139
00143 int isprime(int reps){return mpz_probab_prime_p(value,reps);}
00144
00145 };
00146
00147
00153 class bigmod {
00154 public:
00155 biginteger value;
00156 biginteger modulus;
00157
00161 bigmod(const biginteger& value_ = biginteger(),
00162 const biginteger& modulus_ = biginteger()) : value(value_),modulus(modulus_) {}
00163
00167 std::string str() const;
00168 };
00169
00170
00171
00172
00181 bigmod operator+(const bigmod& rhs, const bigmod& lhs);
00182
00188 bigmod operator-(const bigmod& rhs, const bigmod& lhs);
00189
00195 bigmod operator*(const bigmod& rhs, const bigmod& lhs);
00196
00202 bigmod operator/(const bigmod& rhs, const bigmod& lhs);
00203
00212 bigmod operator%(const bigmod& rhs, const bigmod& lhs);
00213
00224 bigmod pow(const bigmod& base, const bigmod& exp);
00225
00231 bigmod inv(const bigmod& x, const bigmod& m);
00232
00239 bigmod set_modulus(const bigmod& x, const bigmod& m);
00240
00246 bigmod gcd(const bigmod& rhs, const bigmod& lhs);
00247
00253 bigmod lcm(const bigmod& rhs, const bigmod& lhs);
00254
00255