4

Usually when people discuss getting the polar form of a vector $v$, they present the following two formulas:

$$\text{Magnitude}(v) = \sqrt{x^2 + y^2}$$

$$\text{Angle}(v) = \arctan \left(\frac{y}{x} \right)$$

$$ \text{ Where } \space v = \begin{bmatrix} x \\ y \end{bmatrix}$$

I believe that this formula for the angle is only partially true. I think a better and more complete formula for the angle should be:

$$ \text{Angle}(v) = \begin{cases} \arctan \left(\frac{y}{x} \right) &; \space x \gt 0 \\ \pi +\arctan \left(\frac{y}{x} \right) &; \space x \lt 0 \\ {\begin{cases} \operatorname{sign}(y) \frac{\pi}{2} &; y \neq 0 \\ \text{undefined} &; \space y = 0 \end{cases}} &; x = 0 \end{cases} $$

Is there some sort of way to simplify this or to better express this, or is this it?

Bernard
  • 175,478
  • 4
    The "atan2" function was devised to help with this. – Blue Jan 20 '19 at 14:59
  • 2
    BTW: You can see atan2 being used in the "Converting between polar and Cartesian coordinates" section of Wikipedia's "polar coordinate system" entry. Who are these people you've been talking-to who "usually" just use arctan? – Blue Jan 20 '19 at 15:06
  • @Blue I see what you mean. Although the definition, in the link to Wikipedia you sent me, matches up with my definition, it does not seem as compact as my definition. – Gustav Streicher Jan 20 '19 at 15:08
  • @Blue On stack exchange, on multiple websites when googling the same problem... these are the usual cases I am talking about. People almost never use this arctan2 definition you are talking about. – Gustav Streicher Jan 20 '19 at 15:09
  • @Blue I have even come across the same partial definition in the textbooks assigned by my university and in lecture slides from Lecturers at other universities. – Gustav Streicher Jan 20 '19 at 15:11
  • 1
    That's unfortunate. :/ – Blue Jan 20 '19 at 15:13
  • 1
    @Blue Thank you for your sympathy. :) – Gustav Streicher Jan 20 '19 at 15:16
  • 3
    I'll note that the computer algebra Mathematica uses a "smart" ArcTan function: when fed just one argument, it gives the regular inverse-tangent; when given both $x$ and $y$ arguments, it gives the quadrant-savvy version of the angle (equivalent to atan2). – Blue Jan 20 '19 at 15:20
  • After following the links in these comments, I see I could have saved myself some effort in writing my answer. (I basically rederived the atan2 formula given in Wikipedia. Note that it is not necessarily a merit of a formula that it can be written in fewer lines, just as fewer lines of code do not always make a better software program. My criteria would be that the formula gives good answers and is easy to understand. – David K Jan 20 '19 at 17:09
  • @Gustav: For a compact formula, one can use simply $$\operatorname{sgn}{+}y,\cdot,\arccos\dfrac{x}{\sqrt{x^2+y^2}}$$ where $\operatorname{sgn}{+}$ is what I'll call the "positively-biased signum function", which returns $-1$ for negative arguments, and $+1$ for non-negative arguments. Note that the undefined-ness for $x=y=0$ is built-in. (This happens to be a slight refinement of a formula in the "Converting between polar and Cartesian coordinates" discussion on Wikipedia.) – Blue Jan 21 '19 at 02:39

2 Answers2

7

If you want an answer in form of a mathematical function definition, using only functions that were in common use in undergraduate instruction fifty years ago, I do not think you can do much better than the excellent answer by Rhys Hughes. As noted in the answer, this is how mathematicians often define a function equivalent to yours in textbooks. The only detail you might want to add is something to deal with the case $x = y = 0.$

Note that the formulas in that answer do not tell you how to find $\theta,$ but they do uniquely identify the output of the function for any possible input. There is always some value of $\theta$ that will satisfy both equations when $x^2 + y^2 \neq 0,$ and there will never be more than one value of $\theta$ that satisfies both equations.

If you want a formula to compute the angle using only functions that were in common use in undergraduate instruction fifty years ago, I think the formula you wrote is close to the best you can get, though I would handle one or two cases a bit differently.

If you want a nice way to represent your function in other formulas, you can borrow the two-parameter arc tangent function that is defined in many software packages. That is, define a function $\operatorname{atan2}(y, x)$ whose value is the angle of the vector $\begin{bmatrix} x \\ y \end{bmatrix}.$ You can define $\operatorname{atan2}(y, x)$ either in the style of the complex analysis textbooks described in the other answer, or you can define it in your style:

$$ \operatorname{atan2}(y, x) = \begin{cases} \arctan\left(\frac yx\right) & x \gt 0 \\ \arctan\left(\frac yx\right) + \pi \quad & x \lt 0, \ y \geq 0 \\ \arctan\left(\frac yx\right) - \pi \quad & x \lt 0, \ y < 0 \\ \frac\pi2 & x = 0,\ y > 0 \\ -\frac\pi2 & x = 0,\ y < 0 \\ \text{undefined} & x = 0,\ y = 0. \end{cases} $$

When this is implemented in software I think the "undefined" case usually returns $0.$

If you have to write formulas involving the direction angles of several two-dimensional vectors in terms of their components, then you might find the notation $\operatorname{atan2}(y, x)$ convenient.

David K
  • 98,388
  • @Gustav Wow! I mean wow squared. You complain that "they" leave a little bit for you to figure out instead of giving you exactly what you want on a silver platter, then you speculate this is because they are lazy? Wow. (This is an example of what they mean by "irony". If you visit the_internet.com you can apply for a refund of the monry you paid...) – David C. Ullrich Jan 20 '19 at 16:29
  • @Gustav Not frustrated at all, more amused. – David C. Ullrich Jan 20 '19 at 16:36
  • @Gustav I understood the question. And I have no problem with anything you said about the answer - my comment had to do with you complaining about him being lazy. (Do you know what the word "irony" means?) – David C. Ullrich Jan 20 '19 at 16:45
  • @David Thank you for pointing this out to me. It seems I have misunderstood Rhys's answer. I think it would be best if we deleted these comments as they have served their purpose in clearing out the misunderstanding we have had. I will then proceed to accept your answer. – Gustav Streicher Jan 20 '19 at 17:05
  • 1
    No problem; I've deleted earlier comments. – David K Jan 20 '19 at 17:12
  • @David K. Thank you. – Gustav Streicher Jan 20 '19 at 17:20
  • 1
    @David C. Ullrich. Could you also remove your previous comments as they no longer serve any purpose? – Gustav Streicher Jan 20 '19 at 17:20
4

This looks very similar to complex numbers, where $\text{Magnitude}(v)$ is displayed by $|v|$ and $\text{Angle}(v)$ is written $\text{arg}(v)$. We have that $\arg(v)$ is the unique angle $\in (-\pi, \pi]$ where:

$$\cos\theta =\frac{x}{|v|}=\frac{x}{\sqrt{x^2+y^2}}$$ and $$\sin\theta=\frac{y}{|v|}=\frac{y}{\sqrt{x^2+y^2}}$$

Rhys Hughes
  • 12,842
  • When working with only arctan, if we look at its graph, we can clearly see that it only gives values in the interval [$\frac{-\pi}{2}$ , $\frac{\pi}{2}$]. This only covers the first and fourth quadrants. So, in order to access the second and third quadrants, we need another definition. On top of that we need a definition for when the denomination in the usual definition would be zero, etc. – Gustav Streicher Jan 20 '19 at 15:22
  • 1
    When the denominator equals to zero, the 2-norm or magnitude of the vector is 0 anyway. In other words, the 0 vector has no angle (Linear Algebra tells us that 0 is in the null space of any system fully described by the $R^2$). – Joel Biffin Jan 20 '19 at 15:52
  • @Joel When the denominator is zero, only one of the components is zero, not both. So no, the magnitude is not zero. – Gustav Streicher Jan 20 '19 at 15:58
  • @Gustav you literally called $\sqrt{x^2+y^2}$ the magnitude in your question! – Rhys Hughes Jan 20 '19 at 16:01
  • 2 comments back, I accidentally wrote denomination instead of denominator. With this I mean the value of x. Sorry about that. – Gustav Streicher Jan 20 '19 at 16:03
  • @Rhys And your point is? We are discussing the angle, not the magnitude? – Gustav Streicher Jan 20 '19 at 16:06
  • @Rhys If x is zero, but y is not, then the magnitude is |y| , not zero. – Gustav Streicher Jan 20 '19 at 16:12
  • When $x=0$ we get $\cos \theta = 0$ and $\sin\theta= \pm 1$. It's easy to see $\theta=\pm \frac \pi2$, dependent on whether $y$ is positive or negative. – Rhys Hughes Jan 20 '19 at 16:13
  • My definition includes that. That's what I was referring to from the beginning. I'm not the one with the misunderstanding here. – Gustav Streicher Jan 20 '19 at 16:44
  • I think it's clear by now that this answer is based on a misunderstanding of what you want. Whether it was a misunderstanding of what you asked is another matter, which could have been avoided if the question had said something like, "Please don't give me some kind of abstract mathematical definition; I want an algorithm that I can follow step by step." (A perfectly reasonable request!) But that's easy to say in hindsight. – David K Jan 20 '19 at 17:00
  • @David I agree. I guess I could have included that in my question. – Gustav Streicher Jan 20 '19 at 17:09
  • @Rhys In an earlier comment, which I have deleted, I stated that your answer does not solve the problem. I have realized, with the help of David K, that I was mistaken. Thank you for your answer. – Gustav Streicher Jan 20 '19 at 17:12