source: git/Singular/spSpolyLoop.pl @ 5480da

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