Isusefulnumber()

Is there a function available to check in a large vector, if one of the following occurs in a vector:

  • missing
  • nothing
  • Inf
  • NaN

?
Here is my snippet to investigate a vector for such elements:

a = [1.3, NaN, Inf, missing, nothing, 1.3]
# ---
println('-'^100)
for i_element in a
    if isa(i_element, Number)
        println("is number:    ", i_element," ,\t isnan(i_element): ", isnan(i_element), ", \t isinf(i_element)", isinf(i_element))
    else
        println("is no number: ", i_element)
    end
end
println('-'^100)
# --- find non-number plus Inf plus NaN elements: -----------------------------------------
vector_ = isa.(a, Number)
b = a[vector_]
c = isinf.(b) .| isnan.(b)
b_contains_bad_elements = any(c)
if b_contains_bad_elements
    println("vector \"a\" contains bad elements")
end
# --- local function: -----------------------------------------------------------------------------------
function _contains_bad_elements(_vec::AbstractVector)
    _isnumber = isa.(_vec, Number)
    _nonumber = false .== _isnumber
    _numbers = _vec[_isnumber]
    return any(vcat(isinf.(_numbers) .| isnan.(_numbers), _nonumber))
end
# ---
_isusefulnumber(a)

It’s not too hard to implement:

julia> is_useful(::Any) = false
is_useful (generic function with 3 methods)

julia> is_useful(x::Number) = isfinite(x)
is_useful (generic function with 3 methods)

julia> a = [1.3, NaN, Inf, missing, nothing, 1.3, "a"]
7-element Vector{Any}:
   1.3
 NaN
  Inf
    missing
    nothing
   1.3
    "a"

julia> is_useful.(a)
7-element BitVector:
 1
 0
 0
 0
 0
 1
 0

julia> any(is_useful, a)
true

julia> all(is_useful, a)
false
10 Likes

I love dispatch.

1 Like