Skip to content

Commit

Permalink
do not convert the InputForm to a string twice
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Apr 20, 2024
1 parent 15fcf52 commit 636a2c0
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/sage/interfaces/fricas.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def get_unparsed_InputForm(self, var):
'(1..3)$Segment(PositiveInteger())'
"""
return self.get_string('unparse((%s)::InputForm)' % str(var))
return self.get_string('unparse((%s)::InputForm)' % var)

def get_InputForm(self, var):
"""
Expand Down Expand Up @@ -1716,7 +1716,6 @@ def _sage_expression(fricas_InputForm):
sage: f[1].sage()
-1/2*sqrt(1/3)*sqrt((3*(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(2/3) + 4)/(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(1/3)) + 1/2*sqrt(-(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(1/3) + 6*sqrt(1/3)/sqrt((3*(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(2/3) + 4)/(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(1/3)) - 4/3/(1/18*I*sqrt(229)*sqrt(3) + 1/2)^(1/3))
"""
# a FriCAS expressions may contain implicit references to a
# rootOf expression within itself, as for example in the
Expand Down Expand Up @@ -1995,42 +1994,53 @@ def _sage_(self):
return R([self.coefficient(i).sage()
for i in range(ZZ(self.degree()) + 1)])

# finally translate domains with InputForm
try:
unparsed_InputForm = P.get_unparsed_InputForm(self._name)
except RuntimeError as error:
raise NotImplementedError("the translation of the FriCAS object\n\n%s\n\nto sage is not yet implemented:\n%s" % (self, error))
# finally translate domains with InputForm - we do this
# lazily, because sometimes we can use unparse, sometimes we
# need our custom sageprint

def unparsed_InputForm():
try:
return P.get_unparsed_InputForm(self._name)
except RuntimeError as error:
raise NotImplementedError("the translation of the FriCAS object\n\n%s\n\nto sage is not yet implemented:\n%s" % (self, error))

if head == "Boolean":
return unparsed_InputForm == "true"
return unparsed_InputForm() == "true"

if head in ["Integer", "NonNegativeInteger", "PositiveInteger"]:
return ZZ(unparsed_InputForm)
return ZZ(unparsed_InputForm())

if head == "String":
return unparsed_InputForm
return unparsed_InputForm()

if head == "Float":
# Warning: precision$Float gives the current precision,
# whereas length(mantissa(self)) gives the precision of
# self.
prec = max(P.new("length mantissa(%s)" % self._name).sage(), 53)
R = RealField(prec)
x, e, b = unparsed_InputForm.lstrip('float(').rstrip(')').split(',')
x, e, b = unparsed_InputForm().lstrip('float(').rstrip(')').split(',')
return R(ZZ(x) * ZZ(b)**ZZ(e))

if head == "DoubleFloat":
return RDF(unparsed_InputForm)
return RDF(unparsed_InputForm())

if head == "AlgebraicNumber":
s = unparsed_InputForm[:-len("::AlgebraicNumber()")]
s = unparsed_InputForm()[:-len("::AlgebraicNumber()")]
return sage_eval("QQbar(" + s + ")")

if head == "IntegerMod" or head == "PrimeField":
# one might be tempted not to go via InputForm here, but
# it turns out to be safer to do it.
n = unparsed_InputForm[len("index("):]
n = n[:n.find(")")]
return self._get_sage_type(domain)(n)
s = unparsed_InputForm()[len("index("):]
s = s[:s.find(")")]
return self._get_sage_type(domain)(s)

if head == 'DistributedMultivariatePolynomial':
base_ring = self._get_sage_type(domain[2])
vars = domain[1].car()
R = PolynomialRing(base_ring, vars)
return R(unparsed_InputForm())

if head == "Polynomial":
base_ring = self._get_sage_type(domain[1])
Expand All @@ -2040,11 +2050,12 @@ def _sage_(self):

# the following is a bad hack, we should be getting a list here
vars = P.get_unparsed_InputForm("variables(%s)" % self._name)[1:-1]
s = unparsed_InputForm()
if vars == "":
return base_ring(unparsed_InputForm)
else:
R = PolynomialRing(base_ring, vars)
return R(unparsed_InputForm)
return base_ring(s)

R = PolynomialRing(base_ring, vars)
return R(s)

if head in ["OrderedCompletion", "OnePointCompletion"]:
# it would be more correct to get the type parameter
Expand All @@ -2056,13 +2067,7 @@ def _sage_(self):
# Integer just the same
return FriCASElement._sage_expression(P.get_InputForm(self._name))

if head == 'DistributedMultivariatePolynomial':
base_ring = self._get_sage_type(domain[2])
vars = domain[1].car()
R = PolynomialRing(base_ring, vars)
return R(unparsed_InputForm)

raise NotImplementedError("the translation of the FriCAS object %s to sage is not yet implemented" % (unparsed_InputForm))
raise NotImplementedError("the translation of the FriCAS object %s to sage is not yet implemented" % (unparsed_InputForm()))


@instancedoc
Expand Down

0 comments on commit 636a2c0

Please sign in to comment.