1

I am creating finite element code in Python that relies on numpy and scipy for array, matrix and linear algebra calculations. The initial generated code seems to be working and I am getting the results I need.

However, for some other feature I need to call a function that performs the analysis more than one time and when I review the results they differ completely from the first call although both are called using the same inputs. The only thing I can think of is that the garbage collection is not working and the memory is being corrupted.

Here is the procedure used:

  1. call setup file to generate model database: mDB = F0(inputs)
  2. call first analysis with some variable input: r1 = F1(mDB, v1)
  3. repeat first analysis with the same variable from step2: r2 = F1(mDB, v1)

Since nothing has changed, I would expect that the results from step#2 and step#3 would be the same, however, my code produces different results (verified using matplotlib).

I am using:

Python 2.7 (32bit) on Windows 7 with numpy-1.6.2 and scipy-0.11.0

2
  • 1
    Does F1 modify mDB or v1 at all? Does this irregularity happen consistently and deterministically? I.E. do you get the exact same error each time? Commented Dec 14, 2012 at 1:46
  • No they are not. The exact calling is as such: mDB = F0(inputs), r1 = F1(mDB, v1). Both mDB and v1 are dictionaries.
    – Nader
    Commented Dec 14, 2012 at 4:09

2 Answers 2

2

If your results are sensitive to rounding error (e.g. you have some programming error in your code), then in general floating point results are not reproducible. This occurs already due to the way modern compilers optimize code, so it does not require e.g. accessing uninitialized memory.

Please see: http://www.nccs.nasa.gov/images/FloatingPoint_consistency.pdf

Another likely possibility is that your computation function modifies the input data. The point you mention in the comment above does not exclude this possibility, as Python is pass-by-reference.

0

Ok, based on suggestion from above I found the problem.

I rely in my code on dictionaries (hash tables). I call the contents of the original input dictionary mDB and modify those, and I thought the original contents do not get changed inside a separate function, but they do. I come from Fortran and Matlab where these do not change.

The answer was to deepcopy the contents of my original dictionary rather than simple assignment. Note that I tried simple copy as in:

A = mDB['A'].copy()

but that did not work either. I had to use:

import copy
A = copy.deepcopy(mDB['A'])

I know some would say that I should read the manual that "Assignment statements in Python do not copy objects, they create bindings between a target and an object" (documentsation), but this is still new and weird behavior for me.

Any suggestions for using other than dictionaries for storing my original data?

Not the answer you're looking for? Browse other questions tagged or ask your own question.