1
0
mirror of https://github.com/kalmarek/GroupRings.jl.git synced 2024-12-28 18:50:29 +01:00

add promotion, rand isapprox and isunit

This commit is contained in:
kalmarek 2019-06-04 22:48:27 +02:00
parent 1949b908e5
commit 194cdf64eb
No known key found for this signature in database
GPG Key ID: 8BF1A3855328FC15

View File

@ -121,3 +121,49 @@ function ==(A::GroupRing, B::GroupRing)
end
end
###############################################################################
#
# promotion, rand, isapprox
#
###############################################################################
Base.promote_rule(::Type{<:GroupRingElem{T}},::Type{T}) where T = GroupRingElem{T}
function Base.promote_rule(::Type{<:GroupRingElem{T}}, ::Type{U}) where {T, U<:RingElement}
return (promote_rule(T, U) == T ? GroupRingElem{T} : Union{})
end
function Base.rand(RG::GroupRing, density=0.05, args...)
l = length(RG)
if cachesmultiplication(RG)
nzind = rand(1:size(RG.pm, 1), floor(Int, density*l))
else
nzind = rand(1:l, floor(Int, density*l))
end
nzval = [rand(base_ring(RG), args...) for _ in nzind]
return GroupRingElem(sparsevec(nzind, nzval, l), RG)
end
function Base.isapprox(X::GroupRingElem{T}, Y::GroupRingElem{S};
atol::Real=sqrt(eps())) where {T,S}
parent(X) == parent(Y) || return false
return isapprox(X.coeffs, Y.coeffs, atol=atol)
end
Base.isapprox(X::GroupRingElem{T}, a::T; atol::Real=sqrt(eps())) where T = isapprox(X, RG(a))
Base.isapprox(a::T, X::GroupRingElem{T}; atol::Real=sqrt(eps())) where T = isapprox(X,a)
###############################################################################
#
# Additional functionality
#
###############################################################################
function AbstractAlgebra.isunit(X::GroupRingElem)
count(!iszero, X.coeffs) == 1 || return false
return isunit(X[supp(X)[1]])
end