# \#boolean

**URL:** https://discourse.julialang.org/tag/boolean/1010.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Juniper Boolean variable example with MTK](https://discourse.julialang.org/t/juniper-boolean-variable-example-with-mtk/126144)

<div class="topic-metadata">

**Author:** [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Replies:** 3\
**Last updated:** [February 25, 2025, 7:31pm UTC](https://discourse.julialang.org/t/juniper-boolean-variable-example-with-mtk/126144 "2025-02-25T19:31:42Z")

</div>

I want to reproduce the Juniper README example (with Binary variable and nonlinear non-convex objective). It has been done with JuMP.jl, with Optimization.jl (here). But I cannot reproduce it using ModelingToolKit.jl I…

---

## [Create a boolean mask between arrays of different size](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986)

<div class="topic-metadata">

**Author:** [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Replies:** 10\
**Last updated:** [October 30, 2024, 4:43pm UTC](https://discourse.julialang.org/t/create-a-boolean-mask-between-arrays-of-different-size/121986 "2024-10-30T16:43:34Z")

</div>

Hi, I would like to do the same thing as in python: import numpy as np a=\[1,2,3,4,5,6,7\] b=\[2,4,6\] mask=np.isin(a,b) giving \>\>\> mask array(\[False, True, False, True, False, True, False\]) In julia I can: mask = in…

---

## [How to filter dataframe rows with a boolean mask?](https://discourse.julialang.org/t/how-to-filter-dataframe-rows-with-a-boolean-mask/121873)

<div class="topic-metadata">

**Author:** [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Replies:** 2\
**Last updated:** [October 28, 2024, 3:28pm UTC](https://discourse.julialang.org/t/how-to-filter-dataframe-rows-with-a-boolean-mask/121873 "2024-10-28T15:28:45Z")

</div>

Hi, I would like to the same thing as this python code: import pandas as pd array = \[1,2,3\] df = pd.DataFrame(array) mask = \[True, False, True\] new\_df = df\[mask\] \>\>\> print(new\_df) 0 0 1 2 3 Looked around, but …

---

## [Vector/array and boolean mask using logic operators](https://discourse.julialang.org/t/vector-array-and-boolean-mask-using-logic-operators/121369)

<div class="topic-metadata">

**Author:** [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Replies:** 4\
**Last updated:** [October 16, 2024, 10:33am UTC](https://discourse.julialang.org/t/vector-array-and-boolean-mask-using-logic-operators/121369 "2024-10-16T10:33:58Z")

</div>

Hi, I would like to do the same thing as in Python: import numpy as np arr=np.array(\[1,2,3,4,5,6\]) mask=(arr\<3)|(arr\>=4) which gives as result array(\[ True, True, False, True, True, True\]) I tried stuff in Juli…

---

## [Is it idiomatic to use \`Bool\` as \`Number\`?](https://discourse.julialang.org/t/is-it-idiomatic-to-use-bool-as-number/121154)

<div class="topic-metadata">

**Author:** [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Replies:** 4\
**Last updated:** [October 10, 2024, 3:47pm UTC](https://discourse.julialang.org/t/is-it-idiomatic-to-use-bool-as-number/121154 "2024-10-10T15:47:21Z")

</div>

Both f and g below do the same thing f(x) = x ? 0 : 1 g(x) = x \* 0 + (!x) \* 1 They lower to the same llvm code too: julia\> @code\_llvm debuginfo=:none f(true) define i64 @julia\_f\_7188(i8 zeroext %0) #0 { top: %1 = an…

---

## [Dataframes subset w/ nullable logic](https://discourse.julialang.org/t/dataframes-subset-w-nullable-logic/117484)

<div class="topic-metadata">

**Author:** [@mikemartind](https://discourse.julialang.org/u/mikemartind)\
**Replies:** 4\
**Last updated:** [July 25, 2024, 9:07pm UTC](https://discourse.julialang.org/t/dataframes-subset-w-nullable-logic/117484 "2024-07-25T21:07:36Z")

</div>

Working on a subset call that filters on multiple columns in a dataframe, but with variable conditions set by an external dict. So if the dict has a value, it checks for equality in the corresponding column. If the dict …

---

## [Why are booleans printed as numbers?](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164)

<div class="topic-metadata">

**Author:** [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Replies:** 4\
**Last updated:** [July 17, 2024, 10:21pm UTC](https://discourse.julialang.org/t/why-are-booleans-printed-as-numbers/117164 "2024-07-17T22:21:01Z")

</div>

With Julia 1.10.4: julia\> \[("a", true), ("b", false)\] 2-element Vector{Tuple{String, Bool}}: ("a", 1) ("b", 0) Here true / false are confusingly printed as 1 / 0.

---

## [Is it expected that a zero-one variable may have a non-integer value after an optimal solution is found?](https://discourse.julialang.org/t/is-it-expected-that-a-zero-one-variable-may-have-a-non-integer-value-after-an-optimal-solution-is-found/107430)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 5\
**Last updated:** [December 11, 2023, 12:37pm UTC](https://discourse.julialang.org/t/is-it-expected-that-a-zero-one-variable-may-have-a-non-integer-value-after-an-optimal-solution-is-found/107430 "2023-12-11T12:37:37Z")

</div>

I formulate a mathematical optimization (MIQCP) problem with MathOptInterface.jl and solve it with Gurobi.jl. Certain variables in my problem are constrained (with MOI.add\_constrained\_variables(optimizer, \[MOI.ZeroOne() …

---

## [Speeding up the calculation of Liouville function](https://discourse.julialang.org/t/speeding-up-the-calculation-of-liouville-function/105447)

<div class="topic-metadata">

**Author:** [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Replies:** 4\
**Last updated:** [October 27, 2023, 5:15pm UTC](https://discourse.julialang.org/t/speeding-up-the-calculation-of-liouville-function/105447 "2023-10-27T17:15:20Z")

</div>

Hi! In my journey to learn Julia, I was trying to find the smallest counter example of Polya’s conjecture which is a little less than 10^9, while using as little memory as possible. Any tips on how to optimize the algo…

---

## [Fastest way to negate some booleans in an array](https://discourse.julialang.org/t/fastest-way-to-negate-some-booleans-in-an-array/89261)

<div class="topic-metadata">

**Author:** [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Replies:** 10\
**Last updated:** [October 26, 2022, 12:28am UTC](https://discourse.julialang.org/t/fastest-way-to-negate-some-booleans-in-an-array/89261 "2022-10-26T00:28:31Z")

</div>

Hi, I am trying to optimize a code for a number theory side project. Currently, I have a large vector of booleans, say A=rand(Bool,10^8) I want to negate all the even-index terms. One possible way that I have found is …

---

## [How to convert from bool to float](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216)

<div class="topic-metadata">

**Author:** [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)\
**Replies:** 2\
**Last updated:** [October 24, 2022, 10:40pm UTC](https://discourse.julialang.org/t/how-to-convert-from-bool-to-float/89216 "2022-10-24T22:40:58Z")

</div>

I would like to take a matrix that is currently made of bools and turn it into floats. If I is this matrix of bools, I’ve been trying commands like convert(Int8, I) and getting errors like ERROR: MethodError: Cannot \`c…

---

## [Sampling an index from a boolean array](https://discourse.julialang.org/t/sampling-an-index-from-a-boolean-array/87931)

<div class="topic-metadata">

**Author:** [@yuvalw](https://discourse.julialang.org/u/yuvalw)\
**Replies:** 7\
**Last updated:** [October 3, 2022, 11:15am UTC](https://discourse.julialang.org/t/sampling-an-index-from-a-boolean-array/87931 "2022-10-03T11:15:24Z")

</div>

Hello, I often need to sample an index from a boolean array. For example: julia\> A = rand(Bool,3,3) 3×3 Matrix{Bool}: 1 1 1 1 0 0 1 0 0 julia\> idx = rand(findall(A)) CartesianIndex(2, 1) Can I somehow sample …

---

## [Allocations due to Boolean keyword arguments - how to avoid them?](https://discourse.julialang.org/t/allocations-due-to-boolean-keyword-arguments-how-to-avoid-them/87654)

<div class="topic-metadata">

**Author:** [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Replies:** 9\
**Last updated:** [September 22, 2022, 6:00pm UTC](https://discourse.julialang.org/t/allocations-due-to-boolean-keyword-arguments-how-to-avoid-them/87654 "2022-09-22T18:00:08Z")

</div>

Deep in one of my projects I have a function that slightly changes behavior depending on a boolean keyword. It seems if the keyword is Val{Bool} there are no allocations but if it is Bool, then it allocates. Here is a co…

---

## [Logical aggregation functions](https://discourse.julialang.org/t/logical-aggregation-functions/84250)

<div class="topic-metadata">

**Author:** [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Replies:** 2\
**Last updated:** [July 15, 2022, 7:36am UTC](https://discourse.julialang.org/t/logical-aggregation-functions/84250 "2022-07-15T07:36:10Z")

</div>

Does Julia have logical aggregation functions ? Something like AND( \[true, true, false\] ) = false OR( \[true, true, false\] ) = true

---

## [Slicing and boolean indexing in multidimensional arrays](https://discourse.julialang.org/t/slicing-and-boolean-indexing-in-multidimensional-arrays/83522)

<div class="topic-metadata">

**Author:** [@bryce](https://discourse.julialang.org/u/bryce)\
**Replies:** 5\
**Last updated:** [June 30, 2022, 5:21am UTC](https://discourse.julialang.org/t/slicing-and-boolean-indexing-in-multidimensional-arrays/83522 "2022-06-30T05:21:20Z")

</div>

I’m curious why the following 3D array slicing works for the i index but not for the j index: T = Array(reshape(1:2\*3\*4, 2,3,4)) i = zeros(Bool, 2,3) i\[1,2:3\] .= 1 display(T\[i,:\]) j = zeros(Bool, 3,4) j\[1,2:3\] .= 1 disp…

---

## [Boolean Indexing](https://discourse.julialang.org/t/boolean-indexing/82556)

<div class="topic-metadata">

**Author:** [@lazycoder](https://discourse.julialang.org/u/lazycoder)\
**Replies:** 2\
**Last updated:** [June 10, 2022, 1:30pm UTC](https://discourse.julialang.org/t/boolean-indexing/82556 "2022-06-10T13:30:24Z")

</div>

I’m a little confused on boolean indexing, I’m trying to do something like this: test=5.4 res=fill(0,size(test)) res\[test .\> 5\] .=5 The size of test could be one or more, but when test is a size of one I get an error w…

---

## [Bools in arithmetic operations](https://discourse.julialang.org/t/bools-in-arithmetic-operations/52504)

<div class="topic-metadata">

**Author:** [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Replies:** 8\
**Last updated:** [December 29, 2020, 1:30pm UTC](https://discourse.julialang.org/t/bools-in-arithmetic-operations/52504 "2020-12-29T13:30:14Z")

</div>

Bools seem to get converted/promoted to numerical zero/one: julia\> false + 42 42 julia\> true + 42 43 julia\> false + 3.14 3.14 julia\> true + 3.14 4.140000000000001 julia\> false + complex(1, 2) 1 + 2im julia\> true + …
