On Tuesday, February 23, 2021 at 9:53:03 PM UTC+1 Oscar wrote:
> On Tue, 23 Feb 2021 at 18:08, Bruno Nicenboim <bruno.n...@gmail.com>
> wrote:
> >
> > Hi,
> > This is my first email. I'm starting to use sympy, which I find
> fascinating, and I have some questions that couldn't be answered after
> going through the documentation and stackoverflow website.
>
> Hi Bruno and welcome!
>
> Feel free to ask questions here. I didn't see any actual questions in
> your email though...
>
> Yeah, I just wanted to check that the mail goes through :)
I'm cross posting with stackoverflow where I didn't get any answer:
https://stackoverflow.com/questions/66200069/restricting-the-domain-of-the-solution-of-solveset-with-sympy
I'm trying to impose a constraint on the variable that I want to solve for
with sympy. (That is setting a domain that needs to be respected). And I
want to use `solveset` (I've seen other answers that use `solve`).
Say I have the following `K_p` and I want to solve `K_p - x = 0` for `t`:
```
from sympy import *
psi, mu_1, mu_2, tau, t, x = symbols("psi, mu_1, mu_2, tau, t, x", positive
= True, real = True)
K_p =(mu_1*mu_2*psi*t**2*tau**4 - 2*mu_1*mu_2*t*tau**2 - mu_1*psi*t*tau**2
+ mu_1 - mu_2*psi*t*tau**2 + mu_2 + psi)/(mu_1*mu_2*t**2*tau**4 -
mu_1*t*tau**2 - mu_2*t*tau**2 + 1)
```
How do I impose the following constraints:
`t < 1/(mu_1 * tau**2)` and `t < 1/(mu_2 * tau**2)`
If I do this I have an extra solution that shouldn't be there:
```
s_x = solveset(K_p - x, t, domain=S.Reals)
```
I've tried this:
```
cset = ConditionSet(t, t < 1/(mu_1 * tau**2)).intersect(ConditionSet(t, t
< 1/(mu_1 * tau**2)))
s_x = solveset(K_p - x, t, domain=cset)
int = Interval(-oo, 1/(maximum(mu_1,mu_2)*tau**2))
s_x = solveset(K_p - x, t, domain=int)
```
But then sympy doesn't really solve it.
Notice that I manage to get which one of the two solutions with this dumb
brute force approach:
```{python}
s_x = solveset(K_p - x, t, domain=S.Reals)
eq1 = s_x.args[0].args[1].args[0]
eq2 = s_x.args[0].args[1].args[1]
v_mu_1 = 2
v_mu_2 = 3
v_x = 50
v_psi = 10
v_tau =1
mint = min(1/(v_mu_1* v_tau**2), 1/(v_mu_2* v_tau**2))
eq1.subs([(mu_1, v_mu_1), (mu_2,v_mu_2), (tau, v_tau), (x, v_x), (psi,
v_psi)]) < mint
eq2.subs([(mu_1, v_mu_1), (mu_2,v_mu_2), (tau, v_tau), (x, v_x), (psi,
v_psi)]) < mint
```
Only one of the equations give me `True` as it should be. I would like
that the result of solveset would be that equation. Is this even possible?
> Oscar
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/8214f43e-0e91-49b4-b5a3-57663c1e3a58n%40googlegroups.com.