Skip to content

Commit 6d849d8

Browse files
committed
add new function check_same_variable which checks non-constant polynomials equality of variable; close #217
1 parent dea65b3 commit 6d849d8

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "1.0.4"
5+
version = "1.0.5"
66

77
[deps]
88
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/common.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = P([0
253253
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
254254
variable(var::SymbolLike = :x) = variable(Polynomial{Int})
255255

256+
"""
257+
check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial)
258+
259+
Check if either `p` or `q` is constant or if `p` and `q` share the same variable
260+
"""
261+
check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial) =
262+
(Polynomials.isconstant(p) || Polynomials.isconstant(q)) || p.var == q.var
263+
256264
#=
257265
Linear Algebra =#
258266
"""
@@ -318,6 +326,17 @@ has a nonzero coefficient. The degree of the zero polynomial is defined to be -1
318326
"""
319327
degree(p::AbstractPolynomial) = iszero(p) ? -1 : length(p) - 1
320328

329+
330+
"""
331+
isconstant(::AbstractPolynomial)
332+
333+
Is the polynomial `p` a constant.
334+
"""
335+
isconstant(p::AbstractPolynomial) = degree(p) <= 0
336+
337+
338+
339+
321340
hasnan(p::AbstractPolynomial) = any(isnan.(coeffs(p)))
322341

323342
"""
@@ -535,9 +554,7 @@ function Base.isapprox(p1::AbstractPolynomial{T},
535554
rtol::Real = (Base.rtoldefault(T, S, 0)),
536555
atol::Real = 0,) where {T,S}
537556
p1, p2 = promote(p1, p2)
538-
if p1.var != p2.var
539-
error("p1 and p2 must have same var")
540-
end
557+
check_same_variable(p1, p2) || error("p1 and p2 must have same var")
541558
p1t = truncate(p1; rtol = rtol, atol = atol)
542559
p2t = truncate(p2; rtol = rtol, atol = atol)
543560
if length(p1t) length(p2t)

src/polynomials/ImmutablePolynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function Base.isapprox(p1::ImmutablePolynomial{N,T},
154154
p2::ImmutablePolynomial{M,S};
155155
rtol::Real = (Base.rtoldefault(T, S, 0)),
156156
atol::Real = 0,) where {N,T,M,S}
157-
p1.var == p2.var || error("p1 and p2 must have same var")
157+
check_same_variable(p1, p2) || error("p1 and p2 must have same var")
158158
NN = max(N,M)
159159
for i in 1:NN-1
160160
isapprox(p1[i],p2[i], rtol=rtol, atol=atol) || return false

src/polynomials/standard-basis.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ end
8383

8484

8585
function Base.divrem(num::P, den::Q) where {P <: StandardBasisPolynomial, Q <: StandardBasisPolynomial}
86-
num.var != den.var && error("Polynomials must have same variable")
86+
87+
check_same_variable(num, den) || error("Polynomials must have same variable")
8788
var = num.var
8889

8990

test/StandardBasis.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ end
229229

230230
@test P([0.5]) + 2 == P([2.5])
231231
@test 2 - P([0.5]) == P([1.5])
232+
233+
# check isapprox ignores variable mismatch when constants are involved, issue #217
234+
@test Polynomial(1, :x) Polynomial(1, :y)
235+
@test (variable(Polynomial, :x) variable(Polynomial, :x))
236+
@test_throws ErrorException variable(Polynomial, :x) variable(Polynomial, :y)
237+
232238
end
233239
end
234240

0 commit comments

Comments
 (0)