I want to create a function that is modified with a loop and then I can call those different functions from a function vector. The function to be modified would be the Am function.
The problem is that it only uses the last value of the loop, i=1
.
So I have two equal functions, with i=1
.
What I want is to have an Am function with i=0
, and another with i=1
.
Then you can call them from the vector Afunctions
Afunctions = []
def Am(x):
if((x>xn[i])and(x<=xn[i 1])):
return (x-xn[i])/h1
elif((x>xn[i 1])and(x<xn[i 2])):
return (1-(x-xn[i 1])/h1)
else:
return 0
for i in range(0,2,1):
Afunctions.append(Am)
CodePudding user response:
I'm making a big assumption about your intent, namely, that you want Afunctions
to be a list of callables associated with Am
where the value of i
is the same as callable index in Afunctions
. If that is a correct assumption than the following code should work:
from functools import partial
Afunctions = []
def Am(x, i):
if((x>xn[i])and(x<=xn[i 1])):
return (x-xn[i])/h1
elif((x>xn[i 1])and(x<xn[i 2])):
return (1-(x-xn[i 1])/h1)
else:
return 0
for i in range(0,2,1):
Afunctions.append(partial(Am, i=i))
Afunctions
Output
[functools.partial(<function Am at 0x000001D1F6157920>, i=0),
functools.partial(<function Am at 0x000001D1F6157920>, i=1)]