Ask Your Question

Solve Dictionary bug ?

asked 2017-01-17 12:58:49 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I try to solve a simple equation, with dictionary option active. But the following example give me a traceback:

> var('x0')
> print solve([2*x0^2], [x0])
> print solve([2*x0^2], [x0], solution_dict=True)

first I got the right answer:

x0 == 0

and then the traceback is:

--------------------------------------------------------------------------- TypeError
Traceback (most recent call last) <ipython-input-1-3ee9be5134a3> in <module>() 67 var('x0') 68 print solve([Integer(2)x0Integer(2)], [x0]) ---> 69 print solve([Integer(2)x0Integer(2)], [x0], solution_dict=True)

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/symbolic/relation.pyc in solve(f, args, kwds) 809 if is_Expression(f[0]): 810 # if its a symbolic expression call solve method of this expression --> 811 return f[0].solve(args,kwds) 812 # otherwise complain 813 raise TypeError("The first argument to solve() should be a symbolic "

/home/sc_serv/sage/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.solve (/home/sc_serv/sage/src/build/cythonized/sage/symbolic/expression.cpp:59166)() 10893 if solution_dict:
10894 if isinstance(x, (list, tuple)):

10895 X = [{sol.left():sol.right() for sol in b} for b in X] 10896 else: 10897 X = [dict([[sol.left(),sol.right()]]) for sol in X]

TypeError: 'sage.symbolic.expression.Expression' object is not iterable

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-17 14:46:33 -0500

Sage is trying to iterate over a nested list of solutions when there is only one. Remove the square brackets from your single variable to avoid this:

print solve([2*x0^2], x0)
print solve([2*x0^2], x0, solution_dict=True)

It's fine to use the brackets with multiple variables.

edit flag offensive delete link more


That's right, i've done this workaround. But I think that the function should handle both a variable lis [x0] or a single variable x0 in the same way. That's why i wrote "Bug ?" in the title

emc2fred83 gravatar imageemc2fred83 ( 2017-01-17 16:02:03 -0500 )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


Asked: 2017-01-17 12:58:49 -0500

Seen: 46 times

Last updated: Jan 17