source: git/factory/gfops.cc @ bd7a8b

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