diff --git a/src/sage/interfaces/fricas.py b/src/sage/interfaces/fricas.py index 87feb554d16..5281bc4e1a6 100644 --- a/src/sage/interfaces/fricas.py +++ b/src/sage/interfaces/fricas.py @@ -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): """ @@ -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 @@ -1994,19 +1993,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, @@ -2014,22 +2018,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]) @@ -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 @@ -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