File: poly_parser.rules

package info (click to toggle)
polymake 3.0r2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,752 kB
  • ctags: 30,928
  • sloc: cpp: 151,785; perl: 32,510; ansic: 3,597; java: 2,654; python: 278; makefile: 181; xml: 103; sh: 79
file content (184 lines) | stat: -rw-r--r-- 5,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#  Copyright (c) 1997-2015
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

# This file contains methods to parse a polynomial from a string. If the ring
# is very complicated, it might be easier to access the user_function directly,
# which is why it is left available.


property_type Polynomial {

   method construct(Ring, String) {
      toPolynomial($_[2], $_[1], $_[1]->variables);
   }
}

property_type Term {

   method construct(Ring, String) {
      parseTerm($_[2], $_[1]->variables);
   }
}

# @category Data Conversion
# Read a [[Polynomial]] from a String.
# @param String s
# @param ARRAY vars
user_function toPolynomial {
   my ($s, $ring, @vars) = @_;
   # Remove white spaces.
   $s =~ s/\s//g;
   my $stilda = $s;

   foreach my $var (@vars) {
      my $varstring = "$var";
      my $replacement = "m".("a" x (length($varstring)-1));
      $varstring = quotemeta($varstring);
      $stilda =~ s/$varstring/$replacement/g;
   }
   # Check for correct format of stilda:
   $stilda =~ m/^[0123456789\/\*\^\-\+a\(\)m]*$/ or die "Input error: $stilda.";
   return parseSumRecursively($s, $stilda, $ring, @vars);
}

sub parseSumRecursively {
   my ($s,$stilda,$ring,@vars) = @_;
   my ($op) = $stilda =~ m/^([\+\-]?)/x;
   $s = substr($s,length($op),length($s));
   $stilda = substr($stilda,length($op),length($stilda));
   my @term = ();
   my @termtilda = ();
   my $hasbrackets = 0;
   # Match the first summand. While matching unwrap the product structure.
   while (($stilda !~ m/^[+-]/) && (length($stilda) > 0)) {
      if ($stilda =~ m/^\(/) {
         # If we have beginning brackets:
         my ($factor) = $stilda =~ m/(\( (?: [^\(\)]* | (?0) )* \)) /x;
         my ($hat, $pow) = $' =~ m/^(\^?)(\d*)/;
         my $totallength = length($factor)+length($hat)+length($pow);
         $hasbrackets = 1;
         push @termtilda, $factor.$hat.$pow;
         push @term, substr($s,0,$totallength);
         $stilda = substr($stilda,$totallength,length($stilda));
         $s = substr($s,$totallength,length($s));
      } elsif (my ($factor) = $stilda =~ m/^([^\+\-\(\)]*)/x) {
         # If we start with a factor in front:
         push @termtilda, $factor;
         push @term, substr($s,0,length($factor));
         $stilda = substr($stilda,length($factor),length($stilda));
         $s = substr($s,length($factor),length($s));
      }
   }
   my $result = new Polynomial(1, $ring);
   if ($hasbrackets) {
      foreach my $factor (@termtilda) {
         my $f = shift @term;
         if (my ($inner, $pow) = $factor =~ m/^\((.*)\)\^?(\d*)$/) {
            if ($pow) {
               while ($pow>0) {
                  $result = $result * parseSumRecursively(substr($f,1,length($inner)),$inner,$ring,@vars);
                  $pow--;
               }
            } else {
               $result = $result * parseSumRecursively(substr($f,1,length($inner)),$inner,$ring,@vars);
            }
         } else {
            $factor =~ s/^\*?//;
            $factor =~ s/\*?$//;
            $f =~ s/^\*?//;
            $f =~ s/\*?$//;
            if (length($f)>0) {
               # If there was more than a star.
               $result = $result * parseTerm($f,$factor,@vars);
            }
         }
      }
   } else {
      # We must have a monomial!
      $result = new Polynomial(0, $ring) + parseTerm(pop @term, pop @termtilda, @vars);
   }
   if ($op eq "-") {
      $result = -$result;
   }
   if (length($stilda) > 0) {
      $result = parseSumRecursively($s,$stilda, $ring, @vars) + $result;
   }
   return $result;
}



sub parseTerm {
   my ($s, $stilda, @vars) = @_;
   # Sanity check:
   # print "Parsing Monomial.\nS:  ",$s,"\nST: ",$stilda,"\n";
   ($s !~ m/^\*(.*)/) or die "Incorrect monomial form.";
   ($s !~ m/(.*)\*$/) or die "Incorrect monomial form.";
   if ($stilda !~ m/m/) {
      if (index($stilda,"/")>-1) {
         my ($a,$b) = $stilda =~ m/^(\d*)\/(\d*)/;
         return new Rational($a,$b);
      } else {
         return $stilda;
      }
   }
   my %varMap = ();
   my $i = 0;
   foreach my $v (@vars){
      my $varstring = $v."";
      $varMap{$varstring} = $i;
      $i++;
   }
   my $nvars = @vars;
   my $exp = new Vector<Int>($nvars);
   my $totalcoef = 1;
   while (length($s)>0) {
      my ($coef,$star1,$varlength,$hat,$pow,$star2) = $stilda =~ m/^(\d*\/?\d*)(\*?)(ma*)(\^?)(\d*)(\*?)/;
      my $varstring = substr($s, length($coef.$star1), length($varlength));
      if ($pow) {
         $exp->[$varMap{$varstring}] += $pow;
      } else {
         $exp->[$varMap{$varstring}] ++;
      }
      my $totallength = length($coef.$star1.$varlength.$hat.$pow.$star2);
      $s = substr($s, $totallength, length($s));
      $stilda = substr($stilda,$totallength,length($stilda));
      if ($coef) {
         if (index($coef,"/")>-1) {
            my ($a,$b) = $coef =~ m/^(\d*)\/(\d*)/;
            $totalcoef *= new Rational($a,$b);
         } else {
            $totalcoef *= $coef;
         }
      }
   }
   my $result = 1;
   my $i = 0;
   foreach my $var (@vars) {
      my $pow = $exp->[$i];
      while ($pow>0) {
         $result = $result * $var;
         $pow--;
      }
      $i++;
   }
   
   # print "Result: ",$result," ",$exp," ",%varMap,"\n";
   if ($totalcoef != 1) {
      return $totalcoef*$result;
   } else {
      return $result;
   }
}