Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 13, 2024
1 parent f700ed2 commit 59d8b91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/sparse_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 59d8b91

Please sign in to comment.