# What does this result mean?

``````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
``````
2 Likes

Thanks, but why does this code return 1?

``````julia> all([3, 3, 3], dims = 1)
1-element Vector{Bool}:
1
``````
2 Likes
``````julia> for i in 0:10
A = i * ones(Int, 3)
@show  all(A, dims = 1)
end
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
all(A, dims = 1) = Bool
``````
1 Like

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:
 _all
@ .\reduce.jl:1161 [inlined]
 _all
@ .\reducedim.jl:903 [inlined]
 #all#750
@ .\reducedim.jl:901 [inlined]
 all(a::Vector{Int64})
@ Base .\reducedim.jl:901
 top-level scope
@ REPL:1
``````
2 Likes

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.

7 Likes

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.

3 Likes

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).

1 Like

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) | is_truthy(x) | ... | is_truthy(x[end])
all(x) == mapreduce(is_truthy, &, x) == is_truthy(x) & is_truthy(x) & ... & 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.

4 Likes

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:
 top-level scope
@ REPL:1
``````

I get an error. is_truthy not defined.

This is a julia bug. @Raymond, do you want to have the honor of filing an issue since you found it? Oh, Yes.
So it is a bug. I will file an issue in github.

1 Like