Derivative without Simplification

100 views
Skip to first unread message

Michi S

unread,
Jun 1, 2016, 9:34:28 AM6/1/16
to sympy
Hello!

Is there a way to turn off the automatic simplification by calculating the derivative of a function? For example

t = sym.symbols('t')
sym.Derivative( (sin(t)+exp(t))**3 , t ).doit()


gives:
(exp(t) + sin(t))**2*(3*exp(t) + 3*cos(t))

I need the result without any simplifications (in order to detect the inner derivative for further calculations)

Should be:
(exp(t) + sin(t))**2*3*(exp(t) + cos(t))

Michi S

unread,
Jun 1, 2016, 12:05:10 PM6/1/16
to sympy
factor(sym.Derivative( (sin(t)+exp(t))**3 , t ).doit())

works, but it would still be nice if someone could tell me, if there is way to turn the simplification of ;)

Richard Fateman

unread,
Jun 6, 2016, 8:55:44 AM6/6/16
to sympy
I don't understand why you need it, but you could write your own differentiation program
without simplification in about 1/2 page of code.

Christophe Bal

unread,
Jun 6, 2016, 10:21:35 AM6/6/16
to sympy-list
Hello.

Have you try factorization ?


Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French teacher of math in a high school and amateur Python developer

--
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+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d611deec-a8b5-4c5d-9b30-cfd10cde6afa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Jun 6, 2016, 2:43:46 PM6/6/16
to sy...@googlegroups.com
Unfortunately, distribution of numbers across additions is done
automatically, so disabling it is not easy. Using factor() here is
your best solution.

Aaron Meurer
> --
> 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+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/0317a185-3207-413f-885b-cec51d3deb4a%40googlegroups.com.

Michi S

unread,
Jun 7, 2016, 8:58:46 AM6/7/16
to sympy

Thanks for the answers! For the purpose of my master thesis I am trying to optimize the simulaion of helicopter dynamics. These dynamics are pretty complicated, which leads to huge equations for most states. In order to speed up the calculation I need to detect functions that where already evaluated. The inner derivative that is produced due to the chain rule is such a function, that I need to detect.

factor() works only for the first derivative unfortunately. In the second and higher derivatives it does not factorize the inner derivates anymore:

t = sym.symbols('t')
diff( (sin(t)+exp(t))**5 , t )
>> (exp(t) + sin(t))**4*(5*exp(t) + 5*cos(t))

factor(diff( (sin(t)+exp(t))**5 , t ))
>> 5*(exp(t) + sin(t))**4*(exp(t) + cos(t))

factor(diff( (sin(t)+exp(t))**5 , t, t ))
>> 5*(exp(t) + sin(t))**3*(5*exp(2*t) + 8*exp(t)*cos(t) - sin(t)**2 + 4*cos(t)**2)



I think, writing my own differentiation programm will be a little bit to hard to solve my issue. Is there no other way besides factor() and wiriting my own programm?


Thank for your help!



Am Mittwoch, 1. Juni 2016 15:34:28 UTC+2 schrieb Michi S:

Aaron Meurer

unread,
Jun 7, 2016, 1:14:56 PM6/7/16
to sy...@googlegroups.com
On Tue, Jun 7, 2016 at 8:58 AM, Michi S <misc...@gmail.com> wrote:
>
> Thanks for the answers! For the purpose of my master thesis I am trying to
> optimize the simulaion of helicopter dynamics. These dynamics are pretty
> complicated, which leads to huge equations for most states. In order to
> speed up the calculation I need to detect functions that where already
> evaluated. The inner derivative that is produced due to the chain rule is
> such a function, that I need to detect.
>
> factor() works only for the first derivative unfortunately. In the second
> and higher derivatives it does not factorize the inner derivates anymore:
>
> t = sym.symbols('t')
> diff( (sin(t)+exp(t))**5 , t )
>>> (exp(t) + sin(t))**4*(5*exp(t) + 5*cos(t))
>
> factor(diff( (sin(t)+exp(t))**5 , t ))
>>> 5*(exp(t) + sin(t))**4*(exp(t) + cos(t))
>
> factor(diff( (sin(t)+exp(t))**5 , t, t ))
>>> 5*(exp(t) + sin(t))**3*(5*exp(2*t) + 8*exp(t)*cos(t) - sin(t)**2 +
>>> 4*cos(t)**2)

What output were you expecting here? I don't think the second factor
can be factorized. Note however that it can be simplified slightly if
you call trigsimp() on it.

Aaron Meurer

>
>
> I think, writing my own differentiation programm will be a little bit to
> hard to solve my issue. Is there no other way besides factor() and wiriting
> my own programm?
>
>
> Thank for your help!
>
>
> Am Mittwoch, 1. Juni 2016 15:34:28 UTC+2 schrieb Michi S:
>>
>> Hello!
>>
>> Is there a way to turn off the automatic simplification by calculating the
>> derivative of a function? For example
>>
>> t = sym.symbols('t')
>> sym.Derivative( (sin(t)+exp(t))**3 , t ).doit()
>>
>> gives:
>> (exp(t) + sin(t))**2*(3*exp(t) + 3*cos(t))
>>
>> I need the result without any simplifications (in order to detect the
>> inner derivative for further calculations)
>>
>> Should be:
>> (exp(t) + sin(t))**2*3*(exp(t) + cos(t))
>
> --
> 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+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/c5e1f95e-d5e2-4334-b302-39bd6e2ebc9b%40googlegroups.com.

Alan Bromborsky

unread,
Jun 7, 2016, 2:39:20 PM6/7/16
to sy...@googlegroups.com
You might want to look at "New Foundations for Classical Mechanics" by David Hestenes and the rigid body rotor description of equations of motion.

http://www.amazon.com/Foundations-Classical-Mechanics-Fundamental-Theories/dp/0792353021

Should be in your school library.



Richard Fateman

unread,
Jun 7, 2016, 5:22:50 PM6/7/16
to sympy
I think you are merely trying to find common subexpressions to speed up evaluation.
There are lots of ways to do that.
The simplest is to precompute sin(t), cos(t), exp(t) or whatever non-elementary
functions are involved.  Then you can also consider using horner's rule  or
something like factor.  But there should be a common-subexpression extraction
program around, no?
RJF

Aaron Meurer

unread,
Jun 8, 2016, 12:16:06 AM6/8/16
to sy...@googlegroups.com
Yep.  It's called cse().

Aaron Meurer 

Jason Moore

unread,
Jun 8, 2016, 9:48:10 AM6/8/16
to sy...@googlegroups.com
You may also be interested in PyDy which generates optimized C code from symbolic equations of motion. It uses cse under the hood for some of the optimizations. PyDy directly works with the EoMs that are formed from sympy.physics.mechanics.
Reply all
Reply to author
Forward
0 new messages