[SciPy-User] Problem with IndexError

Robert Kern robert.kern@gmail....
Thu Mar 24 22:32:48 CDT 2011


2011/3/8 Krystian Rosiński <krystian.rosinski@gmail.com>:
> Hi,
> I've been using SciPy for some time, but I still have problems with indexing
> and the Python convention relating to the numbering from zero in more
> complex cases.
> I'm trying to translate excellent example "Matrix Structural Analysis of
> Plane Frames using Scilab" to Python. I've spent on this code a few hours
> and I still can't find the cause of the problem of IndexError. Here is my
> code.
> I would be very grateful if someone could look at it and give me any advice.

1. Unrelated, but important: on line 205, use zeros((1,6), dtype=int).
Be sure to do this with other zeros() calls that are intended to hold
indices. The default is dtype=float.

2. In pf_getdof(), I'm sure you want dof[0, 0:3] and dof[0, 3:6].

3. In pf_ssm(), you want range(nmem), not (0, nmem+1).

4. In pf_calclm(), since numpy is 0-indexed, you want to increment nd
*after* you assign it into lm.

5. Typos: ilod -> iload, iloads -> iload.

6. In pf_assemloadvec():

    am = - dot(r.T, memloads[iload, 1:7])
        ii = dof[0, i]

7. Never use np.dot(np.linalg.inv(A), b). Use np.linalg.solve(A, b).
You still have a singular matrix. This may or may not indicate further
errors in your code, but that requires more knowledge of the problem
and algorithm than I have.

8. As a matter of good style, don't use "from numpy import *". Use
"import numpy as np" and use the dotted notation to get to numpy
functions.

I figured most of this out using pdb, Python's standard debugger. I
didn't even look at the original. You can see a tutorial on using the
debugger from the Software Carpentry site:

  http://software-carpentry.org/4_0/python/debugger/

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-User mailing list