Hacker News new | ask | show | jobs
by nitsuaeekcm 2021 days ago
SymPy is pretty niche, but it saved my butt many years ago in my years of collegiate robotics. We spent months unsuccessfully trying to compute kinematics and sensor coordinate transformations on vectors of quarternions by “hand” with numpy. I wouldn’t wish it on my worst enemy. There are so many opportunities for mistakes, and the intermediate numbers can rarely be checked against your intuition.

With SymPy however (and some optional Newtonian physics package), one can simply define their free-bodies and coordinate systems, and SymPy will spit out simplified, compiled tensors of whatever expressions they want! It was truly magical at the time.

5 comments

> SymPy is pretty niche

I don't know; I use isympy (interactive sympy, uses IPython if installed) as a local Wolfram Alpha for all my math problems, and it's really good at that.

> as a local Wolfram Alpha

It’s called Mathematica, son.

Maybe like me, they grew up in a country where mathematics itself is pronounced "mathematica" (which is a huge fraction of languages [0]), leading to avoiding that name for the software.

[0] https://www.indifferentlanguages.com/words/mathematics

Aren't there some things that WA can do that mathematica can't? like step-by-step explanations of how to do an integral
The packages in SymPy that help you do this are:

sympy.physics.vector (https://docs.sympy.org/dev/modules/physics/vector/index.html

and

sympy.physics.mechanics (https://docs.sympy.org/dev/modules/physics/mechanics/index.h...)

You can ultimately simulate and visualize dynamical systems:

https://pydy.readthedocs.io/en/latest/examples/multidof-holo...

I wrote an "anything-regressor" in SymPy, where you provide an equation (like y=mx+b) and a dataset of x's and y's, and it solves for constants to get a line of best fit. Super simple to implement and worked for any equation (quartic, logistic, etc) and any dimension of input variables.

Eventual goal was trying to implement something like XGBoost, but by applying successive regression equations to the residuals instead of successive decision trees. E.g. figure out that a sin wave best fits the initial data, and then a linear best explains the remaining difference.

Worked pretty well on toy data sets, but was much too slow to scale to anything real. Still think the idea has promise for producing human interpretable ML models.

This sounds like symbolic regression (where the space of possible equations is usually searched using genetic algorithm).
Similar situation here. I needed to optimize a critical C++ function that took two 3D line segments and returned a transform matrix that would transform the first line segment into the second. I was able to completely remove the matrix multiplications from the function by using SymPy to pre-compose the scale/rotate/translate matrices and simplify the result, and the benchmarks showed a significant speedup.
I was grappling some very similar problem, and working with numerical results to verify intermediate results wasn't helpful and finally had to turn to maxima to final give analytically derived and simplified expressions.

These symbolic tools can save ones skin on a bad day.