-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
print arrays of Bool as 0/1 #30575
print arrays of Bool as 0/1 #30575
Conversation
Apparently unrelated AppVeyor failures? |
Alternatively, you could go the fortran route and print arrays of booleans as |
@lstagner, besides being confusing in an algebraic context, a Fortran style is a non-starter because it doesn't parse/eval. |
The AppVeyor failure is #30539. My objection to this is that |
Ahh... julia> rand(Bool, 10, 10)
10×10 Array{Bool,2}:
0 1 0 0 0 1 1 0 1 0
0 1 0 0 1 0 0 0 1 1
0 1 0 1 1 0 0 0 1 0
1 0 1 0 1 1 0 1 0 1
1 0 1 1 1 1 1 0 1 1
0 1 1 0 0 1 0 0 1 1
0 0 0 1 0 1 1 1 1 1
1 0 0 1 0 1 0 0 0 1
1 0 0 1 1 1 0 1 0 1
1 1 1 1 0 0 0 0 1 0 That's so much less noisy than before 😁 |
If anything, I think that example further underscores the confusion: You're asking for |
Bools are 1s and 0s. They are just 1s and 0s of a particular bit size which, like various other types of integers in Julia, have special input syntax and are printed differently in some context but not others. |
I mean that they aren't |
But your claim was that Bools aren't 1s and 0s, which is not true. Yes, the bare literals |
I misspoke; I've corrected the comment. I mean that literal ones and zeros cannot be used like
My point is that it isn't the same thing, because |
I doubt that printing a Boolean array is something that usually comes up in the first few minutes of using Julia (except for linear algebra demos where one might print In consequence, I would lean toward optimizing the printing of Bool arrays for slightly more experienced users. |
I tend to think that this will likely help people understand how Bools work in Julia by helping them realize that Bools are numbers and that |
Before this PR, with `x = rand(Bool, 10^6)`, `@time repr(x)` took `0.067077 seconds (43 allocations: 8.003 MiB)`. With the previous version of this PR, I got `0.230414 seconds (2.00 M allocations: 95.556 MiB, 4.60% gc time)` due to the type instability. After this commit I get `0.047411 seconds (43 allocations: 4.003 MiB)`.
Updated to fix a type instability, and performance now seems good. With julia> @time repr(x);
0.067077 seconds (43 allocations: 8.003 MiB) and now I get julia> @time repr(x);
0.044706 seconds (43 allocations: 4.003 MiB) so the cost of the extra branch is outweighed by the shorter output. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have wanted this change for a long time, didn't expect it would be popular!
I personally have no problem with this PR, but if the problem is that 0 and 1 are |
@pablosanjose, see my comment above: #30575 (comment) — we want to preserve the property that |
Whoops, had to update all of the doctests with bool-array output. |
How about julia> bitrand(2,3)
2×3 BitArray{2}:
t t t
f t t
where t=true, f=false |
Would be nice if an optional truechar,falsechar pairs could be supplied. julia> rand(Bool, 10, 10)
10×10 Array{Bool,2}:
- x - - - x x - x -
- x - - x - - - x x
- x - x x - - - x -
x - x - x x - x - x
x - x x x x x - x x
- x x - - x - - x x
- - - x - x x x x x
x - - x - x - - - x
x - - x x x - x - x
x x x x - - - - x - Not only useful for playing naval battle game, but to convey easy to read info to business people. This behavior may however better belong to the julia |
@o314, if you want that display for julia> [Text(a ? '-' : 'x') for a in A]
10×10 Array{Text{Char},2}:
- - - - - x x - x x
- x x - - - - - x x
x x x x x - - - - -
- x - - x x - x - x
- - x - x - - x - x
x x - x x x x - x -
x x x x x - - - x x
- x - x - - x x x x
- x - - x x x x - -
- - x - - x - - x - (Using |
This could have be done easily with a comprehension of course, Second point Text does not appear clearly at http://docs.julialang.org, only the macro @text_str is stated. [a ? text"-" : text"x" for a in A]
A = rand(Bool, 3,3) But that lead to an overwhelming proliferation of api/syntax around the io landscape. As a side effect/near effect, i'm fighting these hours to get a decent table display (think psql or dataframe) without having to load all the dataframe stuff. It will take hours to locate the code - and as much, i guess to cut excessive dependency too. A question of timeframe i hope |
@o314, yes, |
Thanks for your feedback. |
(Without wanting to dive into the pros and cons of this particular proposal...) |
More to the point, I would say printing booleans as 0 and 1 is just not confusing, period. I'm not sure what alternative you're suggesting --- never make a change that could possibly "confuse" a beginner, because there's no way to get data on it until it's too late? |
@karajan9, see also my comment above. |
We appreciate the feedback. Personally, I find this change a breath of fresh air every time I encounter it. I didn't realize how much those long, verbose |
Also note that @stevengj, who is very strongly in favor of this change and has more experience teaching students Julia than anyone (with the possible exception of @alanedelman), is the only person who pushed back on the global scope change. Take that for what it's worth, but I would be a lot more worried about this if both @stevengj and @JeffBezanson weren't also fully in favor. |
Thanks for the replies! As I said the comment wasn't about this change in particular. Not only because I think I personally will like the change very much but also because I'm not really sure myself it complicates matters that much (and finally, as Jeff hinted to, because more drastic methods like discussing this to death, releasing test versions or something similar are not really warranted here). I think nobody in this thread suggested they, personally, wouldn't appreciate the change but they were worried about the consequences for beginners of Julia or programming in general. My point was: if that's the concern an approach like "let's just try it and see if someone objects" isn't really practicable and might come across as neglecting of the issue. To avoid a worst case outcome of everybody screaming bloody murder after the fact, statements like
and
are much more helpful and satisfactory, I think. |
julia> [(true, 0), (false, 1), (true, 2), (false, 3)]
4-element Array{Tuple{Bool,Int64},1}:
(1, 0)
(0, 1)
(1, 2)
(0, 3) Also: julia> [true => false]
1-element Array{Pair{Bool,Bool},1}:
1 => 0
julia> Dict(true => false) # inconsistent with above?
Dict{Bool,Bool} with 1 entry:
true => false
julia> show(Dict(true => false)) # needs Dict{Bool,Bool}?
Dict(1=>0) It looks like (Edit: I re-reported Dict part in #30683) |
Definitely needs to be smoothed out a bit, thanks for filing the Dict issue! |
@test BitSet(Bool[0, 1, 1, 1, 0, 1, 0]) == BitSet([0, 1])
Test Passed isn't a bit confusing now ?
|
@o314, that test wasn't changed by this PR, nor was |
I've found this because it makes this test fail on nightly: https://github.com/diegozea/MIToS.jl/blob/a8c4075673bf439382c10592211e778f75527b7f/test/Scripts/Template.jl#L47 Maybe it will be better to define a binary type for LinearAlgebra instead of using Bool. This is at least confusing... |
Changes to the documentation as bitarrays now display 0s and 1s (JuliaLang#30575)
Changes to the documentation as bitarrays now display 0s and 1s (#30575)
As discussed in #30298, this PR changes the printing of
Bool
arrays to output0
and1
instead oftrue
andfalse
. Rationale:Bool
is already printed in theArray
type.0
and1
is more compact, and is equally correct sinceBool <: Number
. Correspondingly, it is arguably more readable for a large number ofBool
values.true
andfalse
values are confusing in an algebraic context where users are expecting numeric arrays. (This will be exacerbated by Overload UniformScaling to make I(n) create a Diagonal matrix. #30298 if people start usingI(n)
to construct identity matrices.)Technically, a
Bool
value is printed as0
or1
by this PR whenever the container has set:typeinfo=Bool
in theIOContext
, indicating that theBool
type has already been printed. So, for example,true
andfalse
are still printed forAny[true, false]
.Example: