atan2 does not accept complex numbers as input

Good Morning
If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field.
If so , It is possible to implement a complex version of the atan2() function ?
i mean in a way that i can compute atan2(a+i*b , c+i*d) , where a,b,c,d are real and i =sqrt(-1) ,
this both numerically and symbolically
thankyou :-)
Kind regards
Manu1965@wail.ch

4 comentarios

Stephen23
Stephen23 el 13 de Sept. de 2024
Editada: Stephen23 el 13 de Sept. de 2024
"If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field."
I am not aware of any common definition for this: do you have a reference?
"It is possible to implement a complex version of the atan2() function ?"
Hmmm, interesting question. In a quick search of various tools I could not find any such thing implemented.
This seems like an interesting extension to ATAN2 and a valid enhancement request:
Val
Val el 13 de Sept. de 2024
dear Stephen23
ArcTan[u,v] = -i*log((u+i*v)/(sqrt(u^2+v^2)))
(​​I checked in the case of real input values ​​and the result matches Matlab's atan2() )
But I've only seen this expression on WolframAlpha and unfortunately I haven't found any reference in the literature...
hence my initial question
Stephen23
Stephen23 el 13 de Sept. de 2024
Editada: Stephen23 el 13 de Sept. de 2024
I am not sure what the meaning or expected output for complex inputs might be, but for real inputs at least the formula seems to give the same output as ATAN2:
v = 1;
u = 3;
atan2(v,u)
ans = 0.3218
-1i*log((u+1i*v)./sqrt(u.^2+v.^2))
ans = 0.3218 + 0.0000i
V = -10:10;
U = V(:);
A = atan2(V,U);
B = -1i*log((U+1i*V)./sqrt(U.^2+V.^2));
surf(A)
max(abs(imag(B(:)))) % only floating point noise
ans = 1.5266e-16
surf(real(B))
Do you have any complex test values with known outputs?
If that formula works for you, then perhaps you can save it in a function and use it.
Val
Val el 13 de Sept. de 2024
Dear Stephen23
many thanks for the interest

Iniciar sesión para comentar.

 Respuesta aceptada

John D'Errico
John D'Errico el 13 de Sept. de 2024
Editada: John D'Errico el 13 de Sept. de 2024
It seems the answers provided so far are missing the point. If we want to see a 4 quadrant atan2 version, that works for complex X and Y, can we use what Alpha suggests? Thus
atan2(U,V) = -i*log(U+i*V)./sqrt(U.^2 + V.^2)
I'll modify what @Stephen23 did to see what happens, for complex U and V. A serious problem is I would need to have a hyper-dimensional monitor to show the complete behavior. (My own personal holodeck is broken, awaiting parts. But shipping is very slow, coming from the future.) I would need to display essentially 6 dimensional behavior, and a regular monitor is just not up to snuff there. Nor are my eyes or my brain. So I can only do my best to look at what happens.
fun = @(V,U) -1i*log((U+1i*V)./sqrt(U.^2+V.^2));
First, what happens for real U and V?
utest = randn(3);vtest = randn(3);
fun(vtest,utest)
ans =
1.8569 - 0.0000i 1.0516 + 0.0000i 1.4416 - 0.0000i -0.0848 + 0.0000i -2.1910 + 0.0000i 2.0958 + 0.0000i -2.7649 - 0.0000i 1.7415 + 0.0000i 1.0877 - 0.0000i
And that is clearly a problem, since for real U and V, we want the result to return a purely real result. We need to patch it for that case.
function Z = atan2alt(V,U)
% extension of atan2(V,U) into the complex plane
Z = -1i*log((U+1i*V)./sqrt(U.^2+V.^2));
% check for purely real input. if so, zero out the imaginary part.
realInputs = (imag(U) == 0) & (imag(V) == 0);
Z(realInputs) = real(Z(realInputs));
end
atan2alt(vtest,utest)
ans = 3×3
1.8569 1.0516 1.4416 -0.0848 -2.1910 2.0958 -2.7649 1.7415 1.0877
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
so atan2alt now returns purely real output for purely real inputs. Next, is it consistent with atan2?
norm(atan2(vtest,utest) - atan2alt(vtest,utest))
ans = 2.2204e-16
And that is (as we sometimes said in the deep past) good enough for government work. The difference is down into the realm of floating point trash. Now I am at least a little happy, since the two are consistent for real input.
Next is this a 4 quadrant result? Again, that will be difficult to show in detail. But perhaps we can do a little, by plotting the result along a simple path in the complex plane.
V = (-10:10);
U = V(:);
v = V - 0.1i;
u = U + 0.2i;
So u and v both now each live along lines in the complex plane, where the imaginary part is constant, and non-zero.
Z = atan2alt(v,u);
And of course, since atan2alt will now return a complex result, we need to look at both the real and imaginary part.
surf(U,V,real(Z))
xlabel "Real(U)"
ylabel "Real(V)"
zlabel "Real(Z)"
surf(U,V,imag(Z))
xlabel "Real(U)"
ylabel "Real(V)"
zlabel "imag(Z)"
The result is a 4 quadrant version. Here we see the imaginary component is indeed non-zero.
surf(U,V,abs(Z))
xlabel "Real(U)"
ylabel "Real(V)"
zlabel "abs(Z)"
Of course, this would need a great deal more thought, to see if there were additional problems other than the need for a real result. What happens when one or both of the arguments are zero? Again, unless the two are consistent, we have a problem.
atan2([0 1],[0;1])
ans = 2×2
0 1.5708 0 0.7854
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
atan2alt([0 1],[0;1])
ans = 2×2
0 1.5708 0 0.7854
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Hey, that worked. I was actually just a little surprised. I was going to guess the code would fail when U==V==0, and perhaps return a NaN. But as it turns out, the result from the formula is then 0+infi, with an infinite imaginary part. But since it has a zero real part, the check at the end of atan2alt saved me.
I noticed that @M A wanted to see it work also for symbolic inputs. Again, I was wondering for just a second if it would survive here, but it did.
syms u v
atan2alt(v,u)
Some might suggest plots I generated do not form a very strong test, since they presumed constant imaginary part. And, while it is not too difficult to conjure up a pair of paths for U and V that are more complicated, this seems a good point to stop. 'nuff said.

5 comentarios

Stephen23
Stephen23 el 13 de Sept. de 2024
"a hyper-dimensional monitor"
I have wanted one of those for years.
John D'Errico
John D'Errico el 13 de Sept. de 2024
You can have mine, as long as you can get parts. The parts for mine are on backorder (ok, I supose the proper term is forward-order?) and will not show up for a long time. Does Fed-Ex deliver from the 23rd century? Do I really want the Borg to deliver those parts instead?
Val
Val el 13 de Sept. de 2024
Dear D'Errico
Many thanks for your time spent on this matter and testing the W.Alpha formula
Val
Val el 13 de Sept. de 2024
⇧ (ok, I supose the proper term is forward-order?) ⇧ :-)
John D'Errico
John D'Errico el 14 de Sept. de 2024
Editada: John D'Errico el 14 de Sept. de 2024
Lol. That was pretty bad, I agree. But, you know how it is, Sometimes I just can't resist. :)
Anyway, I'm happy to have been of help.

Iniciar sesión para comentar.

Más respuestas (1)

nick
nick el 12 de Sept. de 2024
Hi MA,
I see you're interested in working with complex numbers and calculating angles using MATLAB. You can use 'atan2' in conjunction with the 'imag' and 'real' functions to extract the imaginary and real parts of the complex number.
Here's a simple MATLAB script to demonstrate how you can do this:
z = 4 + 3i;%example complex number
theta = atan2(imag(z),real(z));
This script calculates the angle theta of the complex number z, where 'imag(z)' gives the imaginary part and 'real(z)' gives the real part.
You can refer to the following documentation to learn more about the functions 'imag' and 'real':

7 comentarios

John D'Errico
John D'Errico el 12 de Sept. de 2024
Editada: John D'Errico el 12 de Sept. de 2024
You are missing the point here, in that the question is NOT how to compute atan(z) in a 4 quadrant sense. That correctly uses atan2, and you could do so in the way you show. The question is how to compute atan2(u,v), where both u and v are (or could be) complex numbers, and either or both could be zero.
And, yes, you might compute the ratio of the two complex numbers, separate that into real and complex parts, then use atan2 as you show. But that would fail for the case of atan2(1+i,0), where you would encounter a divide by zero. It would also fail to properly deal with all 4 quadrants for even the simple case of 2 real numbers.
Val
Val el 12 de Sept. de 2024
Editada: Val el 12 de Sept. de 2024
Thankyou Mr D'Errico for your comment
in fact , regarding complex number u,v as input,
Wolfram Alpha gives:
ArcTan[u,v] = -i*log((u+i*v)/(sqrt(u^2+v^2)))
with u , v complex number
but any comments on this expression would be greatly appreciated... :-)
(ambit: Clifford algebra 3D)
Val
Val el 12 de Sept. de 2024
note that the Matlab "atan2" need first the "v" and after the "u"
My understanding of the function 'atan2(z1,z2)' is that the angle formed by z1/z2 in complex plane. With this understanding, I demonstrated the angle computation of the complex variable for a simple case. As suggested, the user can use complex number in the form of z1/z2 and then compute the angle with real and imaginary parts of z1/z2.
I understand that this would not work for the cases when z2=0. However, it works correctly for the case of 2 real numbers as shown below:
z1=1;
z2=-2;
atan2(imag(z1/z2),real(z1/z2))
ans = 3.1416
Kindly let me know if I am missing something in the above case.
Stephen23
Stephen23 el 13 de Sept. de 2024
Editada: Stephen23 el 13 de Sept. de 2024
"Kindly let me know if I am missing something in the above case."
You are missing the fact that the user requires both inputs to be complex. This generalisation is explained here for various inverse trigonometric functions:
ATAN already supports complex inputs:
x = 1+2i;
y = 3-4i;
atan(y./x)
ans = -1.3390 - 0.4024i
which matches exactly the formula given on Wikipedia:
z = y./x;
-1i*log((1-1i*z)./sqrt(1+z.^2))
ans = 1.3390 + 0.4024i
For what it is worth, numpy and Wolfram Alpha gave the same result.
Whereas ATAN2 does not support complex inputs (yet the OP would like it to):
try
atan2(y,x)
catch ME
ME.message
end
ans = 'Inputs must be real.'
By using REAL and IMAG your approach provides two real inputs to ATAN2, which will therefore return another real result (due to the domain of ATAN2 being anyway defined for the entire real plane):
atan2(imag(y/x),real(y/x))
ans = -2.0344
So there is no way to get a complex output using your approach. It is unrelated to what the OP asked about.
nick
nick el 13 de Sept. de 2024
Thank you @Stephen23
"If I'm not mistaken I think that there is a definition of the atan2() function also in the complex field." I misunderstood the OP's statement that the user wanted to use complex number as input to the function and obtain the angle as a real valued output.
Val
Val el 13 de Sept. de 2024
Thankyou nick for your interest
"complex number as input to the function and obtain the angle as a real valued output. "
Not really , the output should also be a complex number in general.
Although I am not clear on the physical meaning, complex angles exist in the context of 3D Clifford algebra

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Versión

R2024a

Preguntada:

Val
el 12 de Sept. de 2024

Editada:

el 14 de Sept. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by