source: git/factory/cf_map_ext.cc @ 2537fa0

spielwiese
Last change on this file since 2537fa0 was bffe62d, checked in by Martin Lee <martinlee84@…>, 11 years ago
chg: skip zz_p::init() if it is already correctly initialized
  • Property mode set to 100644
File size: 11.4 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**/
14//*****************************************************************************
15
16#include "config.h"
17
18#include "cf_assert.h"
19#include "debug.h"
20
21#include "canonicalform.h"
22#include "cf_util.h"
23#include "imm.h"
24#include "cf_iter.h"
25
26#ifdef HAVE_NTL
27#include "NTLconvert.h"
28#endif
29
30// cyclotomoic polys:
31#include "cf_cyclo.h"
32
33#include "cf_map_ext.h"
34
35/// helper function
36int findItem (const CFList& list, const CanonicalForm& item)
37{
38  int result= 1;
39  for (CFListIterator i= list; i.hasItem(); i++, result++)
40  {
41    if (i.getItem() == item)
42      return result;
43  }
44  return 0;
45}
46
47/// helper function
48CanonicalForm getItem (const CFList& list, const int& pos)
49{
50  int j= 1;
51  if ((pos > 0) && (pos <= list.length()))
52  {
53    for (CFListIterator i= list; j <= pos; i++, j++)
54    {
55      if (j == pos)
56        return i.getItem();
57    }
58  }
59  return 0;
60}
61
62#ifdef HAVE_NTL
63/// \f$ F_{p} (\alpha ) \subset F_{p}(\beta ) \f$ and \f$ \alpha \f$ is a
64/// primitive element, returns the image of \f$ \alpha \f$
65static inline
66CanonicalForm mapUp (const Variable& alpha, const Variable& beta)
67{
68  int p= getCharacteristic ();
69  if (fac_NTL_char != p)
70  {
71    fac_NTL_char= p;
72    zz_p::init (p);
73  }
74  zz_pX NTL_mipo= convertFacCF2NTLzzpX (getMipo (beta));
75  zz_pE::init (NTL_mipo);
76  zz_pEX NTL_alpha_mipo= convertFacCF2NTLzz_pEX (getMipo(alpha), NTL_mipo);
77  zz_pE root= FindRoot (NTL_alpha_mipo);
78  return convertNTLzzpE2CF (root, beta);
79}
80
81#endif
82
83/// the CanonicalForm G is the output of map_up, returns F considered as an
84/// element over \f$ F_{p}(\alpha ) \f$, WARNING: make sure coefficients of F
85/// are really elements of a subfield of \f$ F_{p}(\beta ) \f$ which is
86/// isomorphic to \f$ F_{p}(\alpha ) \f$
87static inline
88CanonicalForm
89mapDown (const CanonicalForm& F, const Variable& alpha, const
90          CanonicalForm& G, CFList& source, CFList& dest)
91{
92  CanonicalForm buf, buf2;
93  int counter= 0;
94  int pos;
95  int p= getCharacteristic();
96  int d= degree(getMipo(alpha));
97  int bound= ipower(p, d);
98  CanonicalForm result= 0;
99  CanonicalForm remainder;
100  CanonicalForm alpha_power;
101  if (degree(F) == 0) return F;
102  if (F.level() < 0 && F.isUnivariate())
103  {
104    buf= F;
105    remainder= mod (buf, G);
106    ASSERT (remainder.isZero(), "alpha is not primitive");
107    pos= findItem (source, buf);
108    if (pos == 0)
109      source.append (buf);
110    buf2= buf;
111    while (degree (buf) != 0 && counter < bound)
112    {
113      buf /= G;
114      counter++;
115      if (buf == buf2) break;
116    }
117    ASSERT (counter >= bound, "alpha is not primitive");
118    if (pos == 0)
119    {
120      alpha_power= power (alpha, counter);
121      dest.append (alpha_power);
122    }
123    else
124      alpha_power= getItem (dest, pos);
125    result = alpha_power;
126    return result;
127  }
128  else
129  {
130    for (CFIterator i= F; i.hasTerms(); i++)
131    {
132      buf= mapDown (i.coeff(), alpha, G, source, dest);
133      result += buf*power(F.mvar(), i.exp());
134    }
135    return result;
136  }
137}
138
139/// helper function
140static inline
141CanonicalForm GF2FalphaHelper (const CanonicalForm& F, const Variable& alpha)
142{
143  if (F.isZero())
144    return 0;
145  int exp;
146  CanonicalForm result= 0;
147  InternalCF* buf;
148  if (F.inBaseDomain())
149  {
150    if (F.isOne()) return 1;
151    buf= F.getval();
152    exp= imm2int(buf);
153    result= power (alpha, exp).mapinto();
154    return result;
155  }
156  for (CFIterator i= F; i.hasTerms(); i++)
157    result += GF2FalphaHelper (i.coeff(), alpha)*power (F.mvar(), i.exp());
158  return result;
159}
160
161/// changes representation by primitive element to representation by residue
162/// classes modulo a Conway polynomial
163CanonicalForm GF2FalphaRep (const CanonicalForm& F, const Variable& alpha)
164{
165  Variable beta= rootOf (gf_mipo);
166  return GF2FalphaHelper (F, beta) (alpha, beta);
167}
168
169/// change representation by residue classes modulo a Conway polynomial
170/// to representation by primitive element
171CanonicalForm Falpha2GFRep (const CanonicalForm& F)
172{
173  CanonicalForm result= 0;
174  InternalCF* buf;
175
176  if (F.inCoeffDomain())
177  {
178    if (F.inBaseDomain())
179      return F.mapinto();
180    else
181    {
182      for (CFIterator i= F; i.hasTerms(); i++)
183      {
184        buf= int2imm_gf (i.exp());
185        result += i.coeff().mapinto()*CanonicalForm (buf);
186      }
187    }
188    return result;
189  }
190  for (CFIterator i= F; i.hasTerms(); i++)
191    result += Falpha2GFRep (i.coeff())*power (F.mvar(), i.exp());
192  return result;
193}
194
195/// GF_map_up helper
196static inline
197CanonicalForm GFPowUp (const CanonicalForm & F, int k)
198{
199  if (F.isOne()) return F;
200  CanonicalForm result= 0;
201  if (F.inBaseDomain())
202    return power(F, k);
203  for (CFIterator i= F; i.hasTerms(); i++)
204    result += GFPowUp (i.coeff(), k)*power (F.mvar(), i.exp());
205  return result;
206}
207
208/// maps a polynomial over \f$ GF(p^{k}) \f$ to a polynomial over
209/// \f$ GF(p^{d}) \f$ , d needs to be a multiple of k
210CanonicalForm GFMapUp (const CanonicalForm & F, int k)
211{
212  int d= getGFDegree();
213  ASSERT (d%k == 0, "multiple of GF degree expected");
214  int p= getCharacteristic();
215  int ext_field_size= ipower (p, d);
216  int field_size= ipower ( p, k);
217  int diff= (ext_field_size - 1)/(field_size - 1);
218  return GFPowUp (F, diff);
219}
220
221/// GFMapDown helper
222static inline
223CanonicalForm GFPowDown (const CanonicalForm & F, int k)
224{
225  if (F.isOne()) return F;
226  CanonicalForm result= 0;
227  int exp;
228  InternalCF* buf;
229  if (F.inBaseDomain())
230  {
231    buf= F.getval();
232    exp= imm2int (buf);
233    if ((exp % k) == 0)
234      exp= exp/k;
235    else
236      return -1;
237
238    buf= int2imm_gf (exp);
239    return CanonicalForm (buf);
240  }
241  for (CFIterator i= F; i.hasTerms(); i++)
242    result += GFPowDown (i.coeff(), k)*power (F.mvar(), i.exp());
243  return result;
244}
245
246/// maps a polynomial over \f$ GF(p^{d}) \f$ to a polynomial over
247/// \f$ GF(p^{k})\f$ , d needs to be a multiple of k
248CanonicalForm GFMapDown (const CanonicalForm & F, int k)
249{
250  int d= getGFDegree();
251  ASSERT (d % k == 0, "multiple of GF degree expected");
252  int p= getCharacteristic();
253  int ext_field_size= ipower (p, d);
254  int field_size= ipower ( p, k);
255  int diff= (ext_field_size - 1)/(field_size - 1);
256  return GFPowDown (F, diff);
257}
258
259static inline
260CanonicalForm mapUp (const CanonicalForm& F, const CanonicalForm& G,
261                      const Variable& alpha, const CanonicalForm& H,
262                      CFList& source, CFList& dest)
263{
264  CanonicalForm buf, buf2;
265  int counter= 0;
266  int pos;
267  int p= getCharacteristic();
268  int d= degree (getMipo(alpha));
269  int bound= ipower(p, d);
270  CanonicalForm result= 0;
271  CanonicalForm remainder;
272  CanonicalForm H_power;
273  if (degree(F) <= 0) return F;
274  if (F.level() < 0 && F.isUnivariate())
275  {
276    buf= F;
277    remainder= mod (buf, G);
278    ASSERT (remainder.isZero(), "alpha is not primitive");
279    pos= findItem (source, buf);
280    if (pos == 0)
281      source.append (buf);
282    buf2= buf;
283    while (degree (buf) != 0 && counter < bound)
284    {
285      buf /= G;
286      counter++;
287      if (buf == buf2) break;
288    }
289    ASSERT (counter <= bound, "alpha is not primitive");
290    if (pos == 0)
291    {
292      H_power= buf*power (H, counter);
293      dest.append (H_power);
294    }
295    else
296      H_power= getItem (dest, pos);
297    result = H_power;
298    return result;
299  }
300  else
301  {
302    for (CFIterator i= F; i.hasTerms(); i++)
303    {
304      buf= mapUp (i.coeff(), G, alpha, H, source, dest);
305      result += buf*power(F.mvar(), i.exp());
306    }
307    return result;
308  }
309}
310
311#ifdef HAVE_NTL
312/// determine a primitive element of \f$ F_{p} (\alpha ) \f$,
313/// \f$ \beta \f$ is a primitive element of a field which is isomorphic to
314/// \f$ F_{p}(\alpha ) \f$
315CanonicalForm
316primitiveElement (const Variable& alpha, Variable& beta, bool fail)
317{
318  bool primitive= false;
319  fail= false;
320  primitive= isPrimitive (alpha, fail);
321  if (fail)
322    return 0;
323  if (primitive)
324  {
325    beta= alpha;
326    return alpha;
327  }
328  CanonicalForm mipo= getMipo (alpha);
329  int d= degree (mipo);
330  int p= getCharacteristic ();
331  if (fac_NTL_char != p)
332  {
333    fac_NTL_char= p;
334    zz_p::init (p);
335  }
336  zz_pX NTL_mipo;
337  CanonicalForm mipo2;
338  primitive= false;
339  fail= false;
340  do
341  {
342    BuildIrred (NTL_mipo, d);
343    mipo2= convertNTLzzpX2CF (NTL_mipo, Variable (1));
344    beta= rootOf (mipo2);
345    primitive= isPrimitive (beta, fail);
346    if (primitive)
347      break;
348    if (fail)
349      return 0;
350  } while (1);
351  zz_pX alpha_mipo= convertFacCF2NTLzzpX (mipo);
352  zz_pE::init (alpha_mipo);
353  zz_pEX NTL_beta_mipo= to_zz_pEX (NTL_mipo);
354  zz_pE root= FindRoot (NTL_beta_mipo);
355  return convertNTLzzpE2CF (root, alpha);
356}
357#endif
358
359CanonicalForm
360mapDown (const CanonicalForm& F, const CanonicalForm& prim_elem, const
361          CanonicalForm& im_prim_elem, const Variable& alpha, CFList& source,
362          CFList& dest)
363{
364  return mapUp (F, im_prim_elem, alpha, prim_elem, dest, source);
365}
366
367CanonicalForm
368mapUp (const CanonicalForm& F, const Variable& alpha, const Variable& /*beta*/,
369        const CanonicalForm& prim_elem, const CanonicalForm& im_prim_elem,
370        CFList& source, CFList& dest)
371{
372  if (prim_elem == alpha)
373    return F (im_prim_elem, alpha);
374  return mapUp (F, prim_elem, alpha, im_prim_elem, source, dest);
375}
376
377#ifdef HAVE_NTL
378CanonicalForm
379mapPrimElem (const CanonicalForm& primElem, const Variable& alpha,
380             const Variable& beta)
381{
382  if (primElem == alpha)
383    return mapUp (alpha, beta);
384  else
385  {
386    CanonicalForm primElemMipo= findMinPoly (primElem, alpha);
387    int p= getCharacteristic ();
388    if (fac_NTL_char != p)
389    {
390      fac_NTL_char= p;
391      zz_p::init (p);
392    }
393    zz_pX NTLMipo= convertFacCF2NTLzzpX (getMipo (beta));
394    zz_pE::init (NTLMipo);
395    zz_pEX NTLPrimElemMipo= convertFacCF2NTLzz_pEX (primElemMipo, NTLMipo);
396    zz_pE root= FindRoot (NTLPrimElemMipo);
397    return convertNTLzzpE2CF (root, beta);
398  }
399}
400
401CanonicalForm
402map (const CanonicalForm& primElem, const Variable& alpha,
403     const CanonicalForm& F, const Variable& beta)
404{
405  CanonicalForm G= F;
406  int order= 0;
407  while (!G.isOne())
408  {
409    G /= primElem;
410    order++;
411  }
412  int p= getCharacteristic ();
413  if (fac_NTL_char != p)
414  {
415    fac_NTL_char= p;
416    zz_p::init (p);
417  }
418  zz_pX NTL_mipo= convertFacCF2NTLzzpX (getMipo (beta));
419  zz_pE::init (NTL_mipo);
420  zz_pEX NTL_alpha_mipo= convertFacCF2NTLzz_pEX (getMipo(alpha), NTL_mipo);
421  zz_pE NTLBeta= to_zz_pE (convertFacCF2NTLzzpX (beta));
422  vec_zz_pE roots= FindRoots (NTL_alpha_mipo);
423  long ind=-1;
424  for (long i= 0; i < roots.length(); i++)
425  {
426    if (power (roots [i], order)== NTLBeta)
427    {
428      ind= i;
429      break;
430    }
431  }
432  return (convertNTLzzpE2CF (roots[ind], beta));
433}
434
435CanonicalForm
436findMinPoly (const CanonicalForm& F, const Variable& alpha)
437{
438  ASSERT (F.isUnivariate() && F.mvar()==alpha,"expected element of F_p(alpha)");
439
440  if (fac_NTL_char != getCharacteristic())
441  {
442    fac_NTL_char= getCharacteristic();
443    zz_p::init (getCharacteristic());
444  }
445  zz_pX NTLF= convertFacCF2NTLzzpX (F);
446  int d= degree (getMipo (alpha));
447
448  zz_pX NTLMipo= convertFacCF2NTLzzpX (getMipo(alpha));
449  zz_pE::init (NTLMipo);
450  vec_zz_p pows;
451  pows.SetLength (2*d);
452
453  zz_pE powNTLF;
454  set (powNTLF);
455  zz_pE NTLFE= to_zz_pE (NTLF);
456  zz_pX buf;
457  for (int i= 0; i < 2*d; i++)
458  {
459    buf= rep (powNTLF);
460    buf.rep.SetLength (d);
461    pows [i]= buf.rep[0];
462    powNTLF *= NTLFE;
463  }
464
465  zz_pX NTLMinPoly;
466  MinPolySeq (NTLMinPoly, pows, d);
467
468  return convertNTLzzpX2CF (NTLMinPoly, Variable (1));
469}
470
471#endif
Note: See TracBrowser for help on using the repository browser.