For Grassmann.jl, I would like to customize the printing similar to the
Complex numbers. Consider:
julia> using Grassmann
julia> @basis tangent(ℝ^2,2,2) # 2D Grade, 2nd Order, 2 Variables
(⟨++₁₂⟩, v, v₁, v₂, ∂₁, ∂₂, v₁₂, ∂₁v₁, ∂₂v₁, ∂₁v₂, ∂₂v₂, ∂₁₂, ∂₁v₁₂, ∂₂v₁₂, ∂₁₂v₁, ∂₁₂v₂, ∂₁₂v₁₂)
julia> ∇ = ∂1v1 + ∂2v2
0v₁₂ + 1∂₁v₁ + 0∂₂v₁ + 0∂₁v₂ + 1∂₂v₂ + 0∂₁₂
:(1 / 0.0v₁ + 0.0v₂ + 1∂₁∂₁ + 1∂₂∂₂)
The problem is that when I embed my number in an
Expr there is ambiguity due to missing parenthesis.
julia> z = 1+im
1 + 1im
:(1 / (1 + 1im))
Complex numbers this is resolved by adding extra
(...) parenthesis around the printed value.
How can I customize the printing so that my numbers are also parenthesized in an
Expr like this?
You can have a look at how it’s defined for
@edit show(stdin, 1.0im). It looks like you just need to overload
Base.show_unquoted. (In the lines below)
1 + 1im
this does not seem to reproduce the print behavior I am after, it should be
(1 + 1im) instead
You need to pass the outer operator precedence as well.
julia> Base.show_unquoted(stdin, 1.0im, 0#=Don't know what this does, but it isn't used by Complex=#, Base.operator_precedence(:*))
(0.0 + 1.0im)
This new definition does what I need, which will go into the AbstractTensors.jl package
function Base.show_unquoted(io::IO, z::T, ::Int, prec::Int) where T<:TensorAlgebra
if T<:TensorMixed && Base.operator_precedence(:+) <= prec