Scary bug with array assignment [message #89257] |
Sun, 10 August 2014 11:57  |
Paddy Leahy
Messages: 7 Registered: August 2013
|
Junior Member |
|
|
Hi,
I'm running IDL 8.2. I'm accumulating arrays stored as separate
fields of a structure into a multidimensional array, in order to average it.
I'm using the recommended notation of array assignment by first index, like this
sum = FLTARR(npix,3)
FOR ifile = 0,9 DO BEGIN
< read data from file into data array>
FOR jj=0,3 sum[0,jj] += data.(jj)
ENDFOR
On the first iteration, each row of sum is set to the data in data, as expected. But on subsequent iterations, the *FIRST* element of data.(jj) is added to *EACH*
element of the jjth row of sum.
It turns out that the use of structures is not required to trigger this problem.
Here are some actual results cut and pasted from the command line:
HIDL> test = fltarr(3,3)
HIDL> sum = fltarr(3,3)
HIDL> data = [[-1,0,1],[-2,0,2],[-3,0,3]]
HIDL> print, sum
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
0.00000 0.00000 0.00000
HIDL> print, data
-1 0 1
-2 0 2
-3 0 3
HIDL> for jj=0,2 do sum[0,jj] += data[*,jj]
HIDL> print, sum
-1.00000 0.00000 1.00000
-2.00000 0.00000 2.00000
-3.00000 0.00000 3.00000
HIDL> for jj=0,2 do sum[0,jj] += data[*,jj]
HIDL> print, sum
-2.00000 -1.00000 0.00000
-4.00000 -2.00000 0.00000
-6.00000 -3.00000 0.00000
HIDL> sum[*] = 0
HIDL> for jj=0,2 do sum[*,jj] += data[*,jj]
HIDL> for jj=0,2 do sum[*,jj] += data[*,jj]
HIDL> print, sum
-2.00000 0.00000 2.00000
-4.00000 0.00000 4.00000
-6.00000 0.00000 6.00000
As I understand it, the two notations sum[*,jj] = data[*,jj] and sum[0,jj] = data[*,jj] should be equivalent (assuming the row length is the same in the two arrays, as above), although the second is supposed to be faster.
But as you see, the first notation gives bizarre results.
Is there a big hole in my understanding of IDL array assignment, or is this a ghastly bug?
|
|
|
Re: Scary bug with array assignment [message #89258 is a reply to message #89257] |
Sun, 10 August 2014 12:25   |
Fabzi
Messages: 305 Registered: July 2010
|
Senior Member |
|
|
On 10.08.2014 20:57, Paddy Leahy wrote:
> Is there a big hole in my understanding of IDL array assignment, or is this a ghastly bug?
The problem is not the assignment, it is the "+="
In your example, if you transform:
IDL> for jj=0,2 do sum[0,jj] += data[*,jj]
into what it really is, i.e:
IDL> for jj=0,2 do sum[0,jj] = sum[0,jj] + data[*,jj]
it may explain your problem.
Cheers,
Fabien
|
|
|
Re: Scary bug with array assignment [message #89259 is a reply to message #89258] |
Sun, 10 August 2014 12:41  |
Paddy Leahy
Messages: 7 Registered: August 2013
|
Junior Member |
|
|
On Sunday, August 10, 2014 8:25:51 PM UTC+1, Fabien wrote:
> On 10.08.2014 20:57, Paddy Leahy wrote:
>
>> Is there a big hole in my understanding of IDL array assignment, or is this a ghastly bug?
>
>
>
> The problem is not the assignment, it is the "+="
>
>
>
> In your example, if you transform:
>
>
>
> IDL> for jj=0,2 do sum[0,jj] += data[*,jj]
>
>
>
> into what it really is, i.e:
>
>
>
> IDL> for jj=0,2 do sum[0,jj] = sum[0,jj] + data[*,jj]
>
>
>
> it may explain your problem.
>
>
>
> Cheers,
>
>
>
> Fabien
aha! That makes sense. Thanks!
|
|
|