Derivative of a pure function with SlotSequence - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-20T10:40:54Z https://mathematica.stackexchange.com/feeds/question/99439 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/99439 22 Derivative of a pure function with SlotSequence Kuba https://mathematica.stackexchange.com/users/5478 2015-11-14T11:04:52Z 2015-11-14T13:35:15Z <p>I can live with this but I can't figure out why the following is <code>0</code>:</p> <pre><code>Derivative[f[##] &amp;][x] </code></pre> <blockquote> <p>0</p> </blockquote> <p>From documentation for <code>Derivative</code>:</p> <blockquote> <p>[...] <strong>Whenever</strong> <code>Derivative[n][f]</code> is generated, the WL rewrites it as <code>D[f[#],{#,n}]&amp;</code>. [...]</p> </blockquote> <p>but </p> <pre><code>D[f[##] &amp;[#], {#, 1}] &amp;[x] </code></pre> <blockquote> <p>f'[x]</p> </blockquote> <p>So where is this <code>0</code> from?</p> https://mathematica.stackexchange.com/questions/99439/-/99441#99441 11 Answer by m_goldberg for Derivative of a pure function with SlotSequence m_goldberg https://mathematica.stackexchange.com/users/3066 2015-11-14T12:35:09Z 2015-11-14T12:35:09Z <p>Consider</p> <pre><code> Derivative[f[##] &amp;] // FullForm </code></pre> <blockquote> <p><code>Function</code></p> </blockquote> <pre><code> Function[x] </code></pre> <blockquote> <p><code>0</code></p> </blockquote> <p>So the snippet you quote from the docs might not be entirely accurate; might not be considering such an edge case as <code>##</code>.</p> <p>I believe that <code>Derivative</code> is looking for head <code>Slot</code> when detects a pure function goes on to rewrite it, so it ignores head <code>SlotSequence</code> -- i.e., treats it same as it would treat head <code>u</code> in </p> <pre><code> Derivative[f[u] &amp;][x] </code></pre> <blockquote> <p><code>0</code></p> </blockquote> https://mathematica.stackexchange.com/questions/99439/-/99448#99448 22 Answer by Michael E2 for Derivative of a pure function with SlotSequence Michael E2 https://mathematica.stackexchange.com/users/4999 2015-11-14T13:35:15Z 2015-11-14T13:35:15Z <p>I believe there are at least three cases treated separately by <code>Derivative</code>.</p> <p><em>1) A function defined by a <code>Symbol</code>.</em> This follows the the rule cited in the documentation.</p> <pre><code>g[x___] := f[x]; Derivative[g][x] // Trace (* { { g' , { g[#1] &lt;-- Here the rule is being applied , f[#1] } , f'[#1] &amp; } , (f'[#1] &amp;)[x] , f'[x] } *) </code></pre> <p><em>2) A function defined by <code>Function</code>, with explicit symbolic arguments.</em> This one cannot mimic <code>f[##] &amp;</code>, but it seems to be a special case not handled in the way explained in the documentation; rather, the body is differentiated directly.</p> <pre><code>Derivative[Function[{x}, f[x]]][x] // Trace (* { { Function[{x}, f[x]]' , Function[{x}, f'[x]] } &lt;-- Differentiates the body , Function[{x}, f'[x]][x] , f'[x]} *) </code></pre> <p><em>3) A "pure" <code>Function</code> (the OP's case).</em> This also is handled by direct differentiation of the body, with respect to <code>Slot</code>. In the OP's example, the expression does not (symbolically) depend on <code>Slot</code>, so its derivative is zero. Apparently, rewriting <code>SlotSequence</code> in terms of <code>Slot</code>, say, in accord with the number of arguments passed to <code>Derivative</code> was either rejected or not considered in the design of <code>Derivative</code>.</p> <pre><code>Derivative[f[##] &amp;][x] // Trace (* { { (f[##1] &amp;)' , 0 &amp; } &lt;-- Differentiates the body , (0 &amp;)[x] , 0} *) </code></pre> <p>The following is equivalent to my view of how <code>Derivative</code> works:</p> <pre><code>deriv[n__][f_] := f /. { HoldPattern[Function[body_]] :&gt; With[{dbody = D[body, Sequence @@ Transpose@ {Array[Slot, Length@{n}], {n}}]}, Function[dbody]], HoldPattern[Function[vars_List, body_]] /; Length[vars] == Length[{n}] :&gt; With[{dbody = D[body, Sequence @@ Transpose@ {vars, {n}}]}, Function[vars, dbody]], HoldPattern[ff_] :&gt; With[{vars = Array[Slot, Length@{n}]}, Evaluate@ D[ff @@ vars, Sequence @@ Transpose@ {vars, {n}}] &amp;]} deriv[g][x] deriv[Function[{x}, f[x]]][x] deriv[f[##] &amp;][x] (* Derivative[f][x] Derivative[f][x] 0 *) </code></pre>