15 ms·
Doing Symbolic Math with SymPy
- prof-dr-ir 6y agoSymPy is fantastic and impressive and I am a fan. It however does few things better than the program it is directly trying to compete with, namely Mathematica. This gives me scary flashbacks to the early 'Linux on the desktop' advocates, whose main argument was that it was all 'free' in every sense of the word. As we know now, that simply was not enough to switch the majority of users. Do people see a space where SymPy can realistically flourish?
- kavalg 6y agoMaybe as a basis for something like an open source Photomath[1]. I noticed that kids use it a lot in my community. And it is not only to cheat at homeworks :) [1] https://photomath.app/en/ https://photomath.app/en/
- xvilka 6y agoMaxima plus Sage Math wrappers works more appropriately, I think. Though I would prefer a faster language than Python for such systems. Something like Sage but with Julia.
- fdej 6y agoNemo (http://nemocas.org/ http://nemocas.org/) and Oscar (https://oscar.computeralgebra.de/ https://oscar.computeralgebra.de/) are such projects. However, they are focused on algebra and have no support for expression manipulation, symbolic integration and the like.
- ChrisRackauckas 6y agoFor symbolic manipulation and all of that, ModelingToolkit.jl (https://mtk.sciml.ai/dev/ https://mtk.sciml.ai/dev/) is the project, and it's getting SymEngine-like speeds in about a day or so. There's still more to do, but where this shines is the connection to numerical computing, i.e. partially solving models and then spitting out optimized parallelized code for the ODE solver for the other half of the model.
- Qem 6y agoAs SymPy is pure Python, if you need speed, you can run it under Pypy just-in-time compiled implementation of Python.
- fdej 6y agoI think SymPy already flourishes in its space: it's a great tool for users who need some computer algebra functionality with the ability to glue that together with other code (thanks to Python). It's fine as long as you don't need high efficiency or specialized functionality. Mathematica has no serious competition as a general-purpose symbolic system. I guess Maple is the closest runner-up, but it's more narrowly focused on computer algebra. Most people only need a fraction of Mathematica to get their work done, though.
- prionassembly 6y agoIt runs on a google colab tab that you can spin up pressing "ctrl-T" in any available computer.
- svantana 6y agoSure, but if you want fast easy math, it's hard to beat wolfram alpha, where you can just enter something like "solve x^2+5a=y for x" and get the answer right away.
- prionassembly 6y agoIt's really not. WA is good at getting natural language questions but gives you next to no control on the outputs. Last time I used sympy I had a complicated function on many variables and wanted to plot certain specific 2D contours. I was distant from any computer with numerical/programming tools and needed to add them to a PPT deck last minute.
- Enginerrrd 6y agoI use it in Jupyter notebooks for civil and structural engineering calcs. The reason is simple: I can print expressions and manipulate the expression with a guarantee that my calcs conform to the pretty printed ones. This allows non-programmers (senior engineers) to check the work on the engineering side with some additional assurances that the calculation is going to be correct compared to just inputting the expression in python seperately from the pretty printing process.
- rwlincoln 6y agoAndes, a Python package for power system simulation, uses SymPy now. In previous versions, computing the derivatives for dynamic models was done using complex equations written out manually. SymPy greatly simplifies the process of adding custom device models, which power companies often wish to do. https://andes.readthedocs.io/ https://andes.readthedocs.io/
- abdullahkhalids 6y agoSymPy's problem is that it is based on an object-oriented language. This results in SymPy (and friends) demanding that users abandon hundreds of years of math notation, to conform to the whims of a programming language. In SymPy you do M.diagonalize(), which makes no sense. The matrix M does not have a property diagonalize. Rather, you should apply a choosen diagonalization algorithm like Diagonalize(M) to produce a new matrix. Mathematica wins, and will continue to win, because the language is functional and conforms to how Mathematicians think, rather than how (EDIT: certain) programmers like to code.
- vanderZwan 6y ago> Mathematica wins, and will continue to win, because the language is functional and conforms to how Mathematicians think, rather than how programmers like to code. Or maybe Python "will win" because it conforms to how programmers think, rather than forcing them to think like mathematicians (also, why capitalize "mathematicians" in your sentence?), letting them get their shit done. More importantly, "win" what exactly? It makes no sense to talk about "winning" if you don't even properly define a victory condition and context first.
- joshuaissac 6y agoIt depends on who the intended users of SymPy are. If they are programmers, then conforming to how programmers would think would be useful. If it is targeted at mathematicians, then confirming to how programmers think would just be another unnecessary hurdle to using the library, and would hurt adoption (which is what "win" means here).
- Koshkin 6y ago> conforms to how programmers think Python is an important tool in general, it allows to automate many tasks that otherwise would have to be done manually. I wish more non-programmers knew how to use it.
- billfruit 6y agoMay be the way mathematicians notate is frankly ambiguous and confusing, and recent decades of programming experience have given good insight into more expressive methods of notation.
- stochastician 6y agoI want to encourage people to think of sympy not just as a competitor to Mathematica but additionally as an incredibly valuable library that can be used _inside_ of other projects. Sometimes, you just want to compute an antiderivative, or you want user-supplied functions that you can manipulate easily, or you want to do some actual algebra. Think of it less as a Mathematica replacement (like "Linux on the Desktop") and more as a crucial library enabling a lot of fun new creative things (like "embedded linux running on your toaster"). For example, in some of my computational chemistry work we use it to allow users to specify certain functionals, which we can then manipulate symbolically, do expression reduction and elimination on, and prove certain properties about. It's great!
- adenozine 6y agoThis may be a stretch, but can you point to any examples or articles about using SymPy outside of a strictly mathematical context?
- animal_spirits 6y agoI would bet it can be useful in physics engines
- mehrdadn 6y agoThe trouble with SymPy is it's, well, buggy. I tried it a few years ago, and as soon as I got serious, I quite quickly ran into problems that I reported, some of which I now see they apparently still haven't gotten around to addressing. [1] [2] Symbolic math is hard; they have my sympathies. I don't think I could do better. But as long as bugs like these exist, it's going to be hard to convince people to switch away from better tools like Mathematica. [1] https://github.com/sympy/sympy/issues/12561 https://github.com/sympy/sympy/issues/12561 [2] https://github.com/sympy/sympy/issues/12562 https://github.com/sympy/sympy/issues/12562
- BeetleB 6y agoYikes those are bad. As a non-user interested in it, I must confess that seeing those issues (especially the 2nd one) makes me not want to use it! I do get that it is a hard problem, but I would then recommend they not have an option for "real" if they can't get it right. Users will expect it to work (unless the docs point out it is unreliable).
- mehrdadn 6y agoYeah. For what it's worth (not sure if it makes you feel better or worse...) I've found similarly bad bugs in SciPy too.
- BeetleB 6y agoCurious what those were.
- mehrdadn 6y agoHere's the one I can find (might not have reported/written others so I don't recall them): https://github.com/scipy/scipy/issues/7332 https://github.com/scipy/scipy/issues/7332 I can't tell if I'm just exceptionally good at making bugs explode in my face or something, but these are so simple that it boggles my mind when I'm the first person to report them...
- thatcherc 6y agoWorth noting that Julia's SymPy binding [1] is pretty pretty nice to work with too. If anyone's looking for big Julia project, I think a symbolic math package written fully in Julia would be a really exciting development. As far as I know, there isn't one yet. The better-known symbolic math packages for Julia still use bindings to C++ (SymEngine.jl [2]) or Python (SymPy.jl, Symata.jl [3]). [1] - https://github.com/JuliaPy/SymPy.jl https://github.com/JuliaPy/SymPy.jl [2] - https://github.com/symengine/SymEngine.jl https://github.com/symengine/SymEngine.jl [3] - https://github.com/jlapeyre/Symata.jl https://github.com/jlapeyre/Symata.jl
- ChrisRackauckas 6y agoModelingToolkit.jl is a symbolic math package written fully in Julia (with a bunch of extra symbolic-numerics features) https://github.com/SciML/ModelingToolkit.jl https://github.com/SciML/ModelingToolkit.jl https://mtk.sciml.ai/dev/tutorials/symbolic_functions/ https://mtk.sciml.ai/dev/tutorials/symbolic_functions/ It's more like SymEngine in terms of completeness right now, though there's a good amount of simplification and equation solving built in. It's still growing, it's not at SymPy yet, but it's moving fast.
- snicker7 6y agoWhat is great about ModelingToolkit.jl is how its used in practical ways for other packages. E.g. NeuralPDE.jl. Compared to SymPy, I feel that it is less of a "how do I integrate this function" package and more about "how can I build this DSL" framework. https://github.com/SciML/NeuralPDE.jl https://github.com/SciML/NeuralPDE.jl
- ivan_ah 6y ago+100 this; SymPy is the best! One of the coolest things about SymPy is the Live Shell, that let's you try basic calculations (without plotting) in the browser: https://live.sympy.org https://live.sympy.org There is even an option to send the entire session encoded as URL querystring which makes it tweetable, emailable, etc. e.g. https://live.sympy.org/?evaluate=factor(x**2%2B5*x%2B6)%0A%23--%0A https://live.sympy.org/?evaluate=factor(x**2%2B5*x%2B6)%0A%2... (to generate the session-as-querystring for your current live.sympy.org session, use the thumbtack button below the prompt) I use this very often to send calculations and solutions—not only do you get the answer, but you see all the calculations.
- ivan_ah 6y agoHere is a short tutorial of the SymPy APIs that are relevant for high school math (equations, algebra, functions, etc.) and first-year university (mechanics/calculus/linear algebra): https://minireference.com/static/tutorials/sympy_tutorial.pdf https://minireference.com/static/tutorials/sympy_tutorial.pd...
- jbarrs 6y agoI'd like to give an alternative perspective on SymPy. Whilst I'm not familiar with Mathematica, and therefore cannot draw parallels between the two, I've used SymPy for a number of years and found it to be really useful. I was initially introduced to it as part of a first year programming module in a Physics degree, and it was more than sufficient for that use-case; for that use-case, it absolutely wasn't worth paying for something like Mathematica. When combined with a graphical REPL (some may use Spyder, I personally use jupyter-qtconsole) or a Jupyter Notebook, it becomes really quite useful as it incorporates LaTeX rendering to make its outputs more readable and a little less daunting. Whilst the syntax may not be optimal for the mathematical way of working, for many problems like simple rearrangements, automatic simplification, checking equivalences, etc. it is almost always the first tool I reach for. The syntax is just cosmetic, and I use Python enough that the syntax doesn't even bother me, either. It does its job well, and it's helped me solve and verify solutions for a number of mathematical problems during my degree. Admittedly, I do run into the occasional problem here and there where there are obviously better tools available, but that's the same with any gratis vs paid solution. In most cases, SymPy does a sufficiently good job.
- spuz 6y agoThe article mentions qtconsole interface. Does anyone know how to install and run this interface? I could not find such information either on the linked page or the SymPy docs.
- happy-go-lucky 6y agohttps://github.com/jupyter/qtconsole https://github.com/jupyter/qtconsole > The Qtconsole is a very lightweight application that largely feels like a terminal, but provides a number of enhancements only possible in a GUI, such as inline figures, proper multiline editing with syntax highlighting, graphical calltips, and more.
- leephillips 6y agoMy first mention of qtconsole in the article is a link to an official page that has detailed installation information.
- dang 6y agoIf curious see also a few weeks ago https://news.ycombinator.com/item?id=25254648 https://news.ycombinator.com/item?id=25254648 2020 https://news.ycombinator.com/item?id=23747133 https://news.ycombinator.com/item?id=23747133 2019 https://news.ycombinator.com/item?id=20287486 https://news.ycombinator.com/item?id=20287486 Lots in 2014: https://news.ycombinator.com/item?id=8243452 https://news.ycombinator.com/item?id=8243452 https://news.ycombinator.com/item?id=8109265 https://news.ycombinator.com/item?id=8109265 https://news.ycombinator.com/item?id=7214783 https://news.ycombinator.com/item?id=7214783 https://news.ycombinator.com/item?id=7145219 https://news.ycombinator.com/item?id=7145219 2012 https://news.ycombinator.com/item?id=4463471 https://news.ycombinator.com/item?id=4463471 2011 https://news.ycombinator.com/item?id=3141699 https://news.ycombinator.com/item?id=3141699
- jupdarter 6y agoI’ve had similar problems as others posting here have. To be honest when I’ve had to do symbolic math at work (usually some combination of computational geometry and PDEs), I typically fall back to maxima or Mathematica (if my collaborators also have licenses). Those systems are quite solid and very mature when it comes to symbolic work, and I never saw any value in using SymPy since I can get to maxima from Python via Sage. I will admit though: I tend to not do that since I’m usually pretty happy in maxima or Mathematica directly and don’t see a reason to complicate things with a layer of Python. I taught a course a couple years back where I tried to use sympy since we were using a bunch of other Python tools (numpy, scipy, etc...), and the sympy part felt very awkward. I think I remember showing the students maxima and Mathematica to convince them that computers really could do complex symbolic work and not to get too turned off by the Python tools.
- carapace 6y agoI did a fun thing with Sympy for Advent of Code 2017 December 3rd, here's a Jupyter notebook if anyone's interested: https://osdn.net/projects/joypy/scm/hg/Joypy/blobs/tip/docs/Advent_of_Code_2017_December_3rd.ipynb https://osdn.net/projects/joypy/scm/hg/Joypy/blobs/tip/docs/... The puzzle involves a weird "spiral" RAM: > You come across an experimental new kind of memory stored on an infinite two-dimensional grid. > Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. The problem is to compute "the Manhattan Distance between the location of the data and square 1" for any given square. While working on this I came up with the following equation: from sympy import floor, lambdify, solve, symbols k = symbols('k') E = 2 + 8 * k * (k + 1) / 2 Sympy figures out that this reduces to E = 4k(k + 1) + 2 I needed a function to solve for k given some n... Sympy can do that. Take a new symbol `n`, subtract it from the equation `E`, 0 = 4k(k + 1) + 2 - n and solve for `k`. There are two solutions because the equation is quadratic so it has two roots. n = symbols('n') g, f = solve(E - n, k) In the context of the puzzle we only care about the larger root: (sqrt(n - 1) / 2 - 0.5) + 1 For reasons, I need to take the floor and add 1. Then Sympy can lambdify it and create a fast Python function to compute `k`, given `n`: F = lambdify(n, floor(f) + 1) for n in (9, 10, 25, 26, 49, 50): print(n, int(F(n))) 9 1 10 2 25 2 26 3 49 3 50 4 I'm sorry that it's hard to follow, I'm just excited to share how cool it was to let the computer do the symbolic math for me.