Python (NumPy) Arrays with Alternative Lengths which can not be predefined
Question: I'm currently working on a numerical python code (from scratch) to solve the
following thought problem:

Imagine I have a 2D problem with a rigid ground, and a flexible layer on top
of that, which is connected to the ground by equidistant spaced springs. On
top of the flexible layer there is a cylinder, which is (if you like) glued to
the edge of the flexible layer. Now the cylinder is rolled horizontally in
numerous increments effecting an opening of the springs, starting from the
edge.

Now my question concerns the following; as an effect of the balance between
the combined spring forces and the moment applied to the cylinder a certain
number of springs will be opened at equilibrium. This number of opened springs
is not known in advance due to the nonlinearity of the problem. Furthermore, I
wish not to model the whole geometry, but rather only take into accounts the
relevant opened springs. During this process I would like, for example, to
store the resultant forces in every opened spring corresponding to a certain
moment increment.

* * *

Thus, in my opinion, the programming problem here is to:

  * Create a Force `array` which contains for every increment a list of all the forces in the individual springs. The complexity emerges from the fact that this list does not have a predefined length nor is this length the same for every numerical increment. Note that in this case the number of increments is predefined through for example a `linspace`

* * *

I have been looking for a suitable Pythonic solution to this problem, but have
not found one yet. I think the NumPy library should offer a convenient
solution, but I lack the Python experience nor did I find a nice example. I am
open to alternative approaches also, if more convenient in your opinion for
the above described setup.

Currently, I have the following code example to showcase my approach:

    
    
    from numpy import array , zeros , linspace
    
    area = 2.0
    Forces = []                 # Initialize complete Force array
    
    for inc in range(1,4):
        stresses = linspace(0.,10.,inc)
        n = len(stresses)
        Forcej = zeros( n )     # Initialize Forces in current increment
    
        for i, stress in enumerate(stresses):
            Forcej[i] = area*stress
    
        print Forcej
        Forces.append(Forcej)
    
    
    print Forces
    

Which outputs:

    
    
    [ 0.]
    [  0.  20.]
    [  0.  10.  20.]
    [array([ 0.]), array([  0.,  20.]), array([  0.,  10.,  20.])]
    

I suppose this is the desired outcome, but I'm thinking this is far from the
most elegant solution, could anyone assist or comment on this example? Thanks
already for considering my Question.


Answer: To be honest, I would recommend using a full array, so one column for every
spring. In combination with for example the numexpr
(<https://code.google.com/p/numexpr>) module, it is very easy and fast to
extract all "active" springs at each timestep.

The reason I believe a full array is the way to go is because it maps really
nice into how the underlying system handles memory access. So unless the
number of springs are hugh (say >100000), I believe that dragging around a lot
of zeros will not degrade the performance. Another option might be to look for
efficient sparse matrices. I know SciPy has support for it. I am not sure if
the matrix "profile" that you will get from your problem fits a sparse matrix
very well.

