From b778db0ed853764b7a4fad451f0d4e48639ccd9c Mon Sep 17 00:00:00 2001 From: jverzani Date: Fri, 28 Jul 2023 17:23:49 -0400 Subject: [PATCH] WIP: fiddle with promotion --- src/polynomial-basetypes/mutable-dense-polynomial.jl | 6 ++---- src/promotions.jl | 11 +++++++++++ test/StandardBasis.jl | 5 +++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/polynomial-basetypes/mutable-dense-polynomial.jl b/src/polynomial-basetypes/mutable-dense-polynomial.jl index e520b570..1d1c45cd 100644 --- a/src/polynomial-basetypes/mutable-dense-polynomial.jl +++ b/src/polynomial-basetypes/mutable-dense-polynomial.jl @@ -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} @@ -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 diff --git a/src/promotions.jl b/src/promotions.jl index cb4b3c05..0689b1ea 100644 --- a/src/promotions.jl +++ b/src/promotions.jl @@ -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} +## 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}} = diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index ec2b08ae..06e8a242 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -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 @@ -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