diff --git a/src/coefficients.jl b/src/coefficients.jl index 735be39..f7fd687 100644 --- a/src/coefficients.jl +++ b/src/coefficients.jl @@ -48,30 +48,25 @@ If `ac` can be brought to canonical form in-place one has to implement otherwise `canonical(ac)` needs to be implemented. """ -canonical(ac::AbstractCoefficients) = ac -MA.operate(::typeof(canonical), x) = canonical(x) # fallback? +function canonical end +function MA.promote_operation(::typeof(canonical), ::Type{C}) where {C} + return C +end # example implementation for vectors -function MA.mutability( - ::Type{<:Union{<:SparseVector,<:Vector}}, - ::typeof(canonical), - ::Vararg{Type}, -) - return MA.IsMutable() -end MA.operate!(::typeof(canonical), sv::SparseVector) = dropzeros!(sv) MA.operate!(::typeof(canonical), v::Vector) = v function Base.:(==)(ac1::AbstractCoefficients, ac2::AbstractCoefficients) - ac1 = MA.operate!!(canonical, ac1) - ac2 = MA.operate!!(canonical, ac2) + MA.operate!(canonical, ac1) + MA.operate!(canonical, ac2) all(x -> ==(x...), zip(keys(ac1), keys(ac2))) || return false all(x -> ==(x...), zip(values(ac1), values(ac2))) || return false return true end function Base.hash(ac::AbstractCoefficients, h::UInt) - ac = MA.operate!!(canonical, ac) + MA.operate!(canonical, ac) return foldl((h, i) -> hash(i, h), nonzero_pairs(ac); init = h) end diff --git a/src/diracs_augmented.jl b/src/diracs_augmented.jl index 904a874..e555472 100644 --- a/src/diracs_augmented.jl +++ b/src/diracs_augmented.jl @@ -18,7 +18,7 @@ function Base.getindex(aδ::Augmented{K}, i::K) where {K} return zero(w) end -canonical(aδ::Augmented) = aδ +MA.operate!(::typeof(canonical), aδ::Augmented) = aδ Base.keys(aδ::Augmented) = (k = keys(aδ.elt); (one(first(k)), first(k))) function Base.values(aδ::Augmented) @@ -129,5 +129,6 @@ function coeffs!( SparseCoefficients((target[Augmented(x)],), (1,)), ) end - return MA.operate!!(canonical, res) + MA.operate!(canonical, res) + return res end diff --git a/src/mstructures.jl b/src/mstructures.jl index d0fc193..2cf3e73 100644 --- a/src/mstructures.jl +++ b/src/mstructures.jl @@ -49,8 +49,9 @@ function MA.operate_to!(res, ms::MultiplicativeStructure, v, w) throw(ArgumentError("No alias allowed")) end MA.operate!(zero, res) - res = MA.operate!(UnsafeAddMul(ms), res, v, w) - return MA.operate!!(canonical, res) + MA.operate!(UnsafeAddMul(ms), res, v, w) + MA.operate!(canonical, res) + return res end function MA.operate!( diff --git a/test/test_example_acoeffs.jl b/test/test_example_acoeffs.jl index 3a21966..2c9272f 100644 --- a/test/test_example_acoeffs.jl +++ b/test/test_example_acoeffs.jl @@ -10,7 +10,7 @@ ACoeffs(v::AbstractVector) = ACoeffs{valtype(v)}(v) ## Basic API Base.keys(ac::ACoeffs) = (k for (k, v) in pairs(ac.vals) if !iszero(v)) Base.values(ac::ACoeffs) = (v for v in ac.vals if !iszero(v)) -SA.canonical(ac::ACoeffs) = ac +MA.operate!(::typeof(SA.canonical), ac::ACoeffs) = ac function SA.star(b::SA.AbstractBasis, ac::ACoeffs) return ACoeffs([ac.vals[star(b, k)] for k in keys(ac)]) end