source: git/factory/gfops.cc @ ac8e1a

spielwiese
Last change on this file since ac8e1a was ac8e1a, checked in by Martin Lee <martinlee84@…>, 13 years ago
compiler warnings git-svn-id: file:///usr/local/Singular/svn/trunk@14267 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id$ */
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
28const int gf_maxtable = 63001;
29const int gf_maxbuffer = 200;
30
31const 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
43int gf_q = 0;
44int gf_p = 0;
45int gf_n = 0;
46int gf_q1 = 0;
47int gf_m1 = 0;
48char gf_name = 'Z';
49
50unsigned short * gf_table = 0;
51
52CanonicalForm gf_mipo(0);
53
54static CanonicalForm intVec2CF ( int degree, int * coeffs, int level )
55{
56    int i;
57    CanonicalForm result;
58    for ( i = 0; i <= degree; i++ )
59    {
60        result += CanonicalForm( coeffs[ i ] ) * power( Variable( level ), degree - i );
61    }
62    return result;
63}
64
65static void gf_get_table ( int p, int n )
66{
67    char buffer[gf_maxbuffer];
68    int q = ipower( p, n );
69
70    // do not read the table a second time
71    if ( gf_q == q )
72    {
73        return;
74    }
75
76    if ( gf_table == 0 )
77        gf_table = new unsigned short[gf_maxtable];
78
79#ifdef SINGULAR
80    // just copy the table if Singular already read it
81    //printf("init_gf(gf_get_table) q=%d, nfCharQ=%d\n",q,nfCharQ);
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", &degree );
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            {
150                if ( i == gf_q1 )
151                    gf_m1 = 0;
152                else
153                    gf_m1 = i;
154            }
155            i++; k++;
156        }
157    }
158    gf_table[0] = gf_table[gf_q1];
159    gf_table[gf_q] = 0;
160
161    (void)fclose( inputfile );
162}
163
164//static bool gf_valid_combination ( int p, int n )
165//{
166//    int i = 0;
167//    while ( i < gf_primes_len && gf_primes[i] != p ) i++;
168//    if ( i == gf_primes_len )
169//        return false;
170//    else
171//    {
172//        i = n;
173//        int a = 1;
174//        while ( a < gf_maxtable && i > 0 )
175//        {
176//            a *= p;
177//            i--;
178//        }
179//        if ( i > 0 || a > gf_maxtable )
180//            return false;
181//        else
182//            return true;
183//    }
184//}
185
186void 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
193int gf_gf2ff ( int a )
194{
195    if ( gf_iszero( a ) )
196        return 0;
197    else
198    {
199        // starting from z^0=1, step through the table
200        // counting the steps until we hit z^a or z^0
201        // again.  since we are working in char(p), the
202        // latter is guaranteed to be fulfilled.
203        int i = 0, ff = 1;
204        do
205        {
206            if ( i == a )
207                return ff;
208            ff++;
209            i = gf_table[i];
210        } while ( i != 0 );
211        return -1;
212    }
213}
214
215bool gf_isff ( int a )
216{
217    if ( gf_iszero( a ) )
218        return true;
219    else
220    {
221        // z^a in GF(p) iff (z^a)^p-1=1
222        return gf_isone( gf_power( a, gf_p - 1 ) );
223    }
224}
Note: See TracBrowser for help on using the repository browser.