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

Fixes for Chebyshev #23

Merged
merged 2 commits into from
Mar 16, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Base.eltype(b::AbstractBasis) = eltype(typeof(b))
Base.keytype(::Type{<:AbstractBasis{T,I}}) where {T,I} = I
Base.keytype(b::AbstractBasis) = keytype(typeof(b))

key_type(b) = keytype(b)
# `keytype(::Type{SparseVector{V,K}})` is not defined so it falls
# back to `keytype{::Type{<:AbstractArray})` which returns `Int`.
key_type(::Type{SparseArrays.SparseVector{V,K}}) where {V,K} = K
key_type(v::SparseArrays.SparseVector) = key_type(typeof(v))

"""
ImplicitBasis{T,I}
Implicit bases are not stored in memory and can be potentially infinite.
Expand Down
37 changes: 7 additions & 30 deletions src/mstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ struct UnsafeAddMul{M<:Union{typeof(*),MultiplicativeStructure}}
structure::M
end

function MA.operate_to!(
res::SparseCoefficients,
ms::MultiplicativeStructure,
v::AbstractCoefficients,
w::AbstractCoefficients,
)
function MA.operate_to!(res, ms::MultiplicativeStructure, v, w)
if res === v || res === w
throw(ArgumentError("No alias allowed"))
end
MA.operate!(zero, res)
res = MA.operate!(UnsafeAddMul(ms), res, v, w)
__canonicalize!(res)
return res
return __canonicalize!(res)
end

function MA.operate!(
Expand All @@ -66,36 +63,16 @@ function MA.operate!(
return mc
end

function MA.operate!(
ms::UnsafeAddMul,
res::SparseCoefficients,
v::AbstractCoefficients,
w::AbstractCoefficients,
)
function MA.operate!(ms::UnsafeAddMul, res, v, w)
for (kv, a) in pairs(v)
for (kw, b) in pairs(w)
c = ms.structure(kv, kw) # ::AbstractCoefficients
c = ms.structure(kv, kw)
MA.operate!(UnsafeAddMul(*), res, a * b, c)
end
end
return res
end

function MA.operate_to!(
res::AbstractVector,
ms::MultiplicativeStructure,
X::AbstractVector,
Y::AbstractVector,
)
if res === X || res === Y
throw(ArgumentError("No alias allowed"))
end
MA.operate!(zero, res)
MA.operate!(UnsafeAddMul(ms), res, X, Y)
res = __canonicalize!(res)
return res
end

__canonicalize!(sv::SparseVector) = dropzeros!(sv)
__canonicalize!(v::AbstractVector) = v
struct DiracMStructure{Op} <: MultiplicativeStructure
Expand Down
10 changes: 9 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ struct AlgebraElement{A,T,V} <: MA.AbstractMutable
parent::A
end

function AlgebraElement(coeffs::AbstractVector, A::AbstractStarAlgebra)
function _sanity_checks(coeffs, A::AbstractStarAlgebra)
@assert key_type(coeffs) == keytype(basis(A))
end
function _sanity_checks(coeffs::AbstractVector, A::AbstractStarAlgebra)
@assert key_type(coeffs) == keytype(basis(A))
@assert Base.haslength(basis(A))
@assert length(coeffs) == length(basis(A))
end

function AlgebraElement(coeffs, A::AbstractStarAlgebra)
_sanity_checks(coeffs, A)
return AlgebraElement{typeof(A),valtype(coeffs),typeof(coeffs)}(coeffs, A)
end

Expand Down
Loading