N-dim arrays in SymPy

360 views
Skip to first unread message

Francesco Bonazzi

unread,
Dec 4, 2015, 4:32:14 AM12/4/15
to sympy
Hello,

the code I wrote to add N-dim arrays to SymPy is (almost) ready. The only point is the order of the constructor arguments.

https://github.com/sympy/sympy/pull/10180

Four classes are provided:

  • MutableDenseNDimArray
  • MutableSparseNDimArray
  • ImmutableDenseNDimArray
  • ImmutableSparseNDimArray

Immutable ones are SymPy objects (they subclass Expr), mutable ones are not. The structure loosely reflects the matrix module.


The remaining issue: argument order

Currently the dimensions should preceed the iterable, that is, construction would be like:

In [1]: from sympy.tensor.array import *

In [2]: from sympy import *

In [3]: a = ImmutableDenseNDimArray(2, 3, 4, range(2*3*4))

In [4]: a
Out[4]: [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]



SymPy matrices specify dimensions before the iterable:


In [5]: Matrix(2, 3, range(6))
Out[5]:
Matrix([
[0, 1, 2],
[3, 4, 5]])


I thought it would be a good idea to preserve that order. Unfortunately, being the number of dimensions variable, one cannot (prior to Python 3.5) use the shortened form ImmutableDenseNDimArray(*dims, iterable).

Do you think we should postpone the dimensions?

That is:

ImmutableDenseNDimArray(iterable, *dims)

instead of

ImmutableDenseNDimArray(dim1, dim2, ... , iterable)

Consider that the different standard used in matrices could be resolved by adding a new argument order in the matrix construction, e.g. by allowing an expression like Matrix(range(6), 2, 3) to mean Matrix(2, 3, range(6)).

Ondřej Čertík

unread,
Dec 4, 2015, 10:39:54 AM12/4/15
to sympy
Hi Francesco,
I would try to stay close to NumPy. I am actually not sure how to
properly construct 3D arrays. One way is this:

In [12]: reshape(array(range(2*3*4)), (2, 3, 4))
Out[12]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])

So for this reason, it looks like your proposal

ImmutableDenseNDimArray(iterable, *dims)

is better. We can provide perhaps a shape argument, to be used as follows:

ImmutableDenseNDimArray(range(2*3*4), shape=[2, 3, 4])


Ondrej

Aaron Meurer

unread,
Dec 4, 2015, 11:28:31 AM12/4/15
to sy...@googlegroups.com
Is there a reasonable default value for the dimensions (1-dimensional
I suppose)? If so, it's better to make it a keyword argument as Ondrej
suggests.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CADDwiVANyerbj%3D5jckf5iDo-jWuxiDd55XJO%2BPxWB7g8Xq%2BxpA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Francesco Bonazzi

unread,
Dec 4, 2015, 4:39:36 PM12/4/15
to sympy


On Friday, 4 December 2015 17:28:31 UTC+1, Aaron Meurer wrote:
Is there a reasonable default value for the dimensions (1-dimensional
I suppose)? If so, it's better to make it a keyword argument as Ondrej
suggests.

OK, let's go that way.
Reply all
Reply to author
Forward
0 new messages