CoCoALib-0.9905 date: 23 May 2007


SmallFpLogImpl.H

Go to the documentation of this file.
00001 #ifndef CoCoA_SmallFpLogImpl_H
00002 #define CoCoA_SmallFpLogImpl_H
00003 
00004 //   Copyright (c)  2005  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib 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 CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 
00023 // Header file for the class SmallFpLogImpl
00024 
00025 
00026 #include "CoCoA/assert.H"
00027 #include "CoCoA/config.H"
00028 #include "CoCoA/ZZ.H"
00029 
00030 #include <cstddef>
00031 //using std::size_t;
00032 #include <iosfwd>
00033 // using std::ostream;
00034 #include <vector>
00035 // using std::vector
00036 
00037 
00038 namespace CoCoA
00039 {
00040 
00041   class OpenMathOutput; // forward declaration -- defined in OpenMath.H
00042 
00043   class SmallFpLogImpl
00044   {
00045   public:
00046     SmallFpLogImpl(unsigned long p);
00047     SmallFpLogImpl(const ZZ& P);
00048   private:
00049     SmallFpLogImpl(const SmallFpLogImpl&);            // NEVER DEFINED -- copy ctor disabled
00050     SmallFpLogImpl& operator=(const SmallFpLogImpl&); // NEVER DEFINED -- assignment disabled
00051 
00052   public:
00053     typedef SmallFpLogElem_t value_t;
00054     static const std::size_t DatumSize = sizeof(value_t);
00055 
00056     void myAssignZero(value_t& lhs) const;                        ///< lhs = 0
00057     void myAssign(value_t& lhs, value_t x) const;                 ///< lhs = x
00058     void myAssign(value_t& lhs, long n) const;                    ///< lhs = n
00059     void myAssign(value_t& lhs, const ZZ& N) const;               ///< lhs = N
00060     void myNegate(value_t& lhs, value_t x) const;                 ///< lhs = -x
00061     void myAdd(value_t& lhs, value_t x, value_t y) const;         ///< lhs = x+y
00062     void mySub(value_t& lhs, value_t x, value_t y) const;         ///< lhs = x-y
00063     void myMul(value_t& lhs, value_t x, value_t y) const;         ///< lhs = x*y
00064     void myDiv(value_t& lhs, value_t x, value_t y) const;         ///< lhs = x/y
00065     bool myIsDivisible(value_t& lhs, value_t x, value_t y) const; ///< lhs = x/y, if y is non-zero
00066     void myPower(value_t& lhs, value_t x, unsigned long n) const; ///< lhs = x^n
00067     void myOutput(std::ostream& out, value_t x) const;            ///< out << x
00068     void myOutput(OpenMathOutput& OMOut, value_t x) const;        ///< OMOut << x
00069     bool myIsPrintAtom(value_t x) const;
00070     bool myIsPrintedWithMinus(value_t x) const;
00071 
00072     bool myIsZero(value_t x) const;                        ///< x == 0
00073     bool myIsOne(value_t x) const;                         ///< x == 1
00074     bool myIsMinusOne(value_t x) const;                    ///< x == -1
00075     bool myIsInteger(ZZ& N, value_t x) const;  ///< copy value of x into n, result is always true
00076     bool myIsZeroAddMul(value_t& lhs, value_t y, value_t z) const;// lhs += y*z, result says whether lhs == 0.
00077     bool myIsEqual(value_t x, value_t y) const;  // x == y
00078 
00079   private: // data members
00080     const value_t myModulus;
00081     const value_t myDrop;     // PUBLIC???
00082     const value_t myIterLimit;// PUBLIC???
00083     const value_t myRoot;
00084     typedef unsigned short FpTableElem; // the log/exp table elements are of this type
00085     std::vector<FpTableElem> myLog;
00086     std::vector<FpTableElem> myExp;
00087 
00088     value_t myReduceMod(value_t n) const; // Assumes n >= 0.
00089   private:
00090     static value_t CheckCtorArg(unsigned long p);
00091     static value_t CheckCtorArg(const ZZ& P);
00092     static value_t CalcDrop(value_t p);
00093     static value_t CalcIterLimit(value_t p);
00094     void myCtorBody();
00095   };  // end of class SmallFpLogImpl
00096 
00097 
00098   //------------------------------------------------------------
00099   // SmallFpLogImpl inline functions
00100   //------------------------------------------------------------
00101 
00102 //   inline std::size_t SmallFpLogImpl::myDatumSize()
00103 //   {
00104 //     return sizeof(value_t);
00105 //   }
00106 
00107 
00108   inline SmallFpLogImpl::value_t SmallFpLogImpl::myReduceMod(value_t n) const
00109   {
00110     return n%myModulus;
00111   }
00112 
00113 
00114   inline void SmallFpLogImpl::myAssignZero(value_t& lhs) const
00115   {
00116     lhs = 0;
00117   }
00118 
00119 
00120   inline void SmallFpLogImpl::myAssign(value_t& lhs, value_t x) const
00121   {
00122     CoCoA_ASSERT(x == x%myModulus);
00123     lhs = x;
00124   }
00125 
00126 
00127   inline void SmallFpLogImpl::myAssign(value_t& lhs, long n) const
00128   {
00129     if (n > 0) { lhs = n%long(myModulus); return; }
00130     lhs = myModulus - ((-n)%long(myModulus));
00131     if (lhs == myModulus) lhs = 0;
00132   }
00133 
00134 
00135   inline void SmallFpLogImpl::myAssign(value_t& lhs, const ZZ& N) const
00136   {
00137     lhs = mpz_fdiv_ui(mpzref(N), myModulus);
00138   }
00139 
00140 
00141   inline void SmallFpLogImpl::myNegate(value_t& lhs, value_t x) const
00142   {
00143     CoCoA_ASSERT(lhs == lhs%myModulus);
00144     if (x == 0) lhs = x;
00145     else lhs = myModulus-x;
00146   }
00147 
00148 
00149   inline void SmallFpLogImpl::myAdd(value_t& lhs, value_t x, value_t y) const
00150   {
00151     CoCoA_ASSERT(x == x%myModulus);
00152     CoCoA_ASSERT(y == y%myModulus);
00153     lhs = x+y;
00154     if (lhs >= myModulus) lhs -= myModulus;
00155   }
00156 
00157 
00158   inline void SmallFpLogImpl::mySub(value_t& lhs, value_t x, value_t y) const
00159   {
00160     CoCoA_ASSERT(x == x%myModulus);
00161     CoCoA_ASSERT(y == y%myModulus);
00162     if (x < y) lhs = x + (myModulus-y);  // avoid trying to create a negative value
00163     else lhs = x-y;
00164   }
00165 
00166 
00167   inline void SmallFpLogImpl::myMul(value_t& lhs, value_t x, value_t y) const
00168   {
00169     CoCoA_ASSERT(x == x%myModulus);
00170     CoCoA_ASSERT(y == y%myModulus);
00171     if (x == 0 || y == 0) lhs = 0;
00172     else lhs = myExp[myLog[x] + myLog[y]];
00173   }
00174 
00175 
00176   inline void SmallFpLogImpl::myDiv(value_t& lhs, value_t x, value_t y) const
00177   {
00178     CoCoA_ASSERT(x == x%myModulus);
00179     CoCoA_ASSERT(y == y%myModulus);
00180     CoCoA_ASSERT(!myIsZero(y));
00181     if (x == 0) lhs = 0;
00182     else lhs = myExp[myModulus-1 + myLog[x] - myLog[y]];
00183   }
00184 
00185 
00186   inline bool SmallFpLogImpl::myIsDivisible(value_t& lhs, value_t x, value_t y) const
00187   {
00188     CoCoA_ASSERT(x == x%myModulus);
00189     CoCoA_ASSERT(y == y%myModulus);
00190     if (y == 0) return false;
00191     myDiv(lhs, x, y);
00192     return true;
00193   }
00194 
00195 
00196   inline bool SmallFpLogImpl::myIsPrintAtom(value_t x) const
00197   {
00198     CoCoA_ASSERT(x == x%myModulus);
00199     return true;
00200   }
00201 
00202 
00203   inline bool SmallFpLogImpl::myIsPrintedWithMinus(value_t x) const
00204   {
00205     CoCoA_ASSERT(x == x%myModulus);
00206     return false;
00207   }
00208 
00209 
00210   inline bool SmallFpLogImpl::myIsZero(value_t x) const
00211   {
00212     CoCoA_ASSERT(x == x%myModulus);
00213     return x == 0;
00214   }
00215 
00216 
00217   inline bool SmallFpLogImpl::myIsOne(value_t x) const
00218   {
00219     CoCoA_ASSERT(x == x%myModulus);
00220     return x == 1;
00221   }
00222 
00223 
00224   inline bool SmallFpLogImpl::myIsMinusOne(value_t x) const
00225   {
00226     CoCoA_ASSERT(x == x%myModulus);
00227     return x == myModulus-1;
00228   }
00229 
00230 
00231   inline bool SmallFpLogImpl::myIsInteger(ZZ& N, value_t x) const
00232   {
00233     N = x; // would cause trouble if x were greater than the biggest signed long
00234     return true;
00235   }
00236 
00237 
00238   inline bool SmallFpLogImpl::myIsZeroAddMul(value_t& lhs, value_t y, value_t z) const
00239   {
00240     CoCoA_ASSERT(y == y%myModulus);
00241     CoCoA_ASSERT(z == z%myModulus);
00242     lhs = myReduceMod(lhs + y*z);
00243     //??? value_t tmp; myMul(tmp,y,z); myAdd(lhs, lhs, tmp); ???
00244     return myIsZero(lhs);
00245   }
00246 
00247 
00248   inline bool SmallFpLogImpl::myIsEqual(value_t x, value_t y) const
00249   {
00250     CoCoA_ASSERT(x == x%myModulus);
00251     CoCoA_ASSERT(y == y%myModulus);
00252     return x==y;
00253   }
00254 
00255 
00256 } // end of namespace CoCoA
00257 
00258 
00259 // RCS header/log in the next few lines
00260 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/SmallFpLogImpl.H,v 1.1.1.1 2007/03/09 15:16:11 abbott Exp $
00261 // $Log: SmallFpLogImpl.H,v $
00262 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
00263 // Imported files
00264 //
00265 // Revision 1.7  2007/03/08 18:42:05  cocoa
00266 // Cleaned up whitespace.
00267 //
00268 // Revision 1.6  2007/01/11 14:07:42  cocoa
00269 // -- changed names to arguments called "rsh"
00270 //
00271 // Revision 1.5  2006/12/06 17:30:02  cocoa
00272 // -- rearranged #include
00273 //
00274 // Revision 1.4  2006/11/24 17:22:05  cocoa
00275 // -- removed OpenMathFwd.H
00276 //
00277 // Revision 1.3  2006/11/02 13:25:44  cocoa
00278 // Simplification of header files: the OpenMath classes have been renamed.
00279 // Many minor consequential changes.
00280 //
00281 // Revision 1.2  2006/10/06 14:04:15  cocoa
00282 // Corrected position of #ifndef in header files.
00283 // Separated CoCoA_ASSERT into assert.H from config.H;
00284 // many minor consequential changes (have to #include assert.H).
00285 // A little tidying of #include directives (esp. in Max's code).
00286 //
00287 // Revision 1.1.1.1  2006/05/30 11:39:37  cocoa
00288 // Imported files
00289 //
00290 // Revision 1.5  2006/05/12 16:10:58  cocoa
00291 // Added OpenMathFwd.H, and tidied OpenMath.H.
00292 // Many consequential but trivial changes.
00293 //
00294 // Revision 1.4  2006/03/27 12:21:26  cocoa
00295 // Minor silly changes to reduce number of complaints from some compiler or other.
00296 //
00297 // Revision 1.3  2006/03/21 09:43:14  cocoa
00298 // Changed names of some member fns of ideals (dealing with setting and testing
00299 // the flags for primeness and maximality).  Hope icc will complain less now.
00300 //
00301 // Revision 1.2  2006/03/12 21:28:34  cocoa
00302 // Major check in after many changes
00303 //
00304 // Revision 1.1.1.1  2005/10/17 10:46:54  cocoa
00305 // Imported files
00306 //
00307 // Revision 1.5  2005/10/14 15:25:07  cocoa
00308 // Major tidying and cleaning to small prime finite fields.
00309 // Several consequential changes.  Improved their documentation.
00310 //
00311 // Added Makefile and script to include/CoCoA/ directory to
00312 // keep library.H up to date.
00313 //
00314 // Revision 1.4  2005/10/12 15:52:09  cocoa
00315 // Completed test-RingFp1 and corrected/cleaned the SmallFp*
00316 // and RingFp* files.
00317 //
00318 // Some minor tidying elsewhere.
00319 //
00320 // Revision 1.3  2005/10/11 16:37:30  cocoa
00321 // Added new small prime finite field class (see RingFpDouble).
00322 //
00323 // Cleaned makefiles and configuration script.
00324 //
00325 // Tidied PPMonoid code (to eliminate compiler warnings).
00326 //
00327 // Fixed bug in RingFloat::myIsInteger.
00328 //
00329 // Revision 1.2  2005/09/22 18:04:17  cocoa
00330 // It compiles; the tests run OK.  The examples compile.
00331 // No documentation -- the mindless eurocrats have rendered
00332 // me mindless too.
00333 //
00334 // Revision 1.1.1.1  2005/05/03 15:47:31  cocoa
00335 // Imported files
00336 //
00337 // Revision 1.4  2005/04/19 14:06:04  cocoa
00338 // Added GPL and GFDL licence stuff.
00339 //
00340 // Revision 1.3  2005/02/28 15:58:56  cocoa
00341 // Resynch after some minor changes.
00342 //
00343 // Revision 1.2  2005/02/11 16:45:24  cocoa
00344 // Removed the useless and misleading functions myInit and myKill
00345 // from the SmallFp*Impl classes; various consequential changes.
00346 //
00347 // Revision 1.1.1.1  2005/01/27 15:12:13  cocoa
00348 // Imported files
00349 //
00350 // Revision 1.4  2004/11/12 15:49:29  cocoa
00351 // Tidying prior to 0.90 release.
00352 // (a) documentation improved (or marked as poor)
00353 // (b) sundry minor improvements to the code
00354 //
00355 // Revision 1.3  2004/11/04 18:47:42  cocoa
00356 // (1) Ring member functions which previously expected mpz_t args
00357 //     now expect ZZ args.  Numerous minor consequential changes.
00358 // (2) Renamed function which gives access to the mpz_t value inside
00359 //     a ZZ object: previously was raw(...), now is mpzref(...).
00360 //     Plenty of calls had to be altered.
00361 //
00362 // Revision 1.2  2004/07/16 15:45:12  cocoa
00363 // First stage of new RingElem implementation completed.
00364 //
00365 // Revision 1.1  2004/07/14 16:40:42  cocoa
00366 // Separated RingFpLog from its implementation which now resides in
00367 // a new class: SmallFpLogImpl.  This is analogous to the change made
00368 // to RingFp yesterday.
00369 //
00370 // Some tidying and other sundry minor changes.
00371 //
00372 //
00373 
00374 
00375 #endif

Generated on Wed May 23 13:43:36 2007 for CoCoALib by  doxygen 1.4.6