[Numpy-discussion] Segfault on wrong use of indexing
Arnar Flatberg
arnar.flatberg@gmail....
Mon Sep 17 10:10:19 CDT 2007
Hi list
A pretty common use (for me) is to create arrays at the beginning of
my code and then fill in parts as I go along. Today, my code hit a
segmentation fault. The error was that an index-vector ,that usually
is a list with several members, now contained only one member and I
tried to insert a 1-dim vector.
Here is a short example of what went wrong:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as n
>>> n.__version__
'1.0.1'
>>> a = n.random.rand(4,2)
>>> a
array([[ 0.79561255, 0.4932522 ],
[ 0.84008255, 0.88258233],
[ 0.35143166, 0.82214487],
[ 0.12490114, 0.15799074]])
Let us insert [2,2] into the first row.
Correct use:
>>> a[0,:] = [2,2]
>>> a
array([[ 2. , 2. ],
[ 0.84008255, 0.88258233],
[ 0.35143166, 0.82214487],
[ 0.12490114, 0.15799074]])
or:
a[[0],:] = [[2,2]]
However, when I tried to to insert a 1-dim array with a 'two-dim'
index things went wrong:
>>> a[[0],:] = [2,2]
Segmentation fault
Sometimes, (with some lucky pointers, I guess) the latter code will
run, with incorrect values in the array. With incorrect, I mean
compared to the correct use of indices :-)
My fix was to call "atleast_2d" on the parts I wanted to insert into the array.
Am I just a horrible user of indices, or is this a bug?
The situation were incorrect use of indices just passes silently is
what I find a little disturbing.
Thanks,
Arnar
More information about the Numpy-discussion
mailing list