From d64ecada19f603abe4a56461f27cd6cc92c4accd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 12 Jun 2024 16:25:56 +0200 Subject: [PATCH] up --- src/arithmetic.jl | 13 +++++++++++++ src/sparse_coeffs.jl | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/src/arithmetic.jl b/src/arithmetic.jl index 080cf77..85aaf6c 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -11,6 +11,10 @@ function _preallocate_output(X::AlgebraElement, Y::AlgebraElement, op) return similar(X, T) end +function MA.promote_operation(::typeof(similar), ::Type{<:Vector}, ::Type{T}) where {T} + return Vector{T} +end + # module structure: Base.:*(X::AlgebraElement, a::Number) = a * X @@ -27,12 +31,21 @@ function Base.:div(X::AlgebraElement, a::Number) return MA.operate_to!(_preallocate_output(X, a, div), div, X, a) end +function MA.promote_operation( + op::Union{typeof(+),typeof(-)}, + ::Type{AlgebraElement{A,T,VT}}, + ::Type{AlgebraElement{A,S,VS}}, +) where {A,T,VT,S,VS} + U = MA.promote_operation(op, T, S) + return AlgebraElement{A,U,MA.promote_operation(similar, VT, U)} +end function Base.:+(X::AlgebraElement, Y::AlgebraElement) return MA.operate_to!(_preallocate_output(X, Y, +), +, X, Y) end function Base.:-(X::AlgebraElement, Y::AlgebraElement) return MA.operate_to!(_preallocate_output(X, Y, -), -, X, Y) end + function Base.:*(X::AlgebraElement, Y::AlgebraElement) return MA.operate_to!(_preallocate_output(X, Y, *), *, X, Y) end diff --git a/src/sparse_coeffs.jl b/src/sparse_coeffs.jl index bd828c9..c9ebeb0 100644 --- a/src/sparse_coeffs.jl +++ b/src/sparse_coeffs.jl @@ -42,6 +42,14 @@ function Base.zero(sc::SparseCoefficients) return SparseCoefficients(empty(keys(sc)), empty(values(sc))) end +function MA.promote_operation( + ::typeof(similar), + ::Type{SparseCoefficients{K,V,Vk,Vv}}, + ::Type{T}, +) where {K,V,Vk,Vv,T} + return SparseCoefficients{K,T,Vk,MA.promote_operation(similar, Vv, T)} +end + function Base.similar(s::SparseCoefficients, ::Type{T} = valtype(s)) where {T} return SparseCoefficients(similar(s.basis_elements), similar(s.values, T)) end