-
-
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
skip show when denominator is one #45396
Conversation
Whom should I ping to get this going? @KristofferC maybe? |
Jeff thinks this is too probably fancy (and is fairly different from |
Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
Not sure I understand @oscardssmith: show for |
I don't know what means "too fancy", but people who look often at rational matrices, like mathematicians, do care. |
It's also not clear that this is actually an improvement in looking at these matrices. For example, it messes up the alignment. It also doesn't really save space unless all the values happen to be integral (or the integral values are the only ones that are large, which seems unlikely). Is the subjective benefit to highlight integer-valued entries and make them easier to notice? |
At least, it makes the position of the zero entries much easier to find. As for messing the alignment, it is a more general problem |
To me, the main advantage is that it reduces visual noise. Here is an example I just encountered: julia> G⁻¹ = [j > i ? 0 : (-1)^(i+j) * binomial(i, j-1) for i in 1:10, j in 1:10];
julia> G = inv(Rational.(G⁻¹))
10×10 Matrix{Rational{Int64}}:
1//1 0//1 0//1 0//1 0//1 0//1 0//1 0//1 0//1 0//1
1//2 1//2 0//1 0//1 0//1 0//1 0//1 0//1 0//1 0//1
1//6 1//2 1//3 0//1 0//1 0//1 0//1 0//1 0//1 0//1
0//1 1//4 1//2 1//4 0//1 0//1 0//1 0//1 0//1 0//1
-1//30 0//1 1//3 1//2 1//5 0//1 0//1 0//1 0//1 0//1
0//1 -1//12 0//1 5//12 1//2 1//6 0//1 0//1 0//1 0//1
1//42 0//1 -1//6 0//1 1//2 1//2 1//7 0//1 0//1 0//1
0//1 1//12 0//1 -7//24 0//1 7//12 1//2 1//8 0//1 0//1
-1//30 0//1 2//9 0//1 -7//15 0//1 2//3 1//2 1//9 0//1
0//1 -3//20 0//1 1//2 0//1 -7//10 0//1 3//4 1//2 1//10
julia> map(x -> isinteger(x) ? Int(x) : x, G)
10×10 Matrix{Real}:
1 0 0 0 0 0 0 0 0 0
1//2 1//2 0 0 0 0 0 0 0 0
1//6 1//2 1//3 0 0 0 0 0 0 0
0 1//4 1//2 1//4 0 0 0 0 0 0
-1//30 0 1//3 1//2 1//5 0 0 0 0 0
0 -1//12 0 5//12 1//2 1//6 0 0 0 0
1//42 0 -1//6 0 1//2 1//2 1//7 0 0 0
0 1//12 0 -7//24 0 7//12 1//2 1//8 0 0
-1//30 0 2//9 0 -7//15 0 2//3 1//2 1//9 0
0 -3//20 0 1//2 0 -7//10 0 3//4 1//2 1//10 Can you say anything about the matrix structure of From the second output OTOH you immediately recognize that the upper triangle is just all zeros and that there's some kind of band structure with occasional all-zero bands going on in the lower triangle. I really think this would be a big improvement in legibility and this seems very much in line with the decision made in #30575. |
In @simeonschaub 's example, an alternative change would solve the issue of showing the matrix structure: show |
Some matrix structure can be seen easily only by showing 1 instead 1//1. There is really no reason to restrict this |
From triage 👍 p.s. make sure to make it print correctly for matrices with a column of integers. |
@LilithHafner I just built it again and get these results: julia> using Random; Random.seed!(1)
TaskLocalRNG()
julia> x = rand(-3:3, 5,5).//rand(1:6, 5, 5)
5×5 Matrix{Rational{Int64}}:
-1//2 -2//3 0 1//4 0
-1//3 2//3 0 -2//3 1
1//6 2//5 -1//3 -3//5 -1//3
1//6 1//4 -3 -1//3 1
3//5 -2//3 0 -1 0
julia> x[:, 1] .= 0;
julia> x
5×5 Matrix{Rational{Int64}}:
0 -2//3 0 1//4 0
0 2//3 0 -2//3 1
0 2//5 -1//3 -3//5 -1//3
0 1//4 -3 -1//3 1
0 -2//3 0 -1 0
julia> x[:, 2] .= -1;
julia> x
5×5 Matrix{Rational{Int64}}:
0 -1 0 1//4 0
0 -1 0 -2//3 1
0 -1 -1//3 -3//5 -1//3
0 -1 -3 -1//3 1
0 -1 0 -1 0
julia> x[:, end] = rand(-4:4, 5);
julia> x
5×5 Matrix{Rational{Int64}}:
0 -1 0 1//4 2
0 -1 0 -2//3 -4
0 -1 -1//3 -3//5 0
0 -1 -3 -1//3 -3
0 -1 0 -1 0
To me it seems that everything runs as intended; |
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.
Spacing seems fine. I'd like a test for sprint(show, MIME"text/plain"(), Union{Int, Rational{Int}}[7 3//6; 6//3 2])
, though.
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
The new test is failing. Showing |
9a075c6
to
024b454
Compare
I would have swore I did run the tests before committing ;) |
Aside from "needs news" (add this to https://github.com/JuliaLang/julia/blob/master/NEWS.md), this LGTM |
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
Internet-based tests failed across the board; merging master in to rerun tests. |
Thanks for your contribution and your persistence, @kalmarek! This is a nice improvement in readability in some cases. |
@LilithHafner thanks for leading the final push ;) I understand that since 1.9rc1 has already landed this will end up in 1.10? |
correct. |
It's a pitty to miss the window by a few days, but better late than never ;) |
this was well past the window. We branched 1.9 in November. |
good to know |
Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com> Co-authored-by: Elliot Saba <staticfloat@gmail.com> Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
following #42626 with this pull we have: