What does it mean to take the absolute of an complex number? And why is it always positive?
34 views (last 30 days)
Show older comments
Hi,
I have a number which is part imaginary or complex.
-0.993061325012476 + 1.03511742045580e-16i
When I use the function abs(x) on this number like so:
abs(-0.993061325012476 + 1.03511742045580e-16i)
ans =
0.99306
Then I receive a positive number.
Can someone kindly explain to me how this works and if the abs(x) function makes it positive or if the abs(x) function simply never returns negative numbers. If so why?
Or is it maybe the case that I need to be better versed in complex numbers in general?
Thanks!
0 Comments
Accepted Answer
Steven Lord
on 26 Apr 2022
I think one potential source of confusion is that the abs of this particular complex number looks an awful lot like its real part.
n = -0.993061325012476 + 1.03511742045580e-16i;
R = abs(n)
That's not due to abs simply taking the real part of the complex number, it's due to the fact that the imaginary part of this number is extremely small. If you were to plot this number you'd see that it's very close to being on the real line.
setupFigure();
% This plots using the real part of n as the x coordinate
% and the imaginary part of n as the y coordinate. I made the marker
% larger so it stands out
plot(n, 'bo', 'MarkerSize', 10)
If we plotted another point, one whose real and imaginary parts were both not small, you'd see the abs of that number was the distance between it and the origin. For the point n2 the red dotted line is the line whose length is measured by taking abs(n2).
setupFigure();
n2 = 1+1i;
plot(n, 'bo', 'MarkerSize', 10)
plot(n2, 'ks', 'MarkerSize', 10)
plot([0 n2], 'r:', 'LineWidth', 2)
fprintf("The red dotted line is %g units long.\n", abs(n2)) % sqrt(2)
function setupFigure()
% Set up the plot to look nice
figure
axes('XAxisLocation', 'origin', 'YAxisLocation', 'origin')
axis([-1, 1, -1, 1])
xlabel('real(n)')
ylabel('imag(n)')
xticks(-1:0.25:1)
yticks(-1:0.25:1)
grid on
hold on
end
0 Comments
More Answers (2)
Torsten
on 25 Apr 2022
abs(z) where z=x+i*y is the distance of the point (x/y) from the point (0/0), thus always >=0 and =0 only if z = 0.
2 Comments
Torsten
on 26 Apr 2022
Why should anything change ?
z=x+i*y is a static point in the x-y plane. If you interprete C as R^2, then this point z is P=(x/y). And abs(z) returns the distance of P from (0/0), thus sqrt(x^2+y^2).
Walter Roberson
on 26 Apr 2022
syms a b real
c = a + b*1i
d = abs(c)
With the real and imaginary components each being real, their squares are going to be non-negative numbers, and the sum of non-negative numbers is non-negative, and the square root of a non-negative number is non-negative
5 Comments
Walter Roberson
on 26 Apr 2022
Edited: Walter Roberson
on 26 Apr 2022
absolute value is the Euclidean magnitude function. If you represent the points as ordered tuples of components, < x1, x2, x3,... xn > for a vector in n dimensions, then absolute value is sqrt(sum of (x(K)^2), K=1 to n). In the case of complex numbers which you could represent as the tuple < xreal, ximaginary > then sqrt(xreal^2 + ximaginary^2)
This is a formal Distance, and formal Distance are never negative.
Perhaps you might be wondering about the actual implementation. The actual implementation for scalar real IEEE floating point values is delegated to hardware. The algorithm is
1. check if the value represents NaN. If it does then the result is the same as the input 2. Otherwise, zero the most significant bit of the IEEE representation of the number. This works even for infinity and negative zero.
IEEE floating point representation uses "separate sign" which is 0 for non-negative values and 1 for negative values.
At the moment I do not know what the formal algorithm is for IEEE scalar complex values; the model is sqrt of the sum of squares of the components
Walter Roberson
on 26 Apr 2022
For complex numbers, it appears that hypot() is used internally. hypot() is a modified sum of squares algorithm that detects when the squaring would underflow or overflow and uses alternate calculations in those cases.
See Also
Categories
Find more on Special Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!