Live data from Hacker News

SymPy: Symbolic Mathematics in Python

sympy.org

51–60 of 137 posts

Re: SymPy: Symbolic Mathematics in Python

#51
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

Have you considered using jax? you can efficiently compute the jacobian (even using the gpu if you want) without leaving python at all. The api is also numpy compatible!

Re: SymPy: Symbolic Mathematics in Python

#52
post #36
post #6

I use SymPy as my calculator with https://github.com/idanpa/calcpy

This looks perfect. I've found native sympy to be a little cumbersome to use.

Same here, I've been slogging through with raw sympy this tool looks great.

How do I speed up the start time for it - any ideas? Recompile it with PyPy or similar?

Re: SymPy: Symbolic Mathematics in Python

#53
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

Have you considered using jax? you can efficiently compute the jacobian (even using the gpu if you want) without leaving python at all. The api is also numpy compatible!

We target very low-level & specific hardware. I don't think it would be easy to deploy Jax on it. But it's an interesting idea, maybe for robots running linux !

Re: SymPy: Symbolic Mathematics in Python

#54
post #21
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

Why are you calculating the Jacobian symbolically? AFAIK, for complex cases, the numerical Jacobian is often faster / more numerically stable.

Especially if you actually require vector-Jacobian or Jacobian-vector products instead of the full Jacobian.

Re: SymPy: Symbolic Mathematics in Python

#55
post #43
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

I've used SymPy in a very similar way, in my case it was some nasty derivatives that were needed for some orbital mechanics calculations. First write down the equations, then let SymPy do the laborious math part and turn the output into C code. I'm guessing this is why you don't see a lot of SymPy projects in the wild. It was used for some intermediate calculations, and the results were turned into the product's code…

There might be a better way, but my habit is to include the symbolic code as a comment in the C. I've thanked myself for doing this.

Re: SymPy: Symbolic Mathematics in Python

#57
It's a seriously cool way to solve problems, never used it but discovered that the winner of one of the Advent of Code days in 22 or 23 basically just set up equations with SymPy and let it solve everything beating out the usual cadre of "fast-hackers" in the top.

Re: SymPy: Symbolic Mathematics in Python

#58
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

I just did this exact thing today, writing a cloth simulator! I wanted to try updating each node position in turn using a Newton step, holding the other nodes fixed.

Re: SymPy: Symbolic Mathematics in Python

#59
post #43

Earlier quoted context omitted.

I've used SymPy in a very similar way, in my case it was some nasty derivatives that were needed for some orbital mechanics calculations. First write down the equations, then let SymPy do the laborious math part and turn the output into C code. I'm guessing this is why you don't see a lot of SymPy projects in the wild. It was used for some intermediate calculations, and the results were turned into the product's code…

There might be a better way, but my habit is to include the symbolic code as a comment in the C. I've thanked myself for doing this.

I commit the symbolic code on its own file, in the same commit which adds the c code.

Re: SymPy: Symbolic Mathematics in Python

#60
post #21
post #11

Working in the field of robotics, I often have to write big vectors ( usually computed as the result of 3D transformations ) and then compute their Jacobian ( their derivative with respect to several state-variables ), which quickly becomes very nasty equations. I use sympy to (i) compute these big vectors, expressed in a very declarative way, (ii) compute the jacobian and (iii) export the results in C-code, immediat…

Why are you calculating the Jacobian symbolically? AFAIK, for complex cases, the numerical Jacobian is often faster / more numerically stable.

I though finite difference differentiation was notoriously unstable.
Post reply on HN