Skip to content

Commit

Permalink
WIP: fiddle with promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jul 28, 2023
1 parent 65f8e07 commit b778db0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/polynomial-basetypes/mutable-dense-polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This polynomial type essentially uses an offset vector (`Vector{T}`,`order`) to
The typical offset is to have `0` as the order, but, say, to accomodate Laurent polynomials, or more efficient storage of basis elements any order may be specified.
This type trims trailing zeros and when the offset is not 0, trims the leading zeros.
This type trims trailing zeros and the leading zeros on construction.
"""
struct MutableDensePolynomial{B,T,X} <: AbstractUnivariatePolynomial{B,T, X}
Expand All @@ -20,9 +20,7 @@ struct MutableDensePolynomial{B,T,X} <: AbstractUnivariatePolynomial{B,T, X}
i = findlast(!iszero, cs)
if i == nothing
xs = T[]
elseif iszero(order)
xs = T[cs[i] for i 1:i]
else # shift if not 0
else
j = findfirst(!iszero, cs)
xs = T[cs[i] for i j:i]
order = order + j - 1
Expand Down
11 changes: 11 additions & 0 deletions src/promotions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Base.promote_rule(::Type{P}, ::Type{Q}) where {B,T,S,X,
P<:AbstractUnivariatePolynomial{B,T,X},
Q<:AbstractPolynomial{S,X}} = MutableDensePolynomial{B,promote_type(T,S),X}

Check warning on line 11 in src/promotions.jl

View check run for this annotation

Codecov / codecov/patch

src/promotions.jl#L11

Added line #L11 was not covered by tests

## XXX these are needed for rational-functions
Base.promote_rule(::Type{P}, ::Type{Q}) where {B,T,S,X,
P<:Polynomial{T,X},
Q<:AbstractUnivariatePolynomial{B,S,X}} = Polynomial{promote_type(T,S),X}

Base.promote_rule(::Type{P}, ::Type{Q}) where {B,T,S,X,
P<:AbstractUnivariatePolynomial{B,T,X},
Q<:Polynomial{S,X}} = Polynomial{promote_type(T,S),X}



# Methods to ensure that matrices of polynomials behave as desired
Base.promote_rule(::Type{P},::Type{Q}) where {T,X, P<:AbstractPolynomial{T,X},
S, Q<:AbstractPolynomial{S,X}} =

Check warning on line 26 in src/promotions.jl

View check run for this annotation

Codecov / codecov/patch

src/promotions.jl#L26

Added line #L26 was not covered by tests
Expand Down
5 changes: 3 additions & 2 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,9 @@ end
@testset for P in (Polynomial, ImmutablePolynomial, SparsePolynomial, LaurentPolynomial)

p,q = P([1,2], :x), P([1,2], :y)
P′′ = P <: Polynomials.AbstractUnivariatePolynomial ? LaurentPolynomial : Polynomial
#P′′ = P == LaurentPolynomial ? P : P′ # different promotion rule
P′′ = P′ #XXX treat LaurentPolynomial no differently
P′′ = Polynomial #XXX

# * should promote to Polynomial type if mixed (save Laurent Polynomial)
@testset "promote mixed polys" begin
Expand Down Expand Up @@ -1636,7 +1637,7 @@ end
@test printpoly_to_string(Polynomial(BigInt[1,0,1], :y)) == "1 + y^2"

# negative indices
@test printpoly_to_string(LaurentPolynomial([-1:3;], -2)) == "-x^-2 + 1 + 2*x + 3*x^2" # "-x⁻² + 1 + 2*x + 3*x²"
@test printpoly_to_string(LaurentPolynomial([-1:3;], -2)) == "-x⁻² + 1 + 2*x + 3*x²"
@test printpoly_to_string(SparsePolynomial(Dict(.=>(-2:2, -1:3)))) == "-x^-2 + 1 + 2*x + 3*x^2"
end

Expand Down

0 comments on commit b778db0

Please sign in to comment.