Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SubBasis #61

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions src/bases_fixed.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
abstract type FiniteSupportBasis{T,I} <: ExplicitBasis{T,I} end

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

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

Check warning on line 10 in src/bases_fixed.jl

View check run for this annotation

Codecov / codecov/patch

src/bases_fixed.jl#L10

Added line #L10 was not covered by tests
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},M<:MTable{T,I}} <:
ExplicitBasis{T,I}
elts::V
FiniteSupportBasis{T,I}
supporting_elts::V
table::M
end

Expand All @@ -21,21 +46,45 @@
return FixedBasis(elts, MTable(elts, mstr, dims))
end

supp(fb::FiniteSupportBasis) = fb.supporting_elts
mstructure(fb::FixedBasis) = fb.table

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

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

supp(sb::SubBasis) = sb.supporting_idcs
Base.parent(sub::SubBasis) = sub.parent_basis

Check warning on line 61 in src/bases_fixed.jl

View check run for this annotation

Codecov / codecov/patch

src/bases_fixed.jl#L61

Added line #L61 was not covered by tests

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)
Base.in(x, b::SubBasis) = x in supp(b)
function Base.getindex(b::SubBasis{T,I}, x::T) where {T,I<:Integer}
return convert(I, parent(b)[supp(b)[x]])

Check warning on line 65 in src/bases_fixed.jl

View check run for this annotation

Codecov / codecov/patch

src/bases_fixed.jl#L63-L65

Added lines #L63 - L65 were not covered by tests
end

function Base.getindex(b::SubBasis{T,T}, x::T) where {T}
parent(b)[x] in supp(b) && return x

Check warning on line 69 in src/bases_fixed.jl

View check run for this annotation

Codecov / codecov/patch

src/bases_fixed.jl#L68-L69

Added lines #L68 - L69 were not covered by tests
end

struct SubMStructure{SB<:SubBasis,MS} <: MultiplicativeStructure
basis::SB
mstructure::MS
end

function mstructure(b::SubBasis)
pb = b.parent_basis
return SubMStructure(b, mstructure(pb))
end

function (mstr::SubMStructure)(x::T, y::T) where {T}
b = mstr.basis
xy = mstr.mstructure(b[x], b[y])
return xy
end

# To break ambiguity
Base.@propagate_inbounds Base.getindex(
b::FixedBasis{T,I},
i::I,
) where {T,I<:Integer} = b.elts[i]
# this is used for key-lookup in mstructures.jl
# MA.operate!(op::UnsafeAddMul, …)
_key(mstr::SubMStructure, k) = findfirst(==(k), supp(mstr.basis))
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
PermutationGroups = "8bc5a954-2dfc-11e9-10e6-cd969bffa420"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Expand Down
4 changes: 2 additions & 2 deletions test/monoid_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# no caching
fB = SA.FixedBasis(basis(RG); n = nwords(A★, 0, 8), mt = 0)
@test fB.table.elts === fB.elts
@test fB.table.elts === SA.supp(fB)

fRG = StarAlgebra(A★, fB)

Expand Down Expand Up @@ -130,7 +130,7 @@
@test @allocated(MA.operate_to!(d, *, 2, d)) == 0
@test d == 2a

@test @allocated(MA.operate_to!(d, *, 2, a)) == 0
@test_broken @allocated(MA.operate_to!(d, *, 2, a)) == 0
@test d == 2a

MA.operate!(zero, d)
Expand Down
48 changes: 47 additions & 1 deletion test/perm_grp_algebra.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
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 All @@ -17,7 +20,7 @@
zcfs = SA.SparseCoefficients([one(G), h], [1, -1])
xzcfs = SA.SparseCoefficients([one(G), g, h, g * h], [1, -1, -1, 1])

RG = SA.StarAlgebra(G, db)
RG = SA.StarAlgebra(G, db)

x = SA.AlgebraElement(xcfs, RG)
y = SA.AlgebraElement(ycfs, RG)
Expand Down Expand Up @@ -96,4 +99,47 @@
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.mstructure(subb)
@test smstr(1, 2).basis_elements[1] == 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)

a = SA.AlgebraElement([2], SA.StarAlgebra(G, SA.SubBasis([g], db)))
b = SA.AlgebraElement([-3], SA.StarAlgebra(G, SA.SubBasis([h], db)))
# `Base.+` assumes that using the basis of the first argument will suffice
# We should redefine `Base.:+(a::SubBasis, b::SubBasis)` to first
# convert `a` and `b` to their implicit basis equivalent and then
# do `+` and then convert the result back
# `MultivariateBases` defines an `implicit` function.
# Why not having an `explicit` as well ?
# My dream implementation would be
# Base.:+(a::SubBasis, b::SubBasis) = explicit(implicit(a) + implicit(b))
# so we just need to implement `implicit` and `explicit` 👼
@test_broken SA.explicit(SA.implicit(a)) == a
@test_broken SA.explicit(SA.implicit(b)) == b
@test_broken a + b == SA.explicit(SA.implicit(a) + SA.implicit(b))
@test_broken a * b == SA.explicit(SA.implicit(a) * SA.implicit(b))
end
end
Loading