On Friday, April 25, 2014 11:19:15 PM UTC+2, Aaron Meurer wrote:
>
> > I preserved the current behavior of the other classes with
> _diff_wrt=True,
> > but I think the is room for improvement there as well. For example
> should
> > the following code really return "x"?
>
> Yes it should. See the docstring of Derivative.
>
That docstring documents that it is this way, but it does not give any
reason why it _should_ be this way. For example, the docstring states that
>>> (2*sqrt(1 - sin(x)**2)).diff(cos(x))
0
but it does not really state why this would be a good idea. Its more like a
warning, telling my to avoid such expressions. Now consider the following
code for Function._eval_derivative_wrt:
def _eval_derivative_wrt(self, expr, new_name):
new_self = Dummy(new_name)
new_expr = expr.subs(self, new_self)
if new_expr.free_symbols.isdisjoint(self.free_symbols):
return (new_expr, new_self)
return None
This will detect such cases and not attempt to evaluate a derivative wrt
cos(x) if the expression contains something like sin(x):
>>> from sympy import cos, sin, sqrt
>>> from sympy.abc import x
>>> (2*cos(x)).diff(cos(x))
2
>>> (2*sqrt(1 - sin(x)**2)).diff(cos(x))
Derivative(2*sqrt(-sin(x)**2 + 1), cos(x))
Afics the legit use cases for differentiating wrt a function are those
where you actually get the right result. And my understanding of the
documentation is that atm it is my responsibility as user to avoid the
other cases. I think we should shift as much of this responsibility as
possible from the user to SymPy.
PS: There may be cases where one would prefer a blind substitution, but I
think that this should not be the default behavior of diff(), as it will
return wrong results in some cases and therefore is not really a plain
derivative anymore.
--
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/f15e5572-06b8-452d-9201-00a7f1a660b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.