[Numpy-discussion] Functions for indexing into certain parts of an array (2d)
Fernando Perez
fperez.net@gmail....
Sun Jun 7 03:28:51 CDT 2009
On Sat, Jun 6, 2009 at 2:01 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Sat, Jun 6, 2009 at 13:30, Fernando Perez <fperez.net@gmail.com> wrote:
> Oops! Never mind. I thought it was using mask_indices like the others.
> There is a neat trick for accessing the diagonal of an existing array
> (a.flat[::a.shape[1]+1]), but it won't work to implement
> diag_indices().
Neat! A version valid for all dimensionalities (always writing the
main diagonal) is:
if a.ndim == 2:
# Explicit, fast formula for the common case
step = a.shape[1] + 1
else:
step = np.cumprod((1,)+a.shape[:-1]).sum()
a.flat[::step] = val
Do you want this as part of the patch? If so, where should it go
(it's not 2-d only)? If you want it, should I add a check for equal
dimensions? (I'd be inclined to allow non-square in the 2-d case but
to avoid it in other cases, where the formula completely breaks down.
In 2-d it can be useful to fill the diagonal of rectangular arrays.)
>> - Are doctests considered enough testing for numpy, or are separate
>> tests also required?
>
> I don't think we have the testing machinery hooked up to test the
> docstrings on the functions themselves (we made the decision to keep
> examples as clean and pedagogical as possible rather than complete
> tests). You can use doctests in the test directories, though.
Got it.
>> - Where should these go?
>
> numpy/lib/twodim_base.py to go with their existing counterparts, I would think.
OK. Will send it in when I know whether you'd want the fill_diagonal
one, and where that should go.
I'll make a ticket with the patch attached.
>> - Any interest in also having the stuff below? I'm needing to build
>> structured random arrays a lot (symmetric, anti-symmetric, symmetric
>> with a particular diagonal, etc), and these are coming in handy. If
>> you want them, I'll put the whole thing together (these use the
>> indexing utilities from the previous suggestion).
>
> I wouldn't mind having a little gallery of matrix generators in numpy,
> but someone else has already made a much more complete collection:
>
> http://pypi.python.org/pypi/rogues
Ah, great! This stuff would be really nice to have in numpy/scipy,
actually. A lot more than my 15-minute hack :)
OK, I'll keep mine for symmetric/antisymmetric random matrices, since
that's what I need now, but it's great to know about that resource.
Cheers,
f
More information about the Numpy-discussion
mailing list