Skip to content

Commit 1b4ebdc

Browse files
authored
Merge pull request #96 from JuliaAlgebra/bl/convert_alg
Conversion of AlgebraElement
2 parents 81e4736 + 2be46ef commit 1b4ebdc

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/sparse_coeffs.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ function Base.similar(s::SparseCoefficients, ::Type{T} = value_type(s)) where {T
120120
return SparseCoefficients(collect(s.basis_elements), _similar(s.values, T), s.isless)
121121
end
122122

123+
function Base.convert(
124+
::Type{SparseCoefficients{K,V,Vh,Vv,L}},
125+
s::SparseCoefficients{K,V,Vh,Vv,L},
126+
) where {K,V,Vh,Vv,L}
127+
return s
128+
end
129+
130+
_collect_if_needed(::Type, v) = v
131+
_collect_if_needed(::Type{<:AbstractVector}, t::Tuple) = collect(t)
132+
133+
function Base.convert(
134+
::Type{SparseCoefficients{K,V,Vh,Vv,L}},
135+
s::SparseCoefficients,
136+
) where {K,V,Vh,Vv,L}
137+
return SparseCoefficients{K,V,Vh,Vv,L}(
138+
_collect_if_needed(Vh, s.basis_elements),
139+
_collect_if_needed(Vv, s.values),
140+
s.isless,
141+
)
142+
end
143+
123144
function map_keys(f::Function, s::SparseCoefficients)
124145
return SparseCoefficients(map(f, s.basis_elements), s.values, s.isless)
125146
end

src/types.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,13 @@ function AlgebraElement{T}(X::AlgebraElement) where {T}
146146
end
147147
return AlgebraElement(w, parent(X))
148148
end
149+
150+
function Base.convert(::Type{AlgebraElement{A,T,V}}, a::AlgebraElement{A,T,V}) where {A,T,V}
151+
return a
152+
end
153+
154+
# Useful for instance if `V` is `SparseCoefficients` with `Tuple`
155+
# and `U` is `SparseCoefficients` with `Vector`
156+
function Base.convert(::Type{AlgebraElement{A,T,U}}, a::AlgebraElement{A,T,V}) where {A,T,U,V}
157+
return AlgebraElement(convert(U, coeffs(a)), parent(a))
158+
end

test/caching_allocations.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ end
8787
(1,),
8888
)
8989
Z = AlgebraElement(z, fRG)
90+
91+
@test convert(typeof(SA.coeffs(Y)), SA.coeffs(Y)) === SA.coeffs(Y)
92+
@test convert(typeof(Y), Y) === Y
93+
@test convert(typeof(Y), Z) !== Z
94+
@test convert(typeof(Y), Z) isa typeof(Y)
95+
@test convert(typeof(Y), Z) == Z
96+
9097
@test _test_op(+, Z, Z) == _test_op(*, 2, Z)
9198
@test _test_op(+, Z, Z) == _test_op(+, Y, Y)
9299
@test _test_op(+, Y, Z) == _test_op(+, Y, Y)

0 commit comments

Comments
 (0)