comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Scary bug with array assignment
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Scary bug with array assignment [message #89257] Sun, 10 August 2014 11:57 Go to next message
Paddy Leahy is currently offline  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 Go to previous messageGo to next message
Fabzi is currently offline  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 Go to previous message
Paddy Leahy is currently offline  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!
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: cgContour with different clipping
Next Topic: How to assign command line arguments to variables in another class

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:11:36 PDT 2025

Total time taken to generate the page: 0.00542 seconds