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

Commit 9552c8a

Browse files
Release Managervbraun
authored andcommitted
Trac #25296: RingConverter should handle named variables by default
In the current version of Sage, when a !RingConverter is used to convert from the symbolic ring to an algebraic ring, all converted variables must be specified in the !RingConverter's dictionary. For example, {{{ sage: from sage.symbolic.expression_conversions import RingConverter sage: R = QQ['x'] sage: RC = RingConverter(R, {x: R.0}) sage: RC(x) x }}} Attempting to convert without a dictionary produces an error: {{{ sage: RC = RingConverter(R) sage: RC(x) ... TypeError: }}} This patch changes this behavior so that if a variable is not found in the !RingConverter's dictionary, an attempt is make to convert it to the ring using the ring's element constructor. This allows the last example to work as expected; the symbolic variable "x" is converted to the ring element of the same name. {{{ sage: from sage.symbolic.expression_conversions import RingConverter sage: R = QQ['x'] sage: RC = RingConverter(R) sage: R(x) x sage: x.parent() Symbolic Ring sage: R(x).parent() Univariate Polynomial Ring in x over Rational Field }}} URL: https://trac.sagemath.org/25296 Reported by: gh-BrentBaccala Ticket author(s): Brent Baccala Reviewer(s): Marc Mezzarobba
2 parents 1858c23 + 2a55bc2 commit 9552c8a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/sage/symbolic/expression_conversions.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,8 +1801,9 @@ def __init__(self, R, subs_dict=None):
18011801

18021802
def symbol(self, ex):
18031803
"""
1804-
All symbols appearing in the expression must appear in *subs_dict*
1805-
in order for the conversion to be successful.
1804+
All symbols appearing in the expression must either appear in *subs_dict*
1805+
or be convertable by the ring's element constructor in order for the
1806+
conversion to be successful.
18061807
18071808
EXAMPLES::
18081809
@@ -1815,12 +1816,18 @@ def symbol(self, ex):
18151816
sage: R(x+pi)
18161817
Traceback (most recent call last):
18171818
...
1818-
TypeError
1819+
TypeError: unable to simplify to a real interval approximation
1820+
1821+
sage: R = RingConverter(QQ['x'])
1822+
sage: R(x^2+x)
1823+
x^2 + x
1824+
sage: R(x^2+x).parent()
1825+
Univariate Polynomial Ring in x over Rational Field
18191826
"""
18201827
try:
18211828
return self.ring(self.subs_dict[ex])
18221829
except KeyError:
1823-
raise TypeError
1830+
return self.ring(ex)
18241831

18251832
def pyobject(self, ex, obj):
18261833
"""

0 commit comments

Comments
 (0)