The function CoordinateTransformData in Mathematica only works for three dimension. For example,

CoordinateTransformData["Cartesian" -> "Spherical", "Mapping", {1, 2, 3, 4}]

CoordinateTransformData::dimp: Evaluation point {1,2,3,4} has dimension 4, which does not match dimension specification 3. >>

Can anybody provide an efficient version of CoordinateTransformData that will work for any dimension?

  • $\begingroup$ Since you were asking about an efficient way, my hunch is that if you use a pure function (which is returned by the form CoordinateTransformData["Cartesian" -> "Spherical", "Mapping"]), it'll be easier to write fast code because pure functions can be auto-compiled in many situations. But it would all depend on how precisely the transformation would be used. The best thing would be to manually Compile is as a listable function. $\endgroup$
    – Szabolcs
    Feb 27 '14 at 15:02

CoordinateTransformData is just a big encyclopaedia of various transformations. You can think of it as one of those handbooks for integrals and special functions, except this is specialized for transformations. Its purpose is not efficiency but simply having the formulae at hand, so you don't have to reach for a handbook or derive them yourself.

You can list all available entries by evaluating


You'll see that there are various transformations for various dimensional spaces (and geometries). For each of them you can retrieve several properties, "Mapping" being just one.

For 2D, we have "Polar":

CoordinateTransformData["Cartesian" -> "Polar", "Mapping", {x, y}]

(* ==> {Sqrt[x^2 + y^2], ArcTan[x, y]} *)

For 3D, we have "Spherical":

CoordinateTransformData["Cartesian" -> "Spherical", "Mapping", {x, y, z}]

(* ==> {Sqrt[x^2 + y^2 + z^2], ArcTan[z, Sqrt[x^2 + y^2]], ArcTan[x, y]} *)

For 4D, we have "Hyperspherical":

CoordinateTransformData["Cartesian" -> "Hyperspherical", "Mapping", {x, y, z, w}]

(* ==> {Sqrt[w^2 + x^2 + y^2 + z^2], ArcCos[x/Sqrt[w^2 + x^2 + y^2 + z^2]], ArcCos[y/Sqrt[w^2 + y^2 + z^2]], ArcTan[z, w]} *)

And lots of others.

Choose the one you need.

Do make sure to check the documentation which describes this in more detail!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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