
SymPy - a Python library for symbolic mathematics - ogogmad
https://www.sympy.org/en/index.html
======
ivan_ah
SymPy is the best!

In addition to the already-excellent official tutorial
[https://docs.sympy.org/latest/tutorial/index.html](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...](https://minireference.com/static/tutorials/sympy_tutorial.pdf)

For anyone interested in trying SymPy without installing, there is also the
online shell [https://live.sympy.org/](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](https://news.ycombinator.com/item?id=23158095)

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

~~~
satish-setty
Typo there, it should be colab.research.google.com

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

------
anonymou2
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.

------
Majromax
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))
      print(str(foo))
      # Prints exp(-I*t + x) + exp(I*t + x)
      print(str(foo.simplify()))
      # 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.

~~~
ogogmad
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.

~~~
Majromax
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.

------
mucinoab
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.

------
albertzeyer
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](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.

~~~
ogogmad
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.

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

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

In my case I got a 7x speedup.

~~~
jbay808
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.

~~~
westurner
SymEngine
[https://github.com/symengine/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](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_

------
tikej
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...](https://github.com/sympy/sympy/blob/58e1e9abade7c38c66871fd06bf76ebac8c1df78/sympy/integrals/integrals.py#L153-L370\]))
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.

~~~
bocklund
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).

~~~
tikej
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).

~~~
ChrisRackauckas
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/](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.

------
nishnik
To add more info they have been building a C++ code-base for the most
important features:
[https://github.com/symengine/symengine](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.

~~~
ChrisRackauckas
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/](https://mtk.sciml.ai/dev/tutorials/symbolic_functions/)

------
smabie
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!

~~~
sundarurfriend
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.

~~~
westurner
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

------
dang
If curious see also

2014
[https://news.ycombinator.com/item?id=7214783](https://news.ycombinator.com/item?id=7214783)

2012
[https://news.ycombinator.com/item?id=4463471](https://news.ycombinator.com/item?id=4463471)

------
tomkat0789
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?

~~~
hansvm
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.

------
blondin
wow this is super cool, [https://sympygamma.com](https://sympygamma.com)

------
dqpb
If you like this, you may also like the Lean Theorem Prover:
[https://leanprover.github.io/](https://leanprover.github.io/)

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

------
vagab0nd
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?

~~~
shirakawasuna
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).

------
lpasselin
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?

~~~
cf
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.

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

------
29athrowaway
I use SymPy through Sagemath. It is fantastic.

~~~
ur-whale
As pointed out by other commenters, Sage uses Maxima under the hood.

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

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

[https://doc.sagemath.org/html/en/reference/calculus/sage/cal...](https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/test_sympy.html)

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

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

