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!!!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.