codegen for time derivatives defined by dynamic symbols

33 views
Skip to first unread message

Ash

unread,
Oct 28, 2019, 7:03:57 AM10/28/19
to sympy
Hello
I am trying to develop equations of motion using sympy of a complex system. It takes 4-5 minutes to get the mass matrix (M) and coriolis vector (C). Hence I decided to use sympy's codegen and try to make it fast.
I can write my equations as

d^2 q/ dt^2 = M_inverse (Tau - C - G)
but M, C, Tau (torque) and G(gravity) are defined with respect to state variables and its derivatives. And I cant figure out how to do it.

 I am attaching a simple two degree of freedom planar robot's code for easy understanding of my problem. My state variables are the angles q1(t) and q2(t) (dynamic symbols). There are other variables like lengths, mass, inertia of the links which are defined as symbols. M(q(t)), C(q(t), dq(t)), G(q(t)) matrices are derived symbolically (line 14). My doubt is if I want to play around with the numerical values of the variables in M, C, and G, how can I efficiently generate code and cythonize and build wrapper?

Any help would be really appreciated

Thanks

2R_manipulator_dynamics.ipynb

Jason Moore

unread,
Oct 28, 2019, 8:39:33 AM10/28/19
to sympy
https://github.com/pydy/pydy will do all this for you with just a few commands.

If you want to use the code generation tools in sympy directly, it may help to replace the functions of time with ordinary symbols.

Jason


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/eb589af0-2dca-4b8d-bfff-290a4767e20a%40googlegroups.com.

Ashith Shyam

unread,
Oct 28, 2019, 9:00:06 AM10/28/19
to sy...@googlegroups.com
Thanks Jason for the reply. As I have mentioned before, my actual system is a bit more complex with the manipulator attached to a floating base. There are several non-holonomic constraints as well. I am not sure if these could be addressed by pydy or may be it will but still I need to go through examples and learn and develop again.
Could you please let me know how to replace the functions of time with ordinary symbols. I am expecting something like q1(t) to be q[0] and d q1(t) /dt to be dq[0]?

Ash



--

Jason Moore

unread,
Oct 28, 2019, 11:07:53 AM10/28/19
to sympy
If you have an expression you can use `.replace({q1: q1_sym})`.

The code generation tools in PyDy can be directly used to code gen matrices with dynamicsymbols: https://pydy.readthedocs.io/en/stable/codegen/codegen.html

Jason

Ashith Shyam

unread,
Oct 28, 2019, 11:57:13 AM10/28/19
to sy...@googlegroups.com
Thank You Jason. Really appreciate it.

Ash

Jason Moore

unread,
Oct 28, 2019, 11:59:37 AM10/28/19
to sympy

David Bailey

unread,
Oct 28, 2019, 2:24:15 PM10/28/19
to sy...@googlegroups.com

I used to work as a Mathematica consultant, and I have seen a number of Mathematica users concerned about performance issues. There are plenty of subtle ways to make Mathematica code run slowly - often dramatically so. I am sure the same is true of all algebra systems.

As a simple example from Mathematica, simply ensuring that every component of a numerical matrix is the same type can improve performance a lot. It is so easy to write some zero components as 0 (i.e. an integer) as opposed to 0.0 (a real). If you do that in a language like Fortran, the compiler will automatically convert the components of the matrix to the type of the variable that holds them, but not in Mathematica or (I think) SymPy.

I am not quite sure if your basic problem is speed or formulating your problem. However, if you have a problem with speed, it would definitely be worth taking the time to extract a simple example of a SymPy calculation that seems slow to you.

David


Ashith Shyam

unread,
Nov 1, 2019, 6:35:33 AM11/1/19
to sy...@googlegroups.com
Thanks David for the reply. It's a lot better now

--
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.
Reply all
Reply to author
Forward
0 new messages