Skip to content

Commit

Permalink
Fix similar for Tuple (#51)
Browse files Browse the repository at this point in the history
* Fix similar for Tuple

* Add tests
  • Loading branch information
blegat authored Jul 2, 2024
1 parent c01cd3f commit f9ff304
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
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

0 comments on commit f9ff304

Please sign in to comment.