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

Needed for SumOfSquares #34

Closed
wants to merge 1 commit into from
Closed
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/algebra_elts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
(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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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
Loading