source: git/Singular/mpsr_Tok.cc @ 237b3e4

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