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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
# Copyright (c) 1997-2024
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://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.
#-------------------------------------------------------------------------------
package Polymake::polytope::PortaParser;
use Polymake::Struct (
[ new => '$' ],
[ '$portaFile' => '#1' ],
'$name',
'$has_points',
'$computed',
'$dim',
'@Points',
'@Ineq',
'@Eq',
);
sub new {
my $self=&_new;
replace_special_paths($self->portaFile);
$self->portaFile =~ m{([^/]+)\.(?:(poi)|ieq)(\.(?(2)ieq|poi))?$}
or die "only *.poi or *.ieq files allowed\n";
$self->name=$1;
$self->computed=defined($3);
$self->has_points=defined($2) != $self->computed;
open my $in, $self->portaFile
or die "Can't read ", $self->portaFile, ": $!\n";
local $_;
while (<$in>) {
if (/^DIM\s*=\s*(\d+)$/) {
$self->dim=$1;
} elsif (/^CON([VE])_SECTION$/) {
$self->parse_points($in, $1 eq "V");
} elsif (/^INEQUALITIES_SECTION$/) {
$self->parse_facets($in);
} elsif (/^(?:VALID|ELIMINATION_ORDER|(?:LOWER|UPPER)_BOUNDS)$/) {
do { $_=<$in> } until (/\S/); # skip it
} elsif (/^END$/) {
last;
} elsif (/\S/) {
die "file ", $self->portaFile, ", line $.: unrecognized input: $_\n";
}
}
if ($self->has_points) {
@{$self->Points} or die "file ", $self->portaFile, " lacking CONV_SECTION\n";
} else {
@{$self->Ineq} or die "file ", $self->portaFile, " lacking INEQUALITIES_SECTION\n";
}
$self;
}
sub parse_points {
my ($self, $P, $CONV_flag)=@_;
my $first= $CONV_flag ? 1 : 0;
while (<$P>) {
next unless /\S/;
unless (/\d|\(hex\)/) {
seek $P, -length, 1;
last;
}
s/^\s*\([\s\d]+\)\s*//;
s"\s*/\s*"/"g;
push @{$self->Points}, [ $first, map { parse_number($_) } split ];
}
}
sub parse_facets {
my ($self, $P)=@_;
while (<$P>) {
next unless /\S/;
unless (/^(?:\s*\([\s\d]+\))?(.*)([<=>])=/) {
seek $P, -length, 1;
last;
}
$_=$1;
my $eq= $2 eq "=";
my $gt= $2 eq ">";
my $neg_sign= $gt ? "-" : "+";
my $right=$';
$right=~s/\s//g;
my @x=(parse_number($right), (0) x $self->dim);
if ($gt) {
$x[0] =~ s/^-// or $x[0] =~ s/^/-/;
}
s/^\s*(?=[\dx])/+/;
while (/\G\s* ([+-]) (.*?) x(\d+)/gx) {
my $neg= $1 eq $neg_sign && "-";
my $coef=$2;
my $i=$3;
$coef =~ s/\s//g;
if (length($coef)) {
$x[$i]=$neg.parse_number($coef);
} else {
$x[$i]=$neg."1";
}
}
push @{$eq ? $self->Eq : $self->Ineq}, \@x;
}
}
sub hextodec {
my @dec=(0);
foreach my $h (split(//,shift)) {
$h=~tr/abcdef/012345/ and $h+=10;
for (my $i=$#dec; $i>=0; $i--) {
my $d=$dec[$i]*16+$h; $dec[$i]=$d%10; $h=$d/10;
}
unshift (@dec,$h%10), $h/=10 if $h;
unshift (@dec,$h) if $h;
}
join "", @dec
}
sub parse_number {
my $n=shift;
$n =~ s/\(hex\)([\da-f]+)/&hextodec($1)/eg;
$n
}
##############################################################################
package Polymake::polytope::PortaConverter;
use Polymake::Struct (
[ new => '$' ],
[ '$out' => '#1' ],
'$must_close',
);
sub new {
my $self=&_new;
if (is_string($self->out)) {
if ($self->out eq "STDOUT" or $self->out eq "-") {
$self->out = *STDOUT;
} else {
open my $out, ">", $self->out
or die "Can't create output file ", $self->out, "\n";
$self->out=$out;
$self->must_close=1;
}
} elsif (ref($self->out) ne "GLOB") {
croak( "usage: new PortaConverter('filename' || *IOHANDLE)" );
}
$self;
}
sub print_dim {
my ($self, $dim)=@_;
$self->out->print("DIM=$dim\n");
}
sub print_valid_point {
my ($self, $V)=@_;
$self->out->print("VALID\n", $V->slice(range_from(1)), "\n");
}
sub print_points {
my ($self, $Points, $Lin)=@_;
my (@conv, @cone);
foreach my $p (@$Points) {
if (!$p->[0]) {
push @cone, join(" ", $p->slice(range_from(1)))."\n";
} else {
push @conv, join(" ", $p->slice(range_from(1)))."\n";
}
}
if ( defined($Lin) ) {
foreach my $p (@$Lin) {
push @cone, join(" ", $p->slice(range_from(1)))."\n";
push @cone, join(" ", -$p->slice(range_from(1)))."\n";
}
}
$self->out->print("CONV_SECTION\n", @conv) if @conv;
$self->out->print("CONE_SECTION\n", @cone) if @cone;
}
sub print_ineq_matrix {
my ($Matrix, $out, $sign)=@_;
foreach my $h (@$Matrix) {
my $n=0;
my @lhs=map { ++$n; !$_ ? () : ($_>0 && "+").$_."x".$n } @{$h->slice(range_from(1))};
# skip the far hyperplane
if (@lhs) {
print $out @lhs, $sign, -$h->[0], "\n";
}
}
}
sub print_inequalities {
my ($self, $Ineq, $Eq)=@_;
$self->out->print("INEQUALITIES_SECTION\n");
print_ineq_matrix($Ineq, $self->out, ">=");
print_ineq_matrix($Eq, $self->out, "==") if defined($Eq);
}
sub finish {
my ($self)=@_;
$self->out->print("END\n");
if ($self->must_close) {
close $self->out;
}
}
1
# Local Variables:
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|