Alright, so this is what im trying to do.
Lets say my input into the function is (D,y) Y is how many elements in the array, so if Y is 3 the array is ([1,2,3]) if it's 51, the array is ([1,2,3....51]). so what im doing for this is
def okay(D,y):
T=np.arange(y)
the equation i want to apply to the array is the following
k= D*(T[k+1]-T[k])+D*(T[k-1]-T[k])
So, k is any index in the T array, D is the value specified at the start of the function.
So, what is basically happening, is that the new value of any element of the array after the operation is D*([the element in front of it]-[the element itself])+ D*([the elemeny before it]-[the element itself])
So, this generally seems simple enough. I was able to do it on a simple array by specifying each individual index
d=3
T=np.array([23,17,46,2])
T[1] = d*(T[2]-T[1])+d*(T[0]-T[1])
print(T)
However, my problems appear whenever i try and make a function that does this for every single element in the area. I cant figure out how to set the k aspect, such that the function understands that i mean any point in the array. So, if i have an array of 5000 values, it will do the operation on each one, using the index itself, and the one infront/behind it.
I tried using int and i and "for element in T" but it gives me never ending errors.
Secondly, the function should do a different equation for endpoints, since they dont have any value prior or after them:
change in T[0] = D*(T[1]-T[0])
change in T[L-1] = D*(T[L-2]-T[L-1])
I don't know how to incorporate these into the function.
Thanks for any help!
Are you intentionally changing T
as you go though it? So that one value depends on modified T
from the previous step? Or is each step independent?
Lets assume the latter, since it is simpler. In that case we, I think, calculate:
In [528]: T = np.arange(10)
In [529]: R = np.zeros(10)
In [533]: for i in range(10):
if i==0:
R[i]=3*(T[i+1]-T[i])
elif i==9:
R[i]=2*(T[i-1]-T[i])
else:
R[i]=3*(T[i+1]-T[i])+2*(T[i-1]-T[i])
.....:
In [534]: R
Out[534]: array([ 3., 1., 1., 1., 1., 1., 1., 1., 1., -2.])
the middle values aren't very interesting since T
is uniformly spaced; we'll live it that for now.
The middle values can be calculated all at one, using slices:
In [535]: 3*(T[2:]-T[1:-1])+2*(T[:-2]-T[1:-1])
Out[535]: array([1, 1, 1, 1, 1, 1, 1, 1])
In [536]: R[1:-1]=3*(T[2:]-T[1:-1])+2*(T[:-2]-T[1:-1])
In [537]: R
Out[537]: array([ 3., 1., 1., 1., 1., 1., 1., 1., 1., -2.])
Or we can get the end values as well with:
In [542]: R = np.zeros(10)
In [543]: R[:-1] += 3*(T[1:]-T[:-1])
In [544]: R[1:] += 2*(T[:-1]-T[1:])
In [545]: R
Out[545]: array([ 3., 1., 1., 1., 1., 1., 1., 1., 1., -2.])
Now if R[i-1]
enters into the calculation of R[i]
, we'll have to reconsider the vectorized calculations:
In [546]: R = T.copy()
In [547]: for i in range(10):
if i==0:
R[i]=3*(T[i+1]-T[i])
elif i==9:
R[i]=2*(R[i-1]-T[i])
else:
R[i]=3*(T[i+1]-T[i])+2*(R[i-1]-T[i])
.....:
In [548]: R
Out[548]: array([ 3, 7, 13, 23, 41, 75, 141, 271, 529, 1040])
There are some cumulative functions and unbuffered calculations that might be useful
np.cumsum()
np.add.at()
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.