2
$\begingroup$

I'm trying to define a module (in Mathematica) that receives a point $\mathbf{p}(\mathbf{x})\in\mathbb{R}^n$ (expressed in the coordinate system $\mathbf{x}$) and a coordinate transformation $\bar{\mathbf{x}}(\mathbf{x})$ as inputs, and returns $\mathbf{p}(\bar{\mathbf{x}})$ as output; i.e., the module will return the point $\mathbf{p}$ expressed in the new coordinate system $\bar{\mathbf{x}}$.

Example: If $\mathbf{p}=(-1,1)$ in cartesian coordinates ($\mathbf{x}=(x,y)$) and $\bar{\mathbf{x}}(\mathbf{x})=(\sqrt{x^2+y^2},\arctan_2(y,x))$ (i.e., $\bar{\mathbf{x}}=(r,\theta)$), then the expected output is $(\sqrt{2},3\pi/4)$. If $\mathbf{p}=(1,2,3)$ in cartesian coordinates and $\bar{\mathbf{x}}(\mathbf{x})=(y+x,y-x,z-1)$, then the expected output is $(3,1,2)$. I want the module to work with a general coordinate transformation and dimension.

I feel like I tried everything. The module should be extremely simple, it's just that the syntax confuses me. How do I receive a function as an input? If the module reads "$f(x,y)"$, how do I connect between $x$ and $y$ to the first and second coordinate of $\mathbf{p}$ respectively?

Thanks!

$\endgroup$
4
  • $\begingroup$ Welcome to the Mathematica Stack Exchange. As phrased, this is a math problem but if you would like to do it using the software called Mathematica then please take a look at this image. Best of luck. $\endgroup$
    – Syed
    Mar 30, 2022 at 8:15
  • $\begingroup$ I'm familiar with the function, but I wanna do it with a general coordinate transformation, not only polar. $\endgroup$
    – Amit Zach
    Mar 30, 2022 at 8:19
  • 1
    $\begingroup$ You could also just write a function? Which will work for any transformation depending on how you define it. transform[{x_, y_}] := {Sqrt[x^2 + y^2], ArcTan[x, y]}; p = {-1, 1}; transform[p] which gives {Sqrt[2], (3 Pi)/4} $\endgroup$
    – Nasser
    Mar 30, 2022 at 8:19
  • $\begingroup$ @Nasser I can but this is not what I am required to do. $\endgroup$
    – Amit Zach
    Mar 30, 2022 at 8:22

2 Answers 2

4
$\begingroup$
Clear["Global`*"]

coordTransform[pt_List, transform_] :=
 Module[{var = Variables[Level[transform, {-1}]], f},
  f = Function @@ {var, transform}; f @@ pt]

coordTransform[{-1, 1}, {Sqrt[x^2 + y^2], ArcTan[x, y]}]

(* {Sqrt[2], (3 π)/4} *)

coordTransform[{1, 2, 3}, {y + x, y - x, z - 1}]

(* {3, 1, 2} *)

EDIT: The original response assumed that the variables associated with pt where in canonical order (e.g., {x, y, z}}. The results would be wrong if they were not. To allow for arbitrarily ordered variables, an optional argument is necessary.

Clear["Global`*"]

coordTransform[pt_List, transform_, var_ : Automatic] := 
 Module[{v, f},
  v = If[var === Automatic, Variables[Level[transform, {-1}]], var];
  f = Function @@ {v, transform};
  f @@ pt]

For canonical variables the use and results are the same.

coordTransform[{1, 2, 3}, {y + x, y - x, z - 1}]

(* {3, 1, 2} *)

However, for non-canonical variables the order must be provided. For example, if pt represents {x, y, c} (non-canonical order) then

coordTransform[{1, 2, 3}, {y + x, y - x, c - 1}, {x, y, c}]

(* {3, 1, 2} *)
$\endgroup$
1
  • $\begingroup$ Thank you very much!! $\endgroup$
    – Amit Zach
    Mar 30, 2022 at 17:22
3
$\begingroup$

Is this what the OP desires?

Clear[coordtransfed]
coordtransfed[oldcoords_, transffunc_] := transffunc[oldcoords]
coordtransfed[{-1, 1}, ToPolarCoordinates]
coordtransfed[{1, 2, 3}, ({x, y, z} \[Function] {y + x, y - x, z - 1}) @@ # &]
{Sqrt[2], (3 \[Pi])/4}
{3, 1, 2}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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