JuliaMath / NaNMath.jl

Julia math built-ins which return NaN and accumulator functions which ignore NaN
Other
52 stars 26 forks source link

proposal: findNaNmax #31

Open floswald opened 5 years ago

floswald commented 5 years ago

how do people feel about a PR with

julia> function findNaNmax(x::AbstractArray{T}) where T<:AbstractFloat
           result = convert(eltype(x), NaN)
           indmax = 0
           for (i,v) in enumerate(x)
               if !isnan(v)
                   if (isnan(result) || v > result)
                       result = v
                       indmax = i
                   end
               end
           end
           return (indmax,result)
       end
findNaNmax (generic function with 1 method)

julia> Y1 = rand(10_000_000);

julia> Y3 = ifelse.(rand(length(Y1)) .< 0.9, Y1, NaN);

julia> @btime NaNMath.maximum(Y3);
  21.103 ms (1 allocation: 16 bytes)

julia> @btime findNaNmax(Y3);
  23.513 ms (1 allocation: 32 bytes)

?

or maybe even

function findNaNmax2(x::AbstractArray{T}) where T<:AbstractFloat
    result = convert(eltype(x), NaN)
    indmax = 0
    @inbounds @simd for i in eachindex(x)
        v = x[i]
        if !isnan(v)
            if (isnan(result) || v > result)
                result = v
                indmax = i
            end
        end
    end
    return (indmax,result)
end

julia> @btime findNaNmax2(Y3);
  22.294 ms (1 allocation: 32 bytes)
truedichotomy commented 4 years ago

Don't have a strong preference, but findnanmax and findnanmin would make great additions to the package!

truedichotomy commented 4 years ago

To be consistent with findmin and findmax, I'd say return (result, ind) instead of (ind, result).