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 similar for Tuple #51

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function MA.operate_to!(
X::AlgebraElement,
Y::AlgebraElement,
)
@assert parent(res) === parent(X)
@assert parent(X) === parent(Y)
@assert parent(res) == parent(X)
@assert parent(X) == parent(Y)
MA.operate_to!(coeffs(res), +, coeffs(X), coeffs(Y))
return res
end
Expand Down
7 changes: 6 additions & 1 deletion src/sparse_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ function Base.zero(sc::SparseCoefficients)
return SparseCoefficients(empty(keys(sc)), empty(values(sc)))
end

_similar(x::Tuple) = _similar(x, typeof(x[1]))
_similar(x::Tuple, ::Type{T}) where {T} = Vector{T}(undef, length(x))
_similar(x) = similar(x)
_similar(x, ::Type{T}) where {T} = similar(x, T)

function Base.similar(s::SparseCoefficients, ::Type{T} = valtype(s)) where {T}
return SparseCoefficients(similar(s.basis_elements), similar(s.values, T))
return SparseCoefficients(_similar(s.basis_elements), _similar(s.values, T))
end

function MA.mutability(
Expand Down
25 changes: 25 additions & 0 deletions test/caching_allocations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ end
_alloc_test(YY, +, Y, YY, 2)
end
end

@testset "tuple" begin
alph = [:a, :b, :c]
A★ = FreeWords(alph)
B = SA.DiracBasis(A★)

fB = SA.FixedBasis(B; n = nwords(A★, 2), mt = UInt32(nwords(A★, 2)))
fRG = StarAlgebra(A★, fB)

y = SA.SparseCoefficients(
[first(fB)],
[1],
)
Y = AlgebraElement(y, fRG)

z = SA.SparseCoefficients(
(first(fB),),
(1,),
)
Z = AlgebraElement(z, fRG)
@test Z + Z == 2 * Z
@test Z + Z == Y + Y
@test Y + Z == Y + Y
@test Z + Y == Y + Y
end
Loading