source: git/Singular/mpsr_Tok.cc @ b15225f

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