source: git/factory/gfops.cc @ 38b236

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