# Convert polar to cartesian coordinates without built in commands?

**URL:** <https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379>\
**Category:** Questions\
**Created:** [October 6, 2016, 5:49pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379 "2016-10-06T17:49:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 6, 2016, 5:49pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/1 "2016-10-06T17:49:02Z")

</div>

First I just want to say I am NOT going to use the built in command FromPolarCoordinates, I am attempting to write my own function that will do the same thing..

I currently am able to convert between one pair of polar coordinates and Cartesian with the following code and usage of replacement rules:

```
Clear[blah],
blah[r_,theta_]:= {r,theta}/. {r -> r*Cos[theta], theta -> r*Sin[theta]};
blah[1,pi/4]

```

which produces

```
{1/Sqrt[2], 1/Sqrt[2]}

```

But what if I wanted to enter a list into my blah function, such as blah[{1,pi/4},{pi/4,pi/4}] and so on…?  
I was thinking the “Switch” command might come in handy

Thanks,

B

---

<div class="post-metadata">

**Author:** ![mhelmle](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mhelmle](https://community.wolfram.com/u/mhelmle)\
**Post date:** [October 6, 2016, 6:23pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/2 "2016-10-06T18:23:22Z")

</div>

Hello Brandon,

you could do it this way:

```
In[1]:= blah[polar_] := polar[[1]] {Cos[polar[[2]]], Sin[polar[[2]]]}
blah[polar_] := Map[blah[#] &, polar] /; MatrixQ[polar]

In[3]:= lst = RandomReal[5, {10, 2}];

In[4]:= blah[lst]

Out[4]= {{-1.33346, -1.73984}, {-4.74456, -1.01628}, {1.07804, 
  0.471759}, {2.08963, 3.48567}, {2.89406, 2.14102}, {3.02783, 
  0.775746}, {0.019113, 0.0156172}, {0.0448616, -0.754736}, {2.85285, 
  1.6724}, {1.62422, 1.72386}}

In[5]:= blah[{1., 0.5}]

Out[5]= {0.877583, 0.479426}

```

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 6, 2016, 8:56pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/3 "2016-10-06T20:56:17Z")

</div>

Hey Michael,

This is a cool way to go about it. I was trying something along the lines of:

```
Clear[blah,bleh],
blah[r_,theta_]:= {r,theta}/. {r -> r*Cos[theta], theta -> r*Sin[theta]};
bleh[list_]:=list /. List -> blah[r,Theta];
bleh[{1,pi/4},{pi/4,pi/4}]

```

Obviously this is wrong because all I get as an output is just bleh[{1,pi/4},{pi/4,pi/4}] but I feel like I am somewhat in the right direction. I tried to run your code but you have “polar” defined twice so there was an error.

But anyways, I appreciate your help!

B

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [October 7, 2016, 9:08am UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/4 "2016-10-07T09:08:20Z")

</div>

One way would be this:

```
Clear[blah];
blah[r_ /; Not[Head[r] === List], 
   theta_] := {r, theta} /. {r -> r*Cos[theta], theta -> r*Sin[theta]};
blah[pts___List] := blah @@@ {pts}

```

so that `blah[{r1, theta1}, {r2, theta2}]` gives the list you want.

---

<div class="post-metadata">

**Author:** ![mhelmle](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mhelmle](https://community.wolfram.com/u/mhelmle)\
**Post date:** [October 7, 2016, 5:38pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/5 "2016-10-07T17:38:11Z")

</div>

Hey Brandon,

the code I supplied works perfectly for me. I have defined blah[..] twice, but one definition is conditional (/; MatrixQ[..]). This is similiar to functional overloading in C++. I am running M11.0.1 on Windows10.

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [October 7, 2016, 8:32pm UTC](https://community.wolfram.com/t/convert-polar-to-cartesian-coordinates-without-built-in-commands/9379/6 "2016-10-07T20:32:05Z")

</div>

Hey Gianluca,

This code works great but the second output doesn’t evaluate correctly.

Thanks for your input!!

B
