If we override _eval_derivative on MatrixElement can we make it do the right thing?
Aaron Meurer On Mon, Apr 21, 2014 at 6:50 AM, Clifford Wolf <[email protected]> wrote: > Hello, > > On Thursday, August 22, 2013 6:23:36 PM UTC+2, Matthew wrote: >> >> Fortunately this issue does not descend from the larger issue and can be >> solved independently. See https://github.com/sympy/sympy/pull/2402 > > > I think setting MatrixElement._diff_wrt = True was a bad decision. > > https://github.com/sympy/sympy/issues/7421 > > > When running "expr.diff(wrt)", then the code in Derivative.__new__ that > handles _diff_wrt works under the assumption that the terms in expr are > either independent from wrt or contain an identical replica. Consider code > like the following: > > i,n = symbols('i,n') > w = MatrixSymbol('w', n, 1) > result = Sum(123*w[i, 0], (i, 0, n - 1)).diff(w[0,0]) > > Derivative.__new__ will replace w[0,0] with a dummy symbol and attempt to > also replace it in the expression. But in the expression we only have w[i,0] > and Expr.subs does not understand how w[i,0] relates to w[0,0], yielding > something like: > > result = Sum(123*w[i, 0], (i, 0, n - 1)).diff(_x1) > > > Therefore result is set to 0 instead of 123. > > I can understand the need for supporting differentiation with respect to > MatrixElements, but I think it can't be done with a simple Expr._diff_wrt > property. Instead we would need something like > MatrixElement._diff_subs_wrt(self, expr) that returns a tuple of the new > expression and the dummy variable, or None if differentiation is not > possible. Such a method could then check for terms in expr that might or > might not overlap with wrt in a way that can not be handled correctly at the > moment, or even rewrite the expression (for example unroll the case i==0 out > of the sum) to make differentiation possible. > > regards, > - clifford > > PS: I'm new to sympy (this is the first time I looked into the sympy source > code), so I don't really have a much insight yet. If what I say makes no > sense, it might be because I have no idea what I am talking about.. ;) > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/b18af010-e570-4182-a5a4-389b159d5213%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6L_5tJLBDOcoLS_PrDD0XsA%3DtPoiWo1fZSoau7QcAqUkA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
