JuliaPhysics / Measurements.jl

Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.

Home Page:https://juliaphysics.github.io/Measurements.jl/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Summing Complex Measurements

JonasIsensee opened this issue · comments

Hi,
the following caught me by surprise...
Is there any good reason that the "true" sum function is so much more expensive?

julia> using Measurements
[ Info: Precompiling Measurements [eff96d63-e80a-5855-80a2-b1b0885c5ab7]

julia> x = rand(1000) .± rand(1000);

julia> z = complex.(x,x);

julia> @time sum(x)
  0.124190 seconds (351.04 k allocations: 18.032 MiB)
496.0 ± 18.0

julia> @time sum(z)
  2.022132 seconds (1.20 M allocations: 56.428 MiB, 1.64% gc time)
(496.0 ± 18.0) + (496.0 ± 18.0)im

julia> Base.sum(z::Array{Complex{Measurement{T}}}) where T= sum(real.(z)) .+ im*sum(imag.(z))

julia> @time sum(z)
  0.126569 seconds (321.87 k allocations: 16.926 MiB)
(496.0 ± 18.0) + (496.0 ± 18.0)im

Yes, this is a duplicate of #25. In particular see #25 (comment)