Solving equations and using values ​​obtained in other calculations - SAGE

the solve () function in SAGE returns symbolic values ​​for variables i that solve equations for. eg,

sage: s=solve(eqn,y) sage: s [y == -1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) + 8*x^4 + 11*x^3 + 12*x^2)/(15*x + 1), y == 1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) - 8*x^4 - 11*x^3 - 12*x^2)/(15*x + 1)] 

My problem is that I need to use the values ​​obtained for y in other calculations, but I cannot assign these values ​​to any other variable. Can someone please help me with this?

+4
source share
1 answer

(1) You should visit ask.sagemath.org , a stack overflow forum for Sage users, experts, and developers! </plug>

(2) If you want to use the values ​​of the solve () call in something, then probably the easiest way is to use the solution_dict flag:

 sage: x,y = var("x, y") sage: eqn = x**4+5*x*y+3*xy==17 sage: solve(eqn,y) [y == -(x^4 + 3*x - 17)/(5*x - 1)] sage: solve(eqn,y,solution_dict=True) [{y: -(x^4 + 3*x - 17)/(5*x - 1)}] 

This option provides solutions in the form of a list of dictionaries instead of a list of equations. We can access the results, like any other dictionary:

 sage: sols = solve(eqn,y,solution_dict=True) sage: sols[0][y] -(x^4 + 3*x - 17)/(5*x - 1) 

and then we can assign this something else if you want:

 sage: z = sols[0][y] sage: z -(x^4 + 3*x - 17)/(5*x - 1) 

and replace:

 sage: eqn2 = y*(5*x-1) sage: eqn2.subs(y=z) -x^4 - 3*x + 17 

et cetera. Although IMHO above is more convenient, you can also access the same results without a dict solution via .rhs ():

 sage: solve(eqn,y)[0].rhs() -(x^4 + 3*x - 17)/(5*x - 1) 
+7
source

All Articles