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

spielwiese
Last change on this file since 0e1846 was 0e1846, checked in by Olaf Bachmann <obachman@…>, 27 years ago
This commit was generated by cvs2svn to compensate for changes in r59, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@60 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.8 KB
Line 
1/***************************************************************
2 *
3 * File:       mpsr_Tok.cc
4 * Purpose:    Routines which realize Singular CMD <-> MP (dict, cop) mappings
5 *             (and ordering mappings)
6 * Author:     Olaf Bachmann (1/97)
7 *
8 * Change History (most recent first):
9 *
10 ***************************************************************/
11
12#include"mod2.h"
13
14#ifdef HAVE_MPSR
15
16#include"febase.h"
17#include"tok.h"
18#include"ring.h"
19
20#include "mpsr.h"
21#include "mpsr_Tok.h"
22
23#include"MP_BasicDict.h"
24#include"MP_MatrixDict.h"
25#include"MP_PolyDict.h"
26#include"MP_NumberDict.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_SetError(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_SetError(mpsr_UnknownDictionary);
100
101  tok = mp2tok[sr_dict][cop];
102  if (tok == MAX_TOK)
103    return mpsr_SetError(mpsr_UnkownOperator);
104
105  *o_tok = tok;
106  return mpsr_Success;
107}
108
109
110#define MAX_ORD ringorder_unspec
111// the following assume that the ringorders are defiend as follows:
112// enum
113// {
114//   ringorder_no = 0,
115//   ringorder_a,
116//   ringorder_c,
117//   ringorder_C,
118//   ringorder_M,
119//   ringorder_lp,
120//   ringorder_dp,
121//   ringorder_Dp,
122//   ringorder_wp,
123//   ringorder_Wp,
124//   ringorder_ls,
125//   ringorder_ds,
126//   ringorder_Ds,
127//   ringorder_ws,
128//   ringorder_Ws,
129//   ringorder_unspec
130// };
131
132static MP_Common_t ord2mp[] =
133{
134  MP_CcPolyOrdering_No,
135  MP_CcPolyOrdering_a,
136  MP_CcPolyOrdering_c,
137  MP_CcPolyOrdering_C,
138  MP_CcPolyOrdering_M,
139  MP_CcPolyOrdering_lp,
140  MP_CcPolyOrdering_Dp,
141  MP_CcPolyOrdering_wp,
142  MP_CcPolyOrdering_Wp,
143  MP_CcPolyOrdering_ls,
144  MP_CcPolyOrdering_ds,
145  MP_CcPolyOrdering_Ds,
146  MP_CcPolyOrdering_ws,
147  MP_CcPolyOrdering_Ws,
148  MP_CcPolyOrdering_Unspec
149};
150
151
152MP_Common_t mpsr_ord2mp(int sr_ord)
153{
154  if (sr_ord > 0 && sr_ord < MAX_ORD)
155    return ord2mp[sr_ord];
156
157  return MP_CcPolyOrdering_Unspec;
158}
159
160short mpsr_mp2ord(MP_Common_t mp_ord)
161{
162  int i;
163  for (i=0; i< MAX_ORD; i++)
164    if (ord2mp[i] == mp_ord) return i;
165
166  return ringorder_unspec;
167}
168 
169 
170#ifdef GENTABLE
171
172// This returns 1 if tok is a token which can appear in a Singular
173// (quoted) command, and 0 otherwise
174#include"ipshell.h" // has declarations of dArith
175
176
177  // some special cmds which do not fit in with the others, and
178  // nevertheless need to be transmitted
179short ExtraCmds[] =
180{
181  OPTION_CMD,
182  VERBOSE_CMD,
183  NAMES_CMD,
184//  RESERVEDNAME_CMD,
185  PROC_CMD,
186  MAP_CMD,
187  '=',
188  0
189};
190 
191 
192// This the list of all tokens which have an MP representation as a
193// cop in the Singular dictionary
194short sr_cmds[] =
195{
196  OPTION_CMD,
197  VERBOSE_CMD,
198  NAMES_CMD,
199  ATTRIB_CMD,
200  CHARSTR_CMD,
201  CLOSE_CMD,
202  DEF_CMD,
203  DEGREE_CMD,
204  DEFINED_CMD,
205  E_CMD,
206  FREEMODULE_CMD,
207  INT_CMD,
208  INTERRED_CMD,
209  INTMAT_CMD,
210  INTVEC_CMD,
211  IS_RINGVAR,
212  KILLATTR_CMD,
213  MAP_CMD,
214  MEMORY_CMD,
215  MONITOR_CMD,
216  NAMEOF_CMD,
217  NUMBER_CMD,
218  NPARS_CMD,
219  NVARS_CMD,
220  OPEN_CMD,
221  ORDSTR_CMD,
222  PAR_CMD,
223  PARSTR_CMD,
224  PARDEG_CMD,
225  POLY_CMD,
226  PRINT_CMD,
227  READ_CMD,
228  SORTVEC_CMD,
229  STRING_CMD, 
230  SYSTEM_CMD,
231  TYPEOF_CMD,
232  VECTOR_CMD,
233  VAR_CMD,
234  VARSTR_CMD,
235  WEIGHT_CMD,
236  '(',
237  COEF_CMD,
238  DELETE_CMD,
239  FETCH_CMD,
240  FIND_CMD,
241  IMAP_CMD,
242  INSERT_CMD,
243  SIMPLIFY_CMD,
244  SRES_CMD,
245  DBPRINT_CMD,
246  TEST_CMD,
247  PROC_CMD,
248  MSTD_CMD,
249  RESERVEDNAME_CMD,
250  WRITE_CMD,
251};
252
253// struct used for specifying the cmd <-> cop relations
254typedef struct cmd_cop
255{
256  short cmd;
257  MP_Common_t cop;
258} cmd_op;
259
260typedef struct cmd_dictcop
261{
262  MP_DictTag_t  dict;
263  cmd_op        cmd_ops[255];
264} cmd_dictcop;
265
266cmd_dictcop cmd_dictcops[] =
267{
268  {
269    MP_PolyDict,
270    // This is the list of all tokens which have an MP representation as a
271    // cop in the Poly dictionary
272    {
273      {BETTI_CMD, MP_CopPolyBetti},
274      {CHARACTERISTIC_CMD, MP_CopPolyChar},
275      {CHAR_SERIES_CMD, MP_CopPolyCharSeries},
276      {CONTENT_CMD, MP_CopPolyClearDenom },
277      {DEG_CMD, MP_CopPolyDeg},
278      {DIM_CMD, MP_CopPolyDim},
279      {FAC_CMD, MP_CopPolyFactorize},
280      {FACSTD_CMD, MP_CopPolyFacStd},
281      {HILBERT_CMD, MP_CopPolyHilb},
282      {HOMOG_CMD, MP_CopPolyHomog},
283      {INDEPSET_CMD, MP_CopPolyInDepSet},
284      {IDEAL_CMD, MP_CopPolyIdeal},
285      {KBASE_CMD, MP_CopPolyKbase},
286      {LEAD_CMD, MP_CopPolyLead},
287      {LEADCOEF_CMD, MP_CopPolyDictLeadCoef},
288      {LEADEXP_CMD, MP_CopPolyDictLeadExp},
289      {MAXID_CMD, MP_CopPolyMaxIdeal},
290      {MINBASE_CMD, MP_CopPolyMinBase},
291      {MINRES_CMD, MP_CopPolyMinRes},
292      {MODUL_CMD, MP_CopPolyModule},
293      {MULTIPLICITY_CMD, MP_CopPolyMultiplicity},
294      {ORD_CMD, MP_CopPolyOrder},
295      {PRUNE_CMD, MP_CopPolyPrune},
296      {QHWEIGHT_CMD, MP_CopPolyQHWeight},
297      {REGULARITY_CMD, MP_CopPolyRegularity},
298      {RESULTANT_CMD, MP_CopPolyResultant},
299      {STD_CMD, MP_CopPolyStd},
300      {SYZYGY_CMD, MP_CopPolySyz},
301      {VDIM_CMD, MP_CopPolyVdim},
302      {COEFFS_CMD,  MP_CopPolyCoeffs},
303      {CONTRACT_CMD, MP_CopPolyContract},
304      {ELIMINATION_CMD, MP_CopPolyEliminate},
305      {JET_CMD, MP_CopPolyJet},
306      {LIFT_CMD, MP_CopPolyLift},
307      {LIFTSTD_CMD, MP_CopPolyLiftstd},
308      {MODULO_CMD, MP_CopPolyModulo},
309      {MRES_CMD, MP_CopPolyMres},
310      {QUOTIENT_CMD, MP_CopPolyQuotient},
311      {REDUCE_CMD, MP_CopPolyReduce},
312      {PREIMAGE_CMD, MP_CopPolyPreimage},
313      {RES_CMD, MP_CopPolyRes},
314      {RING_CMD, MP_CopPolyRing},
315      {MAX_TOK, 0}
316    }
317  },
318  {
319    MP_NumberDict,
320    // This is the list of all tokens which have an MP representation as a
321    // cop in the Number dictionary
322    {
323      {PRIME_CMD, MP_CopNumberPrime},
324      {EXTGCD_CMD, MP_CopNumberExtGcd},
325      {GCD_CMD, MP_CopNumberGcd},
326      {RANDOM_CMD, MP_CopNumberRandom},
327      {MAX_TOK, 0}
328    }
329  },
330  {
331    MP_MatrixDict,
332    // This is the list of all tokens which have an MP representation as a
333    // cop in the Matrix dictionary
334    {
335      {BAREISS_CMD, MP_CopMatrixBareiss},
336      {COLS_CMD, MP_CopMatrixCols},
337      {DET_CMD, MP_CopMatrixDet},
338      {JACOB_CMD, MP_CopMatrixJacobi},
339      {MATRIX_CMD, MP_CopMatrixDenseMatrix},
340      {ROWS_CMD, MP_CopMatrixRows},
341      {TRACE_CMD, MP_CopMatrixTrace},
342      {TRANSPOSE_CMD, MP_CopMatrixTranspose},
343      {KOSZUL_CMD, MP_CopMatrixKoszul},
344      {MINOR_CMD, MP_CopMatrixMinor},
345      {WEDGE_CMD, MP_CopMatrixWedge},
346      {MAX_TOK, 0}
347    }
348  },
349  {
350    MP_BasicDict,
351    // This is the list of all tokens which have an MP representation as a
352    // cop in the MP Basic dictionary
353    {
354      {PLUSPLUS, MP_CopBasicInc},
355      {MINUSMINUS,  MP_CopBasicDec},
356      {COUNT_CMD, MP_CopBasicSize}, 
357      {LIST_CMD, MP_CopBasicList},
358      {'+', MP_CopBasicAdd},
359      {'-', MP_CopBasicMinus},
360      {'*', MP_CopBasicMult},
361      {'/', MP_CopBasicDiv},
362      {'%', MP_CopBasicMod},
363      {'^', MP_CopBasicPow},
364      {GE, MP_CopBasicGreaterEqual},
365      {'<', MP_CopBasicGreater},
366      {LE, MP_CopBasicLessEqual},
367      {'>', MP_CopBasicLess},
368      {'&', MP_CopBasicAnd},
369      {'|', MP_CopBasicOr},
370      {'=', MP_CopBasicAssign},
371      {EQUAL_EQUAL, MP_CopBasicEqual},
372      {NOTEQUAL, MP_CopBasicNotEqual},
373      {DOTDOT, MP_CopBasicRange},
374      {'[', MP_CopBasicIndex},
375      {DIFF_CMD, MP_CopBasicDiff},
376      {INTERSECT_CMD, MP_CopBasicInterSect},
377      {SUBST_CMD, MP_CopBasicSubst},
378      {NOT, MP_CopBasicNot},
379      {MAX_TOK, 0}
380    }
381  }
382};
383
384
385static short IsCmdToken(short tok)
386{
387  short i = 0;
388  // cmds with one arg
389  while (dArith1[i].cmd != 0)
390    if (dArith1[i].cmd == tok) return 1;
391    else i++;
392 
393  // cmds with two args
394  i=0;
395  while (dArith2[i].cmd != 0)
396    if (dArith2[i].cmd == tok) return 1;
397    else i++;
398
399  // cmds with three args
400  i=0;
401  while (dArith3[i].cmd != 0)
402    if (dArith3[i].cmd == tok) return 1;
403    else i++;
404
405  // cmds with many args
406  i=0;
407  while (dArithM[i].cmd != 0)
408    if (dArithM[i].cmd == tok) return 1;
409    else i++;
410
411  // cmds which are somewhat special (like those having 0 args)
412  i=0;
413  while (ExtraCmds[i] != 0)
414    if (ExtraCmds[i] == tok) return 1;
415    else i++;
416
417  return 0;
418}
419
420// Given a Singular token, find matching (dict,op): Return 1 if one is
421// found, 0, otherwise
422static short GetMPDictTok(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
423{
424  short i, l, j;
425  cmd_op *cmd_ops;
426
427  // first, look through Singular specific commands
428  l = sizeof(sr_cmds)/sizeof(short);
429  if (l > MAX_COP)
430  {
431    fprintf(stderr,
432            "Error: There are more than 256 entries in MP_SingularDict\n");
433    exit(1);
434  }
435  for (i=0; i<l; i++)
436    if (sr_cmds[i] == tok)
437    {
438      *dict = MP_SingularDict;
439      *cop = i;
440      return 1;
441    }
442 
443  // look through all the other dicts
444  for (j=0; j<MAX_SR_DICT-1; j++)
445  {
446    cmd_ops = cmd_dictcops[j].cmd_ops;
447    for (i=0; (cmd_ops[i]).cmd != MAX_TOK; i++)
448    {
449      if (i > MAX_COP)
450      {
451        fprintf(stderr,
452                "Error: There are more than 256 entries in dict %d's\n",j);
453        exit(1);
454      }
455      if (cmd_ops[i].cmd == tok)
456      {
457        *dict = cmd_dictcops[j].dict;
458        *cop = cmd_ops[i].cop;
459        return 1;
460      }
461    }
462  }
463  return 0;
464}
465 
466
467// This actually generates the tables of mpsr_tok.inc
468void mpsr_ttGen()
469{
470  mpsr_cmd mpsrcmds[MAX_TOK];
471  short tok2mp[MAX_TOK];
472  short mp2tok[MAX_SR_DICT][MAX_COP];
473  short max_cmd = 0, i, j;
474  MP_Common_t cop;
475  FILE *outfile;
476  MP_DictTag_t dict;
477 
478 
479  // init all arrays
480  for (i=0; i<MAX_TOK; i++)
481  {
482    mpsrcmds[i].tok = MAX_TOK;
483    tok2mp[i] = MAX_TOK;
484  }
485  for (i=0; i<MAX_SR_DICT; i++)
486    for (j=0; j<MAX_COP; j++)
487      mp2tok[i][j] = MAX_TOK;
488 
489  // Now go through all the token and test them
490  for (i=0; i<MAX_TOK; i++)
491  {
492    if (IsCmdToken(i))
493    {
494      if (GetMPDictTok(i, &dict, &cop))
495      {
496        mpsrcmds[max_cmd].tok = i;
497        mpsrcmds[max_cmd].dict = dict;
498        mpsrcmds[max_cmd].cop = cop;
499        tok2mp[i] = max_cmd;
500        mp2tok[mpdict2srdict(dict)][cop] = i;
501        max_cmd++;
502      }
503      else
504      {
505        fprintf(stderr, "Warning: mpsr_ttGen: Unknown Cmd Token: %d\n", i);
506      }
507    }
508  }
509
510  // Generate the template file
511  outfile = fopen("mpsr_Tok.inc", "w");
512  if (outfile == NULL)
513  {
514    fprintf(stderr, "Error: mpsr_ttGen: Cannot open file mpsr_Tok.inc\n");
515    exit(1);
516  }
517
518  // header
519  fprintf(outfile,"/***************************************************************
520 *
521 * File:       mpsr_tok.inc
522 * Purpose:    tables for mapping Singular cmds to/from MP (dict, op)
523 *
524 * THIS FILE WAS AUTOMATICALLY GENERATED BY mpsr_ttGen(). DO NOT EDIT!
525 *
526 ***************************************************************/
527
528mpsr_cmd mpsr_cmds[] =
529{
530  { %d,\t %d,\t %d }", mpsrcmds[0].tok, mpsrcmds[0].dict, mpsrcmds[0].cop);
531
532  // mpsrcmds
533  for (i=1; i<max_cmd; i++)
534  {
535    fprintf(outfile, ",\n  { %d,\t %d,\t %d }",
536            mpsrcmds[i].tok, mpsrcmds[i].dict, mpsrcmds[i].cop);
537  }
538  fprintf(outfile,"\n};\n\n");
539
540  // tok2mp
541  fprintf(outfile, "short tok2mp[] = { %d", tok2mp[0]);
542  for (i=1; i<MAX_TOK; i++)
543    fprintf(outfile, ", %d", tok2mp[i]);
544  fprintf(outfile, "};\n\n");
545
546  // mp2tok
547  fprintf(outfile, "short mp2tok[MAX_SR_DICT][MAX_COP] = \n{");
548  for (i=0; i<MAX_SR_DICT; i++)
549  {
550    fprintf(outfile, "\n{\n");
551    for (j=0; j<MAX_COP; j++)
552      fprintf(outfile, " %d,",mp2tok[i][j]);
553    fseek(outfile, 1, -1);
554    fprintf(outfile, "},");
555  }
556  fseek(outfile, 1, -1);
557  fprintf(outfile,"\n};");
558  fclose(outfile);
559} // That's all
560
561#endif // GENTABLE
562
563#endif // HAVE_MPSR
Note: See TracBrowser for help on using the repository browser.