# [PYTHON MATRIX-SIG] A problem with slicing

Thu, 14 Sep 1995 14:21:02 -0400

```   One way out of this is to adopt the syntax

M[i, j]

for simple indexing.  This would require only a minor tweaking of the
grammar I believe.  This could be extended to support

M[i1:i2, j]
M[i1:i2, j1:j2]
M[i, j1:j2]

(and of course higher-dimensional equivalents).

How about allowing each of the index expressions to be an array
(of integers) itself? That gives an enormous flexibility (consider
that the indexing array could be a very complicated expression!).

Of course adopting such a change would completely ruin any possbility
of using things like

M[3, 4, 7] = [1, 10, 100]

as roughly equivalent to

M = 1
M = 10
M = 100

but then again I'm not too fond of that anyway (as a matter of fact,
I'd oppose it strongly).

According to my proposal, that could be done as

M[[3,4,7]] = [1, 10, 100]

- Should M[i][j] still be equivalent to M[i, j]?

Basically the question is whether M[i] should be allowed (and with
what meaning) if M has more than one dimension. Maybe it is better
not to allow it at all, as it creates some confusion due to the
imperfect analogy of arrays with (nested) sequences.

- Now we have multidimensional sequence types, should be have a
multidimensional equivalent of len()?  Some ideas:

- len(a, i) would return a's length in dimension i; len(a, i) == len(a)

- dim(a) (or rank(a)?) would return the number of dimensions

- shape(a) would return a tuple giving a's dimensions, e.g. for a
3x4 matrix it would return (3, 4), and for a one-dimensional
sequence such as a string or list, it would return a singleton
tuple: (len(a),).

How about having len() return the total number of elements? shape()
would be necessary anyway, and your proposed len(a,i) would
just be equivalent to shape(a)[i]. Similarly, dim(a) would
be len(shape(a)).

APL actually has only shape, which returns a one-dimensional
array of the lengths along each axis, i.e. a zero-length
vector in case of a scalar. Then dim(a) is just shape(shape(a)).

- How about multidimensional map(), filter(), reduce()?

map() is no problem, as you pointed out. Actually, if one adopts
a J-style rank concept, it wouldn't even be necessary. filter()
and reduce() should operate along a specified axis. filter()
would thereby return an array of equal dimension, but shorter
along one axis, and reduce() would return an array with the
dimension reduced by one.

- Multidimensional for loops?  Or should for iterate over the first
dimension only?

Again, iteration over an arbitrary axis would be useful. Iteration
over all elements could be handled by map().

-------------------------------------------------------------------------------
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. A                | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org