julia> A = [2, 2, 2]
3-element Vector{Int64}:
2
2
2
julia> all(A, dims = 1)
1-element Vector{Bool}:
0
Why does this code return 0
, not 1
?
julia> A = [2, 2, 2]
3-element Vector{Int64}:
2
2
2
julia> all(A, dims = 1)
1-element Vector{Bool}:
0
Why does this code return 0
, not 1
?
In Julia unlike python for example values are not considered truthy. See a discussion about this concept here: On the arbitrariness of truth(iness).
If you would like to test whether all values are nonzero for example you could provide a function to the all function:
julia> all(!iszero, [2,2,2], dims=1)
1-element Vector{Bool}:
1
Thanks, but why does this code return 1?
julia> all([3, 3, 3], dims = 1)
1-element Vector{Bool}:
1
julia> for i in 0:10
A = i * ones(Int, 3)
@show all(A, dims = 1)
end
all(A, dims = 1) = Bool[0]
all(A, dims = 1) = Bool[1]
all(A, dims = 1) = Bool[0]
all(A, dims = 1) = Bool[1]
all(A, dims = 1) = Bool[0]
all(A, dims = 1) = Bool[1]
all(A, dims = 1) = Bool[0]
all(A, dims = 1) = Bool[1]
all(A, dims = 1) = Bool[0]
all(A, dims = 1) = Bool[1]
all(A, dims = 1) = Bool[0]
Wow, that’s wild. Looks like a bug to me. This should probably just error, like it does without the dims
keyword argument:
all([1, 1, 1])
ERROR: TypeError: non-boolean (Int64) used in boolean context
Stacktrace:
[1] _all
@ .\reduce.jl:1161 [inlined]
[2] _all
@ .\reducedim.jl:903 [inlined]
[3] #all#750
@ .\reducedim.jl:901 [inlined]
[4] all(a::Vector{Int64})
@ Base .\reducedim.jl:901
[5] top-level scope
@ REPL[21]:1
This boils down to
julia> mapreduce(identity, &, [3,3,3]; dims=1)
1-element Vector{Bool}:
1
julia> mapreduce(identity, &, [2,2,2]; dims=1)
1-element Vector{Bool}:
0
and definitely is a bug.
Related:
julia> any([1, 2], dims=1)
ERROR: InexactError: Bool(3)
Like, I guess it’s good that it gives an error, but it’s not really the right error.
Could you provide some tips how to get there? Thanks.
@edit all([3, 3, 3], dims = 1)
and scroll down to
$(_fname)(f, A, dims; kw...) = mapreduce(f, $(op), A; dims=dims, kw...)
You could also @enter all([3, 3, 3], dims = 1)
(in VS Code or with Debugger.jl loaded) and then step around a bit until you end up in that function (which may be annoying because the kwarg wrapper heuristics are kinda borked).
Thanks Sebastian. And sorry, my brain is overheating: I cannot see the connection between “all vector elements being true” and that unfamiliar mapreduce expression. (if all vector elements are booleans, it is a bit more easy to see)
Well,
any(x) == mapreduce(is_truthy, |, x) == is_truthy(x[1]) | is_truthy(x[2]) | ... | is_truthy(x[end])
all(x) == mapreduce(is_truthy, &, x) == is_truthy(x[1]) & is_truthy(x[2]) & ... & is_truthy(x[end])
For booleans, is_truthy == identity
. This of course breaks down when the eltype
of x
is e.g. an integer like in the example above, because in that case |
/&
means bitwise-or/and.
Thanks a lot for your explanation, it is very clear now.
julia> mapreduce(is_truthy, &, [3, 3, 3])
ERROR: UndefVarError: is_truthy not defined
Stacktrace:
[1] top-level scope
@ REPL[91]:1
I get an error. is_truthy not defined.
Oh, Yes.
So it is a bug. I will file an issue in github.