Group Abstract Group Abstract

Message Boards Message Boards

0
|
3K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Best practices while working with vectors?

Posted 9 years ago

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?

POSTED BY: Sebastian Sylvan
4 Replies

No dice, that's still pretty "scalar" looking and messy. Is this as good as it gets maybe? It's a bit surprising to me that it doesn't do a better job here (or alternatively, that the "correct" way of doing this is so hard to figure out).

POSTED BY: Sebastian Sylvan

Perhaps you get a less horrid formula if you give the variable d the form of a 3-vector:

d = {d1, d2, d3};
dist[p_] := Norm[d d.p - p] /. Abs -> Identity
FullSimplify[Grad[dist[{x, y, z}], {x, y, z}]]

It seems that Simplify does not act on the assumption that d is a vector in the following case:

Simplify[d.{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, Element[d, Vectors[3]]]

I would expect to get d as answer.

POSTED BY: Gianluca Gorni

That make some sense, but I can't just get rid of the square root - it's part of the actual problem I'm solving (this feeds into a gauss newton optimzer).

Is there an easy way to tell Mathematica to just treat things as real? Ive tried using Refine and Simplify (both in the dist function and wrapped "around" the Grad call) with assumptions but it makes no difference (e.g. x [Element] Reals). I also tried putting the funky "/. Abs -> Simplify" just to get rid of the abs, but without squaring the norm and that at least got rid of all the redundant "Abs" stuff, but presumably the simplifier still doesn't know that this stuff is Real so the output is still kind of horrid:

dist[p_] :=  Norm[d d.p - p] /. Abs -> Identity
FullSimplify[Grad[dist[{x, y, z}], {x, y, z}]]
Out={(x - d d.{x, y, z} - 
  d (x + y + z - 
     3 d d.{x, y, z}) d.{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})/Sqrt[
 x^2 + y^2 + z^2 + 
  d d.{x, y, z} (-2 (x + y + z) + 3 d d.{x, y, z})], (
 y - d d.{x, y, z} - 
  d (x + y + z - 
     3 d d.{x, y, z}) d.{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})/Sqrt[
 x^2 + y^2 + z^2 + 
  d d.{x, y, z} (-2 (x + y + z) + 3 d d.{x, y, z})], (
 z - d d.{x, y, z} - 
  d (x + y + z - 
     3 d d.{x, y, z}) d.{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})/Sqrt[
 x^2 + y^2 + z^2 + d d.{x, y, z} (-2 (x + y + z) + 3 d d.{x, y, z})]}

Any ideas? It just seems like I'm missing something.

POSTED BY: Sebastian Sylvan

Part of the complication may be that by default Norm assumes the vector to be complex, so that the norm will contain a lot of Abs[], which will mess up the gradients. If your vectors are real, you get simpler expression by replacing Abs with Identity, and using the square distance instead of the distance:

d = {1, 1, 1};
distSq[p_] := Norm[d d.p - p]^2 /. Abs -> Identity;
Grad[distSq[{x, y, z}], {x, y, z}] // Simplify
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard