# Revision history [back]

### substitute algebraic numbers into a symbolic expression

How can I substitute algebraic numbers into a symbolic expression, in order to evaluate that expression with specific values for these variables? I tried the following:

var('a')
(a*3).substitute(a=AA(2))


but this yields

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_43.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("KGEqMykuc3Vic3RpdHV0ZShhPUFBKDEpKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>

File "/private/var/folders/ft/9rycr1td4nxdkvbf122dg4rr0000gn/T/tmpaq8Q8E/___code___.py", line 3, in <module>
exec compile(u'(a*_sage_const_3 ).substitute(a=AA(_sage_const_1 ))
File "", line 1, in <module>

File "expression.pyx", line 3759, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:18273)
File "expression.pyx", line 2304, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:13187)
File "parent_old.pyx", line 234, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3573)
File "parent.pyx", line 1000, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8227)
TypeError: no canonical coercion from Algebraic Real Field to Symbolic Ring


How could I modify my code above to compute the result 6 using algebraic numbers along the way? In my real life scenario, I have several variables, so turning everything into polynomials in between doesn't feel right. On the other hand, all my coefficients in my expressions are integers, so there should be no problems due to inexact floating point numbers.