Reputation: 3575
I want to define a sum that contains derivatives of a function, where the summation index is the derivative order. Simple example:
x, i = symbols("x i")
f = Function("f")(x)
Sum(diff(f,x,i), [i,1,3])
However, this only returns a sum of zeros. I assume this is because it tries to differentiate f wrt x first, and then wrt i. Since f is not a function of i, it evaluates to zero before it is processed by the Sum function. What I want to happen is
diff(f,x,1)
diff(f,x,2)
diff(f,x,3)
etc.
Is there a way to make this work?
Upvotes: 2
Views: 339
Reputation: 32474
sympy.diff(f,x,i)
is equivalent to i
'th order derivative of f
only if i
is an integer. In your case it is a symbol.
Use instead the builtin sum()
along with a generator expression:
>>> sum(diff(f,x,j) for j in range(1,4))
Derivative(f(x), x) + Derivative(f(x), x, x) + Derivative(f(x), x, x, x)
Upvotes: 3