SymPy's subs
is only going to replace things that appear exactly in an expression. It has a couple of minor heuristics (like handling of (x**4).subs(x**2, y)
, but nothing that will factor or otherwise rewrite expressions for you.
One trick is to rearrange the substitution so that you are only substituting one term, like
small_exp.subs(L_p*sin(alpha), L_r*cos(alpha) + L_r_tilde)
After simplification, everything should cancel out.
For your specific expression, it looks like every instance of your subexpression is multiplied by sin(alpha)
, so you could just include that term in your definition of L_r_tilde
, or you can replace L_r*cos(alpha)*sin(alpha) -L_p*sin(alpha)**2
with L_r_tilde*sin(alpha)
.
I'll also point out the cse()
function, which pulls out common subexpressions automatically.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…