Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_, as_, isreal, real, isintegerpoly & asintegerpoly #259

Merged
merged 17 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export fromroots,
fit,
integrate,
derivative,
variable
variable,
isintegerpoly,
asintegerpoly

"""
fromroots(::AbstractVector{<:Number}; var=:x)
Expand Down Expand Up @@ -298,6 +300,47 @@ function Base.iszero(p::AbstractPolynomial)
return all(iszero.(coeffs(p))) && p[0] == 0
end

# See discussions in https://github.com/JuliaMath/Polynomials.jl/issues/258
is_(fn, p::AbstractPolynomial) = all(fn, coeffs(p)) # Do not export
as_(fn, p::P) where {P<:AbstractPolynomial} = ⟒(P)(fn.(coeffs(p)), p.var) # Do not export

"""
isreal(p::AbstractPolynomial)

Determine whether a polynomial is a real polynomial, i.e., having only real numbers as coefficients.

See also: [`real`](@ref)
"""
Base.isreal(p::AbstractPolynomial) = is_(isreal, p)
"""
real(p::AbstractPolynomial)

Construct a real polynomial from the real parts of the coefficients of `p`.

See also: [`isreal`](@ref)

!!! note
This could cause losing terms in `p`. This method is usually called on polynomials like `p = Polynomial([1, 2 + 0im, 3.0, 4.0 + 0.0im])` where you want to chop the imaginary parts of the coefficients of `p`.
"""
Base.real(p::AbstractPolynomial) = as_(real, p)

"""
isintegerpoly(p::AbstractPolynomial)

Determine whether a polynomial is an integer polynomial, i.e., having only integers as coefficients.
singularitti marked this conversation as resolved.
Show resolved Hide resolved

See also: [`asintegerpoly`](@ref)
"""
isintegerpoly(p::AbstractPolynomial) = is_(isinteger, p)
"""
asintegerpoly(p::AbstractPolynomial)

Construct an integer polynomial from the coefficients of `p`.

See also: [`isintegerpoly`](@ref)
"""
asintegerpoly(p::AbstractPolynomial) = as_(x -> convert(Integer, x), p)
singularitti marked this conversation as resolved.
Show resolved Hide resolved

"""
coeffs(::AbstractPolynomial)

Expand Down
19 changes: 19 additions & 0 deletions test/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,26 @@ fit(Poly, xx,yy,2)
## Issue with overflow and polyder Issue #159
@test !iszero(polyder(Poly(BigInt[0, 1])^100, 100))

@testset "`isreal` and `real`" begin
x = Polynomial([1 // 2, 2 + 0im, 3.0, 4.0 + 0.0im])
y = Polynomial([1 // 2, 2 + 0im, 3.0, 4.0 + 0.1im])
@test isreal(x) === true
@test isequal(x, real(x)) === true
@test eltype(real(x)) === Float64
@test real(x) == Polynomial([1 // 2, 2, 3, 4.0])
@test isreal(y) === false
@test real(y) == real(x)
end

@testset "`isintegerpoly` and `asintegerpoly`" begin
x = Polynomial([1 // 1, Int8(2) + 0im, 3.0, Int16(4) + 0im])
y = Polynomial([1 // 2, Int8(2) + 0im, 3.0, Int16(4) + 0im])
@test isintegerpoly(x) === true
@test eltype(asintegerpoly(x)) === Int
@test asintegerpoly(x) == Polynomial([1, 2, 3, 4])
@test isintegerpoly(y) === false
@test_throws InexactError asintegerpoly(y)
end



Expand Down