0

I am trying write a code to work with arrays. I have arrays for variables and I am trying to call certain numbers from those variables for a defined function. I have attached a simplified snip of what I am trying to accomplish. The code works fine when I just have the variables as a single number but when I incorporate arrays it doesn't work.

import numpy as np

noe = 3


# E of members ksi
E = 29000*np.ones((1, noe))
# Area of members in^2
A = 1000*np.array([20, 30, 20])
# length of members in
L = np.array([144, 240, 144])
# moment of inertia of members in^4
I = np.array([600, 1200, 600])

def k_frame_local_6x6(E, I, A, L):
    return (E/L)*np.array([[A, 0, 0, -A, 0, 0], [0, (12*I)/(L**2), (6*I)/L, 0, (-12*I)/(L**2), (6*I)/L], [0, (6*I)/L, 4*I, 0, (-6*I)/L, 2*I], [-A, 0, 0, A, 0, 0], [0, (-12*I)/(L**2), (-6*I)/L, 0, (12*I)/(L**2), (-6*I)/L], [0, (6*I)/L, 2*I, 0, (-6*I)/L, 4*I]])


m=k_frame_local_6x6(E[0, 1], I[0, 1], A[0, 1], L[0, 1])

print(m)

The Error I receive is "IndexError: too many indices for array"

When I manually enter the values I am trying to get the function to read it works, that looks like this:

def k_frame_local_6x6(E, I, A, L):
    return (E/L)*np.array([[A, 0, 0, -A, 0, 0], [0, (12*I)/(L**2), (6*I)/L, 0, (-12*I)/(L**2), (6*I)/L], [0, (6*I)/L, 4*I, 0, (-6*I)/L, 2*I], [-A, 0, 0, A, 0, 0], [0, (-12*I)/(L**2), (-6*I)/L, 0, (12*I)/(L**2), (-6*I)/L], [0, (6*I)/L, 2*I, 0, (-6*I)/L, 4*I]])

m=k_frame_local_6x6(29000, 1200, 30000, 240)

print(m)

and the results I get are:

[[  3.62500000e+06   0.00000000e+00   0.00000000e+00  -3.62500000e+06
    0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   3.02083333e+01   3.62500000e+03   0.00000000e+00
   -3.02083333e+01   3.62500000e+03]
 [  0.00000000e+00   3.62500000e+03   5.80000000e+05   0.00000000e+00
   -3.62500000e+03   2.90000000e+05]
 [ -3.62500000e+06   0.00000000e+00   0.00000000e+00   3.62500000e+06
    0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00  -3.02083333e+01  -3.62500000e+03   0.00000000e+00
    3.02083333e+01  -3.62500000e+03]
 [  0.00000000e+00   3.62500000e+03   2.90000000e+05   0.00000000e+00
   -3.62500000e+03   5.80000000e+05]]

One thing I have just noticed is with my ones array I have two sets of brackets:

E = 29000*np.ones((1, noe))

and the result is:

array([[ 29000.,  29000.,  29000.]])

However with the rest of the arrays I only get one bracket set:

A = 1000*np.array([20, 30, 20])

gives me:

array([20000, 30000, 20000])

Let me know if I need to clarify anything else. Thank you all!!!

3
  • 3
    Sorry, I could not understand what you wrote. Could you post the script that does not work, the expected output and the error you are getting? Commented Jan 6, 2016 at 22:05
  • 2
    A, I and L are one-dimensional arrays, so eg. A[0, 1] fails. Take a look at NumPy Indexing Commented Jan 6, 2016 at 22:07
  • I shall repost again once I get back to my computer this time with some more clarity Commented Jan 6, 2016 at 22:30

1 Answer 1

1

Your k_frame_local_6x6 - which is ugly, virtually unreadable for ordinary humans, produces a 6x6 array when given 4 numbers.

But as best I can tell, none of the terms is designed to work with arrays:

def k_frame_local_6x6(E, I, A, L):
    temp = np.array([[A, 0, 0, -A, 0, 0], 
                     [0, (12*I)/(L**2), (6*I)/L, 0, (-12*I)/(L**2), (6*I)/L], 
                     [0, (6*I)/L, 4*I, 0, (-6*I)/L, 2*I], 
                     [-A, 0, 0, A, 0, 0], 
                     [0, (-12*I)/(L**2), (-6*I)/L, 0, (12*I)/(L**2), (-6*I)/L], 
                     [0, (6*I)/L, 2*I, 0, (-6*I)/L, 4*I]
                     ])
     return (E/L)*temp

Try, just for example the simplest row of terms

np.array([-A, 0,0,A,0,0])

That's fine if A is a number. But it does not work if A is an array. Well it does work, but does not return something meaningful

In [105]: A = 1000*np.array([20, 30, 20])

In [106]: np.array([A, 0, 0, -A, 0, 0])
Out[106]: 
array([array([20000, 30000, 20000]), 0, 0, array([-20000, -30000, -20000]),
       0, 0], dtype=object)

You try to pass A[0, 1]. Have you tried that expression?

In [107]: A[0,1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-107-33f18b7a38c5> in <module>()
----> 1 A[0,1]

IndexError: too many indices

In [108]: A.shape
Out[108]: (3,)

It does not work because A is defined as 1d 3 element array. You can't index it with 2 values.

E is defined as 2d array, but all the rest are 1d. Is that intentional, or a mistake?

It looks like you are jumping into defining a complex array without understanding the basics of numpy array creation and indexing. Or are you coming from a MATLAB world where everything is 2d (or more)? In numpy arrays can be 1d.

Sign up to request clarification or add additional context in comments.

2 Comments

HAHA you caught me. I am coming from MATLAB. I am doing my Masters in Structural Engineering and we use MATLAB for all academic work but I have been told by many people that firms don't use MATLAB to much because of the price. I am trying to convert my MATLAB code to Python. The variables E, A, I, and L are meant to be 1xn matrices that correspond to individual elements. The function will go in a while loop and create a 3D matrix (6x6xnum_of_elements).
@JustinKordas you can use np.r_ to emulate MATLAB's [] array construction syntax, e.g. np.r_[A, 0, 0, -A, 0, 0]. Also, read this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.