move array protocol for GroupRings to misc.jl and enhance it

This commit is contained in:
kalmarek 2019-06-04 20:08:22 +02:00
parent a55d2e18f6
commit c516ce286a
No known key found for this signature in database
GPG Key ID: 8BF1A3855328FC15
2 changed files with 55 additions and 47 deletions

View File

@ -12,51 +12,6 @@ import Base: convert, show, hash, ==, +, -, *, ^, //, /, length, getindex, setin
export GroupRing, GroupRingElem, complete!, create_pm, star, aug, supp
###############################################################################
#
# Basic manipulation && Array protocol
#
###############################################################################
function hash(X::GroupRingElem, h::UInt)
return hash(X.coeffs, hash(parent(X), hash(GroupRingElem, h)))
end
function getindex(X::GroupRingElem, n::Int)
return X.coeffs[n]
end
function getindex(X::GroupRingElem, g::GroupElem)
return X.coeffs[parent(X).basis_dict[g]]
end
function setindex!(X::GroupRingElem, value, n::Int)
X.coeffs[n] = value
end
function setindex!(X::GroupRingElem, value, g::GroupElem)
RG = parent(X)
if !(g in keys(RG.basis_dict))
g = (RG.group)(g)
end
X.coeffs[RG.basis_dict[g]] = value
end
Base.size(X::GroupRingElem) = size(X.coeffs)
Base.IndexStyle(::Type{GroupRingElem}) = Base.LinearFast()
dense(X::GroupRingElem{T, A}) where {T, A<:DenseVector} = X
function dense(X::GroupRingElem{T, Sp}) where {T, Sp<:SparseVector}
return parent(X)(Vector(X.coeffs))
end
SparseArrays.sparse(X::GroupRingElem{T, Sp}) where {T, Sp<:SparseVector} = X
function SparseArrays.sparse(X::GroupRingElem{T, A}) where {T, A<:Vector}
return parent(X)(sparse(X.coeffs))
end
###############################################################################
#
# String I/O
@ -151,8 +106,6 @@ end
#
###############################################################################
length(X::GroupRingElem) = count(!iszero, X.coeffs)
LinearAlgebra.norm(X::GroupRingElem, p::Int=2) = norm(X.coeffs, p)
aug(X::GroupRingElem) = sum(X.coeffs)

55
src/misc.jl Normal file
View File

@ -0,0 +1,55 @@
###############################################################################
#
# Array protocol
#
###############################################################################
### getindex for GroupRing
function Base.getindex(RG::GroupRing, i::Integer)
@assert hasbasis(RG)
return RG.basis[i]
end
function Base.getindex(RG::GroupRing{R,G,El}, g::El) where {R,G,El<:GroupElem}
@assert hasbasis(RG)
return RG.basis_dict[g]
end
Base.length(RG::GroupRing) = hasbasis(RG) ? length(RG.basis) : maximum(RG)
### Array protocol for GroupRingElem
Base.eltype(::Type{<:GroupRingElem{T}}) where T = T
Base.size(X::GroupRingElem) = size(X.coeffs)
Base.IndexStyle(::Type{GroupRingElem}) = Base.LinearFast()
Base.@propagate_inbounds function Base.getindex(X::GroupRingElem, i::Integer)
return X.coeffs[i]
end
Base.@propagate_inbounds function Base.setindex!(X::GroupRingElem, val, i::Integer)
return X.coeffs[i] = val
end
Base.similar(X::GroupRingElem) = GroupRingElem(similar(X.coeffs), parent(X), check=false)
function Base.similar(X::GroupRingElem, ::Type{T}) where T
RG = change_base_ring(parent(X), parent(T()))
return GroupRingElem(similar(X.coeffs, T), RG)
end
### indexing via group elements
Base.@propagate_inbounds function Base.getindex(X::GroupRingElem, g::GroupElem)
RG = parent(X)
@assert hasbasis(RG)
return X.coeffs[RG[g]]
end
Base.@propagate_inbounds function Base.setindex!(X::GroupRingElem, val, g::GroupElem)
RG = parent(X)
@assert hasbasis(RG)
return X.coeffs[RG[g]] = val
end