So I've just started using Mathematica and my first task was to compute the gradient for an analytic cost function (to be optimized elsewhere). Part of the cost function is this:
dist[p_] := Norm[d d.p - p]
Which just computes the distance of a 3D point to a ray (centered at the orgin, with a (unit) direction d). I'd like to compute the gradient for this, so I do this:
Grad[dist[{x, y, z}], {x, y, z}]
This produces a correct expression, but it's super messy (won't even paste it here, it's just a huge barf of scalar expressions). Even with FullSimplify it never really gets any more manageable. What I ended up doing is manually simplifying this to a nice clean expression along the lines of
gradient = (1 - 2 d d\[Transpose] + (d d\[Transpose])^2) p
Which is much nicer, but was pretty painful/error prone. So my question is, what am I doing wrong to make the Mathematica output so much worse? Presumably it's something to do with passing in {x,y,z} to Grad, but I wasn't sure how to give the whole vector a name (like 'p') while also producing a 3-element gradient. Anyone have any hints for my workflow here so that next time I can get a prettier expression from the start?