Skip to content

Commit 7a8cbac

Browse files
committed
Improve conversion
1 parent 9f7ca6d commit 7a8cbac

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/conversion.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
export variable
22

3-
convertconstant(::Type{P}, α) where P<:APL = P(α)
3+
function convertconstant end
44
Base.convert(::Type{P}, α) where P<:APL = convertconstant(P, α)
5-
Base.convert(::Type{P}, p::APL) where P<:AbstractPolynomial = P(polynomial(p))
5+
function Base.convert(::Type{P}, p::P) where P<:AbstractPolynomial
6+
return p
7+
end
8+
function Base.convert(::Type{P}, p::APL) where {T, P<:AbstractPolynomial{T}}
9+
return convert(P, polynomial(p, T))
10+
end
611

712
Base.convert(::Type{Any}, p::APL) = p
813
# Conversion polynomial -> scalar

test/algebra.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
@test p == x^3 + 2x^2*y + x + 2y
4141
@test p isa AbstractPolynomial{Float64}
4242

43+
p = @inferred CustomPoly(x + 2y) + 1
44+
@test p == x + 2y + 1
45+
@test p isa AbstractPolynomial{Int}
46+
47+
p = @inferred CustomPoly(x + 2y) + 1.0
48+
@test p == x + 2y + 1
49+
@test p isa AbstractPolynomial{Float64}
50+
4351
@testset "Inference" begin
4452
@inferred x^2
4553
@inferred x^2-2x

test/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ struct CustomPoly{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
33
end
44
CustomPoly(p::AbstractPolynomial{T}) where T = CustomPoly{T, typeof(p)}(p)
55
MultivariatePolynomials.polynomial(p::CustomPoly) = p.p
6+
MultivariatePolynomials.polynomial(p::CustomPoly, T::Type) = polynomial(p.p, T)
7+
MultivariatePolynomials.variables(p::CustomPoly) = variables(p.p)
8+
MultivariatePolynomials.monomialtype(::Type{<:CustomPoly{T, P}}) where {T, P} = monomialtype(P)
9+
MultivariatePolynomials.constantmonomial(p::CustomPoly) = constantmonomial(p.p)
610

711
struct CustomTerms{T, P<:AbstractPolynomial{T}} <: AbstractPolynomialLike{T}
812
p::P
913
end
1014
CustomTerms(p::AbstractPolynomial{T}) where T = CustomTerms{T, typeof(p)}(p)
1115
MultivariatePolynomials.terms(p::CustomTerms) = terms(p.p)
16+
MultivariatePolynomials.variables(p::CustomTerms) = variables(p.p)
17+
MultivariatePolynomials.monomialtype(::Type{<:CustomTerms{T, P}}) where {T, P} = monomialtype(P)
18+
MultivariatePolynomials.constantmonomial(p::CustomPoly) = constantmonomial(p.p)
1219

1320
function _typetests(x, ::Type{T}) where T
1421
@test (@inferred coefficienttype(x)) == Int

0 commit comments

Comments
 (0)