Skip to content

Commit

Permalink
Fixes for SumOfSquares
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 14, 2024
1 parent c01cd3f commit b91aa72
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/algebra_elts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ end
(a::AlgebraElement)(x) = coeffs(a)[basis(a)[x]]
Base.setindex!(a::AlgebraElement, v, idx) = a.coeffs[basis(a)[idx]] = v

function nonzero_pairs(a::AlgebraElement)
return Base.Generator(nonzero_pairs(coeffs(a))) do (k, v)
return basis(a)[k], v

Check warning on line 23 in src/algebra_elts.jl

View check run for this annotation

Codecov / codecov/patch

src/algebra_elts.jl#L21-L23

Added lines #L21 - L23 were not covered by tests
end
end

# AlgebraElement specific functions

function supp(a::AlgebraElement)
Expand Down
20 changes: 19 additions & 1 deletion src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function _preallocate_output(op, args::Vararg{Any,N}) where {N}
return similar(args[1], T)
end

function MA.promote_operation(::typeof(similar), ::Type{<:Vector}, ::Type{T}) where {T}
return Vector{T}

Check warning on line 13 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L12-L13

Added lines #L12 - L13 were not covered by tests
end

# module structure:

Base.:*(X::AlgebraElement, a::Number) = a * X
Expand All @@ -25,12 +29,21 @@ function Base.:div(X::AlgebraElement, a::Number)
return MA.operate_to!(_preallocate_output(div, X, a), div, X, a)
end

function MA.promote_operation(

Check warning on line 32 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L32

Added line #L32 was not covered by tests
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)}

Check warning on line 38 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L37-L38

Added lines #L37 - L38 were not covered by tests
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 Expand Up @@ -92,7 +105,7 @@ function MA.operate_to!(
X::AlgebraElement,
Y::AlgebraElement,
)
@assert parent(res) === parent(X) === parent(Y)
@assert parent(res) == parent(X) == parent(Y)
MA.operate_to!(coeffs(res), -, coeffs(X), coeffs(Y))
return res
end
Expand Down Expand Up @@ -138,3 +151,8 @@ function unsafe_push!(a::Vector, k, v)
a[k] = MA.add!!(a[k], v)
return a
end

function unsafe_push!(a::AlgebraElement, k, v)
unsafe_push!(coeffs(a), basis(a)[k], v)
return a

Check warning on line 157 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L155-L157

Added lines #L155 - L157 were not covered by tests
end
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(

Check warning on line 45 in src/sparse_coeffs.jl

View check run for this annotation

Codecov / codecov/patch

src/sparse_coeffs.jl#L45

Added line #L45 was not covered by tests
::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)}

Check warning on line 50 in src/sparse_coeffs.jl

View check run for this annotation

Codecov / codecov/patch

src/sparse_coeffs.jl#L50

Added line #L50 was not covered by tests
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
7 changes: 6 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ struct StarAlgebra{O,T,B<:AbstractBasis{T}} <: AbstractStarAlgebra{O,T}
end

basis(A::StarAlgebra) = A.basis
MA.promote_operation(::typeof(basis), ::Type{StarAlgebra{O,T,B}}) where {O,T,B} = B

Check warning on line 28 in src/types.jl

View check run for this annotation

Codecov / codecov/patch

src/types.jl#L28

Added line #L28 was not covered by tests
object(A::StarAlgebra) = A.object

function algebra end

struct AlgebraElement{A,T,V} <: MA.AbstractMutable
coeffs::V
parent::A
end

Base.parent(a::AlgebraElement) = a.parent
Base.eltype(a::AlgebraElement) = valtype(coeffs(a))
Base.eltype(a::AlgebraElement) = eltype(typeof(a))
Base.eltype(::Type{<:AlgebraElement{A,T}}) where {A,T} = T
coeffs(a::AlgebraElement) = a.coeffs
function coeffs(x::AlgebraElement, b::AbstractBasis)
return coeffs(coeffs(x), basis(x), b)
Expand All @@ -42,6 +46,7 @@ function adjoint_coeffs(a::AlgebraElement, target::AbstractBasis)
return adjoint_coeffs(coeffs(a), target, basis(a))
end
basis(a::AlgebraElement) = basis(parent(a))
MA.promote_operation(::typeof(basis), ::Type{<:AlgebraElement{A}}) where {A} = MA.promote_operation(basis, A)

Check warning on line 49 in src/types.jl

View check run for this annotation

Codecov / codecov/patch

src/types.jl#L49

Added line #L49 was not covered by tests

function AlgebraElement(coeffs, A::AbstractStarAlgebra)
_sanity_checks(coeffs, A)
Expand Down

0 comments on commit b91aa72

Please sign in to comment.