source: git/factory/gfops.cc @ a472b9

spielwiese
Last change on this file since a472b9 was a472b9, checked in by Jens Schmidt <schmidt@…>, 26 years ago
* gfops.cc, int_int.cc, readcf.y: order of includes fixed git-svn-id: file:///usr/local/Singular/svn/trunk@2131 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.5 1998-06-12 14:33:41 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 "gf_tabutil.h"
13#include "cf_util.h"
14#include "canonicalform.h"
15#include "variable.h"
16#ifdef SINGULAR
17#include "singext.h"
18#endif
19#include "gfops.h"
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    FILE * inputfile = fopen( buffer, "r" );
88#else
89    sprintf( buffer, "gftables/%d", q );
90    FILE * inputfile = feFopen( buffer, "r" );
91#endif
92    STICKYASSERT( inputfile, "can not open GF(q) table" );
93
94    // read ID
95    char * bufptr;
96    char * success;
97    success = fgets( buffer, gf_maxbuffer, inputfile );
98    STICKYASSERT( success, "illegal table (reading ID)" );
99    STICKYASSERT( strcmp( buffer, "@@ factory GF(q) table @@\n" ) == 0, "illegal table" );
100    // read p and n from file
101    int pFile, nFile;
102    success = fgets( buffer, gf_maxbuffer, inputfile );
103    STICKYASSERT( success, "illegal table (reading p and n)" );
104    sscanf( buffer, "%d %d", &pFile, &nFile );
105    STICKYASSERT( p == pFile && n == nFile, "illegal table" );
106    // skip (sic!) factory-representation of mipo
107    // and terminating "; "
108    bufptr = (char *)strchr( buffer, ';' ) + 2;
109    // read simple representation of mipo
110    int i, degree;
111    sscanf( bufptr, "%d", &degree );
112    bufptr = (char *)strchr( bufptr, ' ' ) + 1;
113    int * mipo = new int[degree + 1];
114    for ( i = 0; i <= degree; i++ ) {
115        sscanf( bufptr, "%d", mipo + i );
116        bufptr = (char *)strchr( bufptr, ' ' ) + 1;
117    }
118
119    gf_p = p; gf_n = n;
120    gf_q = q; gf_q1 = q-1;
121    gf_mipo = intVec2CF( degree, mipo, 1 );
122    delete [] mipo;
123
124    // now for the table
125    int k, digs = gf_tab_numdigits62( gf_q );
126    i = 1;
127    while ( i < gf_q ) {
128        success = fgets( buffer, gf_maxbuffer, inputfile );
129        STICKYASSERT( strlen( buffer ) - 1 == (size_t)digs * 30, "illegal table" );
130        bufptr = buffer;
131        k = 0;
132        while ( i < gf_q && k < 30 ) {
133            gf_table[i] = convertback62( bufptr, digs );
134            bufptr += digs;
135            if ( gf_table[i] == gf_q )
136                if ( i == gf_q1 )
137                    gf_m1 = 0;
138                else
139                    gf_m1 = i;
140            i++; k++;
141        }
142    }
143    gf_table[0] = gf_table[gf_q1];
144    gf_table[gf_q] = 0;
145
146    (void)fclose( inputfile );
147}
148
149static bool
150gf_valid_combination ( int p, int n )
151{
152    int i = 0;
153    while ( i < gf_primes_len && gf_primes[i] != p ) i++;
154    if ( i == gf_primes_len )
155        return false;
156    else {
157        i = n;
158        int a = 1;
159        while ( a < gf_maxtable && i > 0 ) {
160            a *= p;
161            i--;
162        }
163        if ( i > 0 || a > gf_maxtable )
164            return false;
165        else
166            return true;
167    }
168}
169
170void
171gf_setcharacteristic ( int p, int n, char name )
172{
173    ASSERT( gf_valid_combination( p, n ), "illegal immediate GF(q)" );
174    gf_name = name;
175    gf_get_table( p, n );
176}
177
178int
179gf_gf2ff ( int a )
180{
181    if ( gf_iszero( a ) )
182        return 0;
183    else {
184        // starting from z^0=1, step through the table
185        // counting the steps until we hit z^a or z^0
186        // again.  since we are working in char(p), the
187        // latter is guaranteed to be fulfilled.
188        int i = 0, ff = 1;
189        do {
190            if ( i == a )
191                return ff;
192            ff++;
193            i = gf_table[i];
194        } while ( i != 0 );
195        return -1;
196    }
197}
198
199bool
200gf_isff ( int a )
201{
202    if ( gf_iszero( a ) )
203        return true;
204    else {
205        // z^a in GF(p) iff (z^a)^p-1=1
206        return gf_isone( gf_power( a, gf_p - 1 ) );
207    }
208}
Note: See TracBrowser for help on using the repository browser.