Search code examples
wolfram-mathematicadifferentiation

How can I get mathematica to use the chain rule?


I want to teach mathematica that Subscript[w, i] differentiated by Subscript[w, j] is KroneckerDelta[i, j].

I tried

Unprotect[D]; D[Subscript[x_, i_], Subscript[x_, j_]] := 
                                            KroneckerDelta[i, j]; Protect[D]

And this works with D[Subscript[w, i], Subscript[w, j]], but not with more complex expressions, e.g. D[Times[k, Subscript[w, i]], Subscript[w, j]]

I understand from the answer to this question: How to define a function that commutes with D in Mathematica that mathematica isn't matching my rule, but I don't understand why. Why does Mathematica not use the product rule, and then invoke my rule?


Solution

  • I think I see now what is happening.

    Mathematica does not recursively define the D operator using the chain rule, presumably because this is too slow. It does some pattern matching on subexpressions to see if they contain the variable of differentiation, and subexpressions which don't are treated as constants; so my pattern for D is never applied.

    The way around this turns out to be to tell Mathematica explicitly that

    Subscript[w, i]
    

    is not a constant.

    My pattern now looks like this

    Unprotect[D]; 
    D[Subscript[x_, i_], Subscript[x_, j_], 
        NonConstants -> {___, Subscript[x_, i_], ___} ] := KroneckerDelta[i, j]; 
    Protect[D]   
    

    And I have to apply it this way:

    D[k * Subscript[w, i], Subscript[w, j], NonConstants -> Subscript[w, i]]