[PYTHON MATRIX-SIG] take() again

Carlos Fonseca fonseca@gaivota.demon.co.uk
Tue, 20 Aug 1996 03:14:01 +0100 (BST)

I was thinking about take(), indexing, structural operations, etc., and
got some more ideas on take() which should make it possible to combine
both mine and Konrad's expectations into a single function. The source of
confusion in my mind seems to have been that, whereas a+b expects arrays
to be aligned at their last dimension, indexing works on the outer
dimension. In the case of take(), it should work on the outer

My proposal is a take(a,indices,axis) where

* the dimensions of indices must be aligned with the *outer* (or leftmost) 
dimensions of a (and not the inner ones, as I proposed before).

* the default axis is the inner axis of _indices_. This is not necessarily
the inner axis of a, as indices my have lower rank than a. We would still
say axis=-1, but this would refer to indices, not to a. 

Now, for some examples:

If indices is 1d, axis=-1 is the same as axis=0, and we have the current

If indices has the same shape as a, then take(a,argsort(a,-1)) will work
as expected.

Even better, if indices has shape (m,n) and a has shape (m,n,p),
take(a,indices) will rearrange (or select) the rows of a. I believe this
to be consistent with the idea of a structural operation, and is what I
will actually need in my application. 

If indices is 2d and we want the current behaviour, then take([a],indices)
will do it. In this way, the rank of the result will only increase if we
explicitly want it to. 

Note that in all of the above situations, the default axis produced the
desired results.

Would this be acceptable?


MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org