Re: Derivative of Dot[]

*To*: mathgroup at smc.vnet.net*Subject*: [mg91085] Re: Derivative of Dot[]*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Wed, 6 Aug 2008 05:02:18 -0400 (EDT)*Organization*: The Open University, Milton Keynes, UK*References*: <g791cq$9hc$1@smc.vnet.net>

Eitan Grinspun wrote: > I would like to compute the gradient F' of a scalar-valued function F > of a vector-valued argument, without knowing a-priori the dimensions > of the vector. > > I am having some trouble with a very simple case. > > Consider the following function: > > F[x_] := Dot[x,x] > > Evaluating this function works as expected: F[{1,2}] evaluates to 5. > > I differentiate this function w.r.t. its sole argument, F' evaluates > to 1.#1+#1.1& > > This is reasonable, and as expected. I would think that, since the The above expression make sense only for one dimensional vectors, say on the real line; in other words, a vector space where the unit vector is {1} and any vector {x} has only one component x. Also, keep in mind that Mathematica does not have the notion of "vector." Vectors, in the mathematical sense, are represented by one dimensional lists in Mathematica. (Note that there is no explicit notion of row or column vectors.) Moreover, Mathematica usually manipulates -- rewrites -- expressions at the syntactic level. For instance, exact -- infinite precision -- zero times a string returns zero, while multiplying floating-point zero by a string returns unevaluated. In[1]:= 0*Infinity During evaluation of In[1]:= \[Infinity]::indet: Indeterminate \ expression 0 \[Infinity] encountered. >> Out[1]= Indeterminate In[2]:= 0*"Infinity" Out[2]= 0 In[3]:= 0.*"Infinity" Out[3]= 0. "Infinity" > argument of Dot must be a list, the derivative of Dot would have been > designed to return something that is useful. While I presume that this > is the case, I have been unable to move ahead. > > I evaluate the derivative at {1,2}: F'[{1, 2}] returns 1.{1,2}+{1,2}.1 > > The Dot is not defined for scalar arguments, and therefore Simplify > does not reduce this further. I could of course program a rule so that > Dot[1,x_]->x, but my intent here is to understand why the derivative > of Dot was designed the way it was---presumably there is a reason, and > there is a proper way to make use of the derivative. > > Once I have the derivative, I should be able to contract it with a > (tangent) vector to obtain the (linearized) change the (scalar) > function value: > > F'[{1, 2}].{3,4} > > Alas, this returns (1.{1,2}+{1,2}.1).{3,4} which does not simplify > (even after Distribute) because Dot does not operate on scalar > arguments. > > I'd like some help in understanding how to use Derivative with Dot (it > was evidently designed to be used, or there would not be a rule built > in). You may want to write you own differentiating function such as the following: In[1]:= SetAttributes[myDiff, HoldFirst] myDiff[fun_[x_?VectorQ]] := Module[{v = Array[a, Length[x]]}, D[fun[v], {v}] /. Thread[v -> x]] f[x_?VectorQ] := Dot[x, x] myDiff[f[{1, 2}]] myDiff[f[{1, 2}]].{3, 4} g[y_] := 2 f[y] myDiff[g[{1, 2}]] myDiff[g[{1, 2}]].{3, 4} Out[4]= {2, 4} Out[5]= 22 Out[7]= {4, 8} Out[8]= 44 Regards, -- Jean-Marc

**Follow-Ups**:**Re: Re: Derivative of Dot[]***From:*"Jean-Marc Gulliet" <jeanmarc.gulliet@gmail.com>

**Re: Re: Derivative of Dot[]***From:*"Eitan Grinspun" <eitan@grinspun.com>

**Re: Running Notebooks inside a notebook:**

**Re: Please help me.....**

**Re: Derivative of Dot[]**

**Re: Re: Derivative of Dot[]**