source: git/Singular/spSpolyLoop.pl @ 1caa72

spielwiese
Last change on this file since 1caa72 was 1caa72, checked in by Olaf Bachmann <obachman@…>, 26 years ago
1998-04-06 Olaf Bachmann <obachman@mathematik.uni-kl.de> * spSpolyLoop.h: neww calling interface for spGetSpolyLoop * kstd1.cc (kNF): moved strat->ak field initailization out of initBuchMora into single routines * febase.cc (feGetSearchPath): added feGetSearchPath; changed algorithm for searching files: $SINGULARPATH -> relative to executable -> burnt-in locations * added find_exec.c to get absolute pathname of executable git-svn-id: file:///usr/local/Singular/svn/trunk@1341 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 16.1 KB
Line 
1#!/usr/local/bin/perl
2###########################################################################
3# $Id: spSpolyLoop.pl,v 1.6 1998-04-06 17:59:36 obachman Exp $
4
5###########################################################################
6##
7## FILE: spSpolyLoops.pl
8## PURPOSE: Generates spSpolyLoop's and spGetSpolyLoop as specified by
9## variable @input. Writes result to stdout, warning/errors to stderr.
10## AUTHOR: obachman (3/98)
11##
12
13###########################################################################
14##
15## How to add/modify generation of spSpolyLoops
16##
17
18# 1.) Add property/characterisitc to property specification and make
19# sure that property is checked for in spGetSpolyLoop(...) in
20# spSpolyLoop.cc
21#
22# 2.) Modify macros of spSpolyLoops so that approriate actions are taken
23# for new properties
24#
25# 3.) Add properties to check for in @input
26
27
28
29###########################################################################
30##
31## Supporting procedures
32##
33
34sub Warning
35{
36  print STDERR $_[0], "\n";
37}
38
39
40# Each occureence of CALL_<fn>(...) in $string is replaced by
41# `eval fn(..., $argv)'. Modified string is returned.
42sub Expand
43{
44  local($string, $argv) = @_;
45  local(@lines, @call, $call, $prefix, $postfix, $whitespaces, $result);
46  local($i, $j);
47
48  @lines = split(/\n/, $_[0]);
49  for ($i = 0; $i <= $#lines; $i++)
50  {
51    if ($lines[$i] =~ /CALL_/)
52    {
53      ($whitespaces, $prefix, $call, $postfix) = &ParseLine($lines[$i]);
54      #insert $argv as last argument to call
55      if ($call =~ /\(\s*\)$/)
56      {
57        $call =~ s/\(\s*\)$/\(\$argv\)/;
58      }
59      else
60      {
61        $call =~ s/\)$/\,\$argv\)/;
62      }
63      $call = "& ".$call.";";
64      $call = eval $call;
65      @call = split(/\n/,"$whitespaces$prefix$call$postfix");
66      for ($j=1; $j <= $#call; $j++)
67      {
68        $call[$j] = $whitespaces.$call[$j];
69      }
70      $lines[$i] = join("\n", @call);
71    }
72  }
73  $result = join("\n", @lines);
74  if ($call) 
75  {
76    return (&Expand($result));
77  }
78  else
79  {
80    return $result;
81  }
82}
83
84# takes a line containing CALL_ apart into and returns
85# ($whitespace, $prefix, $call, $postfix)
86sub ParseLine
87{
88  local($line) = @_;
89  local($i, $c_start, $c_length, $bcount);
90  local($whitespace, $prefix, $call, $postfix);
91 
92  while(substr($line, $i, 1) =~ /\s/ && $i <= length($line))
93  {
94    $whitespace = $whitespace.substr($line, $i, 1);
95    $i++;
96  }
97 
98  while(substr($line, $i) !~ /^CALL_/  && $i <= length($line))
99  {
100    $prefix = $prefix.substr($line, $i, 1);
101    $i++;
102  }
103 
104  $i = $i+5;
105  $c_start = $i;
106  while(substr($line, $i, 1) ne "(" && $i <= length($line))
107  {
108    $i++;
109  }
110  if (substr($line, $i, 1) eq "(")
111  {
112    $bcount = 1;
113    $i++;
114    while ($bcount > 0 && $i <= length($line))
115    {
116      if (substr($line, $i, 1) eq ")")
117      {
118        $bcount--;
119      }
120      elsif (substr($line, $i, 1) eq "(")
121      {
122        $bcount++;
123      }
124      elsif (substr($line, $i, 1) eq "\"")
125      {
126        $i++;
127        while ((substr($line, $i, 1) ne "\"" || 
128                substr($line, $i-1, 1) eq "\\") && 
129               $i <= length($line))
130        {
131          $i++;
132        }
133      }
134      $i++;
135    }
136  }
137  $call = substr($line, $c_start, $i - $c_start);
138  $postfix = substr($line, $i);
139  return ($whitespace, $prefix, $call, $postfix);
140}
141
142
143###########################################################################
144##
145## Specification of the properties which determine an spSpolyLoop
146##
147## Properties need to have following syntax: propVAL, where prop is
148## short "prefix" name of properties, and needs to be in lower caps, and
149## VAL is possible value and needs to be in all upper caps.
150##
151## Furthermore, observe the following conventions
152## @Protperty = ("propGEN", "propVAL1", ...)
153## $EnumType{prop} = Property
154## GetProperty($string) should always return a valid value.
155
156## need to hard-wire properties here, because they are needed in the interface to surrounding C++-code:
157
158@Properties = ("ch", "ot", "hom", "nw");
159@Characteristics = ("chGEN", "chMODP");
160$EnumType{"ch"} = "Characteristics";
161sub GetCharacteristic
162{
163  foreach $element (split('_', $_[0]))
164  {
165    return ($element) if ($element =~ /^ch/ && 
166                          grep(/$element/, @Characteristics));
167  }
168  return $Characteristics[0];
169}
170 
171@OrderingTypes = ("otGEN", "otEXP", "otCOMPEXP", "otEXPCOMP");
172$EnumType{"ot"} = "OrderingTypes";
173sub GetOrderingType
174{
175  foreach $element (split('_', $_[0]))
176  {
177    return ($element) if ($element =~ /^ot/ && 
178                         grep(/$element/, @OrderingTypes));
179  }
180  return $OrderingTypes[0];
181}
182
183@Homogs = ("homGEN", "homYES"); 
184$EnumType{"hom"} = "Homogs";
185sub GetHomog
186{
187  foreach $element (split('_', $_[0]))
188  {
189    return ($element) if ($element =~ /^hom/ &&
190                          grep(/$element/, @Homogs));
191  }
192  return $Homogs[0];
193}
194
195@NumWords = ("nwGEN", "nwONE", "nwTWO", "nwEVEN", "nwODD");
196$EnumType{"nw"} = "NumWords";
197sub GetNumWords
198{
199  foreach $element (split('_', $_[0]))
200  {
201    return ($element) if ($element =~ /^nw/ &&
202                          grep(/$element/, @NumWords));
203  }
204  return $NumWords[0];
205}
206
207# given a list of "short" prefix properties, generate enumeration type
208# specification for each property
209sub Generate_EnumTypes
210{
211  local(@evalues, $source);
212 
213  foreach $key (@_)
214  {
215    $etype = $EnumType{$key};
216    if ($etype =~ /\w+/)
217    {
218      @evalues = eval '@'.$etype;
219      if ($#evalues >= 0)
220      {
221        $source = $source."typedef enum $etype {". $evalues[0] . " = 0";
222        shift @evalues;
223        foreach $evalue (@evalues)
224        {
225          $source = $source.", $evalue";
226        }
227        $source = $source."} $etype;\n";
228      }
229      else
230      {
231        & Warning("No enum values for type: $etype");
232      }
233    }
234    else
235    {
236      & Warning("Unknown enumeration type index: $key");
237    }
238  }
239  return $source;
240}
241
242###########################################################################
243##
244## Template of the spSpolyLoop
245## Is modified by Expand
246##
247
248$spSpolyLoopBodyTemplate = <<_EOT_
249(poly a1, poly a2, poly monom, poly spNoether)
250{
251  poly a = monom,                         // collects the result
252       b = pNew(),                        // stores a1*monom
253       c;                                 // used for temporary storage
254  number tm   = pGetCoeff(monom),         // coefficient of monom
255         tneg = CALL_NCOPYNEG(tm),        // - (coefficient of monom)
256         tb;                              // used for tm*coeff(a1)
257  Order_t order;                          // used for homog case
258
259  if (a2==NULL) goto Finish;              // we are done if a2 is 0
260
261  CALL_INITORDER(order, a2);              // inits order for homog case
262 
263
264  CALL_PCOPYADDFAST(b, a1, monom, order);  // now a2 != NULL -- set up b
265
266  // MAIN LOOP:
267  Top:     // compare b = monom*a1 and a2 w.r.t. monomial ordering
268    register long d;
269    CALL_COMPARE(b, a2, d);
270
271  Equal:   // b equals a2
272    assume(pComp0(b, a2) == 0);
273    tb = CALL_NMULT("pGetCoeff(a1)",tm);
274    if (!CALL_NEQUAL("pGetCoeff(a2)",tb))
275    {
276      pSetCoeff0(a2,CALL_NSUB("pGetCoeff(a2)",tb)); // adjust coeff of a2
277      a = pNext(a) = a2; // append a2 to result and advance a2
278      pIter(a2);
279    }
280    else
281    { // coeffs are equal, so their difference is 0:
282      c = a2;  // do not append anything to result: Delete a2 and advance
283      pIter(a2);
284      CALL_NDELETE("&pGetCoeff(c)");
285      pFree1(c);
286    }
287    CALL_NDELETE("&tb");
288    pIter(a1);
289    if (a2 == NULL || a1 == NULL) goto Finish; // are we done ?
290    CALL_PCOPYADDFAST(b, a1, monom, order); // No! So, get new b = a1*monom
291    goto Top;
292
293  NotEqual:     // b != a2
294    if (d < 0)  // b < a2:
295    {
296      assume(pComp0(b, a2) == -1);
297      a = pNext(a) = a2;// append a2 to result and advance a2
298      pIter(a2);
299      if (a2==NULL) goto Finish;;
300      goto Top;
301    }
302    else // now d >= 0, i.e., b > a2
303    {
304      assume(pComp0(b, a2) == 1);
305      pSetCoeff0(b,CALL_NMULT("pGetCoeff(a1)",tneg));
306      a = pNext(a) = b;       // append b to result and advance a1
307      pIter(a1);
308      b = pNew();
309      if (a1 == NULL) goto Finish; // are we done?
310      CALL_PCOPYADDFAST(b, a1, monom, order); // No! So, update b = a1*monom
311      goto Top;
312    }
313 
314 Finish: // a1 or a2 is NULL: Clean-up time
315   assume(a1 == NULL || a2 == NULL);
316   if (a1 == NULL) // append rest of a2 to result
317     pNext(a) = a2;
318   else  // append (- a1*monom) to result
319     CALL_MULTCOPYX(a1, monom, a, tneg, spNoether);
320   CALL_NDELETE("&tneg");
321   pFree1(b);
322}
323
324_EOT_
325  ;
326
327###########################################################################
328##
329## "Macros" needed in expansion of spSpolyLoop
330##
331sub NCOPYNEG
332{
333  local($number, $argv) = @_;
334 
335  return "npNegM($number)" if (& GetCharacteristic($argv) eq "chMODP");
336  return "nNeg(nCopy($number))";
337}
338
339sub NDELETE
340{
341  local($number, $argv) = @_;
342 
343  return "" if (& GetCharacteristic($argv) eq "chMODP");
344  return "nDelete($number)";
345}
346
347sub NMULT
348{
349  local($m1, $m2, $argv) = @_;
350 
351  return "npMultM($m1, $m2)" if (& GetCharacteristic($argv) eq "chMODP");
352  return "nMult($m1, $m2)";
353}
354
355sub NSUB
356{
357  local($m1, $m2, $argv) = @_;
358 
359  return "npSubM($m1, $m2)" if (& GetCharacteristic($argv) eq "chMODP");
360  return "nSub($m1, $m2)";
361}
362
363sub NEQUAL
364{
365  local($m1, $m2, $argv) = @_;
366 
367  return "npEqualM($m1, $m2)" if (& GetCharacteristic($argv) eq "chMODP");
368  return "nEqual($m1, $m2)";
369} 
370
371sub MULTCOPYX
372{
373  local($p, $monom, $n, $exp, $spNoether, $argv) = @_;
374 
375  return "spMultCopyX($p, $monom, $n, $exp, $spNoether)" 
376    if (& GetCharacteristic($argv) eq "chMODP");
377  return "spGMultCopyX($p, $monom, $n, $exp, $spNoether)";
378}
379
380sub INITORDER
381{
382  local($order, $p, $argv) = @_;
383 
384  return "$order = $p->Order" if (&GetHomog($argv) eq "homYES");
385  return "";
386}
387
388 
389sub PCOPYADDFAST
390{
391  local($p1, $p2, $p3, $order, $argv) = @_;
392 
393  return "pCopyAddFastHomog($p1, $p2, $p3, $order)" 
394    if (&GetHomog($argv) eq "homYES");
395  return "pCopyAddFast0($p1, $p2, $p3)";
396}
397
398###########################################################################
399##
400## COMPARE "macro": Besides generating th source code which
401## accomplishes monomial comparisons, it also generates the (global)
402## string $rargv charcaterising the generated spSpolyLoop
403##
404sub COMPARE
405{
406  local($p1, $p2, $d, $argv) = @_;
407  local($ot, $hom, $nw, $res);
408 
409  $rargv = &GetCharacteristic($argv);
410  $ot = &GetOrderingType($argv);
411  $rargv =  $rargv."_".$ot;
412  if ($ot eq "otCOMPEXP" ||
413      $ot eq "otEXPCOMP" ||
414      $ot eq "otEXP")
415  {
416    if ($ot eq "otCOMPEXP")
417    {
418      $res = "$d = pGetComp($p2) - pGetComp($p1);\n";
419      $res = $res."NonZeroTestA($d, pComponentOrder, goto NotEqual);\n";
420      $ot = "otEXP";
421    }
422    $hom = &GetHomog($argv);
423    $rargv =  $rargv."_".$hom;
424    if ($hom ne "homYES")
425    {
426      $res = $res."$d = pGetOrder($p1) - pGetOrder($p2);\n";
427      $res = $res."NonZeroTestA($d, pOrdSgn, goto NotEqual);\n";
428    }
429    $nw = &GetNumWords($argv);
430    $rargv =  $rargv."_".$nw;
431    $res = join("_", $res, pMonComp, $ot, $nw);
432    if ($nw eq "nwONE" || $nw eq "nwTWO")
433    {
434      $res = $res."($p1, $p2, $d, NonZeroA($d, pLexSgn, goto NotEqual ), goto Equal);" 
435    }
436    else
437    {
438      $res = $res."($p1, $p2, pVariables1W, $d, NonZeroA($d, pLexSgn, goto NotEqual ), goto Equal);";
439    }
440    return $res;
441  }
442  else
443  {
444    # general ordering type
445    return ("if (($d = pComp0($p1, $p2))) goto NotEqual; else goto Equal;");
446  }
447}
448
449
450###########################################################################
451##
452## Generates an spSpolyLoop from a given "$argv" input string,
453## i.e. property string
454##
455## Returns spSpolyLoop and sets global associative array
456## $loops{$rargv} to generated loop.
457##
458
459sub Generate_SpolyLoop
460{
461  local($argv)= @_;
462  local($loopbody);
463
464  $loopbody = &Expand($spSpolyLoopBodyTemplate, $argv);
465
466  & Warning("Can not realize $argv: Changed to $rargv") if ($argv ne $rargv);
467
468  if (grep(/$rargv/, keys(%loops)))
469  {
470    & Warning("Duplicate entry $rargv: Ignoring $argv");
471    return;
472  }
473  else
474  {
475    $loops{$rargv} = "static void spSpolyLoop_$rargv\n$loopbody\n";
476    return ($loops{$rargv});
477  }
478}
479
480#gnerates SpolyLoops from array of $argv strings
481sub Generate_SpolyLoops
482{
483  local(%loops);
484 
485  foreach $argv (@_)
486  {
487    & Generate_SpolyLoop($argv);
488  }
489  return (%loops);
490}
491
492###########################################################################
493##
494## Generates array of $argv strings from a given input string by
495## "orthoganization", i.e. komma-separated entries in input string are
496## replaced by two argv strings containing the respective entrie.
497##
498
499sub FlattenInputString
500{
501  local($str) = @_;
502 
503  if ($str =~ /_/)
504  {
505    local(@parts, $head, @subresults, @result);
506    @parts = split(/_/, $str);
507    $head = shift(@parts);
508    @subresults = &FlattenInputString(join("_", @parts));
509    foreach $part (split(/,/, $head))
510    {
511      foreach $subresult (@subresults)
512      {
513        @result = (@result, $part."_".$subresult);
514      }
515    }
516    return (@result);
517  }
518  return (split(/,/, $str));
519}
520
521# generate array of $argv strings from array of input strings
522sub FlattenInput
523{
524  local(@result);
525  foreach $entry (@_)
526  {
527    $entry =~ s/\s//;
528    @result = (@result, & FlattenInputString($entry));
529  }
530  return @result;
531}
532
533
534###########################################################################
535##
536## GetSpolyLoop routines
537## They all work on valid $argv strings, only.
538##
539
540## Given a $prefix and an array of $argv's, return all the "next"
541## values, i.e. strings deliminted by $prefix and next '_'.
542sub GetNextKeys
543{
544  local($prefix, @values, $value);
545 
546  $prefix = shift(@_);
547  $prefix = $prefix."_" unless ($prefix eq "");
548  foreach $element (@_)
549  {
550    if ($prefix eq "")
551    {
552      ($value = $element) =~ s/^([a-zA-Z]*)(\w*)/\1/;
553      @values = ($value, @values) unless grep(/^$value$/, @values);
554    }
555    elsif ($element =~ /^$prefix/)
556    {
557      ($value = $element) =~ s/^($prefix)([a-zA-Z]*)(\w*)/\2/;
558      @values = ($value, @values) unless grep(/^$value$/, @values);
559    }
560  }
561  return @values;
562}
563
564# recursively generates "GetSpolyLoop" by working through all $argv's
565# which math $prefix
566# Sets $checks{$prefix}, if $prefix eq $argv
567sub GenerateBody_GetSpolyLoop
568{
569  local($prefix, $nextprefix, $source, $newsource, $prop, $curr_prop, $gen_key);
570 
571  $prefix = shift(@_);
572 
573  #check for exact match -- then we are done
574  if (grep(/^$prefix$/, @_))
575  {
576    $checks{$prefix} = "return spSpolyLoop_$prefix;\n";
577    return ($checks{$prefix});
578  }
579 
580  foreach $key (& GetNextKeys($prefix, @_))
581  {
582    if ($key =~ /\w+/)
583    {
584      # get prop,
585      ($prop = $key) =~ s/([a-z]*)([A-Z]*)/\1/;
586      #check prop against exiting ones
587      if ($curr_prop)
588      {
589        if ($prop ne $curr_prop)
590        {
591          & Warning("Different propes at same level: $prop : $curr_prop");
592          next;
593        }
594      }
595      else
596      {
597        $curr_prop = $prop;
598      }
599      # delay handling of "GEN" field
600      if ($key =~ /GEN/)
601      {
602        $gen_key = $key;
603        next;
604      }
605      # let's work recursively
606      if ($prefix eq "")
607      {
608        $nextprefix = $key;
609      }
610      else
611      {
612        $nextprefix = $prefix."_".$key;
613      }
614      $newsource=&GenerateBody_GetSpolyLoop($nextprefix,grep(/$nextprefix/,@_));
615      if ($newsource)
616      {
617        $source = $source."if ($prop == $key)\n{\n".$newsource."}\n";
618      }
619    }
620  }
621 
622  # take care of general key, if it exists
623  if ($gen_key)
624  {
625    $gen_key  = $prefix."_".$gen_key unless ($prefix eq "");
626    $source = $source . &GenerateBody_GetSpolyLoop($gen_key,grep(/$gen_key/,@_));
627  }
628  return ($source);
629}
630
631
632sub Generate_GetSpolyLoop
633{
634  local($header);
635 
636  $header = "static spSpolyLoopProc spGetSpolyLoop(";
637 
638  foreach $key (@Properties)
639  {
640    $etype = $EnumType{$key};
641    if ($etype =~ /\w+/)
642    {
643      $header = $header."$etype $key,";
644    }
645  }
646  chop($header);
647 
648  return ($header. 
649          ")\n{\n" . 
650          &GenerateBody_GetSpolyLoop("", @_). 
651          "return NULL;\n}\n");
652}
653
654###########################################################################
655##
656## Input Specification
657##
658
659@input = ("chMODP".
660          "_otEXP,otCOMPEXP,otEXPCOMP".
661          "_homGEN,homYES".
662          "_nwONE,nwTWO,nwEVEN,nwODD");
663
664
665###########################################################################
666##
667## Main program
668##
669
670#flatten out input
671@finput = &FlattenInput(@input);
672
673#generate loops
674%loops = & Generate_SpolyLoops(@finput);
675
676# Generate GetSpolyLoop based on generated loops
677$getspolyloop = & Generate_GetSpolyLoop(sort(keys(%loops)));
678
679# Output results
680print & Generate_EnumTypes(@Properties);
681
682foreach $key (sort(keys(%checks)))
683{
684  print $loops{$key};
685}
686
687print $getspolyloop;
688
Note: See TracBrowser for help on using the repository browser.