Skip to content
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

display of Rationals in containers #42626

Closed
jmichel7 opened this issue Oct 13, 2021 · 5 comments
Closed

display of Rationals in containers #42626

jmichel7 opened this issue Oct 13, 2021 · 5 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects. rationals The Rational type and values thereof

Comments

@jmichel7
Copy link

In my work, I often have to work with Rational matrices resulting from
simple mathematical operations on integer matrices, like:

julia> m=cartan(:A,3)
3×3 Matrix{Int64}:
  2  -1   0
 -1   2  -1
  0  -1   2

julia> n=schur_functor(m,[2,2])
6×6 Matrix{Rational{Int64}}:
   9//1   -6//1    4//1   3//2   -2//1    1//1
 -12//1   16//1  -16//1  -4//1    8//1   -4//1
   4//1   -8//1   16//1   2//1   -8//1    4//1
  12//1  -16//1   16//1  10//1  -16//1   12//1
  -4//1    8//1  -16//1  -4//1   16//1  -12//1
   1//1   -2//1    4//1   3//2   -6//1    9//1

I find the display rather hard to read. I tried for a long time to think
what could be the least disruptive and least controversial change to the
display of rational numbers which could improve the situation. I came up
with the following

function Base.show(io::IO, x::Rational)
   show(io, numerator(x))
   if haskey(io,:typeinfo) && isone(denominator(x)) return end
   print(io, "//")
   show(io, denominator(x))
end

here the only change is the if line, which says if the Rational number
is actually an integer, display it as an integer, if the type of the
current container has already been printed. I think this would cause no
problem: I cannot think of a situation where a Rational Vector,
Matrix or Array would be wrongly read/interpreted because of this. And
it already considerably improves the display. In the example above we get

julia> n
6×6 Matrix{Rational{Int64}}:
   9   -6    4  3//2   -2    1
 -12   16  -16  -4      8   -4
   4   -8   16   2     -8    4
  12  -16   16  10    -16   12
  -4    8  -16  -4     16  -12
   1   -2    4  3//2   -6    9

even in matrices which do not contain many integers, the simple fact that 0
displays as 0 and not 0//1 makes things better.

What do you think about that?

@KristofferC
Copy link
Member

I do think it looks better and it follows similar principles use elsewhere to write things compactly if the extra information can be gotten from the printed element type.

@fredrikekre fredrikekre changed the title display of Rationals display of Rationals in containers Oct 13, 2021
@timholy
Copy link
Member

timholy commented Oct 13, 2021

Another thought might be to display

julia> [1//2 1//4]
1×2 Matrix{Rational{Int64}}:
 1//2  1//4

as something along the lines of

[2 1] // 4

There are numerous challenges, though, such as when the lcm would overflow.

@jmichel7
Copy link
Author

For Tim's proposal my only wish is that the definition of denominator should be extended to Arrays, the meaning being
the lcm of the denominators. Then it is easy to write m*denominator(m) to have your own display. But, as it happens quite often that the lcm of the denominators is very big, I do not think it is the best solution for display in general.

@dkarrasch dkarrasch added the display and printing Aesthetics and correctness of printed representations of objects. label Oct 13, 2021
@ParadaCarleton
Copy link
Contributor

Relevant: Unicode has a fraction slash (U+2044, ) that can be used for pretty fractions like 1⁄4.

@vtjnash
Copy link
Member

vtjnash commented Feb 22, 2024

Fixed by #45396

@vtjnash vtjnash closed this as completed Feb 22, 2024
@vtjnash vtjnash added the rationals The Rational type and values thereof label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. rationals The Rational type and values thereof
Projects
None yet
Development

No branches or pull requests

7 participants
@vtjnash @KristofferC @timholy @dkarrasch @jmichel7 @ParadaCarleton and others