I am investigating the suitability of SymPy for some of my projects and have come across an issue with the interaction between lambdify and IndexedBase.
In short, my applications make heavy use of functions that employ doubled summed array structures. I need to be able to compute both the function and the first through 3rd derivative of the function with respect to an array element.
My questions are thus:
- How do I make lambdify work with diff?
- How do I make a lambdify function where I can specify the index with respect to which I want the derivative?
- How do I extend the above to a second order derivative with a different index (i.e. second order with respect to index i and j)?
Simplified example:
from sympy import IndexedBase, Idx, lambdify, Sum, diff
from numpy import array
i = Idx("i", range=(0,1))
j = Idx("j", range=(0,1))
n = IndexedBase("n")
coefficients = IndexedBase("C")
double_sum = Sum(Sum(n[i]*n[j]*coefficients[i,j],(i,i.lower,i.upper)),(j,j.lower,j.upper))
first_derivative = diff(double_sum, n[i])
second_derivative = diff(first_derivative, n[j])
test_function_1 = lambdify((n,coefficients),double_sum)
test_function_2 = lambdify((n,coefficients,i),first_derivative)
test_function_3 = lambdify((n,coefficients,i,j),second_derivative)
test_vector = array([1, 2])
test_coefficients = array([[1,1],[2,3]])
test_value_1 = test_function_1(test_vector,test_coefficients)
print(test_value_1)
test_value_2 = test_function_2(test_vector,test_coefficients,1)
print(test_value_2)
test_value_3 = test_function_3(test_vector,test_coefficients)
print(test_value_3)
Execution of this code yields the error:
File "<lambdifygenerated-2>", line 9, in _lambdifygenerated
File "<lambdifygenerated-2>", line 9, in <genexpr>
NameError: name 'KroneckerDelta' is not defined