In Julia, I would like to randomly generate an array of arbitrary size, where all the elements of the array are complex numbers with absolute value one. Is there perhaps any way to do this within Julia?
Generate array of complex numbers with absolute value one in Julia?
358 views Asked by James Rider At
3
There are 3 answers
4
On
If you are looking for something faster, here are two options. They return a perhaps slightly unfamiliar type, but it is equivalent to a regular Vector
function f5(n)
r = rand(2, n)
for i in 1:n
a = sqrt(r[1, i]^2 + r[2, i]^2)
r[1, i] /= a
r[2, i] /= a
end
return reinterpret(reshape, ComplexF64, r)
end
using LoopVectorization: @turbo
function f5t(n)
r = rand(2, n)
@turbo for i in 1:n
a = sqrt(r[1, i]^2 + r[2, i]^2)
r[1, i] /= a
r[2, i] /= a
end
return reinterpret(reshape, ComplexF64, r)
end
julia> @btime f5(1000);
4.186 μs (1 allocation: 15.75 KiB)
julia> @btime f5t(1000);
2.900 μs (1 allocation: 15.75 KiB)
I've got four options so far:
We have:
Not a crucial difference.