From 636a2c0c9d077864f1dada86c33a9491607646c5 Mon Sep 17 00:00:00 2001 From: Martin Rubey Date: Sat, 20 Apr 2024 15:07:40 +0200 Subject: [PATCH] do not convert the InputForm to a string twice --- src/sage/interfaces/fricas.py | 59 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/sage/interfaces/fricas.py b/src/sage/interfaces/fricas.py index 6b2036d2a64..4e557a44da0 100644 --- a/src/sage/interfaces/fricas.py +++ b/src/sage/interfaces/fricas.py @@ -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): """ @@ -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 @@ -1995,19 +1994,24 @@ 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, @@ -2015,22 +2019,28 @@ def _sage_(self): # 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]) @@ -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 @@ -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