Skip to content

Hack to "improve" (?) printing of vectors of ring/field/module elements #2064

@fingolfin

Description

@fingolfin
summary(io::IO, a::AbstractArray{<:RingElem}) = oscar_array_summary(io, a, axes(a))
function oscar_array_summary(io::IO, a, inds::Tuple{Vararg{OneTo}})
    print(io, Base.dims2string(length.(inds)), " ")
    showarg(io, a, true)
end

Before:

julia> v
2-element Vector{AbstractAlgebra.Generic.MPoly{nf_elem}}:
 x^2 - 3*y
 1//4*x^2 - 1//2*sqrt(3)*x*y + 3//2*sqrt(3)*x + 3//4*y^2 + 3//2*y

Apply this hack (?):

function Base.showarg(io::IO, x::Vector{<:RingElem}, toplevel::Bool)
    if toplevel
      print(io, "Vector of ring elements")
      return
    end
    # original code follows
    toplevel || print(io, "::")
    print(io, typeof(x))
end

After:

julia> v
2-element Vector of ring elements:
 x^2 - 3*y
 1//4*x^2 - 1//2*sqrt(3)*x*y + 3//2*sqrt(3)*x + 3//4*y^2 + 3//2*y

Of course one could do something similar for FieldElem, ModuleElem, etc.
and the output could be refined, e.g. it could print the parent (though I'd
not do this for now, as it raises many questions: handle empty vectors; should
it check that all entries have the same parent; and if not, what then? etc.)

Questions:

  1. Is this type piracy? Vector is not our type, but RingElem is...
  2. Would this be useful for improving our printing situation?
  3. Where else is using showarg? If the above is too broad, we could also
    instead override summary(io::IO, x::Vector{<:RingElem}).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions