source: git/Singular/mpsr_Tok.cc @ 02064d

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