Sympy - numpy relation

930 views
Skip to first unread message

Hauke Strasdat

unread,
Feb 21, 2012, 3:10:26 PM2/21/12
to sympy
Hi,

I recently discovered sympy and I must say it is really great! I am
mainly interested in matrices (matrix functions)
and symbolic derivatives.

However, one thing I haven't quite understood yet: What is sympy's
relatation to numpy?

Sympy has some support for numpy arrays. But is it in general a good
idea to use numpy.array instead of sympy.Matrix? Ideally I'd like to
define some matrix function which I can use then for numerical as well
as symbolic calculations. As far as I understand, Sympy has also some
numerical computation capabilities. So, is it actually a good idea to
use numpy in conjunction with sympy? Or should I better stick to pure
sympy? I found out there is also mpmath. Should I use this instead? Of
course, there are probably good arguments for all three choices. But,
it would be really great to hear some thoughts/arguments from you
guys.

Thanks a lot,
Hauke

krastano...@gmail.com

unread,
Feb 22, 2012, 7:13:58 PM2/22/12
to sy...@googlegroups.com
On 21 February 2012 15:10, Hauke Strasdat <stra...@gmail.com> wrote:
> Hi,
>
> I recently discovered sympy and I must say it is really great! I am
> mainly interested in matrices (matrix functions)
> and symbolic derivatives.
>
> However, one thing I haven't quite understood yet: What is sympy's
> relatation to numpy?
I would say "undefined".
Check http://code.google.com/p/sympy/issues/detail?id=537

>
> Sympy has some support for numpy arrays. But is it in general a good
> idea to use numpy.array instead of sympy.Matrix? Ideally I'd like to
> define some matrix function which I can use then for numerical as well
> as symbolic calculations. As far as I understand, Sympy has also some
> numerical computation capabilities. So, is it actually a good idea to
> use numpy in conjunction with sympy? Or should I better stick to pure
> sympy? I found out there is also mpmath. Should I use this instead? Of
> course, there are probably good arguments for all three choices. But,
> it would be really great to hear some thoughts/arguments from you
> guys.
I would use numpy arrays for every numerical calculation (and will
take care to actually use the float dtype instead of 'object'). If you
need to create such an array from sympy expression evaluated for
different inputs you can use lambdify (I personally don't like it
because of many corner cases that are not covered by it, but it works
well for simple expressions). Or you can use .subs(...).evalf() and
map/list comprehension and then cast it to an array.

For symbolic stuff I would use sympy Matrix. Recently the MatrixSymbol
and ImmutableMatrix were added to the code base, so you can do much
more with matrices now.
But this is not done yet: http://code.google.com/p/sympy/issues/detail?id=2759

So for arrays and big numerical matrices - numpy and lambdify (but do
you _really_ need lambdify, I would type small simple expressions by
hand)
For small symbolic matrices - sympy and Matrix

Bear in mind that others may have different advices and generally more
trust in the way sympy and numpy interoperate.
>
> Thanks a lot,
> Hauke
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Aaron Meurer

unread,
Feb 23, 2012, 12:19:21 PM2/23/12
to sy...@googlegroups.com
Hi.

On Wed, Feb 22, 2012 at 5:13 PM, krastano...@gmail.com
<krastano...@gmail.com> wrote:
> On 21 February 2012 15:10, Hauke Strasdat <stra...@gmail.com> wrote:
>> Hi,
>>
>> I recently discovered sympy and I must say it is really great! I am
>> mainly interested in matrices (matrix functions)
>> and symbolic derivatives.
>>
>> However, one thing I haven't quite understood yet: What is sympy's
>> relatation to numpy?
> I would say "undefined".
> Check http://code.google.com/p/sympy/issues/detail?id=537

Strictly speaking, they are two independent projects. Neither is a
dependency of the other. However, it is possible, to the extent that
the interoperability works, to use numpy to numerically calculate
SymPy objects, and to put SymPy objects inside of numpy arrays. Note
that most issues with interoperability are bugs on the SymPy side,
rather than the numpy side. We do want things to work well, so if
something doesn't please let us know in our issue tracker.

I would say that it depends on what you want to do. Using numpy is a
clean and fast way to do numerical calculations, especially those
involving arrays. Many people use SymPy to do symbolic minipulation,
and then use lambdify() to get numpy to do numerics.

I should note that mpmath is SymPy's numeric library. So there is no
need to use that separately: if you use .evalf() in SymPy, that is
using mpmath. The advantage of mpmath is that is uses arbitrary
precision arithmetic, so you can represent very large, very small, or
very precise numbers with it. Numpy on the other hand uses machine
arithmetic, so you will always be limited to the dtype. Of course, as
a result, numpy is much faster. This is also the case because numpy
is written in C and mpmath is written in pure Python.

mpmath also has a broader support of special functions--virtually
every one included in SymPy, so if you use a lot of those, you may
have to use SymPy to numerically evaluate them. You can do this
automatically with lambdify by passing module=["numpy", "mpmath",
"sympy"].

Aaron Meurer

Reply all
Reply to author
Forward
0 new messages