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

Implement Base.alignment for our ring element types? #1415

Open
fingolfin opened this issue Mar 16, 2023 · 1 comment
Open

Implement Base.alignment for our ring element types? #1415

fingolfin opened this issue Mar 16, 2023 · 1 comment

Comments

@fingolfin
Copy link
Member

I just noticed the differing alignment in this example:

julia> ZZ.([1,23,456])
3-element Vector{ZZRingElem}:
 1
 23
 456

julia> [1,23,456]
3-element Vector{Int64}:
   1
  23
 456

julia> [0, 1//100,23//9,456//17]
4-element Vector{Rational{Int64}}:
   0//1
   1//100
  23//9
 456//17

julia> QQ.([0, 1//100,23//9,456//17])
4-element Vector{QQFieldElem}:
 0
 1//100
 23//9
 456//17

To improve our output, we could implement Base.alignment methods for our ring types.

A naive implementation for our integers and rationals could look like this:

Base.alignment(io::IO, x::ZZRingElem) = (length(string(x)), 0)
Base.alignment(io::IO, x::QQFieldElem) = (length(string(numerator(x))), length(string(denominator(x))))

which results in this output:

julia> ZZ.([1,23,456])
3-element Vector{ZZRingElem}:
   1
  23
 456

julia> QQ.([0, 1//100,23//9,456//17])
4-element Vector{QQFieldElem}:
   0
   1//100
  23//9
 456//17
@lgoettgens
Copy link
Collaborator

lgoettgens commented Dec 15, 2023

I like this very much.

Would it even be a good idea to give a default to all RingElem in AA, like base does it for Number

Base.alignment(io::IO, x::RingElem) = (Base.alignment_from_show(io, x), 0)

with some special dispatches for e.g. QQFieldElem.
For ZZRingElem this has the same behaviour as above, for polynomials this looks like the following:

julia> Rx, x = polynomial_ring(GF(2), "x")
(Univariate polynomial ring in x over GF(2), x)

julia> a = x^2+3x-4
x^2 + x

julia> [a,a^2,a^3,a^4,a^5]
5-element Vector{fpPolyRingElem}:
                x^2 + x
              x^4 + x^2
  x^6 + x^5 + x^4 + x^3
              x^8 + x^4
 x^10 + x^9 + x^6 + x^5

If not, I would only do it for certain types like originally proposed, but additionally for things like GF(p) which print like numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants