source: git/Singular/mpsr_Tok.cc @ 416465

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