[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[2dd068] | 2 | |
---|
| 3 | #ifndef INCL_GFOPS_H |
---|
| 4 | #define INCL_GFOPS_H |
---|
| 5 | |
---|
[e4fe2b] | 6 | // #include "config.h" |
---|
[25c3c2b] | 7 | |
---|
[bb20e7] | 8 | #ifndef NOSTREAMIO |
---|
[1dc616] | 9 | #ifdef HAVE_IOSTREAM |
---|
| 10 | #include <iostream> |
---|
[181148] | 11 | #define OSTREAM std::ostream |
---|
[1dc616] | 12 | #elif defined(HAVE_IOSTREAM_H) |
---|
[2dd068] | 13 | #include <iostream.h> |
---|
[181148] | 14 | #define OSTREAM ostream |
---|
[1dc616] | 15 | #endif |
---|
[bb20e7] | 16 | #endif /* NOSTREAMIO */ |
---|
[2dd068] | 17 | |
---|
[650f2d8] | 18 | #include "cf_assert.h" |
---|
[bb20e7] | 19 | |
---|
[2dd068] | 20 | #include "cf_defs.h" |
---|
[25c3c2b] | 21 | #include "canonicalform.h" |
---|
[2dd068] | 22 | |
---|
| 23 | extern int gf_q; |
---|
| 24 | extern int gf_p; |
---|
| 25 | extern int gf_n; |
---|
| 26 | extern int gf_q1; |
---|
| 27 | extern int gf_m1; |
---|
| 28 | extern char gf_name; |
---|
| 29 | |
---|
| 30 | extern unsigned short * gf_table; |
---|
| 31 | |
---|
[25c3c2b] | 32 | extern CanonicalForm gf_mipo; |
---|
[2dd068] | 33 | |
---|
[2adb1c] | 34 | //{{{ predicates |
---|
| 35 | inline bool gf_iszero ( int a ) |
---|
| 36 | { |
---|
| 37 | return gf_q == a; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | inline bool gf_isone ( int a ) |
---|
| 41 | { |
---|
| 42 | return 0 == a; |
---|
| 43 | } |
---|
| 44 | //}}} |
---|
| 45 | |
---|
| 46 | //{{{ conversion functions |
---|
[2dd068] | 47 | inline int gf_int2gf ( int i ) |
---|
| 48 | { |
---|
| 49 | while ( i < 0 ) |
---|
| 50 | i += gf_p; |
---|
| 51 | while ( i >= gf_p ) |
---|
| 52 | i -= gf_p; |
---|
| 53 | if ( i == 0 ) |
---|
| 54 | return gf_q; |
---|
| 55 | int c = 0; |
---|
| 56 | while ( i > 1 ) { |
---|
| 57 | c = gf_table[c]; |
---|
| 58 | i--; |
---|
| 59 | } |
---|
| 60 | return c; |
---|
| 61 | } |
---|
[2adb1c] | 62 | //}}} |
---|
[2dd068] | 63 | |
---|
[2adb1c] | 64 | //{{{ zero and one |
---|
[2dd068] | 65 | inline int gf_zero() |
---|
| 66 | { |
---|
| 67 | return gf_q; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | inline int gf_one() |
---|
| 71 | { |
---|
| 72 | return 0; |
---|
| 73 | } |
---|
[2adb1c] | 74 | //}}} |
---|
[2dd068] | 75 | |
---|
[987a48] | 76 | //{{{ inline int gf_sign ( int a ) |
---|
[2adb1c] | 77 | // docu: see imm_sign() |
---|
[987a48] | 78 | inline |
---|
| 79 | int gf_sign ( int a ) |
---|
[2dd068] | 80 | { |
---|
| 81 | if ( gf_iszero( a ) ) |
---|
| 82 | return 0; |
---|
| 83 | else |
---|
| 84 | return 1; |
---|
| 85 | } |
---|
[987a48] | 86 | //}}} |
---|
[2dd068] | 87 | |
---|
[2adb1c] | 88 | //{{{ arithmetic operators |
---|
[2dd068] | 89 | inline int gf_neg ( int a ) |
---|
| 90 | { |
---|
| 91 | // -z^a=z^a*(-1)=z^a*gf_m1; |
---|
| 92 | if ( a == gf_q ) return a; |
---|
| 93 | int i = a + gf_m1; |
---|
| 94 | if ( i >= gf_q1 ) |
---|
| 95 | i -= gf_q1; |
---|
| 96 | return i; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | inline int gf_add ( int a, int b ) |
---|
| 100 | { |
---|
| 101 | // z^a+z^b=z^b*(z^(a-b)+1), if a>=b; |
---|
| 102 | // =z^a*(z^(b-a)+1), if a<b; |
---|
| 103 | if ( a == gf_q ) return b; |
---|
| 104 | if ( b == gf_q ) return a; |
---|
| 105 | int zb, zab, r; |
---|
| 106 | if ( a >= b ) { |
---|
| 107 | zb = b; |
---|
| 108 | zab = a - b; |
---|
| 109 | } |
---|
| 110 | else { |
---|
| 111 | zb = a; |
---|
| 112 | zab = b - a; |
---|
| 113 | } |
---|
| 114 | if ( gf_table[zab] == gf_q ) |
---|
| 115 | r = gf_q; /*if z^(a-b)+1 =0*/ |
---|
| 116 | else { |
---|
| 117 | r= zb + gf_table[zab]; |
---|
| 118 | if ( r >= gf_q1 ) |
---|
| 119 | r -= gf_q1; |
---|
| 120 | } |
---|
| 121 | return r; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | inline int gf_sub ( int a, int b ) |
---|
| 125 | { |
---|
| 126 | return gf_add( a, gf_neg( b ) ); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | inline int gf_mul ( int a, int b ) |
---|
| 130 | { |
---|
| 131 | if ( a == gf_q || b == gf_q ) |
---|
| 132 | return gf_q; |
---|
| 133 | else { |
---|
| 134 | int i = a + b; |
---|
| 135 | if ( i >= gf_q1 ) i -= gf_q1; |
---|
| 136 | return i; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | inline int gf_div ( int a, int b ) |
---|
| 141 | { |
---|
| 142 | ASSERT( b != gf_q, "divide by zero" ); |
---|
| 143 | if ( a == gf_q ) |
---|
| 144 | return gf_q; |
---|
| 145 | else { |
---|
| 146 | int s = a - b; |
---|
| 147 | if (s < 0) |
---|
| 148 | s += gf_q1; |
---|
| 149 | return s; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | inline int gf_inv ( int a ) |
---|
| 154 | { |
---|
| 155 | ASSERT( a != gf_q, "divide by zero" ); |
---|
| 156 | return gf_q1 - a; |
---|
| 157 | } |
---|
[2adb1c] | 158 | //}}} |
---|
[2dd068] | 159 | |
---|
[2adb1c] | 160 | //{{{ input/output |
---|
[bb20e7] | 161 | #ifndef NOSTREAMIO |
---|
[181148] | 162 | inline void gf_print ( OSTREAM & os, int a ) |
---|
[2dd068] | 163 | { |
---|
| 164 | if ( a == gf_q ) |
---|
| 165 | os << "0"; |
---|
| 166 | else if ( a == 0 ) |
---|
| 167 | os << "1"; |
---|
| 168 | else if ( a == 1 ) |
---|
| 169 | os << gf_name; |
---|
| 170 | else |
---|
| 171 | os << gf_name << "^" << a; |
---|
| 172 | } |
---|
[bb20e7] | 173 | #endif /* NOSTREAMIO */ |
---|
[2adb1c] | 174 | //}}} |
---|
[2dd068] | 175 | |
---|
[2adb1c] | 176 | //{{{ exponentation |
---|
[2dd068] | 177 | inline int gf_power ( int a, int n ) |
---|
| 178 | { |
---|
| 179 | if ( n == 0 ) |
---|
| 180 | return 0; |
---|
| 181 | else if ( n == 1 ) |
---|
| 182 | return a; |
---|
| 183 | else |
---|
| 184 | return gf_mul( a, gf_power( a, n-1 ) ); |
---|
| 185 | } |
---|
[2adb1c] | 186 | //}}} |
---|
[2dd068] | 187 | |
---|
| 188 | void gf_setcharacteristic ( int p, int n, char name ); |
---|
| 189 | |
---|
[25c3c2b] | 190 | // Singular needs this |
---|
| 191 | /*BEGINPUBLIC*/ |
---|
| 192 | |
---|
[2dd068] | 193 | int gf_gf2ff ( int a ); |
---|
| 194 | |
---|
| 195 | bool gf_isff ( int a ); |
---|
| 196 | |
---|
[25c3c2b] | 197 | /*ENDPUBLIC*/ |
---|
| 198 | |
---|
[493c477] | 199 | #endif /* ! INCL_GFOPS_H */ |
---|