Skip to content
57 changes: 43 additions & 14 deletions src/bases_fixed.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
# This file is a part of StarAlgebras.jl. License is MIT: https://github.com/JuliaAlgebra/StarAlgebras.jl/blob/main/LICENSE
# Copyright (c) 2021-2025: Marek Kaluba, Benoît Legat

abstract type FiniteSupportBasis{T,I} <: ExplicitBasis{T,I} end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kalmarek What would be the difference between a FiniteSupportBasis and an ExplicitBasis ? I thought that an ExplicitBasis was a basis with a finite number of elements ^^


"""
supp(fb::FiniteSupportBasis)
Return the supporting elements of `fb` as an indexable vector
"""
function supp end
supp(fb::FiniteSupportBasis) = fb.supporting_elts

Base.IteratorSize(::Type{<:FiniteSupportBasis}) = Base.HasLength()
Base.IteratorEltype(::Type{<:FiniteSupportBasis{T}}) where {T} = T
Base.length(b::FiniteSupportBasis) = length(supp(b))

Base.iterate(b::FiniteSupportBasis) = iterate(supp(b))
Base.iterate(b::FiniteSupportBasis, state) = iterate(supp(b), state)
# function Base.IndexStyle(::Type{<:FiniteSupportBasis{T,I,V}}) where {T,I,V}
# return Base.IndexStyle(V)
# end

Base.@propagate_inbounds function Base.getindex(
b::FiniteSupportBasis,
i::Integer,
)
return supp(b)[i]
end

mutable struct FixedBasis{T,I,V<:AbstractVector{T}} <:
ExplicitBasis{T,I}
elts::V
FiniteSupportBasis{T,I}
supporting_elts::V
relts::Dict{T,I}
starof::Vector{I}
end
Expand All @@ -21,20 +47,23 @@ function FixedBasis{T,I}(basis::AbstractBasis{T}; n::Integer) where {T,I}
end

FixedBasis(basis::AbstractBasis{T}; n::Integer) where {T} = FixedBasis{T,typeof(n)}(basis; n)

Base.in(x, b::FixedBasis) = haskey(b.relts, x)
Base.getindex(b::FixedBasis{T}, x::T) where {T} = b.relts[x]
Base.getindex(b::FixedBasis, i::Integer) = b.elts[i]
Base.getindex(b::FixedBasis, i::Integer) = b.supporting_elts[i]

Base.IteratorSize(::Type{<:FixedBasis}) = Base.HasLength()
Base.length(b::FixedBasis) = length(b.elts)
struct SubBasis{T,I,K,V<:AbstractVector{K},B<:AbstractBasis{T,K}} <:
FiniteSupportBasis{T,I}
supporting_idcs::V
parent_basis::B
function SubBasis(supporting_idcs::AbstractVector{K}, parent_basis::AbstractBasis{T,K}) where {T,K}
return new{T,keytype(supporting_idcs),K,typeof(supporting_idcs),typeof(parent_basis)}(supporting_idcs, parent_basis)
end
end

Base.iterate(b::FixedBasis) = iterate(b.elts)
Base.iterate(b::FixedBasis, state) = iterate(b.elts, state)
Base.IndexStyle(::Type{<:FixedBasis{T,I,V}}) where {T,I,V} = Base.IndexStyle(V)
supp(sb::SubBasis) = sb.supporting_idcs
Base.parent(sub::SubBasis) = sub.parent_basis

# To break ambiguity
Base.@propagate_inbounds Base.getindex(
b::FixedBasis{T,I},
i::I,
) where {T,I<:Integer} = b.elts[i]
Base.in(x, b::SubBasis) = b.parent_basis[x] in supp(b)
function Base.getindex(b::SubBasis{T,I}, x::T) where {T,I}
return convert(I, findfirst(isequal(b.parent_basis[x]), supp(b)))
end
31 changes: 31 additions & 0 deletions test/perm_grp_algebra.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This file is a part of StarAlgebras.jl. License is MIT: https://github.com/JuliaAlgebra/StarAlgebras.jl/blob/main/LICENSE
# Copyright (c) 2021-2025: Marek Kaluba, Benoît Legat

using Test
using PermutationGroups
import StarAlgebras as SA
@testset "POC: group algebra" begin
G = PermGroup(perm"(1,2,3,4,5,6)", perm"(1,2)")
g = Permutation(perm"(1,4,3,6)(2,5)", G)
Expand Down Expand Up @@ -99,4 +102,32 @@
end
@test a ≤ b
end

@testset "FiniteSupportBasis" begin
S1 = unique!(rand(G, 7))
S = unique!([S1; [a * b for a in S1 for b in S1]])
subb = SA.SubBasis(S, db)
smstr = SA.DiracMStructure(subb, *)
@test only(smstr(1, 2).basis_elements) == subb[subb[1] * subb[2]]
@test only(smstr(1, 2, eltype(subb)).basis_elements) == subb[1] * subb[2]

sbRG = SA.StarAlgebra(G, subb)

x = let z = zeros(Int, length(SA.basis(sbRG)))
z[1:length(S1)] .= rand(-1:1, length(S1))
SA.AlgebraElement(z, sbRG)
end

y = let z = zeros(Int, length(SA.basis(sbRG)))
z[1:length(S1)] .= rand(-1:1, length(S1))
SA.AlgebraElement(z, sbRG)
end

dx = SA.AlgebraElement(SA.coeffs(x, SA.basis(RG)), RG)
dy = SA.AlgebraElement(SA.coeffs(y, SA.basis(RG)), RG)

@test dx + dy == SA.AlgebraElement(SA.coeffs(x + y, SA.basis(RG)), RG)

@test dx * dy == SA.AlgebraElement(SA.coeffs(x * y, SA.basis(RG)), RG)
end
end