Autodistribution of Number into an Add is how SymPy works and there is no 
flag for differentiation (or for many functions) that would prevent it. 
Simply pass the expression to `factor_terms` to get it cleaned up. (But 
that will extract a factor of `t-t0`, too, which you might not want so you 
could use `Add(*[factor_terms(i) for i in expr.diff(t).args])` in this 
case.)

Some day autodistribution will go away and I expect that we will then ask 
how to get constants to distribute into simple expressions.

/c

On Sunday, February 18, 2024 at 4:52:12 AM UTC-6 [email protected] wrote:

> Hi all.
>
> I have a simple expression:
>
> >>> import sympy as sp
> >>> a, b, t, t0 = sp.symbols('a b t t0')
> >>> expr = a*(t - t0)**3 + b*(t - t0)**2
>
> And I would like to differentiate it with respect to t:
>
> >>> expr.diff(t)
> 3*a*(t - t0)**2 + b*(2*t - 2*t0)
>
> Why is the constant "2" distributed in the second term?
> It seems like an additional step that SymPy does, which doesn't really
> "improve" the situation in this case.
> Maybe there is a more general advantage that's just not visible in
> this simple case?
> But if that is so, would it be possible to tell SymPy to skip the 
> distributing?
>
> To be clear, this is the result I was expecting:
>
> >>> expr.diff(t)
> 3*a*(t - t0)**2 + 2*b*(t - t0)
>
> For context, this question came up in a slightly more complicated
> situation: 
> https://github.com/AudioSceneDescriptionFormat/splines/issues/31
>
> cheers,
> Matthias
>

-- 
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/389f5dd9-1498-455a-b6cc-ffbbff89a9d7n%40googlegroups.com.

Reply via email to