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

fix for failing test in allocations #38

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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: 3 additions & 3 deletions src/algebra_elts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ function Base.deepcopy_internal(a::AlgebraElement, id::IdDict)
end

# call overload:
(a::AlgebraElement)(x) = coeffs(a)[basis(parent(a))[x]]
Base.setindex!(a::AlgebraElement, v, idx) = a.coeffs[basis(parent(a))[idx]] = v
(a::AlgebraElement)(x) = coeffs(a)[basis(a)[x]]
Base.setindex!(a::AlgebraElement, v, idx) = a.coeffs[basis(a)[idx]] = v

# AlgebraElement specific functions

function supp(a::AlgebraElement)
b = basis(parent(a))
b = basis(a)
return [b[i] for (i, _) in nonzero_pairs(coeffs(a))]
end

Expand Down
17 changes: 14 additions & 3 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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 All @@ -102,8 +102,19 @@
X::AlgebraElement,
Y::AlgebraElement,
)
@assert parent(res) === parent(X) === parent(Y)
mstr = mstructure(basis(parent(res)))
@assert parent(res) == parent(X) == parent(Y)
mstr = mstructure(basis(res))
MA.operate_to!(coeffs(res), mstr, coeffs(X), coeffs(Y))
return res
end

# TODO just push to internal vectors once canonical is implemented for SparseVector
function unsafe_push!(a::SparseArrays.SparseVector, k, v)
a[k] = MA.add!!(a[k], v)
return v

Check warning on line 114 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L112-L114

Added lines #L112 - L114 were not covered by tests
end

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

Check warning on line 119 in src/arithmetic.jl

View check run for this annotation

Codecov / codecov/patch

src/arithmetic.jl#L117-L119

Added lines #L117 - L119 were not covered by tests
end
6 changes: 5 additions & 1 deletion src/coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ end

# general mutable API
# why here?
MA.operate!(::typeof(zero), v::SparseVector) = (v .= 0; v)
function MA.operate!(::typeof(zero), v::SparseVector)
empty!(SparseArrays.nonzeroinds(v))
empty!(SparseArrays.nonzeros(v))
return v
end

Base.zero(X::AbstractCoefficients) = MA.operate!(zero, similar(X))
Base.:-(X::AbstractCoefficients) = MA.operate_to!(__prealloc(X, -1, *), -, X)
Expand Down
7 changes: 7 additions & 0 deletions src/sparse_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ function MA.operate!(::typeof(canonical), res::SparseCoefficients, cmp::C) where
return res
end

function MA.operate!(
::typeof(canonical),
res::SparseCoefficients{T,I,Tuple{T},Tuple{I}},
) where {T,I}
return res
end

# arithmetic on coefficients; performance overloads

function MA.operate!(::typeof(zero), s::SparseCoefficients)
Expand Down
2 changes: 1 addition & 1 deletion src/star.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Base.adjoint(a::AlgebraElement) = star(a)
star(x::Any) = x'

function star(X::AlgebraElement)
res = star(basis(parent(X)), coeffs(X))
res = star(basis(X), coeffs(X))
return AlgebraElement(res, parent(X))
end

Expand Down
7 changes: 5 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ end
basis(A::StarAlgebra) = A.basis
object(A::StarAlgebra) = A.object

function algebra end

struct AlgebraElement{A,T,V} <: MA.AbstractMutable
coeffs::V
parent::A
Expand All @@ -36,8 +38,9 @@ Base.parent(a::AlgebraElement) = a.parent
Base.eltype(a::AlgebraElement) = valtype(coeffs(a))
coeffs(a::AlgebraElement) = a.coeffs
function coeffs(x::AlgebraElement, b::AbstractBasis)
return coeffs(coeffs(x), basis(parent(x)), b)
return coeffs(coeffs(x), basis(x), b)
end
basis(a::AlgebraElement) = basis(parent(a))

function AlgebraElement(coeffs, A::AbstractStarAlgebra)
_sanity_checks(coeffs, A)
Expand Down Expand Up @@ -87,7 +90,7 @@ function Base.isone(a::AlgebraElement)
if basis(A) isa DiracBasis
return c == cfs1
else
dc = coeffs(c, basis(parent(a)), DiracBasis{UInt}(object(parent(a))))
dc = coeffs(c, basis(a), DiracBasis{UInt}(object(parent(a))))
return dc == cfs1
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/caching_allocations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end
@test Y * Y isa AlgebraElement
Y * Y
k2 = @allocated Y * Y
@test k2 / k1 < 0.7
@test k2 / k1 < 0.12
end

@test all(!iszero, SA.mstructure(fRG).table)
Expand Down
2 changes: 1 addition & 1 deletion test/group_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
z = sum((one(RG) - RG(g)) * star(one(RG) - RG(g)) for g in G)
@test SA.aug(z) == 0

@test SA.supp(z) == sort(collect(basis(parent(z))))
@test SA.supp(z) == sort(collect(basis(z)))
@test SA.supp(RG(1) + RG(g)) == [one(G), g]
@test SA.supp(a) == [one(G), h, g]

Expand Down
Loading