by rdtsc · 2y ago · 0 comments
>>> import math
>>> z=1+2j
>>> z*complex.conjugate(z)
(5+0j)
>>> math.sqrt((z*complex.conjugate(z)).real)
2.23606797749979
>>> abs(z)
2.23606797749979
As we can see abs(z) does the right thing. Try it with a negative imaginary part too and "nicer" values: >> z = 3-4j
>>> math.sqrt((z*complex.conjugate(z)).real)
5.0
>>> abs(z)
5.0 joe@zap:~ $ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.10.5 (2024-08-27)
_/ |\__'_|_|_|\__'_| |
|__/ |
julia> z=1+2*im
1 + 2im
julia> z*conj(z)
5 + 0im
julia> sqrt(z*conj(z))
2.23606797749979 + 0.0im
julia> abs(z)
2.23606797749979 >>> (1+1j)**(1+1j)
(0.2739572538301211+0.5837007587586147j)
Ironically, the `math` library doesn't handle that: >>> math.pow(1+1j, 1+1j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not complex julia> (1+im)^(1+im)
0.2739572538301211 + 0.5837007587586147im $ sbcl
This is SBCL 2.4.8, an implementation of ANSI Common Lisp.
* (setf z #C(1 2))
#C(1 2)
* (* z (conjugate z))
5
* (sqrt (* z (conjugate z)))
2.236068
* (abs z)
2.236068 x=1
x.conjugate()
x=1+2j
x.conjugate()