Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
sage.features.four_ti_2: New, use it in sage.interfaces.four_ti_2, sa…
Browse files Browse the repository at this point in the history
…ge.sandpiles
  • Loading branch information
Matthias Koeppe committed Jul 19, 2021
1 parent 3c32540 commit ea548d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/sage/features/four_ti_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from sage.env import SAGE_ENV

from sage.features import Executable

class FourTi2Executable(Executable):
r"""
Feature for the 4ti2 executables.
"""
def __init__(self, name):
Executable.__init__(self,
name="4ti2-" + name,
executable=SAGE_ENV.get("FOURTITWO_" + name.upper(), None) or name,
spkg="4ti2")
28 changes: 17 additions & 11 deletions src/sage/interfaces/four_ti_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#*****************************************************************************

from sage.rings.integer_ring import ZZ
from sage.features.four_ti_2 import FourTi2Executable

import os


Expand Down Expand Up @@ -271,7 +273,7 @@ def _process_input(self, kwds):
# Commands #
############

def call(self, command, project, verbose=True):
def call(self, command, project, verbose=True, *, options=()):
r"""
Run the 4ti2 program ``command`` on the project named
``project`` in the directory ``directory()``.
Expand All @@ -281,6 +283,7 @@ def call(self, command, project, verbose=True):
- command -- The 4ti2 program to run.
- project -- The file name of the project to run on.
- verbose -- Display the output of 4ti2 if ``True``.
- options -- A list of strings to pass to the program.
EXAMPLES::
Expand All @@ -292,8 +295,11 @@ def call(self, command, project, verbose=True):
[-5 3 0]
"""
import subprocess

cmd = '%s %s' % (command, project)
feature = FourTi2Executable(command)
feature.require()
executable = feature.executable
options = " ".join(options)
cmd = f'{executable} {options} {project}'
if verbose is False:
cmd += " > /dev/null 2> /dev/null"
subprocess.call(cmd, shell=True, cwd=self.directory())
Expand Down Expand Up @@ -326,7 +332,7 @@ def zsolve(self, mat=None, rel=None, rhs=None, sign=None, lat=None, project=None
"""
project = self._process_input(locals())
self.call('zsolve -q', project)
self.call('zsolve', project, options=['-q'])
return [self.read_matrix(project+'.'+ext) for ext in
['zinhom', 'zhom', 'zfree']]

Expand All @@ -343,7 +349,7 @@ def qsolve(self, mat=None, rel=None, sign=None, project=None):
[[], [ 1 -2 1]]
"""
project = self._process_input(locals())
self.call('qsolve -q -parbitrary', project)
self.call('qsolve', project, options=['-q', '-parbitrary'])
return [self.read_matrix(project+'.'+ext) for ext in
['qhom', 'qfree']]

Expand All @@ -362,7 +368,7 @@ def rays(self, mat=None, project=None):
[2 0 1 0 1 2 1 2 0]
"""
project = self._process_input(locals())
self.call('rays -q -parbitrary', project)
self.call('rays', project, options=['-q', '-parbitrary'])
return self.read_matrix(project+'.ray')

def hilbert(self, mat=None, lat=None, project=None):
Expand All @@ -385,7 +391,7 @@ def hilbert(self, mat=None, lat=None, project=None):
[1 1 1]
"""
project = self._process_input(locals())
self.call('hilbert -q', project)
self.call('hilbert', project, options=['-q'])
return self.read_matrix(project+'.hil')

def graver(self, mat=None, lat=None, project=None):
Expand All @@ -409,7 +415,7 @@ def graver(self, mat=None, lat=None, project=None):
[ 2 1 0]
"""
project = self._process_input(locals())
self.call('graver -q', project)
self.call('graver', project, options=['-q'])
return self.read_matrix(project+'.gra')

def ppi(self, n):
Expand All @@ -428,7 +434,7 @@ def ppi(self, n):
[ 1 -2 1]
"""
self.call('ppi 2> /dev/null', n)
self.call('ppi', f'{n} 2> /dev/null')
return self.read_matrix('ppi%s.gra'%n)

def circuits(self, mat=None, project=None):
Expand All @@ -445,7 +451,7 @@ def circuits(self, mat=None, project=None):
[ 3 0 -1]
"""
project = self._process_input(locals())
self.call('circuits -q -parbitrary', project)
self.call('circuits', project, options=['-q', '-parbitrary'])
return self.read_matrix(project+'.cir')

def minimize(self, mat=None, lat=None):
Expand Down Expand Up @@ -482,7 +488,7 @@ def groebner(self, mat=None, lat=None, project=None):
[ 2 1 0]
"""
project = self._process_input(locals())
self.call('groebner -q -parbitrary', project)
self.call('groebner', project, options=['-q', '-parbitrary'])
return self.read_matrix(project+'.gro')

def _magic3x3(self):
Expand Down
13 changes: 4 additions & 9 deletions src/sage/sandpiles/sandpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,11 @@

from IPython.lib import pretty

import os # CHECK: possibly unnecessary after removing 4ti2-dependent methods
from sage.calculus.functional import derivative
from sage.combinat.integer_vector import integer_vectors_nk_fast_iter
from sage.combinat.parking_functions import ParkingFunctions
from sage.combinat.set_partition import SetPartitions
from sage.combinat.vector_partition import IntegerVectorsIterator
from sage.env import SAGE_LOCAL
from sage.functions.log import exp
from sage.functions.other import binomial
from sage.geometry.polyhedron.constructor import Polyhedron
Expand All @@ -346,10 +344,7 @@
from sage.rings.all import Integer, PolynomialRing, QQ, ZZ
from sage.symbolic.constants import I, pi
from sage.symbolic.ring import SR

# TODO: remove the following line once 4ti2 functions are removed
path_to_zsolve = os.path.join(SAGE_LOCAL, 'bin', 'zsolve')

from sage.features.four_ti_2 import FourTi2Executable


def _sandpile_help(cls, usage, verbose=True):
Expand Down Expand Up @@ -5122,8 +5117,6 @@ def _set_linear_system(self):
This method requires 4ti2.
"""
# import os

L = self._sandpile._laplacian.transpose()
n = self._sandpile.num_verts()

Expand Down Expand Up @@ -5172,7 +5165,9 @@ def _set_linear_system(self):
sign_file.write('\n')
# compute
try:
os.system(path_to_zsolve+' -q ' + lin_sys + ' > ' + lin_sys_log)
import os
path_to_zsolve = FourTi2Executable('zsolve').executable
os.system(path_to_zsolve + ' -q ' + lin_sys + ' > ' + lin_sys_log)
# process the results
zhom_file = open(lin_sys_zhom,'r')
except IOError:
Expand Down

0 comments on commit ea548d7

Please sign in to comment.