[Tutor] Solving physics problems with python

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Apr 14 12:54:34 EDT 2020


On Mon, 13 Apr 2020 at 01:53, Alan Gauld via Tutor <tutor at python.org> wrote:
>
> On 12/04/2020 20:36, Oscar Benjamin wrote:
>
> > Another library you might find interesting is sympy for doing symbolic
> > calculations:
>
> Hi Oscar,
> isn't sympy part of scipy? I thought it was but I might be mistaken.
>
> It's certainly an impressive package, one of the few scipy modules
> I've played with. (A friend was looking for a cheap mathematica
> replacement and I looked at sympy as an option for him.)

Hi Alan,

SymPy is not part of scipy but both are part of the broader scientific
python ecosystem. Your suggestion to install Anaconda would result in
all of numpy, scipy, matplotlib, sympy and many more being installed
together.

Scipy is a library built on top of numpy combining Python, C and
Fortran code for doing numerical work predominantly in floating point.
SymPy is a pure Python library that has only mpmath (also pure Python)
as a hard dependency although gmpy2 is recommended for speed.

The two libraries overlap in the sense that many functions you can
find in numpy/scipy you can also find in sympy but sympy is symbolic
and can work with multiprecision whereas the other are purely numeric
libraries mainly for fixed precision floating point calculations.

SymPy can also be used together with numpy/scipy for example:

>>> from sympy import Symbol, lambdify
>>> x = Symbol('x')
>>> expr = 1/(1 - x)
>>> expr
1/(1 - x)
>>> expr2 = expr.diff(x)
>>> expr2
(1 - x)**(-2)

So we've used sympy to perform some algebraic manipulation which gives
us an expression we are interested in. Now we want to evaluate this
for a large number of floating point inputs but that's faster with
numpy:

>>> eval_expr2 = lambdify(x, expr2, 'numpy')
>>> from numpy import arange
>>> xvals = arange(0, 1, 0.1)
>>> eval_expr2(xvals)
array([  1.        ,   1.2345679 ,   1.5625    ,   2.04081633,
         2.77777778,   4.        ,   6.25      ,  11.11111111,
        25.        , 100.        ])

The lamdify function can be used to bridge the gap between sympy and
other numeric libraries.

Sympy does not automatically use numpy/scipy under the hood even
though they are faster because (apart from them not being pure python)
sympy uses exact calculations or multiprecision mpmath so e.g.:

>>> from sympy import Rational
>>> expr2.subs(x, Rational(1, 10))
100/81
>>> expr2.subs(x, Rational(1, 10)).n()
1.23456790123457
>>> expr2.subs(x, Rational(1, 10)).n(30). # Have as many digits as you like...
1.23456790123456790123456790123

There is a lot of overlap between sympy users and numpy/scipy users
(although sympy's userbase is smaller). Perhaps unlike some of the
other scientific libraries sympy has a good chunk of users who only
use sympy and don't even know what python is let alone what scipy is.
Related are users of sage which incorporates both sympy and scipy as
internal dependencies and presents something like mathematica to the
end user.

--
Oscar


More information about the Tutor mailing list