[4056] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | //-----------------------------------------------------------
|
---|
| 3 | // Class PrimesNumbers (prime numbers)
|
---|
| 4 | // Class QNumber (Rational numbers)
|
---|
| 5 | // SOPHYA class library - (C) UPS+LAL IN2P3/CNRS , CEA-Irfu
|
---|
| 6 | // R. Ansari UPS+LAL IN2P3/CNRS 2012
|
---|
| 7 | //-----------------------------------------------------------
|
---|
| 8 |
|
---|
| 9 | #ifndef PQNUMBER_H_SEEN
|
---|
| 10 | #define PQNUMBER_H_SEEN
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | #include "machdefs.h"
|
---|
| 14 | #include <string>
|
---|
| 15 | #include <vector>
|
---|
| 16 | #include <iostream>
|
---|
| 17 |
|
---|
| 18 | #include "pexceptions.h"
|
---|
| 19 | #include "ppersist.h"
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | namespace SOPHYA {
|
---|
| 23 |
|
---|
| 24 | //--------------------------------------------------------------------------------
|
---|
| 25 | //------------------------------ PrimeNumbers class ------------------------------
|
---|
| 26 | //--------------------------------------------------------------------------------
|
---|
| 27 |
|
---|
| 28 | //! Class providing list of prime number
|
---|
| 29 | class PrimeNumbers {
|
---|
| 30 | public:
|
---|
| 31 | //! Default constructor
|
---|
| 32 | PrimeNumbers();
|
---|
| 33 | //! Copy constructor
|
---|
| 34 | PrimeNumbers(PrimeNumbers const& p);
|
---|
| 35 |
|
---|
| 36 | //! return the next prime number
|
---|
| 37 | inline uint_8 Next()
|
---|
| 38 | { uint_8 rp=Get(my_prime_idx_); my_prime_idx_++; return rp; }
|
---|
| 39 | //! rewind / reset prime number index for the Next() method
|
---|
| 40 | inline void Rewind() { my_prime_idx_=0; }
|
---|
| 41 | //! Reset prime number index for the Next() method
|
---|
| 42 | inline void ResetIndex() { my_prime_idx_=0; }
|
---|
| 43 |
|
---|
| 44 | //! return the \b k th prime number
|
---|
| 45 | uint_8 Get(size_t k) const;
|
---|
| 46 | //! return the \b k th prime number (operator [] overload)
|
---|
| 47 | inline uint_8 operator[](size_t k) const { return Get(k); }
|
---|
| 48 | //! return the \b k th prime number (operator () overload)
|
---|
| 49 | inline uint_8 operator()(size_t k) const { return Get(k); }
|
---|
| 50 | //! return true if the argument \b n is a prime number
|
---|
| 51 | static bool CheckIfPrim(uint_8 n);
|
---|
| 52 | //! return the prime number factorization of \b n
|
---|
| 53 | static std::vector<uint_8> PrimeFactors(uint_8 n, bool fgprt=false);
|
---|
| 54 |
|
---|
| 55 | //! Computes an interval for the n-th (n>=6) prime number
|
---|
| 56 | static void encadre6(size_t nieme,double &nlow,double &nhigh);
|
---|
| 57 | //! Computes an interval for the n-th (n>=40000) prime number
|
---|
| 58 | static void encadre40k(size_t nieme,double &nlow,double &nhigh);
|
---|
| 59 | //! Return an approximate value for the n-th prime number
|
---|
| 60 | static double approx(unsigned int nieme);
|
---|
| 61 |
|
---|
| 62 | private:
|
---|
| 63 | //! Global initialization (Mutex object and prime number array creation)
|
---|
| 64 | static void Init();
|
---|
| 65 | //! Extends the prime number array by finding the next \b nxt prime numbers
|
---|
| 66 | static void Extend(size_t nxt);
|
---|
| 67 | //! Extends the prime number array by finding all prime numbers p such as p<=n
|
---|
| 68 | static void Extend2(uint_8 n);
|
---|
| 69 |
|
---|
| 70 | //! Static private member used by Extend() to check if a number is prime
|
---|
| 71 | static bool CheckIfPrim_P(uint_8 n);
|
---|
| 72 | //! Static private member to find all prime numbers less than P. return the list of flags.
|
---|
| 73 | static unsigned char* eratosthene(uint_8 P, size_t& npremiers);
|
---|
| 74 |
|
---|
| 75 | size_t my_prime_idx_; // prime number index for Next() method
|
---|
| 76 | static std::vector<uint_8> * prime_list_p_; // global prime number array
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | //--------------------------------------------------------------------------------
|
---|
| 80 | //---------------------- QNumber class : rational numbers ------------------------
|
---|
| 81 | //--------------------------------------------------------------------------------
|
---|
| 82 |
|
---|
| 83 | //! Class representing rational numbers : q = m/n with m,n integers)
|
---|
| 84 | class QNumber { // : public AnyDataObj {
|
---|
| 85 | public:
|
---|
| 86 | //! Default constructor, with 0 value (= 0 / 1)
|
---|
| 87 | explicit QNumber()
|
---|
| 88 | { num_=0; den_=1; }
|
---|
| 89 | //! Constructor from two integers, numerator and denominator
|
---|
| 90 | QNumber(int_8 m, int_8 n=1)
|
---|
| 91 | {
|
---|
| 92 | if (n==0) throw MathExc("QNumber(m,n) n=0->null denominator");
|
---|
| 93 | num_=m; den_=n;
|
---|
| 94 | if (den_<0) { num_=-num_; den_=-den_; }
|
---|
| 95 | }
|
---|
| 96 | //! Copy constructor
|
---|
| 97 | QNumber(QNumber const& q)
|
---|
| 98 | { num_=q.num_; den_=q.den_; }
|
---|
| 99 |
|
---|
| 100 | //! copy (equal) operator
|
---|
| 101 | inline QNumber& operator = (QNumber const & q)
|
---|
| 102 | { num_=q.num_; den_=q.den_; return(*this); }
|
---|
| 103 |
|
---|
| 104 | //! Return the simplified rational number (division by all common divisors of numerator and denominator)
|
---|
| 105 | QNumber Simplify() const;
|
---|
| 106 |
|
---|
| 107 | //! Convert to decimal (double precision) number
|
---|
| 108 | inline operator double() const { return (double)num_/(double)den_; }
|
---|
| 109 | //! Return the numerator
|
---|
| 110 | inline int_8 Numerator() const { return num_; }
|
---|
| 111 | //! Return the denominator
|
---|
| 112 | inline int_8 Denominator() const { return den_; }
|
---|
| 113 |
|
---|
| 114 | //! Return the opposite (-q) of the rational number
|
---|
| 115 | inline QNumber Opposite() const
|
---|
| 116 | { return QNumber(-num_, den_); }
|
---|
| 117 | //! Return the inverse (1/q) of the rational number
|
---|
| 118 | inline QNumber Inverse() const
|
---|
| 119 | { return QNumber(den_, num_); }
|
---|
| 120 |
|
---|
| 121 | //! Return true if the rational number is equal to zero , false otherwise
|
---|
| 122 | inline bool isZero() const
|
---|
| 123 | { return ( (num_==0)?true:false ); }
|
---|
| 124 | //! Return true if the rational number represents an integer , false otherwise
|
---|
| 125 | inline bool isInteger() const
|
---|
| 126 | { return ((Simplify().Denominator()==1)?true:false); }
|
---|
| 127 |
|
---|
| 128 | //! Return the rational number in string format (num/den)
|
---|
| 129 | std::string ToString();
|
---|
| 130 | //! Prints the rational number on \b cout (return the cout stream object)
|
---|
| 131 | inline ostream& Print() const
|
---|
| 132 | { return Print(cout); }
|
---|
| 133 | //! Prints the rational number on stream \b os (return the os stream object)
|
---|
| 134 | inline ostream& Print(ostream& os) const
|
---|
| 135 | {
|
---|
| 136 | if (den_==1) os << num_;
|
---|
| 137 | else os << "(" << num_ << '/' << den_ <<")";
|
---|
| 138 | return os;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | //! Add two QNumbers (simplify the result if fgsimp true)
|
---|
| 142 | static QNumber Add(QNumber const & a, QNumber const & b, bool fgsimp=true);
|
---|
| 143 | //! Subtract two QNumbers (simplify the result if fgsimp true)
|
---|
| 144 | static QNumber Subtract(QNumber const & a, QNumber const & b, bool fgsimp=true);
|
---|
| 145 | //! Multiply two QNumbers (simplify the result if fgsimp true)
|
---|
| 146 | static QNumber Multiply(QNumber const & a, QNumber const & b, bool fgsimp=true);
|
---|
| 147 | //! Divide two QNumbers (simplify the result if fgsimp true)
|
---|
| 148 | static QNumber Divide(QNumber const & a, QNumber const & b, bool fgsimp=true);
|
---|
| 149 | //! Compare two QNumbers: return -1 (a<b), 0 (a==b), 1 (a>b)
|
---|
| 150 | static int Compare(QNumber const & a, QNumber const & b);
|
---|
| 151 |
|
---|
| 152 | protected:
|
---|
| 153 | int_8 num_,den_; // numerateur et denominateur
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /*! operator << overloading - prints the QNumber q on \b os*/
|
---|
| 158 | inline ostream& operator << (ostream& s, QNumber q)
|
---|
| 159 | { q.Print(s); return(s); }
|
---|
| 160 |
|
---|
| 161 | /*! Sum operator definition for QNumbers : r = a + b */
|
---|
| 162 | inline QNumber operator + (QNumber a, QNumber b)
|
---|
| 163 | { return QNumber::Add(a,b); }
|
---|
| 164 |
|
---|
| 165 | /*! Difference (subtraction) operator definition for QNumbers : r = a - b */
|
---|
| 166 | inline QNumber operator - (QNumber a, QNumber b)
|
---|
| 167 | { return QNumber::Subtract(a,b); }
|
---|
| 168 |
|
---|
| 169 | /*! Product (multiplication) operator definition for QNumbers : r = a * b */
|
---|
| 170 | inline QNumber operator * (QNumber a, QNumber b)
|
---|
| 171 | { return QNumber::Multiply(a,b); }
|
---|
| 172 |
|
---|
| 173 | /*! Divide operator definition for QNumbers : r = a / b */
|
---|
| 174 | inline QNumber operator / (QNumber a, QNumber b)
|
---|
| 175 | { return QNumber::Divide(a,b); }
|
---|
| 176 |
|
---|
| 177 | /*! Equality comparison operator == for QNumbers : return true if (a == b) */
|
---|
| 178 | inline bool operator == (QNumber a, QNumber b)
|
---|
| 179 | { return ( QNumber::Compare(a,b)==0); }
|
---|
| 180 |
|
---|
| 181 | /*! Not equal comparison operator != for QNumbers : return true if (a != b) */
|
---|
| 182 | inline bool operator != (QNumber a, QNumber b)
|
---|
| 183 | { return ( QNumber::Compare(a,b)!=0); }
|
---|
| 184 |
|
---|
| 185 | /*! Comparison operator < for QNumbers : return true if (a < b) */
|
---|
| 186 | inline bool operator < (QNumber a, QNumber b)
|
---|
| 187 | { return ( QNumber::Compare(a,b)<0); }
|
---|
| 188 |
|
---|
| 189 | /*! Comparison operator > for QNumbers : return true if (a > b) */
|
---|
| 190 | inline bool operator > (QNumber a, QNumber b)
|
---|
| 191 | { return ( QNumber::Compare(a,b)>0); }
|
---|
| 192 |
|
---|
| 193 | /*! Comparison operator <= for QNumbers : return true if (a <= b) */
|
---|
| 194 | inline bool operator <= (QNumber a, QNumber b)
|
---|
| 195 | { return ( QNumber::Compare(a,b)<=0); }
|
---|
| 196 |
|
---|
| 197 | /*! Comparison operator >= for QNumbers : return true if (a >= b) */
|
---|
| 198 | inline bool operator >= (QNumber a, QNumber b)
|
---|
| 199 | { return ( QNumber::Compare(a,b)>=0); }
|
---|
| 200 |
|
---|
| 201 | /*! Writes the two integers representing a QNumber in the POutPersist stream \b os */
|
---|
| 202 | inline POutPersist& operator << (POutPersist& os, QNumber const& q)
|
---|
| 203 | { os << q.Numerator() << q.Denominator(); return(os); }
|
---|
| 204 | /*! Reads the object from the PInPersist stream \b is */
|
---|
| 205 | inline PInPersist& operator >> (PInPersist& is, QNumber& q)
|
---|
| 206 | { int_8 num,den; is >> num >> den; q = QNumber(num,den); return(is); }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | } // namespace SOPHYA
|
---|
| 210 |
|
---|
| 211 | #endif
|
---|