source: git/factory/cf_map_ext.cc @ 03c742

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