Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fricas/do not get inputform twice #37836

Merged
merged 2 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions src/sage/interfaces/fricas.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,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 @@ -1715,7 +1715,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 @@ -1994,42 +1993,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 @@ -2039,11 +2049,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 @@ -2055,13 +2066,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
Loading