Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to input a 3-vectors in spherical coordinates $(r, \theta, \phi)$ and be able to operate on such vectors (dot and cross product) with the results being given in the same spherical coordinate system.

Is there an easy way to achieve this in Mathematica?

share|improve this question
4  
I suggest you look at this tutorial. –  m_goldberg Sep 17 '13 at 12:36
    
Any code you are working on ? –  Sektor Sep 17 '13 at 13:04
    
Thanks @m_goldberg, this helps although it seems that this functionality is new in Mathematica 9, right? –  The Dude Sep 17 '13 at 13:12
    
Yes. Is that a problem for you? –  m_goldberg Sep 17 '13 at 13:32
    
Unfortunately yes but I hope I'll be able to upgrade to version 9 soon. Is there some external similar package for version 8? Thanks anyway @m_goldberg. –  The Dude Sep 17 '13 at 13:49
show 2 more comments

2 Answers

In older versions of Mathematica, the standard package CalculusVectorAnalysisdid the trick, I believe.

Using

SetCoordinates[Spherical]

you entered a new world where

DotProduct[v1,v2], 
CrossProduct[v1,v2] and 
ScalarTripleProduct[v1,v2,v3]

are computed with the selected coordinate system. The computation is performed by MMA converting into Cartesian system and then converting back. You are bound to use those specialized function, but I believe it is still possibile (albeit non advisable) to unprotect the more general Dot and Cross operators to make them work in the new way. Problem is: what happens to the rest of the code that uses them? You might be better off with defining a new shorthand form for the above functions.

[Edit, oh, there were two more comments below, one of which with the link to the documentation, posted way before mine.]


Reference for mma8:

http://reference.wolfram.com/legacy/v8/VectorAnalysis/ref/SetCoordinates.html http://reference.wolfram.com/legacy/v8/VectorAnalysis/tutorial/VectorAnalysis.html

share|improve this answer
add comment

One possible workaround would be to first transform the vectors into Cartesian coordinates, then use standard operations and afterwards to transform them back to spherical ones, if needed. Something like the following.

These two functions make the transformations: toCart - from spherical to Cartesian, while toSph - from Cartesian to spherical:

toCart[a_List] := {a[[1]]*Sin[a[[2]]]*Cos[a[[3]]], 
   a[[1]]*Sin[a[[2]]]*Sin[a[[3]]], a[[1]]*Cos[a[[2]]]}; 

 toSph[r_List] := {Sqrt[r[[1]]^2 + r[[2]]^2 + r[[3]]^2], 
   ArcCos[r[[3]]/Sqrt[r[[1]]^2 + r[[2]]^2 + r[[3]]^2]], 
   ArcSin[r[[2]]/Sqrt[r[[1]]^2 + r[[2]]^2]]};

Consider, for example, two vectors u and v given in spherical coordinates:

u = {1., \[Pi]/6, \[Pi]/4};
v = {2., \[Pi]/3, \[Pi]/2};

Let us transform them into Cartesian ones and call their Cartesian versions u1 and v1

u1 = toCart[u]
v1 = toCart[v]

(*  {0.353553, 0.353553, 0.866025}  *)
(*  {0., 1.73205, 1.}               *)

Now here is their scalar product:

u1.v1

1.4784

And this is the cross product transformed into the spherical coordinates:

Cross[u1, v1] // toSph

(*  {1.34697, 1.09884, -0.299137}  *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.