1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: gfops.cc,v 1.8 2008-08-20 14:44:49 Singular Exp $ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #ifdef HAVE_CSTDIO |
---|
7 | #include <cstdio> |
---|
8 | #include <cstdlib> |
---|
9 | #else |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #endif |
---|
13 | #include <string.h> |
---|
14 | |
---|
15 | #include "assert.h" |
---|
16 | |
---|
17 | #include "cf_defs.h" |
---|
18 | #include "gf_tabutil.h" |
---|
19 | #include "cf_util.h" |
---|
20 | #include "canonicalform.h" |
---|
21 | #include "variable.h" |
---|
22 | #ifdef SINGULAR |
---|
23 | #include "singext.h" |
---|
24 | #endif |
---|
25 | #include "gfops.h" |
---|
26 | |
---|
27 | |
---|
28 | const int gf_maxtable = 63001; |
---|
29 | const int gf_maxbuffer = 200; |
---|
30 | |
---|
31 | const int gf_primes_len = 42; |
---|
32 | static unsigned short gf_primes [] = |
---|
33 | { |
---|
34 | 2, 3, 5, 7, 11, 13, 17, 19, |
---|
35 | 23, 29, 31, 37, 41, 43, 47, 53, |
---|
36 | 59, 61, 67, 71, 73, 79, 83, 89, |
---|
37 | 97, 101, 103, 107, 109, 113, 127, 131, |
---|
38 | 137, 139, 149, 151, 157, 163, 167, 173, |
---|
39 | 179, 181, 191, 193, 197, 199, 223, 211, |
---|
40 | 227, 229, 233, 239, 241, 251 |
---|
41 | }; |
---|
42 | |
---|
43 | int gf_q = 0; |
---|
44 | int gf_p = 0; |
---|
45 | int gf_n = 0; |
---|
46 | int gf_q1 = 0; |
---|
47 | int gf_m1 = 0; |
---|
48 | char gf_name = 'Z'; |
---|
49 | |
---|
50 | unsigned short * gf_table = 0; |
---|
51 | |
---|
52 | CanonicalForm gf_mipo = 0; |
---|
53 | |
---|
54 | static CanonicalForm |
---|
55 | intVec2CF ( int degree, int * coeffs, int level ) |
---|
56 | { |
---|
57 | int i; |
---|
58 | CanonicalForm result; |
---|
59 | for ( i = 0; i <= degree; i++ ) |
---|
60 | { |
---|
61 | result += CanonicalForm( coeffs[ i ] ) * power( Variable( level ), degree - i ); |
---|
62 | } |
---|
63 | return result; |
---|
64 | } |
---|
65 | |
---|
66 | static void |
---|
67 | gf_get_table ( int p, int n ) |
---|
68 | { |
---|
69 | char buffer[gf_maxbuffer]; |
---|
70 | int q = ipower( p, n ); |
---|
71 | if ( gf_table == 0 ) |
---|
72 | gf_table = new unsigned short[gf_maxtable]; |
---|
73 | |
---|
74 | // do not read the table a second time |
---|
75 | if ( gf_q == q ) |
---|
76 | { |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | #ifdef SINGULAR |
---|
81 | // just copy the table if Singular already read it |
---|
82 | if ( q == nfCharQ ) |
---|
83 | { |
---|
84 | gf_p = p; gf_n = n; |
---|
85 | gf_q = q; gf_q1 = q - 1; |
---|
86 | gf_m1 = nfM1; |
---|
87 | gf_mipo = intVec2CF( nfMinPoly[0], nfMinPoly + 1, 1 ); |
---|
88 | (void)memcpy( gf_table, nfPlus1Table, gf_q * sizeof( unsigned short ) ); |
---|
89 | gf_table[gf_q] = 0; |
---|
90 | return; |
---|
91 | } |
---|
92 | #endif |
---|
93 | |
---|
94 | // try to open file |
---|
95 | #ifndef SINGULAR |
---|
96 | sprintf( buffer, GFTABLEDIR "/gftable.%d.%d", p, n ); |
---|
97 | FILE * inputfile = fopen( buffer, "r" ); |
---|
98 | #else |
---|
99 | sprintf( buffer, "gftables/%d", q ); |
---|
100 | FILE * inputfile = feFopen( buffer, "r" ); |
---|
101 | #endif |
---|
102 | STICKYASSERT( inputfile, "can not open GF(q) table" ); |
---|
103 | |
---|
104 | // read ID |
---|
105 | char * bufptr; |
---|
106 | char * success; |
---|
107 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
108 | STICKYASSERT( success, "illegal table (reading ID)" ); |
---|
109 | STICKYASSERT( strcmp( buffer, "@@ factory GF(q) table @@\n" ) == 0, "illegal table" ); |
---|
110 | // read p and n from file |
---|
111 | int pFile, nFile; |
---|
112 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
113 | STICKYASSERT( success, "illegal table (reading p and n)" ); |
---|
114 | sscanf( buffer, "%d %d", &pFile, &nFile ); |
---|
115 | STICKYASSERT( p == pFile && n == nFile, "illegal table" ); |
---|
116 | // skip (sic!) factory-representation of mipo |
---|
117 | // and terminating "; " |
---|
118 | bufptr = (char *)strchr( buffer, ';' ) + 2; |
---|
119 | // read simple representation of mipo |
---|
120 | int i, degree; |
---|
121 | sscanf( bufptr, "%d", °ree ); |
---|
122 | bufptr = (char *)strchr( bufptr, ' ' ) + 1; |
---|
123 | int * mipo = new int[degree + 1]; |
---|
124 | for ( i = 0; i <= degree; i++ ) |
---|
125 | { |
---|
126 | sscanf( bufptr, "%d", mipo + i ); |
---|
127 | bufptr = (char *)strchr( bufptr, ' ' ) + 1; |
---|
128 | } |
---|
129 | |
---|
130 | gf_p = p; gf_n = n; |
---|
131 | gf_q = q; gf_q1 = q-1; |
---|
132 | gf_mipo = intVec2CF( degree, mipo, 1 ); |
---|
133 | delete [] mipo; |
---|
134 | |
---|
135 | // now for the table |
---|
136 | int k, digs = gf_tab_numdigits62( gf_q ); |
---|
137 | i = 1; |
---|
138 | while ( i < gf_q ) |
---|
139 | { |
---|
140 | success = fgets( buffer, gf_maxbuffer, inputfile ); |
---|
141 | STICKYASSERT( strlen( buffer ) - 1 == (size_t)digs * 30, "illegal table" ); |
---|
142 | bufptr = buffer; |
---|
143 | k = 0; |
---|
144 | while ( i < gf_q && k < 30 ) |
---|
145 | { |
---|
146 | gf_table[i] = convertback62( bufptr, digs ); |
---|
147 | bufptr += digs; |
---|
148 | if ( gf_table[i] == gf_q ) |
---|
149 | if ( i == gf_q1 ) |
---|
150 | gf_m1 = 0; |
---|
151 | else |
---|
152 | gf_m1 = i; |
---|
153 | i++; k++; |
---|
154 | } |
---|
155 | } |
---|
156 | gf_table[0] = gf_table[gf_q1]; |
---|
157 | gf_table[gf_q] = 0; |
---|
158 | |
---|
159 | (void)fclose( inputfile ); |
---|
160 | } |
---|
161 | |
---|
162 | static bool |
---|
163 | gf_valid_combination ( int p, int n ) |
---|
164 | { |
---|
165 | int i = 0; |
---|
166 | while ( i < gf_primes_len && gf_primes[i] != p ) i++; |
---|
167 | if ( i == gf_primes_len ) |
---|
168 | return false; |
---|
169 | else |
---|
170 | { |
---|
171 | i = n; |
---|
172 | int a = 1; |
---|
173 | while ( a < gf_maxtable && i > 0 ) |
---|
174 | { |
---|
175 | a *= p; |
---|
176 | i--; |
---|
177 | } |
---|
178 | if ( i > 0 || a > gf_maxtable ) |
---|
179 | return false; |
---|
180 | else |
---|
181 | return true; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | void |
---|
186 | gf_setcharacteristic ( int p, int n, char name ) |
---|
187 | { |
---|
188 | ASSERT( gf_valid_combination( p, n ), "illegal immediate GF(q)" ); |
---|
189 | gf_name = name; |
---|
190 | gf_get_table( p, n ); |
---|
191 | } |
---|
192 | |
---|
193 | int |
---|
194 | gf_gf2ff ( int a ) |
---|
195 | { |
---|
196 | if ( gf_iszero( a ) ) |
---|
197 | return 0; |
---|
198 | else |
---|
199 | { |
---|
200 | // starting from z^0=1, step through the table |
---|
201 | // counting the steps until we hit z^a or z^0 |
---|
202 | // again. since we are working in char(p), the |
---|
203 | // latter is guaranteed to be fulfilled. |
---|
204 | int i = 0, ff = 1; |
---|
205 | do |
---|
206 | { |
---|
207 | if ( i == a ) |
---|
208 | return ff; |
---|
209 | ff++; |
---|
210 | i = gf_table[i]; |
---|
211 | } while ( i != 0 ); |
---|
212 | return -1; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | bool |
---|
217 | gf_isff ( int a ) |
---|
218 | { |
---|
219 | if ( gf_iszero( a ) ) |
---|
220 | return true; |
---|
221 | else |
---|
222 | { |
---|
223 | // z^a in GF(p) iff (z^a)^p-1=1 |
---|
224 | return gf_isone( gf_power( a, gf_p - 1 ) ); |
---|
225 | } |
---|
226 | } |
---|