Search code examples
pythonsympyabstractiondifferentiation

sympy - differentiation over an abstract component


I'm working differentiating in sympy, thanks to this answer I'm almost done, but not quite.

I have the following code in variations:

  1. From the answer
x = IndexedBase('x')
alpha, beta, gamma = symbols('alpha beta gamma', integer=True)
r = sqrt(x[alpha]**2 + x[beta]**2 + x[gamma]**2)
T0 = 1/r
i,j,k,l = symbols('i j k l')
T1 = diff(T0, x[i])
T1.subs(sqrt(x[alpha]**2 + x[beta]**2 + x[gamma]**2), 'r')
  1. Using Vector class
V = CoordSys3D('V')
v = x[alpha]*R.i + x[beta]*R.j + x[gamma]*R.k
r = v.magnitude()
T0 = 1/r
T1 = diff(T0, x[i])
T1.subs(sqrt(x[alpha]**2 + x[beta]**2 + x[gamma]**2), 'r')

Both give the following answer:

However, this has a lot of unwanted delta functions, which only multiply in number after differentiating to higher orders.

Here, alpha beta gamma are just Cartesian components of a vector, and r is its length. Knowing that, of course, those delta functions can never be simultaneously 1, I want to achieve this result:, where i is some of Cartesian components.

Is this possible?

Thanks!


Solution

  • Managed to do it!

    That's the code sample:

    x = IndexedBase('x')
    alpha, beta, gamma, delta = symbols('alpha beta gamma delta', cls=Idx, range=3)
    i = Idx('i', 3)
    x_i = x[i]
    r = sqrt(Sum(x_i**2, i))
    T0 = 1/r
    T1 = diff(T0, x[alpha])
    T1.simplify().subs(sqrt(Sum(x_i**2, i).doit()), 'r')
    

    Outputs: -x[alpha]/r**3