There probably is a better way of doing this in your problem but I
just want to point out a way to "manually" extract parts of an
expression. Let's create an expression with a few parts:
In [1]: a, b, c, x = symbols('a b c x')
In [2]: p = a*x**2 + b*x + c
In [3]: r1, r2 = solve(p, x)
In [4]: r1
Out[4]:
_____________
╱ 2
-b + ╲╱ -4⋅a⋅c + b
─────────────────────
2⋅a
Okay now suppose I want to get the discriminant out from inside the
square root and do something with it. I can use the .args attribute of
each sympy expression to drill down to the part I want like this:
In [5]: r1.args
Out[5]:
⎛ _____________⎞
⎜ 1 ╱ 2 ⎟
⎜1/2, ─, -b + ╲╱ -4⋅a⋅c + b ⎟
⎝ a ⎠
In [6]: r1.args[2]
Out[6]:
_____________
╱ 2
-b + ╲╱ -4⋅a⋅c + b
In [7]: r1.args[2].args
Out[7]:
⎛ _____________ ⎞
⎜ ╱ 2 ⎟
⎝╲╱ -4⋅a⋅c + b , -b⎠
In [8]: r1.args[2].args[0]
Out[8]:
_____________
╱ 2
╲╱ -4⋅a⋅c + b
In [9]: r1.args[2].args[0].args
Out[9]:
⎛ 2 ⎞
⎝-4⋅a⋅c + b , 1/2⎠
In [10]: r1.args[2].args[0].args[0]
Out[10]:
2
-4⋅a⋅c + b
On Sun, 10 Mar 2019 at 16:01, Mark Juers <
mpj...@gmail.com> wrote:
>
> In my real problem, d is an arbitrarily complex expression I'd rather not type out in full, and the factored part is inside a subexpression, so I'm not sure I could get this to work.
>
> To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/1b8b1b7e-22ea-4ae9-9338-f0829e2341b2%40googlegroups.com.