mirror of
https://github.com/kalmarek/GroupRings.jl.git
synced 2024-08-16 10:01:56 +02:00
move array protocol for GroupRings to misc.jl and enhance it
This commit is contained in:
parent
a55d2e18f6
commit
c516ce286a
@ -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
55
src/misc.jl
Normal 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
|
||||
|
Loading…
Reference in New Issue
Block a user