ArrayComprehension or List

33 views
Skip to first unread message

Chris Smith

unread,
Jun 3, 2019, 11:46:03 PM6/3/19
to sympy
From discussion at #16926 where I suggest that perhaps we should be naming ArrayComprehension as List and it was said,

> [ArrayComprehension] is supposed to be a multidimensional list comprehension

It looks like an unevaluated List to me: `List(i + j, (i,0,3), (j,0,4))` is the unevaluated form of `[i + j for i in range(4) for j in range(5)]`. (For comparison, Integral and Sum are examples of an unevaluated Expr, which, after a doit and the right conditions, lose the wrapper and become other Expr.) 

An evaluated form would be `List([1, 2, 3, 4])` where the `[]` would be necessary to distinguish between the evaluated and comprehension form.

Are we going to regret introducing an object that is so closely related to a list or will we always want a list to be an unsympified object in SymPy?

/c

Gagandeep Singh (B17CS021)

unread,
Jun 3, 2019, 11:52:24 PM6/3/19
to sympy
I support `List` because it's intuitive to me have an analogous symbolic unevaluated form of `list`. There are similar containers like `Dict` similar `dict`. This argument is based on intuition. The point raised by Francesco is also valid and should be taken into consideration.
One solution can be is to allow both, `List` as well as `ArrayComprehension`. We should wait for the end users to respond on this.
I hope it is relevant to the point raised in the subject.

Zhiqi KANG

unread,
Jun 4, 2019, 5:23:20 AM6/4/19
to sympy
Hi,

As far as I know, the difference between `ArrayComprehension` and `list` lies in the notion of multidimensionality.

First of all, for clarification,

> `List(i + j, (i,0,3), (j,0,4))` is the unevaluated form of `[i + j for i in range(4) for j in range(5)]`.

is not quite true regarding the implementation of `ArrayComprehension`. In fact, `ArrayComprehension(i + j, (i,0,3), (j,0,4))` will be like `[[i + j for i in range(5)] for j in range(4)]`. Not to mention the different order of dimensions because the limits in `ArrayComprehension` are executed from left to right, the resulting output is a multidimensional list. So each time there is one more limit, the dimension of output list will +1. As you can see, `ArrayComprehension` is more likely to be a such structure: [[[...]...]...]. This multidimensionality comes from Table in Wolfram Mathematica, which is the the example of the implementation of `ArrayComprehension`.

Secondly, the implementation follows the example of `Array` module in SymPy, which means the properties and functions like `shape`, `rank()` have been already implemented.Plus, its output is of type `ImmutableDenseNDimArray`. Since an array has by default a notion of multidimensionality, it would be more coherent to call it as an array. That's why its name is `ArrayComprehension` rather than `ListComprehension`. 

However, it is no doubt that `ArrayComprehension` resembles a lot to list. It would be a good idea to have something similar to list implemented in SymPy, in order to add a notion of 'immutable' to list. Either renaming ArrayComprehension or creating a new module to do so is faisible. 

That's what I have understood while implementing `ArrayComprehension`. I hope that it could help make some concepts clear.

Zhiqi


在 2019年6月4日星期二 UTC+2上午5:46:03,Chris Smith写道:

Oscar Benjamin

unread,
Jun 4, 2019, 6:06:00 AM6/4/19
to sympy
In normal Python the main purpose of list is to be mutable. When you
want an immutable list you can use tuple.

If SymPy had List would it be mutable? If so then it couldn't be a
Basic subclass. If not what purpose would it serve?
> --
> 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 https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f98dd475-7c90-4d3f-827e-f14480272693%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chris Smith

unread,
Jun 4, 2019, 8:51:58 AM6/4/19
to sympy
 As you can see, `ArrayComprehension` is more likely to be a such structure: [[[...]...]...]. 

Ahh...I see what you mean about the multidimensionality, now. It is a reshaped list comprehension:

    >>> reshape([i + j for i in range(4) for j in range(3) for k in range(2)], [[2]]*3)
    [[[0, 0], [1, 1], [2, 2]], [[1, 1], [2, 2], [3, 3]], [[2, 2], [3, 3], [4, 4]], [[3, 3], [4, 4], [5, 5]]]
    >>> _==S(str(ArrayComprehension(i+j ,(i,0,3),(j,0,2),(k,0,1)).doit()))
    True

 So I see the reason not to call it a List.

Aaron Meurer

unread,
Jun 5, 2019, 1:10:06 AM6/5/19
to sympy
Does it support vectorized operations, e.g., should
ArrayComprehension(...) + ArrayComprehension(...) work to add the
corresponding elements? If so, then it's an array. If not, then it's
more like a list or tuple.

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 https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/fbf59f35-2dc2-4d48-a79b-3643276ec8dc%40googlegroups.com.

Zhiqi KANG

unread,
Jun 5, 2019, 4:30:57 PM6/5/19
to sympy
> I see what you mean about the multidimensionality
I am happy to be helpful. :-)

> should ArrayComprehension(...) + ArrayComprehension(...) work to add the corresponding elements?
That is a good point. As far as I know, the addition of two ArrayComprehension doesn't make sense. So it would possibly not support such type of operations. However, its output, if it is expanded to be an Array, supports these vetorized operations. That is to say, we can perform ArrayComprehension().doit() + ArrayComprehension().doit() if neither of them has symbolic dimensions. 

Frankly, based on the argumentation of Aaron, I am a little bit confused about defining this class then. Maybe a multidimensional list? 

在 2019年6月5日星期三 UTC+2上午7:10:06,Aaron Meurer写道:
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages