source: git/factory/cf_map_ext.cc @ 202df9

fieker-DuValspielwiese
Last change on this file since 202df9 was 08daea, checked in by Martin Lee <martinlee84@…>, 13 years ago
new ezgcd, heuristic which gcd to choose and sparse modular gcd all for finite fields git-svn-id: file:///usr/local/Singular/svn/trunk@13661 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.5 KB
Line 
1// -*- c++ -*-
2//*****************************************************************************
3/** @file cf_map_ext.cc
4 *
5 * @author Martin Lee
6 * @date   16.11.2009
7 *
8 * This file implements functions to map between extensions of finite fields
9 *
10 * @par Copyright:
11 *   (c) by The SINGULAR Team, see LICENSE file
12 *
13 * @internal
14 * @version \$Id$
15 *
16**/
17//*****************************************************************************
18
19#include <config.h>
20
21#include "assert.h"
22#include "debug.h"
23
24#include "canonicalform.h"
25#include "cf_util.h"
26
27#ifdef HAVE_NTL
28#include <NTL/ZZ_pEXFactoring.h>
29#include "NTLconvert.h"
30#endif
31
32// cyclotomoic polys:
33#include "cf_cyclo.h"
34
35#include "cf_map_ext.h"
36
37#ifdef HAVE_NTL
38
39/// helper function
40static inline
41int findItem (const CFList& list, const CanonicalForm& item)
42{
43  int result= 1;
44  for (CFListIterator i= list; i.hasItem(); i++, result++)
45  {
46    if (i.getItem() == item)
47      return result;
48  }
49  return 0;
50}
51
52/// helper function
53static inline
54CanonicalForm getItem (const CFList& list, const int& pos)
55{
56  int j= 1;
57  if (pos > list.length() || pos < 1) return 0;
58  for (CFListIterator i= list; j <= pos; i++, j++)
59  {
60    if (j == pos)
61      return i.getItem();
62  }
63}
64
65
66/// \f$ F_{p} (\alpha ) \subset F_{p}(\beta ) \f$ and \f$ \alpha \f$ is a
67/// primitive element, returns the image of \f$ \alpha \f$
68static inline
69CanonicalForm mapUp (const Variable& alpha, const Variable& beta)
70{
71  int p= getCharacteristic ();
72  zz_p::init (p);
73  zz_pX NTL_mipo= convertFacCF2NTLzzpX (getMipo (beta));
74  zz_pE::init (NTL_mipo);
75  zz_pEX NTL_alpha_mipo= convertFacCF2NTLzz_pEX (getMipo(alpha), NTL_mipo);
76  zz_pE root= FindRoot (NTL_alpha_mipo);
77  return convertNTLzzpE2CF (root, beta);
78}
79
80/// the CanonicalForm G is the output of map_up, returns F considered as an
81/// element over \f$ F_{p}(\alpha ) \f$, WARNING: make sure coefficients of F
82/// are really elements of a subfield of \f$ F_{p}(\beta ) \f$ which is
83/// isomorphic to \f$ F_{p}(\alpha ) \f$
84static inline
85CanonicalForm
86mapDown (const CanonicalForm& F, const Variable& alpha, const
87          CanonicalForm& G, CFList& source, CFList& dest)
88{
89  CanonicalForm buf, buf2;
90  int counter= 0;
91  int pos;
92  int p= getCharacteristic();
93  int d= degree(getMipo(alpha));
94  int bound= ipower(p, d);
95  CanonicalForm result= 0;
96  CanonicalForm remainder;
97  CanonicalForm alpha_power;
98  if (degree(F) == 0) return F;
99  if (F.level() < 0 && F.isUnivariate())
100  {
101    buf= F;
102    remainder= mod (buf, G);
103    ASSERT (remainder.isZero(), "alpha is not primitive");
104    pos= findItem (source, buf);
105    if (pos == 0)
106      source.append (buf);
107    buf2= buf;
108    while (degree (buf) != 0 && counter < bound)
109    {
110      buf /= G;
111      counter++;
112      if (buf == buf2) break;
113    }
114    ASSERT (counter >= bound, "alpha is not primitive");
115    if (pos == 0)
116    {
117      alpha_power= power (alpha, counter);
118      dest.append (alpha_power);
119    }
120    else
121      alpha_power= getItem (dest, pos);
122    result = alpha_power*buf;
123    return result;
124  }
125  else
126  {
127    for (CFIterator i= F; i.hasTerms(); i++)
128    {
129      buf= mapDown (i.coeff(), alpha, G, source, dest);
130      result += buf*power(F.mvar(), i.exp());
131    }
132    return result;
133  }
134}
135
136/// helper function
137static inline
138CanonicalForm GF2FalphaHelper (const CanonicalForm& F, const Variable& alpha)
139{
140  int exp;
141  CanonicalForm result= 0;
142  char gf_name_buf= gf_name;
143  InternalCF* buf;
144  if (F.inBaseDomain())
145  {
146    if (F.isOne()) return 1;
147    buf= F.getval();
148    exp= imm2int(buf);
149    result= power (alpha, exp).mapinto();
150    return result;
151  }
152  for (CFIterator i= F; i.hasTerms(); i++)
153    result += GF2FalphaHelper (i.coeff(), alpha)*power (F.mvar(), i.exp());
154  return result;
155}
156
157/// changes representation by primitive element to representation by residue
158/// classes modulo a Conway polynomial
159CanonicalForm GF2FalphaRep (const CanonicalForm& F, const Variable& alpha)
160{
161  Variable beta= rootOf (gf_mipo);
162  return GF2FalphaHelper (F, beta) (alpha, beta);
163}
164
165/// change representation by residue classes modulo a Conway polynomial
166/// to representation by primitive element
167CanonicalForm Falpha2GFRep (const CanonicalForm& F)
168{
169  CanonicalForm result= 0;
170  InternalCF* buf;
171
172  if (F.inCoeffDomain())
173  {
174    if (F.inBaseDomain())
175      return F.mapinto();
176    else
177    {
178      for (CFIterator i= F; i.hasTerms(); i++)
179      {
180        buf= int2imm_gf (i.exp());
181        result += i.coeff().mapinto()*CanonicalForm (buf);
182      }
183    }
184    return result;
185  }
186  for (CFIterator i= F; i.hasTerms(); i++)
187    result += Falpha2GFRep (i.coeff())*power (F.mvar(), i.exp());
188  return result;
189}
190
191/// GF_map_up helper
192static inline
193CanonicalForm GFPowUp (const CanonicalForm & F, int k)
194{
195  if (F.isOne()) return F;
196  CanonicalForm result= 0;
197  if (F.inBaseDomain())
198    return power(F, k);
199  for (CFIterator i= F; i.hasTerms(); i++)
200    result += GFPowUp (i.coeff(), k)*power (F.mvar(), i.exp());
201  return result;
202}
203
204/// maps a polynomial over \f$ GF(p^{k}) \f$ to a polynomial over
205/// \f$ GF(p^{d}) \f$ , d needs to be a multiple of k
206CanonicalForm GFMapUp (const CanonicalForm & F, int k)
207{
208  int d= getGFDegree();
209  ASSERT (d%k == 0, "multiple of GF degree expected");
210  int p= getCharacteristic();
211  int ext_field_size= ipower (p, d);
212  int field_size= ipower ( p, k);
213  int diff= (ext_field_size - 1)/(field_size - 1);
214  return GFPowUp (F, diff);
215}
216
217/// GFMapDown helper
218static inline
219CanonicalForm GFPowDown (const CanonicalForm & F, int k)
220{
221  if (F.isOne()) return F;
222  CanonicalForm result= 0;
223  int exp;
224  InternalCF* buf;
225  if (F.inBaseDomain())
226  {
227    buf= F.getval();
228    exp= imm2int (buf);
229    if ((exp % k) == 0)
230      exp= exp/k;
231    else
232      return -1;
233
234    buf= int2imm_gf (exp);
235    return CanonicalForm (buf);
236  }
237  for (CFIterator i= F; i.hasTerms(); i++)
238    result += GFPowDown (i.coeff(), k)*power (F.mvar(), i.exp());
239  return result;
240}
241
242/// maps a polynomial over \f$ GF(p^{d}) \f$ to a polynomial over
243/// \f$ GF(p^{k})\f$ , d needs to be a multiple of k
244CanonicalForm GFMapDown (const CanonicalForm & F, int k)
245{
246  int d= getGFDegree();
247  ASSERT (d % k == 0, "multiple of GF degree expected");
248  int p= getCharacteristic();
249  int ext_field_size= ipower (p, d);
250  int field_size= ipower ( p, k);
251  int diff= (ext_field_size - 1)/(field_size - 1);
252  return GFPowDown (F, diff);
253}
254
255static inline
256CanonicalForm mapUp (const CanonicalForm& F, const CanonicalForm& G,
257                      const Variable& alpha, const CanonicalForm& H,
258                      CFList& source, CFList& dest)
259{
260  CanonicalForm buf, buf2;
261  int counter= 0;
262  int pos;
263  int p= getCharacteristic();
264  int d= degree (getMipo(alpha));
265  int bound= ipower(p, d);
266  CanonicalForm result= 0;
267  CanonicalForm remainder;
268  CanonicalForm H_power;
269  if (degree(F) <= 0) return F;
270  if (F.level() < 0 && F.isUnivariate())
271  {
272    buf= F;
273    remainder= mod (buf, G);
274    ASSERT (remainder.isZero(), "alpha is not primitive");
275    pos= findItem (source, buf);
276    if (pos == 0)
277      source.append (buf);
278    buf2= buf;
279    while (degree (buf) != 0 && counter < bound)
280    {
281      buf /= G;
282      counter++;
283      if (buf == buf2) break;
284    }
285    ASSERT (counter >= bound, "alpha is not primitive");
286    if (pos == 0)
287    {
288      H_power= power (H, counter);
289      dest.append (H_power);
290    }
291    else
292      H_power= getItem (dest, pos);
293    result = H_power*buf;
294    return result;
295  }
296  else
297  {
298    for (CFIterator i= F; i.hasTerms(); i++)
299    {
300      buf= mapUp (i.coeff(), G, alpha, H, source, dest);
301      result += buf*power(F.mvar(), i.exp());
302    }
303    return result;
304  }
305}
306
307/// determine a primitive element of \f$ F_{p} (\alpha ) \f$,
308/// \f$ \beta \f$ is a primitive element of a field which is isomorphic to
309/// \f$ F_{p}(\alpha ) \f$
310CanonicalForm
311primitiveElement (const Variable& alpha, Variable& beta, bool fail)
312{
313  bool primitive= false;
314  fail= false;
315  primitive= isPrimitive (alpha, fail);
316  if (fail)
317    return 0;
318  if (primitive)
319  {
320    beta= alpha;
321    return alpha;
322  }
323  CanonicalForm mipo= getMipo (alpha);
324  int d= degree (mipo);
325  int p= getCharacteristic ();
326  zz_p::init (p);
327  zz_pX NTL_mipo;
328  CanonicalForm mipo2;
329  primitive= false;
330  fail= false;
331  do
332  {
333    BuildIrred (NTL_mipo, d);
334    mipo2= convertNTLzzpX2CF (NTL_mipo, Variable (1));
335    beta= rootOf (mipo2);
336    primitive= isPrimitive (beta, fail);
337    if (primitive)
338      break;
339    if (fail)
340      return 0;
341  } while (1);
342  zz_pX alpha_mipo= convertFacCF2NTLzzpX (mipo);
343  zz_pE::init (alpha_mipo);
344  zz_pEX NTL_beta_mipo= to_zz_pEX (NTL_mipo);
345  zz_pE root= FindRoot (NTL_beta_mipo);
346  return convertNTLzzpE2CF (root, alpha);
347}
348
349CanonicalForm
350mapDown (const CanonicalForm& F, const CanonicalForm& prim_elem, const
351          CanonicalForm& im_prim_elem, const Variable& alpha, CFList& source,
352          CFList& dest)
353{
354  return mapUp (F, im_prim_elem, alpha, prim_elem, dest, source);
355}
356
357CanonicalForm
358mapUp (const CanonicalForm& F, const Variable& alpha, const Variable& beta,
359        const CanonicalForm& prim_elem, const CanonicalForm& im_prim_elem,
360        CFList& source, CFList& dest)
361{
362  if (prim_elem == alpha)
363    return F (im_prim_elem, alpha);
364  return mapUp (F, prim_elem, alpha, im_prim_elem, source, dest);
365}
366
367CanonicalForm
368mapPrimElem (const CanonicalForm& prim_elem, const Variable& alpha,
369             const Variable& beta)
370{
371  if (prim_elem == alpha)
372    return mapUp (alpha, beta);
373  else
374  {
375    CanonicalForm im_alpha= mapUp (alpha, beta);
376    CanonicalForm result= 0;
377    for (CFIterator i= prim_elem; i.hasTerms(); i++)
378      result += power (im_alpha, i.exp())*i.coeff();
379    return result;
380  }
381}
382
383#endif
Note: See TracBrowser for help on using the repository browser.