News for hackers. Stuff that matters.
Re:SymPy: Symbolic Mathematics in Python by abricq on Wednesday February 28, 2024 @09:19AM (#39538262)
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, immediately importable in my code case.

To illustrate what I mean by "expressing systems of equations in a declarative way", here is a toy-example of how to estimate the position of the a sensor with respect to a robot's center if you have access to a dataset containing robot positions and the sensor positions. The 'naive' approach (it very often works) is to solve an over-constrained system with a gradient-descent. To perform a gradient descent, you need a residual function and its jacobian. Here's how you would do to compute it with Sympy. (Note: you'd just have to define the `transform` and `invert` functions...)

    # Pose of a sensor in robot frame (to be estimated)
    xa, ya = symbols("xa, ya")
    a = Matrix([xa, ya, ta])

    # Position of the robot at time t
    rx, ry, rt = symbols("rx, ry, rt")
    rk = Matrix([rx, ry, rt])
    
    # Measure of the sensor at time t
    gx, gy = symbols("gx, gy")
    gk = Matrix([gx, gy, 0])
    
    # Estimated x (from the measures)
    estimated_a = transform(invert(rk), gk)
     
    # compute the norm of the gk, squared
    n2_mat = norm2(estimated_a - a)
    n2 = sympy.collect(sympy.expand(n2_mat[0, 0]), a).simplify()
    
    # Compute the jacobian
    J = n2_mat.jacobian([xa, ya, ta])
    
    # Print what is necessary for Guass-Markov regression
    print("\n\nres =", n2)
    print("\nJacobian = ", J)
Re:SymPy: Symbolic Mathematics in Python by exDM69 on Wednesday February 28, 2024 @10:33AM (#39539248)
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 and the symbolic code is thrown away.

Re:SymPy: Symbolic Mathematics in Python by analog31 on Wednesday February 28, 2024 @10:55AM (#39539583)
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 by abricq on Wednesday February 28, 2024 @10:59AM (#39539643)
I commit the symbolic code on its own file, in the same commit which adds the c code.
Re:SymPy: Symbolic Mathematics in Python by lucioperca on Wednesday February 28, 2024 @11:33AM (#39540274)
I guess a hypermodern solution would be to produce the parts of the C code with CI/CD from the SymPy Code.
Re:SymPy: Symbolic Mathematics in Python by mattivc on Wednesday February 28, 2024 @09:31AM (#39538410)
You might find this library interesting: https://github.com/symforce-org/symforce
Re:SymPy: Symbolic Mathematics in Python by tonyarkles on Wednesday February 28, 2024 @10:46PM (#39546209)
Was just going to suggest that! I’ve only recently started using it but so far it has been excellent. One tip… the code generator seems to do a lot of cleaning and simplification. Just today I had some ugly equations that took up many pages of Jupyter output but the generated C++ code was only about 150 lines and didn’t look particularly inefficient. It seemed to me on inspection that it found repeated product and sum terms and properly computed them once and stored them in scratch variables.
Re:SymPy: Symbolic Mathematics in Python by eutectic on Wednesday February 28, 2024 @10:57AM (#39539615)
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 by tehsauce on Wednesday February 28, 2024 @10:50AM (#39539525)
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 by abricq on Wednesday February 28, 2024 @10:52AM (#39539557)
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 by fho on Wednesday February 28, 2024 @12:16PM (#39540909)
Funfact: you can probably JIT compile that using JAX for an easy performance gain.
Re:SymPy: Symbolic Mathematics in Python by 6gvONxR4sf7o on Wednesday February 28, 2024 @12:32PM (#39541125)
But then they would have to compile the jit-optimized XLA to C. Do you know if that’s straightforwardly doable?
Re:SymPy: Symbolic Mathematics in Python by fho on Wednesday March 06, 2024 @03:55PM (#39621302)
Hmm ... Good question. I would assume that JAX and XLA is based on LLVM (like everything else these days) and that you could probably grab the IR at some point to compile it to your target.
Re:SymPy: Symbolic Mathematics in Python by nephanth on Wednesday February 28, 2024 @11:05PM (#39546300)
Numba would probably do the job too
Re:SymPy: Symbolic Mathematics in Python by whyever on Wednesday February 28, 2024 @09:32AM (#39538433)
Why are you calculating the Jacobian symbolically? AFAIK, for complex cases, the numerical Jacobian is often faster / more numerically stable.
Re:SymPy: Symbolic Mathematics in Python by abricq on Wednesday February 28, 2024 @09:40AM (#39538534)
Well, the symbolic computation is only used to generate C code. The generated code then evaluates jacobians numerically. And we use several methods for numerical stability on top of that.
Re:SymPy: Symbolic Mathematics in Python by eutectic on Wednesday February 28, 2024 @11:01AM (#39539684)
I though finite difference differentiation was notoriously unstable.
Re:SymPy: Symbolic Mathematics in Python by legobmw99 on Wednesday February 28, 2024 @11:10AM (#39539846)
Perhaps the commenter means something like reverse mode automatic differentiation?

Finite differences does indeed have stability issues, and even if you apply some tricks will only give you about half float precision

Re:SymPy: Symbolic Mathematics in Python by funks_ on Wednesday February 28, 2024 @10:53AM (#39539568)
Especially if you actually require vector-Jacobian or Jacobian-vector products instead of the full Jacobian.
Re:SymPy: Symbolic Mathematics in Python by mitthrowaway2 on Wednesday February 28, 2024 @12:46PM (#39541319)
The poster is talking about using symbolic math to obtain a closed-form expression for the Jacobian that is then numerically evaluated. This will often be faster and more accurate. For example, if your function is sin(x), then the symbolic math tells you your derivative is cos(x), so you put that expression in your code and compile it. When calling this on the angle x = 0.123, your code then just evaluates numerically cosf(0.123), rather than (sinf(0.124) - sinf(0.122)).
Re:SymPy: Symbolic Mathematics in Python by toolslive on Wednesday February 28, 2024 @12:46PM (#39541321)
There are plenty of situations where the scenario goes like this: you first do symbolical calculations. then fill in some of the free variables, your resulting expression will simplify a lot. Then you compile that resulting expression to native code and evaluate the simplified specialized code for a zillion parameter vectors.

Anyway,if your day job needs something like this you're better off using a lisp than python.

Re:SymPy: Symbolic Mathematics in Python by thebiss on Friday March 01, 2024 @12:03PM (#39563828)
For those of us that have never touched lisp - can you explain why?
Re:SymPy: Symbolic Mathematics in Python by toolslive on Wednesday March 06, 2024 @05:04PM (#39622272)
Lisp has quote, unquote, and friends allowing you to turn code into data, then manipulate it and then turn it back into code. These are all the tools you need to create on-the-fly compilers. Here's an article explaining the lost art:

http://gms.freeshell.org/peval.html

Re:SymPy: Symbolic Mathematics in Python by sampo on Wednesday February 28, 2024 @02:38PM (#39542577)
> 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, immediately importable in my code case.

You could maybe calculate the Jacobian in C code using automatic differentiation. Might be less C-code, might be less elementary operations done in the C code, and you would not need to be copy-pasting complex symbolically derived formulas from Python to C.

https://en.wikipedia.org/wiki/Automatic_differentiation

Re:SymPy: Symbolic Mathematics in Python by whatshisface on Wednesday February 28, 2024 @02:53PM (#39542697)
I could be wrong, but I seriously doubt C macros can do this.
Re:SymPy: Symbolic Mathematics in Python by sampo on Wednesday February 28, 2024 @03:11PM (#39542888)
Why are you thinking of macros?
Re:SymPy: Symbolic Mathematics in Python by whatshisface on Wednesday February 28, 2024 @04:03PM (#39543383)
In an embedded environment you would want it to be pre-compiled and optimized.
Re:SymPy: Symbolic Mathematics in Python by dimatura on Wednesday February 28, 2024 @04:07PM (#39543438)
Yes, back in the day I saw people doing this with Maple, glad there's open source options now. There's a cool library (also using Sympy) to do this kind of thing in robotics and computer vision applications, symforce https://github.com/symforce-org/symforce
Re:SymPy: Symbolic Mathematics in Python by belzebalex on Wednesday February 28, 2024 @06:31PM (#39544861)
I tried to use SymPy for a similar problem: computing the Jacobian of a complicated integral using quaternion rotations.

Problem was that the symbolic results were way too complicated and the simplify function didn't help much. So, I got back to manual differentiation.

Re:SymPy: Symbolic Mathematics in Python by luxcem on Thursday February 29, 2024 @11:28AM (#39551511)
I'm not familiar with this field but is this similar to an Extended Kalman Filter?