diff --git a/Project.toml b/Project.toml index 8e9f4da8..7e848813 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Polynomials" uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" license = "MIT" author = "JuliaMath" -version = "1.0.4" +version = "1.0.5" [deps] Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5" diff --git a/src/common.jl b/src/common.jl index 58991362..6830dc2f 100644 --- a/src/common.jl +++ b/src/common.jl @@ -253,6 +253,14 @@ variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = P([0 variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var) variable(var::SymbolLike = :x) = variable(Polynomial{Int}) +""" + check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial) + +Check if either `p` or `q` is constant or if `p` and `q` share the same variable +""" +check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial) = + (Polynomials.isconstant(p) || Polynomials.isconstant(q)) || p.var == q.var + #= Linear Algebra =# """ @@ -318,6 +326,17 @@ has a nonzero coefficient. The degree of the zero polynomial is defined to be -1 """ degree(p::AbstractPolynomial) = iszero(p) ? -1 : length(p) - 1 + +""" + isconstant(::AbstractPolynomial) + +Is the polynomial `p` a constant. +""" +isconstant(p::AbstractPolynomial) = degree(p) <= 0 + + + + hasnan(p::AbstractPolynomial) = any(isnan.(coeffs(p))) """ @@ -535,9 +554,7 @@ function Base.isapprox(p1::AbstractPolynomial{T}, rtol::Real = (Base.rtoldefault(T, S, 0)), atol::Real = 0,) where {T,S} p1, p2 = promote(p1, p2) - if p1.var != p2.var - error("p1 and p2 must have same var") - end + check_same_variable(p1, p2) || error("p1 and p2 must have same var") p1t = truncate(p1; rtol = rtol, atol = atol) p2t = truncate(p2; rtol = rtol, atol = atol) if length(p1t) ≠ length(p2t) diff --git a/src/polynomials/ImmutablePolynomial.jl b/src/polynomials/ImmutablePolynomial.jl index a918c599..cf4916d4 100644 --- a/src/polynomials/ImmutablePolynomial.jl +++ b/src/polynomials/ImmutablePolynomial.jl @@ -154,7 +154,7 @@ function Base.isapprox(p1::ImmutablePolynomial{N,T}, p2::ImmutablePolynomial{M,S}; rtol::Real = (Base.rtoldefault(T, S, 0)), atol::Real = 0,) where {N,T,M,S} - p1.var == p2.var || error("p1 and p2 must have same var") + check_same_variable(p1, p2) || error("p1 and p2 must have same var") NN = max(N,M) for i in 1:NN-1 isapprox(p1[i],p2[i], rtol=rtol, atol=atol) || return false diff --git a/src/polynomials/standard-basis.jl b/src/polynomials/standard-basis.jl index 356ec611..1434f739 100644 --- a/src/polynomials/standard-basis.jl +++ b/src/polynomials/standard-basis.jl @@ -83,7 +83,8 @@ end function Base.divrem(num::P, den::Q) where {P <: StandardBasisPolynomial, Q <: StandardBasisPolynomial} - num.var != den.var && error("Polynomials must have same variable") + + check_same_variable(num, den) || error("Polynomials must have same variable") var = num.var diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index 113681cb..1eef4973 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -229,6 +229,12 @@ end @test P([0.5]) + 2 == P([2.5]) @test 2 - P([0.5]) == P([1.5]) + + # check isapprox ignores variable mismatch when constants are involved, issue #217 + @test Polynomial(1, :x) ≈ Polynomial(1, :y) + @test (variable(Polynomial, :x) ≈ variable(Polynomial, :x)) + @test_throws ErrorException variable(Polynomial, :x) ≈ variable(Polynomial, :y) + end end