source: git/Singular/mpsr_Tok.cc @ fff984

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