Hacker News new | past | comments | ask | show | jobs | submit login
SymPy - a Python library for symbolic mathematics (sympy.org)
209 points by ogogmad 35 days ago | hide | past | favorite | 51 comments

SymPy is the best!

In addition to the already-excellent official tutorial https://docs.sympy.org/latest/tutorial/index.html , I have written a short printable tutorial summary of the functions most useful for students https://minireference.com/static/tutorials/sympy_tutorial.pd...

For anyone interested in trying SymPy without installing, there is also the online shell https://live.sympy.org/ which works great and allows you to send entire computations as a URL, see for example this comment that links to an important linear algebra calculation: https://news.ycombinator.com/item?id=23158095

It's also installed by default in Google's hosted jupyter solution iirc -- colab.google.research.com

Typo there, it should be colab.research.google.com

Oh, thanks for that. Too late for me to edit, but hopefully everyone notices your comment here.

How does it compare to Matlab's symbolic toolbox, aside from not being affordable outside of academia?

NumPy for Matlab users: https://numpy.org/doc/stable/user/numpy-for-matlab-users.htm...

SymPy vs Matlab: https://github.com/sympy/sympy/wiki/SymPy-vs.-Matlab

If you then or later need to do distributed ML, it is advantageous to be working in Python. Dask Distributed, Dask-ML, RAPIDS.ai (CuDF), PyArrow, xeus-cling

It is great, I used it in a web application that I wrote at work that shows and let the user edit a graph of building electric, gas and water meters. A button lets you calculate the relationships between meters. For example one meter may be split into two, the sum of the flows thru the two must equal the flow thru the first. So basically I set up a system of equations based on the graph that the user created , solved it using sympy, then show the user the flows thru some nodes (the unknowns) in terms of others. Very uselful indeed.

I like SymPy as a light-weight CAS. I don't have any heavy symbolic computational needs, but it fills a gap between "simple enough to work out with pen and paper, without error" and "so complicated that it's best to stick to numerical simulation."

However, as a slightly-more-than-beginner user I have a frustrating time getting SymPy to take simplifications that I know are valid. As a result the generated answers are correct but so complicated that the "simple intuitive understanding" goal of the symbolic computation is lost.

This can come down to having implicit assumptions that I haven't told SymPy (such as variables being real-valued, or it being okay to take the principal roots of exp(I <stuff>)), but not always.

As a minimal example:

  import sympy as sp # Import SymPy
  # Define variables; note they're marked as real-valued
  x = sp.Symbol('x', real=True);
  t = sp.Symbol('t', real=True);
  # Get our simple expression: 2*cos(t)*exp(x) but written in complex form
  foo = sp.exp((x+sp.I*t)) + sp.exp((x-sp.I*t))
  # Prints exp(-I*t + x) + exp(I*t + x)
  # Still prints exp(-I*t + x) + exp(I*t + x) -- SymPy thinks this is the simplest form?
  print(str((foo/sp.exp(x)).simplify())) # Help it along
  # Prints 2*cos(t)
Obviously, forcing SymPy to do the desired thing gets worse with more complicated expressions. Sometimes I can bash it into compliance with liberal use of `rewrite` or by demanding it simplify term-by-term, but if I'm deconstructing the expression only to reassemble it at the end then I'm not sure I'm still gaining much out of it as a computer algebra system.

For your particular example, try foo.rewrite(cos). If you work with trigonometric expressions, it often pays to randomly do foo.rewrite(cos) or foo.rewrite(exp) along with simplify().

Your larger point is true though, at least from my experience.

Alas, sp.rewrite doesn't quite do what I want for the slightly more complicated (with new symbol 'k' suitably defined):

  bar = sp.exp(sp.I*(x + k - t)) - sp.exp(sp.I*(x+k+t))
The desired output would be 2 cos(t) exp(I(x+k)), but rewrite and simplify works on an all-or-nothing basis, affecting exp(I(x+k)) as well.

My mental framework is to come at this from something like a signal engineering perspective, looking at amplitude-modulated single frequencies. However, expressing that intent in a way that SymPy understands is for now beyond me.

My problem may ultimately be one of documentation. The basic SymPy tutorials are quite nice, and the raw documentation for the functions... exists, but I have yet to find good examples of intermediate to advanced use discussing the "what and why" of accomplishing nontrivial goals.

SymPy is one of the bests, if not the best Python module I have ever used. Everything is well documented and includes many examples, there even is an interactive shell in their page so you can test and tweak everything they show.

SymPy is great!

I have used it to assist me in some formal proofs. (I hope to publish that later.)

Note that in my case, I hit a couple of bugs. See the GitHub issue tracker (https://github.com/sympy/sympy/issues). The bugs are easy to detect when you just get an exception (which was often the case for me). However, you can also simply get an incorrect result, which can be more tricky to detect. So be careful and maybe don't trust the results right away without double checking them in some other way.

It sometimes provides an incomplete result when solving non-linear equations. For instance, doing

   solve(Eq(w*x + y*z,0), [w,x,y,z], dict=True)
returns the incorrect result

   ⎡⎧   -y⋅z ⎫⎤
   ⎢⎨w: ─────⎬⎥
   ⎣⎩     x  ⎭⎦
which is invalid when x is zero. There are more compelling examples, but I don't know any off the top of my head. Interestingly, Wolfram Alpha seems to be better at returning the whole solution, so I use it in combination with Sympy.

You should definitely raise an issue if you got the bandwidth. The project has generally been responsive on this sort of stuff.

Good to know: sympy does not use numpy so you can use pypy instead of python.

In my case I got a 7x speedup.

You can however use sympy to convert symbolic expressions to numeric numpy functions, using the "lambdify" feature. It's awesome because it lets you symbolically generate and manipulate a numerical program.

SymEngine https://github.com/symengine/symengine

> SymEngine is a standalone fast C++ symbolic manipulation library. Optional thin wrappers allow usage of the library from other languages, e.g.:

> [...] Python wrappers allow easy usage from Python and integration with SymPy and Sage (the symengine.py repository)

https://en.wikipedia.org/wiki/SymPy > Related Projects:

> SymEngine: a rewriting of SymPy's core in C++, in order to increase its performance. Work is currently in progress to make SymEngine the underlying engine of Sage too

One of the difficulties we ran into here though was that "lambdify" was built for simplicity and not for speed. You can see this in how it doesn't support things like lamdification on sparse matrix types. A lot of our applications started with SymPy/SymEngine under the hood, but when push came to shove we had to write a pre Julia symbolic engine (ModelingToolkit) so we could more easily generate fast multithreaded sparse non-allocating functions from symbolic equations (https://mtk.sciml.ai/dev/tutorials/symbolic_functions/#Build...). SymPy is great though, it's just if you try using it on a million equations, oof haha.

Nice. Over at #pypy on IRC we would love to hear about the use case since we rarely get to showcase positive results

SymPy is great!

On one hand it's great that it was written in Python and fairly extensible thanks to that, but on the other it's a shame, since it probably will never be fast enough for high performance symbolic computing.

That said I think CASes (Computer Algebra Systems) are heavily under-utilised nowadays. Schools and universities should use is as widely as possible i.e. it should be even more widely used than calculator is/once was. I believe that for every two hours of regular pen-and-paper exercises there should be at least one hour of CAS assisted computer exercises. It's pretty radical and sadly there aren't enough teachers (even in good universities/colleges), that know CASes enough to teach that, so it isn't gonna happen any time soon. Maybe I'm biassed here (I hated doing repeated exercises, such as algebraic simplifications, fraction simplifications etc. in high school), because I use CASes as crutches for doing tough math, but I believe sciences would benefit if more papers in math and physics was written as a code (less mistakes, plus it's not a mistake – it's a bug!). Hopefully it will happen someday and we will have "open source derivations" of formulas, theories, theorems etc. :)

That said, the thing about CASes I'm missing most is more hand guided, step-by-step and mathematical transformations assistant approach. Recently they added a bit of this kind of functionality to Mathematica 12 (sadly I use it most of the time, despite it being closed source) – functions like AddSides, MutiplySides, ApplySides – but I think it's still too automatic for what I have in mind. Hopefully when I finish my PhD I will have some time to work on the kind of "assistant CAS" I have in mind using projects like SymPy, Maxima and Reduce for some parts. What I have in mind is to make hand-like derivations using CAS and let it do small things, like I want to substitute log(a) as x in integral, so find dx for this substitution (actually SymPy has this functionality, even though slightly limited at the moment! [https://github.com/sympy/sympy/blob/58e1e9abade7c38c66871fd0...) and do more things this way (series, equations, inequalities, derivatives, limits, operators etc).

Yeah, it's probably more suited for series of blog posts than HN comment but if someone knows FOSS software that is focused on this kind of approach I'll be thankful for links to articles code etc.

I agree, although anecdotally, there seems to be an increasing amount of courses teaching Sage. Sometimes free books/lecture notes are published here: https://www.sagemath.org/library-publications.html#books

Sage is a lot larger than Sympy (it's more than 1 million LOC AFAIK), but it still delegates to other libraries to do most types of underlying symbolic and numerical computations.

I usually use sympy (or rather some wrapper of symengine these days) when I'm writing a pure python script, sage otherwise.

For a lot of math and CS courses, I think having some knowledge of some sort of computer-assisted theorem proving. I think the most promising is maybe Lean these days (Coq still might take too long to learn): https://github.com/leanprover-community/mathlib

Even though SymPy is written in Python, there are several options to go much faster. The simplest is when actually using the symbolic expression to do some calculation. For that SymPy has a `lambdify`, which can make a callable using NumPy as the backend. They also have a variety of code printers to write your expression in C/Fortran/Julia/<many others> that you can compile to be very fast.

There’s also the SymEngine project, which is a rewrite of SymPy in C++ with bindings to other languages (like Python where it is a drop-in API replacement for SymPy). It’s not finished yet, but the CAS is a lot faster and it’s lambdify can give you an internal representation or an LLVM-compiled version, both of which are much faster than SymPy in compile-time and run-time for our expressions (which are often quite large, but simple in terms of the mathematical functions used).

Sure, but that's numerics. What I meant by high performance symbolic computing was things integrating hundred term expression or expanding, manipulating and simplifying expressions of similar lengths or solving (diagonalising) 9x9 matrix with symbolic expressions. Unfortunately in these applications SymPy is (painfully) slow, although I haven't tried using PyPy backend for this.

Yeah, SymEngine is a great idea and it is a lot faster, but its functionality is very limited compared to SymPy. The development is slower than SymPy and I suppose its due to the fact that its in C++, which is more difficult to write after all. Therefore I don't believe it will ever catch up with SymPy features and it boils down to the two-language problem. I think Julia (with multiple dispatch, parametric type system and JIT) is a great candidate for CAS, but it's still very far behind SymPy (although the nice thing is that functionalities can be "borrowed" via PyCall until they are rewritten).

Julia's ModelingToolkit.jl is already pretty far along and has a lot of SymPy, plus extra features like parallel simplification and generation of parallel sparse functions via its form of lambify. See the first tutorial on that: https://mtk.sciml.ai/dev/tutorials/symbolic_functions/ . The equation solver is in progress right now and is moving along quite fast. The nice thing is that this will all use Julia's multithreading, since separate tree cases are mostly independent so there's a lot of parallelism that the symbolic pieces can use here.

When I was getting my math degree at a large public state university, a lab class in Mathematica was a requirement for freshmen/sophomore math majors. I had a free student license for my laptop and it was available on every lab computer. I used it extensively during every math class I took during my degree even though I never had another “coding exercise” in any those classes.

I’m damn sure not calculating permutation groups by hand.

To add more info they have been building a C++ code-base for the most important features: https://github.com/symengine/symengine SymEngine can be used from C++ or from the wrappers in Python, Julia, Haskell - and gives a performance boost over vanilla SymPy.

With DifferentialEquations.jl and the SciML ecosystem we used SymEngine for a long time and it was great, but we found we could have much better performance with a pure Julia ecosystem, and so we built out the ModelingToolkit ecosystem. It's been turning out great. One of the nice things is that rule simplification uses Julia's task-parallelism, so you get a lot of parallelism for free. It's really focused on symbolic derivation of high performance numerical code, like in https://mtk.sciml.ai/dev/tutorials/symbolic_functions/

How does it compare to sage math? I used sage math today and was pleasantly surprised. Even has a latex option. If you name your variables correctly, it even outputs Greek symbols and subscript correctly!

As one point of comparison, SymPy is comically slow compared to Sage. This is mostly because SymPy is purely Python; Sage on the other hand uses its own derivative of GiNaC [1], Pynac [2], for its internal symbolic expression representation, and then multiple external libraries for non-trivial operations. Symbolic transformations are mostly Maxima [3], for example. Sage literally converts expressions to strings, pipes them through a Maxima process, and then parses the result back. This is still much faster than the pure Python SymPy.

There is an effort to speed up SymPy core, SymEngine [4], but it's been in development for years now, and still isn't integrated into SymPy. Not sure why.

Case in point: 'expand("(2 + 3 * x + 4 * x * y)^60")' takes 5 seconds with SymPy; Sage (Pynac) does the same in 0.02 seconds.

[1] https://www.ginac.de/

[2] http://pynac.org/

[3] http://maxima.sourceforge.net/

[4] https://github.com/symengine/symengine

As per the Sage FAQ[0], Sage uses Maxima for its symbolic and numerical computation: http://maxima.sourceforge.net/

[0] https://doc.sagemath.org/html/en/faq/faq-general.html#why-di...

I have Sympy set up to do latex printing in my Jupyter notebooks with Greek symbols and subscripts etc.

User 29athrowaway mentions in another comment that they use Sympy through Sagemath, so they probably could answer your question best. It sounds like Sympy is a subset of what Sage offers, but I'm not familiar enough with either tool to know.

There's a lot of overlap and there are syntactical differences. SymPy is included in the CoCalc image. SageMath is now conda-installable.

Things that the SageMath CAS can do that SymPy cannot yet:

- solve multivariate systems of inequalities

Try sympy.latex(expr). You can output latex using Sympy too!

I've never used sympy, but perhaps I should! Is this a tool meant to save people pages of math derivations, sort of like Maple? Is that how it's generally used?

That's how I typically use it (though it has a whole lot more features). Using it in jupyter gives you a notebook environment too, which I really enjoy as I flesh out a problem.

Yes, Sympy is supposed to be an alternative for MAple, Mathematica, sage etc.

You can use it to do so (as an interactive tool).

But it can also be useful as a library, when you develop sth where some symbolic derivation can be useful.

wow this is super cool, https://sympygamma.com

If you like this, you may also like the Lean Theorem Prover: https://leanprover.github.io/

A fun place to start is the Natural Number Game: https://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_gam...

I recently started a trial version of Mathematica and likes it so far. SymPy seems like a good free alternative. How does it compare to Mathematica?

It eliminated any need for me to use Mathematica. It doesn't have quite the depth of Mathematica when it comes to symbolic math, but I never needed that depth anyways.

It's also much more targeted - it's only comparable to the symbolic math portion of Mathematica. To get a similar experience, you should use sympy in a Jupyter notebook environment, use numerical libraries (numpy, scipy, scikit-learn), and learn some plotting libraries (matplotlib, bokeh, or my favorite, plotnine).

Last time I tried sympy was to simplify a function including dot products. In 24h it was not able to converge.

Anyone has tips to reduce time required to simplify?

The main simplify function is full of heuristics and is itself a basket of more specialised simplify functions. One thing to try one or several of these specialised functions.

Another tip I don't think gets mentioned enough, try to provide domain information for your symbolic variables. By default all these systems assume the most general solution. But if you know x is a positive real value, you should tell that to SymPy.

During my stint as a math tutor I used sympy to automate producing worksheets in LaTeX that involved algebra and calculus - great library.

I use SymPy through Sagemath. It is fantastic.

As pointed out by other commenters, Sage uses Maxima under the hood.

Please describe how you managed to get Sage to use sympy, thanks.

In this guide, note the usage of the _sympy_() and _sage_() methods. These help you converting between Sage and SymPy.


Sympy carried me through a few courses during CS degree. Sometimes it takes a bit of practice, but it was a great help.

Used it a lot in school. Saved me lots of time.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact