Solve Dictionary bug ?

asked 2017-01-17 19:58:49 +0200

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 close merge delete

Sort by ยป oldest newest most voted

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:

var('x0')
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.

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

( 2017-01-17 23:02:03 +0200 )edit