recursion - Creating recursively a function gaining variables each iteration in Python -
i want create function foo()
gives output function. output function sum of exponentials (or whatever) free parameter ai each time. output function should have parameters ai plus free variable.
example:
calling foo(3)
should output function equivalent lambda t,a1,a2,a3: exp(a1*t)+exp(a2*t)+exp(a3*t)
i tried programm that:
def foo(n): fh = lambda t,a1:exp(a1*t) def fg(j): if (j==1): return fh else: return lambda t,a1,a2:fg(j-1)(t,a1)+fh(t,a2) return fg(n)
but wrong. remarked foo(n)
can obtained via recusion "doing" lambda t,a1,...,a(n): foo(n-1)(t,a1,...,a(n-1))+exp(a(n)*t)
iteration, didn't manage in simple way...
thanks !
you don't need recursion. can define function, takes arbitrary number of arguments:
def f(t, *a): return sum(exp(x * t) x in a)
Comments
Post a Comment