Commit 3154d6ba authored by antoine lucas's avatar antoine lucas
Browse files

version 0.5-13.5

parent c75a94c4
......@@ -9,28 +9,12 @@
#include <gmp.h>
#define USE_RINTERNALS
#define R_NO_REMAP // avoid collisions with stl definitions
#include <R.h>
#include <Rinternals.h>
// the only thing we use from <Rdefines.h> :
#define AS_INTEGER(x) coerceVector(x,INTSXP)
// but use some handy defs anyways
#undef PROTECT
#undef UNPROTECT
#define PROTECT(x) Rf_protect(x)
#define UNPROTECT(x) Rf_unprotect(x)
#undef Length
#define Length(x) Rf_length(x)
#undef coerceVector
#define coerceVector Rf_coerceVector
#undef error
#define error Rf_error
#undef warning
#define warning Rf_warning
#define class_P(_x_) CHAR(Rf_asChar(Rf_getAttrib(_x_, R_ClassSymbol)))
#ifdef ENABLE_NLS
......@@ -39,5 +23,4 @@
#else
#define _(String) (String)
#endif
#endif
......@@ -297,7 +297,7 @@ class biginteger
/**
* Test prime numbers
*/
int isprime(int reps){return mpz_probab_prime_p(value,reps);}
int isprime(int reps) const {return mpz_probab_prime_p(value,reps);}
/** \brief overload affectation operator
......@@ -312,6 +312,9 @@ class biginteger
/** \brief comparison operator
*/
bool operator!= (const biginteger& rhs, const biginteger& lhs);
inline bool operator== (const biginteger& rhs, const biginteger& lhs){
return !(rhs != lhs);
}
/** \brief comparison operator
......
......@@ -18,7 +18,7 @@
#ifndef T_BIGMOD_BINARY_OPERATION
#define T_BIGMOD_BINARY_OPERATION 1
/// A pointer to a binary operator for bigintegers
typedef bigmod (*biginteger_binary_fn)(const bigmod&, const bigmod&);
typedef DefaultBigMod (*biginteger_binary_fn)(const bigmod&, const bigmod&);
#endif
#ifndef T_BIGMOD_BINARY_OPERATION_LOGICAL
......
......@@ -2,7 +2,7 @@
* \brief Description of class bigmod
*
* \date Created: 22/05/06
* \date Last modified: Time-stamp: <2014-07-10 08:44:04 antoine>
* \date Last modified: Time-stamp: <2019-03-10 10:30:30 (antoine)>
*
* \author Immanuel Scholz
*
......@@ -26,6 +26,7 @@ extern "C" {
}
/**
* \brief class for bigmod values. Represent any integer in Z/nZ
*
......@@ -34,24 +35,30 @@ extern "C" {
* to the operation result is applied. If the value is NA, the result is always NA.
*/
class bigmod {
public:
/** \brief Value of our bigmod */
biginteger value;
/** \brief modulus of our bigmod representation: value %% modulus */
biginteger modulus;
private:
bigmod * inverse;
/** \brief copy operator */
bigmod(const bigmod & rhs) :
value(((bigmod)rhs).getValue()),modulus(((bigmod)rhs).getModulus()) {
/** \brief creator
*/
bigmod(const biginteger& value_ = biginteger(),
const biginteger& modulus_ = biginteger()) :
value(value_),modulus(modulus_) {}
}
/** \brief copy operator */
bigmod(const bigmod & rhs) :
value(rhs.value),
modulus(rhs.modulus){}
protected:
/** \brief Value of our bigmod -- only references*/
biginteger & value;
/** \brief modulus of our bigmod representation: value %% modulus */
biginteger & modulus;
public:
bigmod(biginteger& value_,
biginteger& modulus_) :
inverse(NULL),
value(value_),modulus(modulus_) {};
~bigmod(){};
virtual ~bigmod(){
if(inverse != NULL) delete inverse;
};
/**
* \brief Return as a human readible string
......@@ -63,17 +70,97 @@ class bigmod {
/** \brief return sign (-1 if negative, 0 if 0; +1 if positive)
*/
int sgn() const
inline int sgn() const
{
return(mpz_sgn(value.getValueTemp()));
return(mpz_sgn(getValue().getValueTemp()));
}
bigmod inv () const;
bigmod & inv () ;
biginteger & getValue() {
return value;
}
biginteger & getModulus() {
return modulus;
}
const biginteger & getValue() const{
return value;
}
const biginteger & getModulus() const {
return modulus;
}
};
class DefaultBigMod : public bigmod {
private:
/** \brief Value of our bigmod */
biginteger valueLocal;
/** \brief modulus of our bigmod representation: value %% modulus */
biginteger modulusLocal;
public:
/** \brief creator
*/
DefaultBigMod(const biginteger& value_ = biginteger(),
const biginteger& modulus_ = biginteger()) :
bigmod(valueLocal,modulusLocal),
valueLocal(value_),modulusLocal(modulus_) {
value = valueLocal;
modulus = modulusLocal;
}
/** \brief copy operator */
DefaultBigMod(const bigmod & rhs) :
bigmod(valueLocal,modulusLocal),
valueLocal(rhs.getValue()),modulusLocal(rhs.getModulus()) {
value = valueLocal;
modulus = modulusLocal;
}
/** \brief copy operator */
DefaultBigMod(const DefaultBigMod & rhs) :
bigmod(valueLocal,modulusLocal),
valueLocal(rhs.getValue()),modulusLocal(rhs.getModulus()) {
value = valueLocal;
modulus = modulusLocal;
}
~DefaultBigMod(){};
};
/**
* a bigmod that has only integer.
*/
class BigModInt : public bigmod {
private:
/** \brief modulus of our bigmod representation */
biginteger modulusLocal;
public:
/** \brief creator
*/
BigModInt(biginteger& value_) :
bigmod(value_,modulusLocal),
modulusLocal() {
modulus = modulusLocal;
}
~BigModInt(){};
};
/** \brief comparison operator
*/
bool operator!= (const bigmod& rhs, const bigmod& lhs);
......@@ -92,33 +179,33 @@ bool operator== (const bigmod& rhs, const bigmod& lhs);
* a modulus set. If none modulus for either bigmod is set, the result will not
* have a modulus as well.
*/
bigmod operator+(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod operator+(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Subtract two bigmods.
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod operator-(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod operator-(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Multiply two bigmods.
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod operator*(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod operator*(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Divide two bigmods a / b := a * b^(-1)
*/
bigmod div_via_inv(const bigmod& a, const bigmod& b);
DefaultBigMod div_via_inv(const bigmod& a, const bigmod& b);
/**
* \brief Divide two bigmods.
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod operator/(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod operator/(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Calculate the modulus (remainder) of two bigmods.
......@@ -128,7 +215,7 @@ bigmod operator/(const bigmod& rhs, const bigmod& lhs);
* was before, except if rhs and lhs has both no modulus set,
* in which case the resulting modulus will be unset too.
*/
bigmod operator%(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod operator%(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Return the power of "exp" to the base of "base" (return = base^exp).
......@@ -140,14 +227,14 @@ bigmod operator%(const bigmod& rhs, const bigmod& lhs);
*
* For other modulus description, see operator+(bigmod, bigmod)
*/
bigmod pow(const bigmod& base, const bigmod& exp);
DefaultBigMod pow(const bigmod& base, const bigmod& exp);
/**
* \brief Return the modulo inverse to x mod m. (return = x^-1 % m)
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod inv(const bigmod& x, const bigmod& m);
DefaultBigMod inv(const bigmod& x, const bigmod& m);
/**
* \brief Return a bigmod with value (x % m) and the intern modulus set to m.
......@@ -155,7 +242,7 @@ bigmod inv(const bigmod& x, const bigmod& m);
*
* Do not confuse this with operator%(bigmod, bigmod).
*/
bigmod set_modulus(const bigmod& x, const bigmod& m);
DefaultBigMod set_modulus(const bigmod& x, const bigmod& m);
biginteger get_modulus(const bigmod& b1, const bigmod& b2);
......@@ -164,20 +251,20 @@ biginteger get_modulus(const bigmod& b1, const bigmod& b2);
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod gcd(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod gcd(const bigmod& rhs, const bigmod& lhs);
/**
* \brief Return the least common multiply of both parameter.
*
* For modulus description, see operator+(bigmod, bigmod)
*/
bigmod lcm(const bigmod& rhs, const bigmod& lhs);
DefaultBigMod lcm(const bigmod& rhs, const bigmod& lhs);
/**
* \brief function used to make any binary operation between
* two bigmod that return a bigmod (addition substraction... )
*/
bigmod create_bigmod(const bigmod& lhs, const bigmod& rhs, gmp_binary f,
DefaultBigMod create_bigmod(const bigmod& lhs, const bigmod& rhs, gmp_binary f,
bool zeroRhsAllowed = true) ;
#endif
......@@ -15,7 +15,8 @@
#define BIGVEC_HEADER_ 1
#include "bigmod.h"
#include "templateMatrix.h"
#include <memory>
/** \brief class bigvec
*
......@@ -23,12 +24,16 @@
* can be of different size and a nrow
* parameter (for matrix support)
*/
class bigvec {
class bigvec : public matrix::Matrix<bigmod> {
public:
/** \brief value */
std::vector<biginteger> value;
/** \brief modulus */
std::vector<biginteger> modulus;
/** array with all bigmod, that are references to values in vector. */
std::vector<bigmod *> valuesMod;
/** \brief optional parameter used with matrix -- set to -1 for non-matrix */
int nrow ;
......@@ -41,7 +46,9 @@ class bigvec {
*/
bigvec(const bigvec & vecteur);
~bigvec(){};
virtual ~bigvec();
/**
* \brief construct a bigmod at indice i
......@@ -50,14 +57,19 @@ class bigvec {
*
* \note should not used for assignement
*/
bigmod operator[] (unsigned int i) const;
const bigmod & operator[] (unsigned int i) const;
bigmod & operator[] (unsigned int i) ;
/**
* \brief assign a value at indice i
*/
void set(unsigned int i,const bigmod & val);
void set(unsigned int row, unsigned int col, const bigmod & val) ;
bigmod & get(unsigned int row, unsigned int col) ;
/**
* \brief extend our vectors.
*
......@@ -77,7 +89,7 @@ class bigvec {
/**
* Insert Big Integer value
*/
// void push_back(mpz_t & value_p);
void push_back(biginteger & value_p);
void push_back(const __mpz_struct* value_p);
/**
......@@ -85,6 +97,8 @@ class bigvec {
*/
unsigned int size() const ;
unsigned int nRows() const;
/**
* \brief extend vector value.
*/
......@@ -124,6 +138,11 @@ class bigvec {
private:
void checkValuesMod() ;
void clearValuesMod() ;
};
//
......
......@@ -50,7 +50,7 @@ namespace solve_gmp_R
* solve A X = B (return X) with A & B matrix (of rational or biginteger)
*
* A is of dimension nxn X nxm and B nxm (X will be return a B address)
* We use the Gauss trucmuch algorithm
* We use the Gauss algorithm
*/
template< class T> void solve (T & A , T & B)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment