Ask Your Question
1

substitute algebraic numbers into a symbolic expression

asked 2012-10-16 08:43:20 +0100

MvG gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-10-16 09:07:02 +0100

DSM gravatar image

You could make the coercion explicit:

sage: var('a')
a
sage: (a*3).substitute(a=AA(2))    
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[...]
TypeError: no canonical coercion from Algebraic Real Field to Symbolic Ring
sage: (a*3).substitute(a=SR(AA(2)))
6

or work in a polynomial ring over AA:

sage: R.<a,b,c> = AA[]
sage: R
Multivariate Polynomial Ring in a, b, c over Algebraic Real Field
sage: a*3
3*a
sage: (a*3).subs(a=2)
6
sage: parent((a*3).subs(a=2))
Algebraic Real Field
sage: parent((a*3).subs(a=AA(2)))
Algebraic Real Field
edit flag offensive delete link more

Comments

I prefer the former. Strange, though, that an explicit coercion works but cannot be performed automatically.

MvG gravatar imageMvG ( 2012-10-17 08:25:23 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-10-16 08:43:20 +0100

Seen: 1,223 times

Last updated: Oct 16 '12