Live data from Hacker News

SymPy: Symbolic Mathematics in Python

sympy.org

11–20 of 137 posts

Re: SymPy: Symbolic Mathematics in Python

#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, 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

#12
post #7

For a numerical "physicist" (yes, the quotation marks are indispensable), Sympy was somewhat of a godsend to me. Great for prototyping even more advanced models before optimising them later on in C++. I haven't used Mathematica much, but I have a feeling that it's still more symbolically powerful (or requires less wrangling) than SymPy? I'd appreciate if somebody with more experience in Mathematica could lay it out f…

Stephen Wolfram notwithstanding, Mathematica is still far ahead of most alternatives as a CAS. Maple is good on some integrals. Their downsides are that their languages are not very well suited as general purpose languages, many times the algebraic manipulations you have to perform aren't that complicated and you'd rather work in a "real" language. Yet another case where python isn't the best in class but still worka…

"Python isn't the best tool for anything, but it's the second best tool for most things."

Re: SymPy: Symbolic Mathematics in Python

#13
post #4

Sympy is amazing, but i kinda fail to see whats news about it as its oldest commit is 14 years ago.

python3 didn't exist 14 years ago, and neither did the ipython notebook, and sympy has code specific to both. /usr/share/doc/python3-sympy/changelog.Debian.gz on my laptop shows six new versions in 02022, three new versions in 02021, and eleven new versions in 02020. is it possible you're looking at an outdated mirror of sympy?

oh, i see you said 'oldest commit'. well, what's news about sympy is the 14 years of commits since then

https://news.ycombinator.com/newsguidelines.html says

> On-Topic: Anything that good hackers would find interesting. That includes more than hacking and startups. If you had to reduce it to a sentence, the answer might be: anything that gratifies one's intellectual curiosity.

and, though it's not even stated, it includes more than news

Re: SymPy: Symbolic Mathematics in Python

#17

Is there any GUI for SymPy one could easily use to solve symbolic math like Maple/Maxima/Macsyma?

You can run it in Jupyter notebook. They have a lightweight implementation of that at https://live.sympy.org/ but the closest counterpart to Maple would probably be SageMath, which includes sympy and a lot more. https://www.sagemath.org/

Re: SymPy: Symbolic Mathematics in Python

#20
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…

You might find this library interesting: https://github.com/symforce-org/symforce
Post reply on HN