From 14b37998c4016f8fbe72febfcc2793f402ab8698 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Tue, 5 Dec 2023 16:32:28 +0100 Subject: [PATCH] first try on coefficients --- src/StarAlgebras.jl | 2 ++ src/coefficients.jl | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/coefficients.jl diff --git a/src/StarAlgebras.jl b/src/StarAlgebras.jl index aaeadad..11f2912 100644 --- a/src/StarAlgebras.jl +++ b/src/StarAlgebras.jl @@ -11,6 +11,8 @@ include("bases.jl") include("mstructures.jl") include("mtables.jl") +include("coefficients.jl") + include("types.jl") include("algebra_elts.jl") include("arithmetic.jl") diff --git a/src/coefficients.jl b/src/coefficients.jl new file mode 100644 index 0000000..92183eb --- /dev/null +++ b/src/coefficients.jl @@ -0,0 +1,55 @@ +""" + implements `Base.keys`, `Base.values` +""" +abstract type AbstractCoefficients{V,K} end + +struct DiracDelta{V,K} <: AbstractCoefficients{V,K} + value::V + element::K +end +DiracDelta(x) = DiracDelta(1, x) +Base.getindex(δ::DiracDelta{V,K}, i::K) = + ifelse(i == δ.element, δ.value, zero(δ.value)) + +Base.keys(δ::DiracDelta) = (δ.element,) +Base.values(δ::DiracDelta) = (δ.value,) + +struct SparseCoefficients{V,K,Vv<:AbstractVector{V},Vk<:AbstractVector{K}} <: AbstractCoefficients{V,K} + values::Vv + basis_elements::Vk +end + +Base.keys(sc::SparseCoefficients) = sc.basis_elements +Base.values(sc::SparseCoefficients) = sc.values + +function SparseCoefficients( + values::AbstractVector{V}, + basis_elements::AbstractVector{K} +) where {K,V} + return SparseCoefficients{V,K,typeof(values),typeof(basis)}(values, basis_elements) +end + +function mul!( + ms::MultiplicativeStructure, + res::SparseCoefficients, + v::AbstractCoefficients, + w::AbstractCoefficients, +) + for (a, kv) in pairs(v) + for (b, kw) in pairs(w) + c = ms[kv, kw] + unsafe_append!(res, c => a * b) + end + end + __canonicalize!(res) + return res +end + +function unsafe_append!(mc::SparseCoefficients, p::Pair{<:SparseCoefficients,T}) where {T} + c, val = p + append!(mc.basis_elements, keys(c)) + for v in values(c) + push!(mc.values, val * v) + end + return mc +end