[Numpy-discussion] Getting the indexes of the myarray.min()
perry at stsci.edu
Wed May 12 15:11:02 CDT 2004
Álvaro Tejero Cantero wrote:
> It works great... but what about efficiency? If I do times.min() and
> then numarray.nd_image.minimum_positioan(times) I am running twice
> essentially the same extremum-finding routine, which is prohibitibe for
> large N..., am I right?
Well, yes. But when you ask to find all the things that equal
the minimum, you pretty much must look twice (if you want to know
where they all are if more than one). Once to determine the
minimum, the next time to locate all of them.
You didn't really say whether you needed just the first minimum or
all minima (if I recall correctly).
> Which makes me thing of a more general question: I know that some of the
> array functions are coded in C for speed, but what about the classical
> python-for loop, as in (r Nx3 array of particle positions)
> [ [r[i]-r[j] for i in arange(N)] for j in arange(N)]
> is this handled to C code?
As Tim mentions, yes this can be done efficiently. But there
is no general answer this sort of open question. It depends
on what you are doing. Sometimes there are functions or
tricks to avoid loops in Python, sometimes not.
> Yes... although for the problem at hand that motivated my query, my
> times matrix is symmetric... I don't really need all the minima, but
> does numarray have any special datatype for symmetric matrixes, that
> prevents storage of unneded (e.g. supradiagonal) elements?.
Not for special cases like this. One could probably write a special
subclass to do this, but for a savings of a factor of 2 in memory,
it usually would not be worth the trouble (unlike sparse matrices)
More information about the Numpy-discussion