How to change/influence the dependent/independent variables from solve

122 views
Skip to first unread message

brandon...@gmail.com

unread,
Apr 9, 2021, 2:32:51 PM4/9/21
to sympy
Given the snippet below:

from sympy import symbols, solve
w, x, y, z = symbols("w x y z")
equations = [
    w + x + y - 1,
    z - 1,
    w/(w+y) - 0.5,
    w/(w+y) + y/(w+y) - 1
]
soln = solve(equations, (w, x, y, z), dict=True)[0]  # accept first solution
print(soln)

the soln is:

{
  w: y, 
  x: 1.0 - 2.0*y, 
  z: 1.000
}

Is there a way I can use solve or some other function in SymPy to change/influence the set of independent variables? In this case, I would like to prefer x to be independent (used in the expressions of the soln values) instead of y

This is a simplified of my actual code. In practice, I don't know which symbols will can be independent before I call solve, but trivial for me to order the symbols in order of my preference for them to be independent, if possible.

Cheers,
Brandon Bocklund

Chris Smith

unread,
Apr 9, 2021, 4:03:48 PM4/9/21
to sympy
just list the variables for which you want to solve -- in this case, leave off the "x" and you will get

```
>>> soln
[{w: 0.5 - 0.5*x, y: 0.5 - 0.5*x, z: 1.00000000000000}]

```


/c

brandon...@gmail.com

unread,
Apr 9, 2021, 8:17:27 PM4/9/21
to sympy
Thank you!

This almost worked for me as is. My desired dependent variables are [w, y]. If I just pass those, then I don't get the solution for z=1. So I did two solves:

1. Solve with my desired dependent variables (e.g. [w, y])
2. Identify the set of variables that are not in the solution (as keys or in the values) and add those to my list of desired dependent varaibles ([x])
3. Solve the same set of equations again with the new minimal set of variables ([w, x, y])

Thanks for the pointer!

- Brandon

Oscar Benjamin

unread,
Apr 9, 2021, 8:21:10 PM4/9/21
to sympy
On Sat, 10 Apr 2021 at 01:17, brandon...@gmail.com
<brandon...@gmail.com> wrote:
>
> This almost worked for me as is. My desired dependent variables are [w, y]. If I just pass those, then I don't get the solution for z=1. So I did two solves:
>
> 1. Solve with my desired dependent variables (e.g. [w, y])
> 2. Identify the set of variables that are not in the solution (as keys or in the values) and add those to my list of desired dependent varaibles ([x])
> 3. Solve the same set of equations again with the new minimal set of variables ([w, x, y])

In your problem would it make more sense to have an API based around
eliminating variables rather than solving for them?

brandon...@gmail.com

unread,
Apr 9, 2021, 11:13:26 PM4/9/21
to sympy
> In your problem would it make more sense to have an API based around 
eliminating variables rather than solving for them? 

That might work. Eventually, the solution gets passed to some numerical code that needs to have values for all the symbols that obey the constraints in the system of linear and non-linear equations that I am solving. My goal is to find the relationships between the symbols and it helps the numerical stability to sample certain symbols in a special way, which is why I have a "priority" of symbols that I'd want to be independent, if possible.

Solving the same system of equations twice works for me. Even the largest system I can imagine using in the near future would use would have <25 variables and <10 equations. Sometimes the system of equations is underdetermined and sometimes over. Either is fine for my use case.
Reply all
Reply to author
Forward
0 new messages