Invarianz
Invarianz

Reputation: 461

Behaviour of the conj() function on arrays

The current Julia version in use is 1.1.

I am having problems understanding why the application of the conj() function to a complex array does not yield an error. (It should only work on scalars as far as I understand). The same is true for the real() and imag() functions.

I am currently learning Julia and try to understand if this is an inconsistency in the language syntax or a misunderstanding on my side.

ek = exp.(collect(range(0, length=10, stop=pi))*im)
ek_t = conj(ek)

This does not yield an error and gives me the correct complex conjugation. I would have expected only the following piece of code to work (which also works):

ek_t = conj.(ek)

Upvotes: 2

Views: 195

Answers (1)

mbauman
mbauman

Reputation: 31342

Good question. In short, it's because we treat arrays as not only collections of values, but also as mathematical quantities themselves. A good example is how you can multiply two matrices with either matrix or element-wise multiplication:

julia> A, B = [1 2; 3 4], [10 20; 30 40]
([1 2; 3 4], [10 20; 30 40])

julia> A * B
2×2 Array{Int64,2}:
  70  100
 150  220

julia> A .* B
2×2 Array{Int64,2}:
 10   40
 90  160

Just like how matrices can be multiplied like they're mathematical quantities, matrices themselves can be conjugated. It just so happens that the answer is the same as the element-wise computation, but it's no less valid. You will see some advantages to using conj.(...) if it can fuse with other dot operations.

A good source here is the professor of mathematics who pushed back on our attempt to deprecate these functions alongside many other deprecations: https://github.com/JuliaLang/julia/pull/18495#issuecomment-267215901

Upvotes: 3

Related Questions