source: git/Singular/mpsr_Tok.cc @ 0fb3336

spielwiese
Last change on this file since 0fb3336 was 6bc5bb, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: IsCmdToken -> iparith, some optimizations git-svn-id: file:///usr/local/Singular/svn/trunk@9434 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/* $Id: mpsr_Tok.cc,v 1.32 2006-09-29 08:44:56 Singular Exp $ */
6
7/***************************************************************
8 *
9 * File:       mpsr_Tok.cc
10 * Purpose:    Routines which realize Singular CMD <-> MP (dict, cop) mappings
11 *             (and ordering mappings)
12 * Author:     Olaf Bachmann (1/97)
13 *
14 * Change History (most recent first):
15 *
16 ***************************************************************/
17
18#include"mod2.h"
19
20#ifdef HAVE_MPSR
21
22#include"febase.h"
23#include"tok.h"
24
25#include "mpsr.h"
26#include "mpsr_Tok.h"
27
28
29#define MAX_COP 256 // there may be at most 256 cops
30
31// this is the main data struct for storing the relation
32// Singular token <-> (Dict, OP)
33typedef struct mpsr_cmd
34{
35
36  short         tok;    // The Singular token encoding
37
38  // The MP Dict tag in which this token is defined,
39  MP_DictTag_t  dict;
40
41  // The MP operator corresponding to this token
42  MP_Common_t   cop; // operator
43} mpsr_cmd;
44
45#define MAX_SR_DICT     5
46
47// this provides a mapping from MP dict tags to more useful (small)
48// array indicies
49inline short mpdict2srdict(MP_DictTag_t dict)
50{
51  if (dict == MP_SingularDict) return 0;
52  else if (dict == MP_BasicDict) return 1;
53  else if (dict == MP_PolyDict) return 2;
54  else if (dict == MP_MatrixDict) return 3;
55  else if (dict == MP_NumberDict) return 4;
56  else return MAX_SR_DICT;
57}
58
59#ifdef GENTABLE
60// This is the array which holds all mpsr_cmds
61// It is initialized in mpsr_tok.inc
62static mpsr_cmd mpsr_cmds[MAX_TOK];
63
64// This is the array which stores the mapping from token to an mpsr_cmd
65// A value is either an index into mpsr_cmds, or MAX_TOK
66static short tok2mp[MAX_TOK];
67
68// This is the array which stores the mapping from (dict, cop) to a
69// mpsr_cmd. First index mpdict2srdict(dict), second is cop
70static short mp2tok[MAX_SR_DICT][MAX_COP];
71
72#else
73// Here are the actual definition of these token tables
74#include"mpsr_Tok.inc"
75#endif
76
77
78// And here are the main routines which provide the mappings
79mpsr_Status_t mpsr_tok2mp(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
80{
81  short tok_index = tok2mp[tok];
82
83  if (tok_index != MAX_TOK)
84  {
85    *dict = mpsr_cmds[tok_index].dict;
86    *cop = mpsr_cmds[tok_index].cop;
87    return mpsr_Success;
88  }
89  else
90    return mpsr_UnknownSingularToken;
91}
92
93mpsr_Status_t mpsr_mp2tok(MP_DictTag_t dict, MP_Common_t cop, short *o_tok)
94{
95  short sr_dict = mpdict2srdict(dict);
96  short tok;
97
98  if (sr_dict == MAX_SR_DICT)
99    return mpsr_UnknownDictionary;
100
101  tok = mp2tok[sr_dict][cop];
102  if (tok == MAX_TOK)
103    return mpsr_UnkownOperator;
104
105  *o_tok = tok;
106  return mpsr_Success;
107}
108
109
110#define MAX_ORD ringorder_unspec
111
112static struct
113{
114  int sing_ord;
115  int mp_ord;
116} sing_mp_ord[] =
117{
118  {ringorder_no,    MP_CcPolyOrdering_Unknown},
119  {ringorder_a,     MP_CcPolyOrdering_Vector},
120  {ringorder_c,     MP_CcPolyOrdering_IncComp},
121  {ringorder_C,     MP_CcPolyOrdering_DecComp},
122  {ringorder_M,     MP_CcPolyOrdering_Matrix},
123  {ringorder_lp,    MP_CcPolyOrdering_Lex},
124  {ringorder_dp,    MP_CcPolyOrdering_DegRevLex},
125  {ringorder_Dp,    MP_CcPolyOrdering_DegLex},
126  {ringorder_wp,    MP_CcPolyOrdering_RevLex},
127  {ringorder_Wp,    MP_CcPolyOrdering_Lex},
128  {ringorder_ls,    MP_CcPolyOrdering_NegLex},
129  {ringorder_ds,    MP_CcPolyOrdering_NegDegRevLex},
130  {ringorder_Ds,    MP_CcPolyOrdering_NegDegLex},
131  {ringorder_ws,    MP_CcPolyOrdering_NegRevLex},
132  {ringorder_Ws,    MP_CcPolyOrdering_NegLex},
133  {ringorder_unspec, MP_CcPolyOrdering_Unknown}
134};
135
136MP_Common_t mpsr_ord2mp(int sr_ord)
137{
138  int i = ringorder_no;
139
140  while (sing_mp_ord[i].sing_ord != sr_ord &&
141         sing_mp_ord[i].sing_ord <= ringorder_unspec) i++;
142
143  return sing_mp_ord[i].mp_ord;
144}
145
146short mpsr_mp2ord(MP_Common_t mp_ord)
147{
148  int ord = ringorder_no;
149
150  while (sing_mp_ord[ord].mp_ord != mp_ord &&
151         sing_mp_ord[ord].sing_ord <= ringorder_unspec) ord++;
152  return sing_mp_ord[ord].sing_ord;
153}
154
155
156#ifdef GENTABLE
157
158#include"ipshell.h" // has declarations of dArith
159
160
161// This the list of all tokens which have an MP representation as a
162// cop in the Singular dictionary
163short sr_cmds[] =
164{
165  OPTION_CMD,
166  NAMES_CMD,
167  ATTRIB_CMD,
168  CHARSTR_CMD,
169  CLOSE_CMD,
170  DEF_CMD,
171  DEGREE_CMD,
172  DEFINED_CMD,
173  E_CMD,
174  EXECUTE_CMD,
175  FREEMODULE_CMD,
176  INT_CMD,
177  INTERRED_CMD,
178  INTMAT_CMD,
179  INTVEC_CMD,
180  IS_RINGVAR,
181  KILLATTR_CMD,
182  MAP_CMD,
183  MEMORY_CMD,
184  MONITOR_CMD,
185  NAMEOF_CMD,
186  NUMBER_CMD,
187  NPARS_CMD,
188  NVARS_CMD,
189  OPEN_CMD,
190  ORDSTR_CMD,
191  PAR_CMD,
192  PARSTR_CMD,
193  PARDEG_CMD,
194  POLY_CMD,
195  PRINT_CMD,
196  READ_CMD,
197  SORTVEC_CMD,
198  STRING_CMD,
199  SYSTEM_CMD,
200  TYPEOF_CMD,
201  VECTOR_CMD,
202  VAR_CMD,
203  VARSTR_CMD,
204  WEIGHT_CMD,
205  '(',
206  COEF_CMD,
207  DELETE_CMD,
208  FETCH_CMD,
209  FIND_CMD,
210  IMAP_CMD,
211  INSERT_CMD,
212  SIMPLIFY_CMD,
213  SRES_CMD,
214  DBPRINT_CMD,
215  TEST_CMD,
216  PROC_CMD,
217  MSTD_CMD,
218  RESERVEDNAME_CMD,
219  WRITE_CMD,
220  QRING_CMD,
221  FGLM_CMD,
222  FGLMQUOT_CMD,
223  DUMP_CMD,
224  GETDUMP_CMD,
225  STATUS_CMD,
226  LIB_CMD,
227  PACKAGE_CMD
228};
229
230// struct used for specifying the cmd <-> cop relations
231typedef struct cmd_cop
232{
233  short cmd;
234  MP_Common_t cop;
235} cmd_op;
236
237typedef struct cmd_dictcop
238{
239  MP_DictTag_t  dict;
240  cmd_op        cmd_ops[255];
241} cmd_dictcop;
242
243cmd_dictcop cmd_dictcops[] =
244{
245  {
246    MP_PolyDict,
247    // This is the list of all tokens which have an MP representation as a
248    // cop in the Poly dictionary
249    {
250      {BETTI_CMD, MP_CopPolyBetti},
251      {CHARACTERISTIC_CMD, MP_CopPolyChar},
252      {CHAR_SERIES_CMD, MP_CopPolyCharSeries},
253      {CONTENT_CMD, MP_CopPolyClearDenom },
254      {DEG_CMD, MP_CopPolyDeg},
255      {DIM_CMD, MP_CopPolyDim},
256      {FAC_CMD, MP_CopPolyFactorize},
257      {FACSTD_CMD, MP_CopPolyFacStd},
258      {HILBERT_CMD, MP_CopPolyHilb},
259      {HOMOG_CMD, MP_CopPolyHomog},
260      {INDEPSET_CMD, MP_CopPolyInDepSet},
261      {IDEAL_CMD, MP_CopPolyIdeal},
262      {KBASE_CMD, MP_CopPolyKbase},
263      {LEAD_CMD, MP_CopPolyLead},
264      {LEADCOEF_CMD, MP_CopPolyLeadCoef},
265      {LEADEXP_CMD, MP_CopPolyLeadExp},
266      {MAXID_CMD, MP_CopPolyMaxIdeal},
267      {MINBASE_CMD, MP_CopPolyMinBase},
268      {MINRES_CMD, MP_CopPolyMinRes},
269      {MODUL_CMD, MP_CopPolyModule},
270      {MULTIPLICITY_CMD, MP_CopPolyMultiplicity},
271      {ORD_CMD, MP_CopPolyOrder},
272      {PRUNE_CMD, MP_CopPolyPrune},
273      {QHWEIGHT_CMD, MP_CopPolyQHWeight},
274      {REGULARITY_CMD, MP_CopPolyRegularity},
275      {RESULTANT_CMD, MP_CopPolyResultant},
276      {STD_CMD, MP_CopPolyStd},
277      {SYZYGY_CMD, MP_CopPolySyz},
278      {VDIM_CMD, MP_CopPolyVdim},
279      {COEFFS_CMD,  MP_CopPolyCoeffs},
280      {CONTRACT_CMD, MP_CopPolyContract},
281      {ELIMINATION_CMD, MP_CopPolyEliminate},
282      {JET_CMD, MP_CopPolyJet},
283      {LIFT_CMD, MP_CopPolyLift},
284      {LIFTSTD_CMD, MP_CopPolyLiftstd},
285      {MODULO_CMD, MP_CopPolyModulo},
286      {MRES_CMD, MP_CopPolyMres},
287      {QUOTIENT_CMD, MP_CopPolyQuotient},
288      {REDUCE_CMD, MP_CopPolyReduce},
289      {PREIMAGE_CMD, MP_CopPolyPreimage},
290      {RES_CMD, MP_CopPolyRes},
291      {RING_CMD, MP_CopPolyRing},
292      {MAX_TOK, 0}
293    }
294  },
295  {
296    MP_NumberDict,
297    // This is the list of all tokens which have an MP representation as a
298    // cop in the Number dictionary
299    {
300      {PRIME_CMD, MP_CopNumberPrime},
301      {EXTGCD_CMD, MP_CopNumberExtGcd},
302      {GCD_CMD, MP_CopNumberGcd},
303      {RANDOM_CMD, MP_CopNumberRandom},
304      {MAX_TOK, 0}
305    }
306  },
307  {
308    MP_MatrixDict,
309    // This is the list of all tokens which have an MP representation as a
310    // cop in the Matrix dictionary
311    {
312      {BAREISS_CMD, MP_CopMatrixBareiss},
313      {COLS_CMD, MP_CopMatrixCols},
314      {DET_CMD, MP_CopMatrixDet},
315      {JACOB_CMD, MP_CopMatrixJacobi},
316      {MATRIX_CMD, MP_CopMatrixDenseMatrix},
317      {ROWS_CMD, MP_CopMatrixRows},
318      {TRACE_CMD, MP_CopMatrixTrace},
319      {TRANSPOSE_CMD, MP_CopMatrixTranspose},
320      {KOSZUL_CMD, MP_CopMatrixKoszul},
321      {MINOR_CMD, MP_CopMatrixMinor},
322      {WEDGE_CMD, MP_CopMatrixWedge},
323      {MAX_TOK, 0}
324    }
325  },
326  {
327    MP_BasicDict,
328    // This is the list of all tokens which have an MP representation as a
329    // cop in the MP Basic dictionary
330    {
331      {PLUSPLUS, MP_CopBasicInc},
332      {MINUSMINUS,  MP_CopBasicDec},
333      {COUNT_CMD, MP_CopBasicSize},
334      {LIST_CMD, MP_CopBasicList},
335      {'+', MP_CopBasicAdd},
336      {'-', MP_CopBasicMinus},
337      {'*', MP_CopBasicMult},
338      {'/', MP_CopBasicDiv},
339      {'%', MP_CopBasicMod},
340      {'^', MP_CopBasicPow},
341      {GE, MP_CopBasicGreaterEqual},
342      {'<', MP_CopBasicGreater},
343      {LE, MP_CopBasicLessEqual},
344      {'>', MP_CopBasicLess},
345      {'&', MP_CopBasicAnd},
346      {'|', MP_CopBasicOr},
347      {'=', MP_CopBasicAssign},
348      {EQUAL_EQUAL, MP_CopBasicEqual},
349      {NOTEQUAL, MP_CopBasicNotEqual},
350      {DOTDOT, MP_CopBasicRange},
351      {'[', MP_CopBasicIndex},
352      {DIFF_CMD, MP_CopBasicDiff},
353      {INTERSECT_CMD, MP_CopBasicInterSect},
354      {SUBST_CMD, MP_CopBasicSubst},
355      {NOT, MP_CopBasicNot},
356      {COLONCOLON, MP_CopBasicPackage},
357      {MAX_TOK, 0}
358    }
359  }
360};
361
362
363// Given a Singular token, find matching (dict,op): Return 1 if one is
364// found, 0, otherwise
365static short GetMPDictTok(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
366{
367  short i, l, j;
368  cmd_op *cmd_ops;
369
370  // first, look through Singular specific commands
371  l = sizeof(sr_cmds)/sizeof(short);
372  if (l > MAX_COP)
373  {
374    fprintf(stderr,
375            "Error: There are more than 256 entries in MP_SingularDict\n");
376    exit(1);
377  }
378  for (i=0; i<l; i++)
379    if (sr_cmds[i] == tok)
380    {
381      *dict = MP_SingularDict;
382      *cop = i;
383      return 1;
384    }
385
386  // look through all the other dicts
387  for (j=0; j<MAX_SR_DICT-1; j++)
388  {
389    cmd_ops = cmd_dictcops[j].cmd_ops;
390    for (i=0; (cmd_ops[i]).cmd != MAX_TOK; i++)
391    {
392      if (i > MAX_COP)
393      {
394        fprintf(stderr,
395                "Error: There are more than 256 entries in dict %d's\n",j);
396        exit(1);
397      }
398      if (cmd_ops[i].cmd == tok)
399      {
400        *dict = cmd_dictcops[j].dict;
401        *cop = cmd_ops[i].cop;
402        return 1;
403      }
404    }
405  }
406  return 0;
407}
408
409
410// This actually generates the tables of mpsr_tok.inc
411void mpsr_ttGen()
412{
413  mpsr_cmd mpsrcmds[MAX_TOK];
414  short tok2mp[MAX_TOK];
415  short mp2tok[MAX_SR_DICT][MAX_COP];
416  short max_cmd = 0, i, j;
417  MP_Common_t cop;
418  FILE *outfile;
419  MP_DictTag_t dict;
420
421
422  // init all arrays
423  for (i=0; i<MAX_TOK; i++)
424  {
425    mpsrcmds[i].tok = MAX_TOK;
426    tok2mp[i] = MAX_TOK;
427  }
428  for (i=0; i<MAX_SR_DICT; i++)
429    for (j=0; j<MAX_COP; j++)
430      mp2tok[i][j] = MAX_TOK;
431
432  // Now go through all the token and test them
433  for (i=0; i<MAX_TOK; i++)
434  {
435    if (IsCmdToken(i))
436    {
437      if (GetMPDictTok(i, &dict, &cop))
438      {
439        mpsrcmds[max_cmd].tok = i;
440        mpsrcmds[max_cmd].dict = dict;
441        mpsrcmds[max_cmd].cop = cop;
442        tok2mp[i] = max_cmd;
443        mp2tok[mpdict2srdict(dict)][cop] = i;
444        max_cmd++;
445      }
446      else
447      {
448        fprintf(stderr, "Warning: mpsr_ttGen: Unknown Cmd Token: %d(%s)\n",
449                        i, iiTwoOps(i));
450      }
451    }
452  }
453
454  // Generate the template file
455  outfile = myfopen("mpsr_Tok.inc", "w");
456  if (outfile == NULL)
457  {
458    fprintf(stderr, "Error: mpsr_ttGen: Cannot open file mpsr_Tok.inc\n");
459    exit(1);
460  }
461
462  // header
463  fprintf(outfile,
464   "/***************************************************************\n"
465   "*\n"
466   "* File:       mpsr_tok.inc\n"
467   "* Purpose:    tables for mapping Singular cmds to/from MP (dict, op)\n"
468   "*\n"
469   "* THIS FILE WAS AUTOMATICALLY GENERATED BY mpsr_ttGen(). DO NOT EDIT!\n"
470   "*\n"
471   "***************************************************************/\n"
472   "#ifndef MPSR_STRING_TABLES\n"
473   "mpsr_cmd mpsr_cmds[] =\n"
474   "{\n"
475   "  { %d,\t %d,\t %d }", mpsrcmds[0].tok, mpsrcmds[0].dict, mpsrcmds[0].cop);
476
477  // mpsrcmds
478  for (i=1; i<max_cmd; i++)
479  {
480    fprintf(outfile, ",\n  { %d,\t %d,\t %d }",
481            mpsrcmds[i].tok, mpsrcmds[i].dict, mpsrcmds[i].cop);
482  }
483  fprintf(outfile,"\n};\n\n");
484
485  // tok2mp
486  fprintf(outfile, "short tok2mp[] = { %d", tok2mp[0]);
487  for (i=1; i<MAX_TOK; i++)
488    fprintf(outfile, ", %d", tok2mp[i]);
489  fprintf(outfile, "};\n\n");
490
491  // mp2tok
492  fprintf(outfile, "short mp2tok[MAX_SR_DICT][MAX_COP] = \n{");
493  for (i=0; i<MAX_SR_DICT; i++)
494  {
495    fprintf(outfile, "\n{\n");
496    for (j=0; j<MAX_COP; j++)
497    {
498      fprintf(outfile, " %d",mp2tok[i][j]);
499      if  (j!=MAX_COP-1) fprintf(outfile, ",");
500    }
501    if (i!=MAX_SR_DICT-1) fprintf(outfile, "},");
502    else                  fprintf(outfile, "}");
503  }
504  fprintf(outfile,"\n};\n\n");
505
506  fprintf(outfile, "#else /* MPSR_STRING_TABLE */\n"
507    "mpsr_cmd mpsr_cmds[] =\n"
508    "{\n"
509    "{ \"%s\",\t %d,\t %d }",
510    iiTwoOps(mpsrcmds[0].tok), mpsrcmds[0].dict, mpsrcmds[0].cop);
511
512  for (i=1; i<max_cmd; i++)
513  {
514    fprintf(outfile, ",\n  { \"%s\",\t %d,\t %d }",
515            iiTwoOps(mpsrcmds[i].tok), mpsrcmds[i].dict, mpsrcmds[i].cop);
516  }
517  fprintf(outfile, ",\n { NULL, \t 0, \t 0}");
518  fprintf(outfile,"\n};\n\n#endif /* ! MPSR_STRING_TABLE */");
519
520  fclose(outfile);
521} // That's all
522
523#endif // GENTABLE
524
525#else // NOT HAVE_MPSR
526
527#ifdef GENTABLE
528
529// simply touch mpsr_Tok.inc so that Make does not get confused
530#if !defined(HPUX_9)
531extern "C" int system(char *);
532#else
533#include <stdio.h>
534#endif
535
536void mpsr_ttGen()
537{
538  system("touch mpsr_Tok.inc");
539}
540#endif
541
542#endif // HAVE_MPSR
Note: See TracBrowser for help on using the repository browser.