00001 #line 6220 "MIN_CYCLE_BASIS.lw"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
00089 template<class T>
00090 T fp<T>::ext_gcd( T& a, T& b, T& x, T& y ) {
00091
00092
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
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
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
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
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
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
00230
00231 spvecfp& operator=( const spvecfp& i );
00232
00233
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
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
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 }
00365
00366 #endif // FP_H
00367