Logical Operations
Boolean Operations
Missing docstring for true
. Check Documenter's build log for details.
Missing docstring for false
. Check Documenter's build log for details.
Base.:!
— Function!(x)
Boolean not. Implements three-valued logic, returning missing
if x
is missing
.
See also ~
for bitwise not.
Examples
julia> !true
false
julia> !false
true
julia> !missing
missing
julia> .![true false true]
1×3 BitMatrix:
0 1 0
!f::Function
Predicate function negation: when the argument of !
is a function, it returns a composed function which computes the boolean negation of f
.
See also ∘
.
Examples
julia> str = "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
"∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
julia> filter(isletter, str)
"εδxyδfxfyε"
julia> filter(!isletter, str)
"∀ > 0, ∃ > 0: |-| < ⇒ |()-()| < "
Starting with Julia 1.9, !f
returns a ComposedFunction
instead of an anonymous function.
&&
— Keywordx && y
Short-circuiting boolean AND.
See also &
, the ternary operator ? :
, and the manual section on control flow.
Examples
julia> x = 3;
julia> x > 1 && x < 10 && x isa Int
true
julia> x < 0 && error("expected positive x")
false
||
— Keywordx || y
Short-circuiting boolean OR.
Examples
julia> pi < 3 || ℯ < 3
true
julia> false || true || println("neither is true!")
true
Base.all
— Functionall(itr) -> Bool
Test whether all elements of a boolean collection are true
, returning false
as soon as the first false
value in itr
is encountered (short-circuiting). To short-circuit on true
, use any
.
If the input contains missing
values, return missing
if all non-missing values are true
(or equivalently, if the input contains no false
value), following three-valued logic.
See also: all!
, any
, count
, &
, , &&
, allunique
.
Examples
julia> a = [true,false,false,true]
4-element Vector{Bool}:
1
0
0
1
julia> all(a)
false
julia> all((println(i); v) for (i, v) in enumerate(a))
1
2
false
julia> all([missing, false])
false
julia> all([true, missing])
missing
all(p, itr) -> Bool
Determine whether predicate p
returns true
for all elements of itr
, returning false
as soon as the first item in itr
for which p
returns false
is encountered (short-circuiting). To short-circuit on true
, use any
.
If the input contains missing
values, return missing
if all non-missing values are true
(or equivalently, if the input contains no false
value), following three-valued logic.
Examples
julia> all(i->(4<=i<=6), [4,5,6])
true
julia> all(i -> (println(i); i < 3), 1:10)
1
2
3
false
julia> all(i -> i > 0, [1, missing])
missing
julia> all(i -> i > 0, [-1, missing])
false
julia> all(i -> i > 0, [1, 2])
true
all(A; dims)
Test whether all values along the given dimensions of an array are true
.
Examples
julia> A = [true false; true true]
2×2 Matrix{Bool}:
1 0
1 1
julia> all(A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> all(A, dims=2)
2×1 Matrix{Bool}:
0
1
all(p, A; dims)
Determine whether predicate p
returns true
for all elements along the given dimensions of an array.
Examples
julia> A = [1 -1; 2 2]
2×2 Matrix{Int64}:
1 -1
2 2
julia> all(i -> i > 0, A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> all(i -> i > 0, A, dims=2)
2×1 Matrix{Bool}:
0
1
Base.any
— Functionany(itr) -> Bool
Test whether any elements of a boolean collection are true
, returning true
as soon as the first true
value in itr
is encountered (short-circuiting). To short-circuit on false
, use all
.
If the input contains missing
values, return missing
if all non-missing values are false
(or equivalently, if the input contains no true
value), following three-valued logic.
See also: all
, count
, sum
, |
, , ||
.
Examples
julia> a = [true,false,false,true]
4-element Vector{Bool}:
1
0
0
1
julia> any(a)
true
julia> any((println(i); v) for (i, v) in enumerate(a))
1
true
julia> any([missing, true])
true
julia> any([false, missing])
missing
any(p, itr) -> Bool
Determine whether predicate p
returns true
for any elements of itr
, returning true
as soon as the first item in itr
for which p
returns true
is encountered (short-circuiting). To short-circuit on false
, use all
.
If the input contains missing
values, return missing
if all non-missing values are false
(or equivalently, if the input contains no true
value), following three-valued logic.
Examples
julia> any(i->(4<=i<=6), [3,5,7])
true
julia> any(i -> (println(i); i > 3), 1:10)
1
2
3
4
true
julia> any(i -> i > 0, [1, missing])
true
julia> any(i -> i > 0, [-1, missing])
missing
julia> any(i -> i > 0, [-1, 0])
false
any(A; dims)
Test whether any values along the given dimensions of an array are true
.
Examples
julia> A = [true false; true false]
2×2 Matrix{Bool}:
1 0
1 0
julia> any(A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> any(A, dims=2)
2×1 Matrix{Bool}:
1
1
any(p, A; dims)
Determine whether predicate p
returns true
for any elements along the given dimensions of an array.
Examples
julia> A = [1 -1; 2 -2]
2×2 Matrix{Int64}:
1 -1
2 -2
julia> any(i -> i > 0, A, dims=1)
1×2 Matrix{Bool}:
1 0
julia> any(i -> i > 0, A, dims=2)
2×1 Matrix{Bool}:
1
1