Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

mpi.h

Go to the documentation of this file.
00001 
00003 /* Part of the C++ Multi-Precision arithmetic library
00004 
00005    Written by Brian Sipos <bjs5075@rit.edu> or <brian@sipos.yi.org>
00006    Copyright 2001,2002,2003 Brian Sipos
00007 
00008    The MP++ library is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    The MP++ library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with the MP++ library; if not, write to the Free Software
00020    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef MPI_H
00024 #define MPI_H
00025 
00026 #include "mpn.h"
00027 
00028 namespace mp {
00029 
00048 class mpi_ : public mpn_ {
00049  public:
00050   typedef mp::int_type int_type; 
00051 
00053   static const byte capabilities();
00054 
00059   static void setDefaultBase(radix_type b);
00060 
00063   static radix_type getDefaultBase();
00064 
00065   mpi_() {}
00066   mpi_(int_type n, size_type size = 0) {
00067     init(n, size);
00068   }
00069   mpi_(const mpi_ &n, size_type size = 0) {
00070     init(n, size);
00071   }
00072 
00080   void init(int_type n, size_type size = 0);
00087   void init(const mpi_ &n, size_type size = 0);
00088 
00089   void clear(); 
00090 
00094   void resize(size_type size);
00095 
00100   void swap(mpi_ &n);
00102 
00107   int_type toInt() const;
00108 
00113   bool isNeg() const;
00114 
00117   size_type size() const;
00119 
00129   void setString(const std::string &s, radix_type b = 0);
00130 
00137   std::string getString(radix_type b = string_base) const;
00139 
00145   void shift_l(size_type n);
00149   void shift_r(size_type n);
00151 
00156   void print() const;
00158 
00163   void add(const mpi_ &n);
00164 
00167   void sub(const mpi_ &n);
00168 
00171   void mul(const mpi_ &n);
00172 
00175   void div(const mpi_ &n);
00176 
00179   void mod(const mpi_ &n);
00181 
00184   void negate(); 
00185   void abs(); 
00186 
00187   void sqr(); 
00188 
00194   mpi_ pow(int_type e) const;
00195 
00200   mpi_ pow_mod(const mpi_ &e, const mpi_ &m) const;
00202 
00206   int compare(const mpi_ &n) const;
00207 
00208   void checkNeg(); 
00209 
00210  protected:
00211   static radix_type string_base; 
00212 
00213   bool neg_; 
00214 
00215  private:
00216   void add_sub(const mpi_ &y, bool subtract); 
00217 };
00218 
00229 class mpi : public mpi_ {
00230  public:
00231   mpi(const mpi_ &n) : mpi_(n) {} // Casting up
00232 
00233   mpi() {}
00234   mpi(int_type n, size_type size = 0) : mpi_(n, size) {}
00235   mpi(const mpi &n, size_type size = 0) : mpi_(n, size) {}
00236   mpi(const std::string &s, radix_type b = 0) {
00237     setString(s, b);
00238   }
00239 
00242   operator int_type() const {
00243     return toInt();
00244   }
00245   operator std::string() const {
00246     return getString();
00247   }
00249 
00252   const mpi & operator = (int_type n);
00253   const mpi & operator = (const mpi &n);
00254   const mpi & operator = (const std::string &s);
00256 
00259   const mpi & operator <<= (size_type y);
00260   const mpi & operator >>= (size_type y);
00262 
00265   const mpi & operator &= (const mpi &n);
00266   const mpi & operator |= (const mpi &n);
00267   const mpi & operator ^= (const mpi &n);
00268   mpi operator ~ ();
00270 
00273   mpi & operator ++ ();  // ++var
00274   const mpi operator ++ (int); //   var++
00275 
00276   mpi & operator -- ();  // --var
00277   const mpi operator -- (int); //   var--
00279 
00282   const mpi & operator += (const mpi &n);
00283   const mpi & operator -= (const mpi &n);
00284   const mpi & operator *= (const mpi &n);
00285   const mpi & operator /= (const mpi &n);
00286   const mpi & operator %= (const mpi &n);
00288 
00291   void prime(size_type n);
00292 
00299   bool isPrime(int n = _MP_RM_TESTS);
00300 
00301 };
00302 
00310 std::istream & operator >> (std::istream &is, mpi &n);
00311 
00317 std::ostream & operator << (std::ostream &os, const mpi &n);
00319 
00325 bool operator == (const mpi &x, int_type y);
00326 bool operator != (const mpi &x, int_type y);
00327 bool operator <  (const mpi &x, int_type y);
00328 bool operator <= (const mpi &x, int_type y);
00329 bool operator >  (const mpi &x, int_type y);
00330 bool operator >= (const mpi &x, int_type y);
00331 
00332 bool operator == (int_type x, const mpi &y);
00333 bool operator != (int_type x, const mpi &y);
00334 bool operator <  (int_type x, const mpi &y);
00335 bool operator <= (int_type x, const mpi &y);
00336 bool operator >  (int_type x, const mpi &y);
00337 bool operator >= (int_type x, const mpi &y);
00338 
00339 bool operator == (const mpi &x, const mpi &y);
00340 bool operator != (const mpi &x, const mpi &y);
00341 bool operator <  (const mpi &x, const mpi &y);
00342 bool operator <= (const mpi &x, const mpi &y);
00343 bool operator >  (const mpi &x, const mpi &y);
00344 bool operator >= (const mpi &x, const mpi &y);
00346 
00352 mpi operator << (const mpi &x, mpi::size_type y);
00353 mpi operator >> (const mpi &x, mpi::size_type y);
00355 
00361 mpi operator & (const mpi &x, const mpi &y);
00362 mpi operator | (const mpi &x, const mpi &y);
00363 mpi operator ^ (const mpi &x, const mpi &y);
00365 
00371 mpi operator + (const mpi &x);
00372 mpi operator - (const mpi &x);
00374 
00378 mpi operator + (const mpi &x, int_type y);
00379 mpi operator + (int_type x, const mpi &y);
00380 mpi operator + (const mpi &x, const mpi &y);
00381 
00385 mpi operator - (const mpi &x, int_type y);
00386 mpi operator - (int_type x, const mpi &y);
00387 mpi operator - (const mpi &x, const mpi &y);
00388 
00392 mpi operator * (const mpi &x, int_type y);
00393 mpi operator * (int_type x, const mpi &y);
00394 mpi operator * (const mpi &x, const mpi &y);
00395 
00399 mpi operator / (const mpi &x, int_type y);
00400 mpi operator / (int_type x, const mpi &y);
00401 mpi operator / (const mpi &x, const mpi &y);
00402 
00406 mpi operator % (const mpi &x, int_type y);
00407 mpi operator % (int_type x, const mpi &y);
00408 mpi operator % (const mpi &x, const mpi &y);
00410 
00414 mpi abs(const mpi &n);
00418 mpi sqr(const mpi &n);
00423 mpi pow(const mpi &base, const mpi &p);
00429 mpi pow_mod(const mpi &base, const mpi &p, const mpi &mod);
00430 
00432 mpi euclid(const mpi &x, const mpi &y);
00435 mpi factorial(const mpi &n);
00440 mpi gcd(const mpi &x, const mpi &y);
00441 
00443 } // End namespace mp
00444 #endif

Generated on Fri Feb 4 16:17:55 2005 for LibMP++ by  doxygen 1.4.1