Skip to content

Commit

Permalink
Merge branch 'master' into lg/show-name
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Feb 5, 2024
2 parents 84dbaa3 + f7a5678 commit a4b503a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/docs/build/
Manifest.toml
LocalPreferences.toml
.DS_Store
3 changes: 3 additions & 0 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const import_exclude = [:import_exclude, :QQ, :ZZ,
# imported here and in Generic.jl, and exported below.
# They should not be imported/exported anywhere else.

import LinearAlgebra

import LinearAlgebra: det
import LinearAlgebra: dot
import LinearAlgebra: hessenberg
import LinearAlgebra: ishermitian
import LinearAlgebra: issymmetric
Expand Down
12 changes: 12 additions & 0 deletions src/NCRings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,15 @@ Base.oftype(x::NCRingElem, y::Rational) = parent(x)(y)
###############################################################################

Base.broadcastable(x::NCRingElem) = Ref(x)

################################################################################
#
# Dot
#
################################################################################

dot(x::NCRingElem, y::NCRingElem) = x * y

dot(x::NCRingElem, y::Union{Integer, Rational, AbstractFloat}) = x * y

dot(x::Union{Integer, Rational, AbstractFloat}, y::NCRingElem) = x * y
3 changes: 0 additions & 3 deletions src/NemoStuff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,6 @@ end

Random.gentype(::Type{T}) where {T<:FinField} = elem_type(T)

import LinearAlgebra
LinearAlgebra.dot(a::NCRingElem, b::NCRingElem) = a * b

transpose!(A::MatrixElem) = transpose(A)

function Base.div(f::PolyRingElem, g::PolyRingElem)
Expand Down
69 changes: 56 additions & 13 deletions src/PrettyPrinting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1648,24 +1648,67 @@ end
#
################################################################################

# A non-compiletime preference
function allow_unicode(flag::Bool)
old_flag = is_unicode_allowed()
@set_preferences!("unicode" => flag)
return old_flag
const ALLOW_UNICODE_OVERRIDE_VALUE = Ref{Union{Bool,Nothing}}(nothing)

@doc """
allow_unicode(allowed::Bool; temporary::Bool=false) -> Bool
Set whether unicode characters are allowed in pretty printing and returns the
previous value.
If `temporary` is `true`, then the change is only active for the current worker and session.
Otherwise, the change is permanently saved in the preferences.
A permanent change will always override a temporary change.
This function may behave arbitrarily if called from within the scope of a
`with_unicode` do-block.
"""
function allow_unicode(allowed::Bool; temporary::Bool=false)
if temporary
old_allowed = is_unicode_allowed()
ALLOW_UNICODE_OVERRIDE_VALUE[] = allowed
return old_allowed
else
old_allowed = is_unicode_allowed()
@set_preferences!("unicode" => allowed)
ALLOW_UNICODE_OVERRIDE_VALUE[] = nothing
return old_allowed
end
end

@doc """
is_unicode_allowed() -> Bool
Return whether unicode characters are allowed in pretty printing.
"""
function is_unicode_allowed()
return @load_preference("unicode", default = false)
override = ALLOW_UNICODE_OVERRIDE_VALUE[]
!isnothing(override) && return override
return @load_preference("unicode", default = false)::Bool
end

function with_unicode(f::Function)
old_allow_unicode = allow_unicode(true)
try
f()
finally
allow_unicode(old_allow_unicode)
end
@doc """
with_unicode(f::Function, allowed::Bool=true)
Temporarily set whether unicode characters are allowed in pretty printing
during the execution of `f`.
This is useful for e.g. running doctests independently on the user preference.
`with_unicode` is expected to be called in the following way:
```julia
with_unicode([allowed]) do
# code that should be executed with unicode allowed/disallowed
end
```
"""
function with_unicode(f::Function, allowed::Bool=true)
previous = ALLOW_UNICODE_OVERRIDE_VALUE[]
ALLOW_UNICODE_OVERRIDE_VALUE[] = allowed
try
f()
finally
@assert ALLOW_UNICODE_OVERRIDE_VALUE[] == allowed
ALLOW_UNICODE_OVERRIDE_VALUE[] = previous
end
end

################################################################################
Expand Down
19 changes: 19 additions & 0 deletions test/NCRings-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ end
M = matrix_space(ZZ, 2, rand(3:9))
@test_throws DomainError powers(rand(M, 1:9), rand(1:9))
end

@testset "dot" begin
dot = AbstractAlgebra.LinearAlgebra.dot

Qx, x = QQ["x"]
@test dot([x, x^2], [1, 1]) == x + x^2
@test dot([x, x^2], Rational{BigInt}[1, 1]) == x + x^2
@test dot([1, 1], [x, x^2]) == x + x^2
@test dot(Rational{BigInt}[1, 1], [x, x^2]) == x + x^2
@test dot([x], [x^2]) == x^3

R = matrix_ring(QQ, 2)
x = R([1 2; 3 4])
@test dot([x, x^2], [1, 1]) == x + x^2
@test dot([x, x^2], Rational{BigInt}[1, 1]) == x + x^2
@test dot([1, 1], [x, x^2]) == x + x^2
@test dot(Rational{BigInt}[1, 1], [x, x^2]) == x + x^2
@test dot([x], [x^2]) == x^3
end

0 comments on commit a4b503a

Please sign in to comment.