# How to input a 3-vector in spherical coordinates?

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?

-
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

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:

-

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}  *)

-