Skip to content

Commit

Permalink
Merge pull request #218 from jverzani/issue_217
Browse files Browse the repository at this point in the history
add new function check_same_variable which checks non-constant polyno…
  • Loading branch information
jverzani authored May 12, 2020
2 parents dea65b3 + 6d849d8 commit f69e2d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 20 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 =#
"""
Expand Down Expand Up @@ -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)))

"""
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/polynomials/ImmutablePolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/polynomials/standard-basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 6 additions & 0 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

2 comments on commit f69e2d5

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/14640

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.5 -m "<description of version>" f69e2d52e4766aa3121cc26b52bdd3b89e1bc987
git push origin v1.0.5

Please sign in to comment.