source: git/Singular/mpsr_Tok.cc @ 12310e

spielwiese
Last change on this file since 12310e was 12310e, checked in by Olaf Bachmann <obachman@…>, 26 years ago
* merged in changes from version 1.2.2 * mpsr stuff changes to reflect changes in MP git-svn-id: file:///usr/local/Singular/svn/trunk@2573 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/* $Id: mpsr_Tok.cc,v 1.18 1998-10-15 11:46:05 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  LIB_CMD
256};
257
258// struct used for specifying the cmd <-> cop relations
259typedef struct cmd_cop
260{
261  short cmd;
262  MP_Common_t cop;
263} cmd_op;
264
265typedef struct cmd_dictcop
266{
267  MP_DictTag_t  dict;
268  cmd_op        cmd_ops[255];
269} cmd_dictcop;
270
271cmd_dictcop cmd_dictcops[] =
272{
273  {
274    MP_PolyDict,
275    // This is the list of all tokens which have an MP representation as a
276    // cop in the Poly dictionary
277    {
278      {BETTI_CMD, MP_CopPolyBetti},
279      {CHARACTERISTIC_CMD, MP_CopPolyChar},
280      {CHAR_SERIES_CMD, MP_CopPolyCharSeries},
281      {CONTENT_CMD, MP_CopPolyClearDenom },
282      {DEG_CMD, MP_CopPolyDeg},
283      {DIM_CMD, MP_CopPolyDim},
284      {FAC_CMD, MP_CopPolyFactorize},
285      {FACSTD_CMD, MP_CopPolyFacStd},
286      {HILBERT_CMD, MP_CopPolyHilb},
287      {HOMOG_CMD, MP_CopPolyHomog},
288      {INDEPSET_CMD, MP_CopPolyInDepSet},
289      {IDEAL_CMD, MP_CopPolyIdeal},
290      {KBASE_CMD, MP_CopPolyKbase},
291      {LEAD_CMD, MP_CopPolyLead},
292      {LEADCOEF_CMD, MP_CopPolyLeadCoef},
293      {LEADEXP_CMD, MP_CopPolyLeadExp},
294      {MAXID_CMD, MP_CopPolyMaxIdeal},
295      {MINBASE_CMD, MP_CopPolyMinBase},
296      {MINRES_CMD, MP_CopPolyMinRes},
297      {MODUL_CMD, MP_CopPolyModule},
298      {MULTIPLICITY_CMD, MP_CopPolyMultiplicity},
299      {ORD_CMD, MP_CopPolyOrder},
300      {PRUNE_CMD, MP_CopPolyPrune},
301      {QHWEIGHT_CMD, MP_CopPolyQHWeight},
302      {REGULARITY_CMD, MP_CopPolyRegularity},
303      {RESULTANT_CMD, MP_CopPolyResultant},
304      {STD_CMD, MP_CopPolyStd},
305      {SYZYGY_CMD, MP_CopPolySyz},
306      {VDIM_CMD, MP_CopPolyVdim},
307      {COEFFS_CMD,  MP_CopPolyCoeffs},
308      {CONTRACT_CMD, MP_CopPolyContract},
309      {ELIMINATION_CMD, MP_CopPolyEliminate},
310      {JET_CMD, MP_CopPolyJet},
311      {LIFT_CMD, MP_CopPolyLift},
312      {LIFTSTD_CMD, MP_CopPolyLiftstd},
313      {MODULO_CMD, MP_CopPolyModulo},
314      {MRES_CMD, MP_CopPolyMres},
315      {QUOTIENT_CMD, MP_CopPolyQuotient},
316      {REDUCE_CMD, MP_CopPolyReduce},
317      {PREIMAGE_CMD, MP_CopPolyPreimage},
318      {RES_CMD, MP_CopPolyRes},
319      {RING_CMD, MP_CopPolyRing},
320      {MAX_TOK, 0}
321    }
322  },
323  {
324    MP_NumberDict,
325    // This is the list of all tokens which have an MP representation as a
326    // cop in the Number dictionary
327    {
328      {PRIME_CMD, MP_CopNumberPrime},
329      {EXTGCD_CMD, MP_CopNumberExtGcd},
330      {GCD_CMD, MP_CopNumberGcd},
331      {RANDOM_CMD, MP_CopNumberRandom},
332      {MAX_TOK, 0}
333    }
334  },
335  {
336    MP_MatrixDict,
337    // This is the list of all tokens which have an MP representation as a
338    // cop in the Matrix dictionary
339    {
340      {BAREISS_CMD, MP_CopMatrixBareiss},
341      {COLS_CMD, MP_CopMatrixCols},
342      {DET_CMD, MP_CopMatrixDet},
343      {JACOB_CMD, MP_CopMatrixJacobi},
344      {MATRIX_CMD, MP_CopMatrixDenseMatrix},
345      {ROWS_CMD, MP_CopMatrixRows},
346      {TRACE_CMD, MP_CopMatrixTrace},
347      {TRANSPOSE_CMD, MP_CopMatrixTranspose},
348      {KOSZUL_CMD, MP_CopMatrixKoszul},
349      {MINOR_CMD, MP_CopMatrixMinor},
350      {WEDGE_CMD, MP_CopMatrixWedge},
351      {MAX_TOK, 0}
352    }
353  },
354  {
355    MP_BasicDict,
356    // This is the list of all tokens which have an MP representation as a
357    // cop in the MP Basic dictionary
358    {
359      {PLUSPLUS, MP_CopBasicInc},
360      {MINUSMINUS,  MP_CopBasicDec},
361      {COUNT_CMD, MP_CopBasicSize},
362      {LIST_CMD, MP_CopBasicList},
363      {'+', MP_CopBasicAdd},
364      {'-', MP_CopBasicMinus},
365      {'*', MP_CopBasicMult},
366      {'/', MP_CopBasicDiv},
367      {'%', MP_CopBasicMod},
368      {'^', MP_CopBasicPow},
369      {GE, MP_CopBasicGreaterEqual},
370      {'<', MP_CopBasicGreater},
371      {LE, MP_CopBasicLessEqual},
372      {'>', MP_CopBasicLess},
373      {'&', MP_CopBasicAnd},
374      {'|', MP_CopBasicOr},
375      {'=', MP_CopBasicAssign},
376      {EQUAL_EQUAL, MP_CopBasicEqual},
377      {NOTEQUAL, MP_CopBasicNotEqual},
378      {DOTDOT, MP_CopBasicRange},
379      {'[', MP_CopBasicIndex},
380      {DIFF_CMD, MP_CopBasicDiff},
381      {INTERSECT_CMD, MP_CopBasicInterSect},
382      {SUBST_CMD, MP_CopBasicSubst},
383      {NOT, MP_CopBasicNot},
384      {MAX_TOK, 0}
385    }
386  }
387};
388
389
390static short IsCmdToken(short tok)
391{
392  short i = 0;
393  // cmds with one arg
394  while (dArith1[i].cmd != 0)
395    if (dArith1[i].cmd == tok) return 1;
396    else i++;
397
398  // cmds with two args
399  i=0;
400  while (dArith2[i].cmd != 0)
401    if (dArith2[i].cmd == tok) return 1;
402    else i++;
403
404  // cmds with three args
405  i=0;
406  while (dArith3[i].cmd != 0)
407    if (dArith3[i].cmd == tok) return 1;
408    else i++;
409
410  // cmds with many args
411  i=0;
412  while (dArithM[i].cmd != 0)
413    if (dArithM[i].cmd == tok) return 1;
414    else i++;
415
416  // cmds which are somewhat special (like those having 0 args)
417  i=0;
418  while (ExtraCmds[i] != 0)
419    if (ExtraCmds[i] == tok) return 1;
420    else i++;
421
422  return 0;
423}
424
425// Given a Singular token, find matching (dict,op): Return 1 if one is
426// found, 0, otherwise
427static short GetMPDictTok(short tok, MP_DictTag_t *dict, MP_Common_t *cop)
428{
429  short i, l, j;
430  cmd_op *cmd_ops;
431
432  // first, look through Singular specific commands
433  l = sizeof(sr_cmds)/sizeof(short);
434  if (l > MAX_COP)
435  {
436    fprintf(stderr,
437            "Error: There are more than 256 entries in MP_SingularDict\n");
438    exit(1);
439  }
440  for (i=0; i<l; i++)
441    if (sr_cmds[i] == tok)
442    {
443      *dict = MP_SingularDict;
444      *cop = i;
445      return 1;
446    }
447
448  // look through all the other dicts
449  for (j=0; j<MAX_SR_DICT-1; j++)
450  {
451    cmd_ops = cmd_dictcops[j].cmd_ops;
452    for (i=0; (cmd_ops[i]).cmd != MAX_TOK; i++)
453    {
454      if (i > MAX_COP)
455      {
456        fprintf(stderr,
457                "Error: There are more than 256 entries in dict %d's\n",j);
458        exit(1);
459      }
460      if (cmd_ops[i].cmd == tok)
461      {
462        *dict = cmd_dictcops[j].dict;
463        *cop = cmd_ops[i].cop;
464        return 1;
465      }
466    }
467  }
468  return 0;
469}
470
471
472// This actually generates the tables of mpsr_tok.inc
473void mpsr_ttGen()
474{
475  mpsr_cmd mpsrcmds[MAX_TOK];
476  short tok2mp[MAX_TOK];
477  short mp2tok[MAX_SR_DICT][MAX_COP];
478  short max_cmd = 0, i, j;
479  MP_Common_t cop;
480  FILE *outfile;
481  MP_DictTag_t dict;
482
483
484  // init all arrays
485  for (i=0; i<MAX_TOK; i++)
486  {
487    mpsrcmds[i].tok = MAX_TOK;
488    tok2mp[i] = MAX_TOK;
489  }
490  for (i=0; i<MAX_SR_DICT; i++)
491    for (j=0; j<MAX_COP; j++)
492      mp2tok[i][j] = MAX_TOK;
493
494  // Now go through all the token and test them
495  for (i=0; i<MAX_TOK; i++)
496  {
497    if (IsCmdToken(i))
498    {
499      if (GetMPDictTok(i, &dict, &cop))
500      {
501        mpsrcmds[max_cmd].tok = i;
502        mpsrcmds[max_cmd].dict = dict;
503        mpsrcmds[max_cmd].cop = cop;
504        tok2mp[i] = max_cmd;
505        mp2tok[mpdict2srdict(dict)][cop] = i;
506        max_cmd++;
507      }
508      else
509      {
510        fprintf(stderr, "Warning: mpsr_ttGen: Unknown Cmd Token: %d(%s)\n",
511                        i, iiTwoOps(i));
512      }
513    }
514  }
515
516  // Generate the template file
517  outfile = myfopen("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#ifndef MPSR_STRING_TABLES
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    {
559      fprintf(outfile, " %d",mp2tok[i][j]);
560      if  (j!=MAX_COP-1) fprintf(outfile, ",");
561    }
562    if (i!=MAX_SR_DICT-1) fprintf(outfile, "},");
563    else                  fprintf(outfile, "}");
564  }
565  fprintf(outfile,"\n};\n\n");
566
567  fprintf(outfile, "
568#else /* MPSR_STRING_TABLE */
569mpsr_cmd mpsr_cmds[] =
570{
571  { \"%s\",\t %d,\t %d }", iiTwoOps(mpsrcmds[0].tok), mpsrcmds[0].dict, mpsrcmds[0].cop);
572
573  for (i=1; i<max_cmd; i++)
574  {
575    fprintf(outfile, ",\n  { \"%s\",\t %d,\t %d }",
576            iiTwoOps(mpsrcmds[i].tok), mpsrcmds[i].dict, mpsrcmds[i].cop);
577  }
578  fprintf(outfile, ",\n { NULL, \t 0, \t 0}");
579  fprintf(outfile,"\n};\n\n#endif /* ! MPSR_STRING_TABLE */");
580
581  fclose(outfile);
582} // That's all
583
584#endif // GENTABLE
585
586#else // NOT HAVE_MPSR
587
588#ifdef GENTABLE
589
590// simply touch mpsr_Tok.inc so that Make does not get confused
591#ifndef macintosh
592extern "C" int system(char *);
593#else
594#include <stdio.h>
595#endif
596
597void mpsr_ttGen()
598{
599#ifndef macintosh
600  system("touch mpsr_Tok.inc");
601#else
602  // simulate touch on a macintosh
603  FILE *fd = fopen("mpsr_Tok.inc", "w"); 
604  close(fd);
605#endif
606}
607#endif
608
609#endif // HAVE_MPSR
Note: See TracBrowser for help on using the repository browser.