Get value from solution set returned as finiteset from Sympy

I am creating a script in the Symbian Python library and trying to access the results returned by solveset () and linsolve (). My problem is that the object returned by these functions is of type finiteset, and I want to automatically select some results to re-enter it into other equations. Any body could help me?

Example: I create a list of equations with two unknown variables:

>>>lista=[eq2_1,eq2_2] >>>str(lista) [-3*a1/10 - 3*a2/20 + 1/12, -3*a1/20 - 13*a2/105 + 1/20] 

Then enable it using the linsolve () method.

 >>>a=linsolve(lista,a1,a2) >>>a {(71/369, 7/41)} 

The result is correct, but I cannot get these results in a variable.

O tried files, lists, tuples, indexing commands, but always returned an error. "Finiteset objects do not have the command attribute

+7
python sympy
source share
4 answers

You can use iter to get a set-based iterator, and then next to return one element of that set (if you only need one element).

Example:

 from sympy import * var('x y') sol = linsolve([x+y-2, 2*x-3*y], x, y) (x0, y0) = next(iter(sol)) 

Now x0 is 6/5 and y0 is 4/5.

+3
source share

I found the sympy library method in this link http://docs.sympy.org/latest/tutorial/manipulation.html

Use the .args attribute in a function or result object. If I have a function:

 >>>func = Eq(u(x),βˆ’x+sin(x)) >>>func u(x) = -x + sin(x) >>>func.args[0] u(x) >>>func.args[1] -x+sin(x) 

The same goes for the result, which is the final type of set.

+5
source share

A slightly more general solution is to simply convert FiniteSet to a standard python list

 >>> a=list(linsolve(lista,a1,a2)) >>> a [(71/369, 7/41)] 

You can then retrieve the elements using standard indexing β€” in this case a[0] . But then, if you get several solutions, you can just pull out the one you need.

+2
source share

You can use tuple in conjunction with unpacking the arguments:

 var('xy z') eqs = [ x + y + z - 1, x + y + 2*z - 3 ] sol = linsolve( eqs, x, y, z ) (x0, y0, z0) = tuple(*sol) 

Now you can check the solution with:

 eqs[0].subs( [(x, x0), (y, y0), (z, z0)] ) eqs[1].subs( [(x, x0), (y, y0), (z, z0)] ) 
0
source share

All Articles