[Numpy-discussion] Apparently Non-deterministic behaviour of complex-array instantiation values
Karl Kappler
magnetotellurics at gmail.com
Mon Dec 3 15:10:31 EST 2012
Hello,
This is a continuation of a problem I had last year,
http://old.nabble.com/Apparently-non-deterministic-behaviour-of-complex-array-multiplication-tt32893004.html#a32931369
at least it seems to have similar symptoms. I am working again with complex
valued arrays in numpy (python version 2.7.3). This time however, the
dataset is not very large, and I am able to post a snippet of the code.
I became aware of the problem working in and IDE: Spyder 2.1.9, where I was
repeatedly running code by pushing f5 and checking that my numerical
results were what I expect. What I found was that the output in spyder
varied somewhat randomly. In particular, when initializing a 2x2
complex-valued numpy array on line 32 of the code called “lowTri”.
When I print the value of the upper right element (the one that should be
zero) I often see 1.789+1.543j, or 0+1.543j, or 0+0j. This behavior happens
when I run the code using f5 in spyder, and I thought it may be a Spyder
issue, but further investigation has shown equally strange behavior on the
command line as well.
When I run this script on the command line the output is usually the same
from run to run (although I have seen some variations, which I do not
understand), but most remarkable, and reproducible, is that if I comment
out line 17 (where the complex-valued array zzz is populated), the behavior
of the initialization of lowTri varies.
With Line 17 uncommented I usually get (on the command line):
Lower Triangular [0,1]: 1.543j
[[ 670.9 +1.22400000e-05j 0.0 +1.54300000e+00j]
[ 195.8 -1.17300000e+02j 391.2 +1.46900000e-05j]]
Lower Triangular [0,1]: 1.543j
Real Part 0.0
and with line 17 it commented:
Lower Triangular [0,1]: 0j
[[ 670.9 +1.22400000e-05j 0.0 +0.00000000e+00j]
[ 195.8 -1.17300000e+02j 391.2 +1.46900000e-05j]]
Lower Triangular [0,1]: 0j
Real Part 0.0
I.e.. the imaginary part is initialized to a different value. From reading
up on forums I think I understand that when an array is allocated without
specific values, it will be given random values which are very small, ie.
~1e-316 or so. But it would seem that sometimes initallization is done to a
finite quantity. I know I can try to initialize the array using np.zeros()
instead of np.ndarray(), but it is the principle I am concerned about.
Last year it had been suggested that I had bad RAM, but these issues are
reproducing on four computers, one of which has a new motherboard/RAM and
AMD processor, and the others are Intel. Memtest has been run recently on
at least two of the machines.
Could someone try running this script with and without line 17 commented
out and tell me if they are getting the same sort of behaviour?
Thanks,
Karl
**********************************
import numpy as np
if __name__ == "__main__":
a={}
a[0] = '0.1537E+00 0.1610E+01 -0.4801E+01 -0.3175E+01'
a[1] = '0.1789E+01 0.1543E+01 -0.5524E+00 -0.8423E+00'
c = '0.6709E+03 0.1224E-04 0.1958E+03 -0.1173E+03 0.3912E+03 0.1469E-04'
ztmp = np.zeros((4,2))
zzz = np.zeros((2,2)) + complex(0,1)*np.zeros((2,2));
line = []
for iE in range(2):
line = a[iE].split()
for iElt, elt in enumerate(line):
ztmp[iElt,iE] = float(elt)
zzz[:,0:2] = ztmp[[0,2],:] + complex(0,1)*ztmp[[1,3],:] #commenting this
line seems to affect value of lowTri
stemp = np.zeros((2,3))
nElts = np.prod(stemp.shape)
v = []
line = c.split()
for l in line:
v.append(float(l))
N=len(v)/2
cVec = np.ndarray(shape=(N), dtype=complex)
for i in range(N):
cVec[i] = complex(float(v[2*i]),float(v[2*(i+1)-1]))
lowTri = np.ndarray(shape=(2,2), dtype=complex)
#lowTri = np.zeros(shape=(2,2), dtype=complex)
print("Lower Triangular [0,1]: {}".format(lowTri[0,1]))
TI = np.tril_indices(2)
rows = TI[0]
cols = TI[1]
for iCell in range(len(rows)):
lowTri[rows[iCell],cols[iCell]] = cVec[iCell]
print lowTri
print("Lower Triangular [0,1]: {}".format(lowTri[0,1]))
print("Real Part {}".format(np.real(lowTri[0,1])))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121203/4ea0b0c1/attachment.html>
More information about the NumPy-Discussion
mailing list