diff --git a/src/GroupRings.jl b/src/GroupRings.jl index f733533..1db09a2 100644 --- a/src/GroupRings.jl +++ b/src/GroupRings.jl @@ -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) diff --git a/src/misc.jl b/src/misc.jl new file mode 100644 index 0000000..bdcceda --- /dev/null +++ b/src/misc.jl @@ -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 +