[Numpy-discussion] Functions for indexing into certain parts of an array (2d)
Robert Kern
robert.kern at gmail.com
Sat Jun 6 18:39:45 EDT 2009
On Sat, Jun 6, 2009 at 17:34, Alan G Isaac<aisaac at american.edu> wrote:
> On 6/6/2009 6:02 PM Keith Goodman apparently wrote:
>>> def fill_diag(arr, value):
>> if arr.ndim != 2:
>> raise ValueError, "Input must be 2-d."
>> if arr.shape[0] != arr.shape[1]:
>> raise ValueError, 'Input must be square.'
>> arr.flat[::arr.shape[1]+1] = value
>
>
> You might want to check for contiguity.
> See diagrv in pyGAUSS.py:
> http://code.google.com/p/econpy/source/browse/trunk/pytrix/pyGAUSS.py
Ah, that's the beauty of .flat; it takes care of that for you. .flat
is not a view onto the memory directly. It is a not-quite-a-view onto
what the memory *would* be if the array were contiguous and the memory
directly reflected the layout as seen by Python.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list