'im' as a global variable used for computing a complex wave

If 'im' is used as the name of a global real-number variable, I get some weird results. See details below. Any explanation?


<br />
<br />
Function test()<br />
<br />
make /c /o /N=2   cm1, cm2, cm3, cm4, cm5<br />
<br />
variable a1= 1, b1=1, c1=0<br />
<br />
Nvar im = root:im<br />
Nvar im1 = root:im1<br />
<br />
cm1={a1, b1}<br />
cm2= {a1, b1*im}<br />
cm3= {a1, b1*im1}<br />
<br />
cm4[0]= a1; cm4[1]= b1*im<br />
cm5[0]=cmplx(a1, 0)<br />
cm5[1]=cmplx(b1*im, 0)<br />
<br />
<br />
print "im=",  im,"\r"<br />
print "im1=",  im1, "\r"<br />
printf, "a1= %3.1f, b1=%3.1f, b1*im=%3.1f\r",a1, b1, b1*im<br />
<br />
<br />
print cm1, cm2, cm3, cm4, cm5<br />
<br />
end<br />



We would expect that cm2 = cm3 = cm4 = cm5. However, they are not. Here are the results,

•test()
im= 0
im1= 0
a1= 1.0, b1=1.0, b1*im=0.0
cm1[0]= {cmplx(1,0),cmplx(1,0)}
cm2[0]= {cmplx(1,0),cmplx(0,1)}
cm3[0]= {cmplx(1,0),cmplx(0,0)}
cm4[0]= {cmplx(1,0),cmplx(0,1)}
cm5[0]= {cmplx(1,0),cmplx(0,0)}
Using Igor Pro 6.34 on Macintosh and Windows, I get the same result for cm2, cm3, cm4 and cm5. Here is what I executed from the command line:
Variable/G im=0, im1=0
Test()


Here is what was printed:
cm1[0]= {cmplx(1,0),cmplx(1,0)}
cm2[0]= {cmplx(1,0),cmplx(0,0)}
cm3[0]= {cmplx(1,0),cmplx(0,0)}
cm4[0]= {cmplx(1,0),cmplx(0,0)}
cm5[0]= {cmplx(1,0),cmplx(0,0)}
Thanks, hrodstein.

It was my mistake. I noticed that the problem happened if 'im' was originally defined as a complex number, but was referred a real number in the function. It only prints out the real part of the number. However, it is still treated as a complex number during the computation.

variable /G /c im
im= cmplx(0, 1)






hrodstein wrote:
Using Igor Pro 6.34 on Macintosh and Windows, I get the same result for cm2, cm3, cm4 and cm5. Here is what I executed from the command line:
Variable/G im=0, im1=0
Test()


Here is what was printed:
cm1[0]= {cmplx(1,0),cmplx(1,0)}
cm2[0]= {cmplx(1,0),cmplx(0,0)}
cm3[0]= {cmplx(1,0),cmplx(0,0)}
cm4[0]= {cmplx(1,0),cmplx(0,0)}
cm5[0]= {cmplx(1,0),cmplx(0,0)}

If a global variable is expected to be complex, use NVAR/C to reference it. Then Print will handle it correctly.