fp.h

Go to the documentation of this file.
00001 #line 6220 "MIN_CYCLE_BASIS.lw"
00002 //---------------------------------------------------------------------
00003 // File automatically generated using notangle from DMIN_CYCLE_BASIS.lw
00004 //
00005 // emails and bugs: Dimitris Michail <michail@mpi-inf.mpg.de>
00006 //---------------------------------------------------------------------
00007 //
00008 // This program can be freely used in an academic environment
00009 // ONLY for research purposes, subject to the following restrictions:
00010 //
00011 // 1. The origin of this software must not be misrepresented; you must not
00012 //    claim that you wrote the original software. If you use this software
00013 //    an acknowledgment in the product documentation is required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 // Any other use is strictly prohibited by the author, without an explicit 
00019 // permission.
00020 //
00021 // Note that this program uses the LEDA library, which is NOT free. For more 
00022 // details visit Algorithmic Solutions at http://www.algorithmic-solutions.com/
00023 //
00024 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00025 // ! Any commercial use of this software is strictly !
00026 // ! prohibited without explicit permission by the   !
00027 // ! author.                                         !
00028 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00029 //
00030 // This software is distributed in the hope that it will be useful,
00031 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00032 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00033 //
00034 // Copyright (C) 2004-2005 - Dimitris Michail <michail@mpi-inf.mpg.de>
00035 
00036 
00037 #line 7307 "MIN_CYCLE_BASIS.lw"
00038 
00042 #ifndef FP_H
00043 #define FP_H
00044 
00045 #include <iostream>
00046 
00047 #ifdef LEDA_GE_V5
00048 #include <LEDA/core/array.h>
00049 #include <LEDA/core/list.h>
00050 #include <LEDA/core/tuple.h>
00051 #include <LEDA/system/error.h>
00052 #include <LEDA/system/assert.h>
00053 #else
00054 #include <LEDA/array.h>
00055 #include <LEDA/list.h>
00056 #include <LEDA/tuple.h>
00057 #include <LEDA/error.h>
00058 #include <LEDA/assert.h>
00059 #endif
00060 #include <LEP/mcb/edge_num.h>
00061 #include <LEP/mcb/arithm.h>
00062 
00063 namespace mcb { 
00064 
00065 #if defined(LEDA_NAMESPACE)
00066     using leda::list;
00067     using leda::list_item;
00068     using leda::error_handler;
00069     using leda::two_tuple;
00070     using leda::three_tuple;
00071     using leda::array;
00072 #endif
00073 
00074     
00075 #line 7370 "MIN_CYCLE_BASIS.lw"
00076 template<class T>
00077 class fp { 
00078 
00079     public:
00080         // extended euclidean gcd algorithm
00081         static T ext_gcd( T& a, T& b, T& x, T& y );
00082         static T get_mult_inverse( T& a, T& p );
00083 
00084 };
00085 
00086 
00087 #line 7382 "MIN_CYCLE_BASIS.lw"
00088 // extended euclidean gcd algorithm
00089 template<class T>
00090 T fp<T>::ext_gcd( T& a, T& b, T& x, T& y ) { 
00091 
00092     // initialize
00093     T _x[2], _y[2], _a[2], q;
00094     bool swap, aneg, bneg;
00095     indextype i;
00096 
00097     _x[0] = 1; _x[1] = 0; _y[0] = 0; _y[1] = 1;
00098     aneg = a < 0;
00099     bneg = b < 0;
00100 
00101     a = ( a < 0 )? -a: a;
00102     b = ( b < 0 )? -b: b;
00103     if ( a == 0 ) { y = bneg?-1:1; return b; }
00104     if ( b == 0 ) { x = bneg?-1:1; return a; }
00105 
00106     // swap arguments appropriately
00107     _a[0] = a;  
00108     _a[1] = b;
00109     swap = false;
00110     if ( b > a ) { _a[0] = b; _a[1] = a; swap = true; }
00111 
00112     // do the work
00113     i = 0; 
00114     while( true ) {
00115         q = _a[ i ] / _a[ 1 - i ];
00116         if ( _a[ i ] % _a[ 1 - i ] == 0 ) break;
00117         _a[ i ] = _a[ i ] % _a[ 1 - i ];
00118         _x[ i ] = _x[ i ] - q * _x[ 1 - i ];
00119         _y[ i ] = _y[ i ] - q * _y[ 1 - i ];
00120         i = 1 - i;
00121     }
00122 
00123     // did we swap arguments?
00124     if ( swap ) {
00125         x = _y[ 1 - i ] * ( aneg?-1:1);
00126         y = _x[ 1 - i ] * ( bneg?-1:1);
00127     }
00128     else {
00129         x = _x[ 1 - i ] * ( aneg?-1:1 );
00130         y = _y[ 1 - i ] * ( bneg?-1:1 );
00131     }
00132 
00133 #if ! defined(LEDA_CHECKING_OFF)
00134     assert( _a[ 1 - i ] == ( (aneg)?(-a):(a) ) * x + 
00135             ( (bneg)?(-b):(b) ) * y ); 
00136 #endif
00137     return _a[ 1 - i ];
00138 }
00139 
00140 
00141 #line 7442 "MIN_CYCLE_BASIS.lw"
00142 // compute multiplication inverse of an element
00143 template<class T>
00144 T fp<T>::get_mult_inverse( T& a, T& p ) { 
00145 #if ! defined(LEDA_CHECKING_OFF)
00146     if ( p <= 0 ) 
00147         leda::error_handler(999, "MIN_CYCLE_BASIS: p is \
00148 non-positive");
00149 #endif
00150     T x, y;
00151     if ( fp<T>::ext_gcd( a, p, x, y ) != 1 )
00152         leda::error_handler(999, "MIN_CYCLE_BASIS: mult inverse\
00153 does not exist");
00154     return x;
00155 }
00156 
00157 
00158 #line 7465 "MIN_CYCLE_BASIS.lw"
00159 template<class T>
00160 class primes { 
00161 
00162 public:
00163     // check if a number is prime
00164     static bool is_prime( const T& p ) { 
00165         if ( p == T(1) ) return true;
00166         T t = T(2);
00167 #if ! defined(LEDA_CHECKING_OFF)
00168         assert( p >= t );
00169 #endif
00170         if ( p % 2 == 0 ) return false;
00171         T zero = T(0);
00172         T sqrtt = T( sqrt(p) ) + 1;
00173 #if ! defined(LEDA_CHECKING_OFF)
00174         if ( sqrtt * sqrtt < p ) 
00175             leda::error_handler(999,"MIN_CYCLE_BASIS: is_prime: \
00176 error calculating square"); 
00177 #endif
00178         while( t <= sqrtt ) { 
00179             if ( p % t == zero ) return false;
00180             t++;
00181         }
00182         return true;
00183     }
00184 };
00185 
00186 
00187 
00188 
00189 #line 7509 "MIN_CYCLE_BASIS.lw"
00190 
00205 class spvecfp { 
00206     
00207     public:
00208 
00210     spvecfp();
00215     spvecfp( indextype len, const ptype& p );
00216     
00218     spvecfp( const spvecfp& a );
00219 
00221     ~spvecfp();
00222 
00227     void reset( indextype len, const ptype& p );
00228 
00229     // assign a vector to the current vector
00230     // cast between types, retain the current's vector type
00231     spvecfp& operator=( const spvecfp& i );
00232 
00233     // assign current vector to e_i
00234     spvecfp& operator=( const indextype& i );
00235 
00239     spvecfp operator-() const;
00240 
00245     ptype operator*( const spvecfp& a ) const;
00246 
00251     spvecfp operator+( const spvecfp& a ) const;
00252 
00257     spvecfp operator*( const ptype& a );
00258 
00263     spvecfp& operator+=( const spvecfp& a );
00264 
00269     spvecfp& operator-=( const spvecfp& a );
00270 
00274     void print( std::ostream& o ) const;
00275 
00285     void append( indextype index, const ptype& value );
00286 
00287     // define access stuff, like leda lists
00288 
00292     bool empty() const;
00293 
00295     void clear();
00296 
00300     indextype size() const;
00301 
00305     indextype max_length() const;
00306 
00310     ptype pvalue() const;
00311 
00316     list_item first() const;
00317     
00322     list_item last() const;
00323 
00328     list_item succ( list_item it ) const;
00329 
00334     list_item pred( list_item it ) const;
00335 
00339     indextype index( list_item it ) const;
00340 
00344     ptype inf( list_item it ) const;
00345 
00346     private:
00347 
00348     typedef two_tuple<indextype,ptype> entry;
00349     // list of tuples (tuple = < index, value >)
00350     list< entry > l;
00351     ptype p;
00352     indextype len;
00353 };
00354 
00360 std::ostream& operator<<( std::ostream& o, const spvecfp& v );
00361 
00362 
00363 #line 7345 "MIN_CYCLE_BASIS.lw"
00364 } // end of mcb namespace
00365 
00366 #endif // FP_H
00367 

Generated on Thu Mar 30 16:45:43 2006 for mcb LEDA Extension Package by  doxygen 1.4.6