File: gfan.rules

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (145 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download | duplicates (2)
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
#  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.
#-------------------------------------------------------------------------------

CREDIT gfan
  Gfan is a software package for computing Groebner fans and tropical varieties.
  Copyright by Anders Jensen
  http://home.imf.au.dk/jensen/software/gfan/gfan.html

# path to gfan_bases
custom $gfan_bases;

# path to gfan_secondaryfan
custom $gfan_secondaryfan;

# path to gfan_topolyhedralfan
custom $gfan_topolyhedralfan;

CONFIGURE {
    find_program($gfan_bases, "gfan_bases");
    find_program($gfan_secondaryfan = $gfan_bases =~ s/gfan_bases$/gfan_secondaryfan/r,
		 "gfan_secondaryfan");
    find_program($gfan_topolyhedralfan = $gfan_bases =~ s/gfan_bases$/gfan_topolyhedralfan/r,
		 "gfan_topolyhedralfan");
}

# command, input_generator => serialized BigObject in JSON
sub run_gfan {
  require Polymake::Core::XMLtoJSON;

  my ($input_gen, @pipeline) = @_;
  my $tempfile = new TempTextFile;
  $input_gen->($tempfile);
  $pipeline[0] .= " <$tempfile";
  if (!$DebugLevel) {
    $_ .= " 2>/dev/null" for @pipeline;
  }
  my $cmd = join(" | ", @pipeline) . " --xml |";
  open my $P, $cmd
    or die "can't run `$cmd': $!\n";
  my $data = Core::XMLtoJSON::from_filehandle($P);
  close $P;
  die "`$cmd' failed with exit code $?\n" if $?;
  return $data;
}

sub gfan_to_symmetric_fan {
  my ($data) = @_;
  my $version = $data->{_ns}{polymake}[1];
  if ($version eq "2.9.9") {
    # remove leading "1" from F_VECTOR
    # other adaptations will be done by upgrade rules
    if (defined(my $v = $data->{F_VECTOR})) {
      shift @$v;
    }
  }
  Core::Serializer::deserialize($data)
}


# @category Producing a fan
# Call [[wiki:external_software#gfan]] to compute the secondary fan of a point configuration.
#
# @param Matrix M a matrix whose rows are the vectors in the configuration
# @return PolyhedralFan
# @example Four points in the plane of which none three are on a line give
# us a secondary fan consisting of two opposing cones with 3-dimensional lineality:
# > $f = gfan_secondary_fan(new PointConfiguration(POINTS=>[[1,0,0],[1,1,0],[1,0,1],[1,1,1]]));
# > print $f->RAYS;
# | -1 1 1 -1
# | 1 -1 -1 1
# > print $f->MAXIMAL_CONES;
# | {0}
# | {1}
# > print $f->LINEALITY_SPACE;
# | 1 0 0 -1
# | 0 1 0 1
# | 0 0 1 1
user_function gfan_secondary_fan(Matrix) {
  my ($M) = @_;
  return gfan_to_symmetric_fan(run_gfan(sub { gfan_print_matrix($_[0], eliminate_denominators_entire_affine($M)) },
                                        $gfan_secondaryfan));
}

sub gfan_print_matrix {
  my ($infile, $M) = @_;

  print $infile "{";
  my $r = $M->rows();
  my $c = $M->cols();
  for (my $i = 0; $i < $r; ++$i) {
    print $infile "," if $i != 0;
    print $infile "(";
    for (my $j = 0; $j < $c; ++$j) {
      print $infile "," if $j != 0;
      print $infile $M->elem($i, $j);
    }
    print $infile ")";
  }
  print $infile "}\n";
}

# @category Producing a fan
# Call [[wiki:external_software#gfan]] to compute the secondary fan of a point configuration.
#
# @param polytope::PointConfiguration P
# @return PolyhedralFan
user_function gfan_secondary_fan(polytope::PointConfiguration) {
  my ($pc)=@_;
  return gfan_secondary_fan($pc->POINTS);
}

# @category Producing a fan
# Call [[wiki:external_software#gfan]] to compute the greobner fan of an ideal.
#
# @param ideal::Ideal I input ideal
# @return PolyhedralFan
user_function groebner_fan(ideal::Ideal) {
  my ($I) = @_;
  return gfan_to_symmetric_fan(run_gfan(sub { gfan_print_ideal($_[0], $I) }, $gfan_bases,
                                        $gfan_topolyhedralfan));
}

sub gfan_print_ideal {
  my ($infile, $I) = @_;
  print $infile "Q[", join(",", monomials($I->N_VARIABLES)), "]\n";
  print $infile "{", join(",", @{$I->GENERATORS}), "}\n";
}

# Local Variables:
# mode: perl
# cperl-indent-level:2
# End: