On Fri, Feb 26, 2021 at 9:01 PM Oscar Benjamin <[email protected]> wrote:
> On Tue, 23 Feb 2021 at 21:06, Bruno Nicenboim <[email protected]> > wrote: > > > > On Tuesday, February 23, 2021 at 9:53:03 PM UTC+1 Oscar wrote: > >> > >> On Tue, 23 Feb 2021 at 18:08, Bruno Nicenboim <[email protected]> > 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 don't think your constraints are enough to determine what the > correct solution should be. For example if we have the values: > {mu_1:1, mu_2:2, tau:2, psi:1, x:2} > > Then neither solution satisfies the constraint: > yes, it's true. I realize that a missing piece of information is that psi < min(mu_1,mu_2,x). Is there a way to include this to get only one solution from solveset? And is changing the domain of solveset the right approach? > In [218]: rep = {mu_1:1, mu_2:2, tau:2, psi:1, x:S.Half} > > In [219]: conditions = And(t < 1/(mu_1 * tau**2), t < 1/(mu_2 * tau**2)) > > In [220]: eq = K_p - x > > In [221]: solve(eq.subs(rep)) > Out[221]: > ⎡11 √65 √65 11⎤ > ⎢── - ───, ─── + ──⎥ > ⎣16 16 16 16⎦ > > In [222]: r1, r2 = solve(eq.subs(rep)) > > In [223]: conditions.subs(rep).subs(t, r1) > Out[223]: False > > In [224]: conditions.subs(rep).subs(t, r2) > Out[224]: False > > -- > Oscar > > -- > You received this message because you are subscribed to a topic in the > Google Groups "sympy" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/sympy/oIKJ0nJHZC8/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CAHVvXxTzN5zMMUkNjXh%3Dbf5s4_mxVc%2BGzQpMCu1CmCQkyr5tVA%40mail.gmail.com > . > -- Bests, Bruno Nicenboim -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHRDyEbk2U%3Dk9jGN%2B5AZS2qOrC7dKz5EuJArf%3DB72LUpe9CvUw%40mail.gmail.com.
