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

Fix evalpoly for no coefficients case #56706

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ julia> evalpoly(2, (1, 2, 3))
17
```
"""
function evalpoly(x, p::Tuple)
function evalpoly(x, p::Tuple{Any, Vararg})
if @generated
N = length(p.parameters::Core.SimpleVector)
ex = :(p[end])
Expand All @@ -104,19 +104,22 @@ function evalpoly(x, p::Tuple)
end
end

evalpoly(x, p::Tuple{}) = zero(x)

evalpoly(x, p::AbstractVector) = _evalpoly(x, p)

function _evalpoly(x, p)
Base.require_one_based_indexing(p)
N = length(p)
N == 0 && return zero(x)
ex = p[end]
for i in N-1:-1:1
ex = muladd(x, ex, p[i])
end
ex
end

function evalpoly(z::Complex, p::Tuple)
function evalpoly(z::Complex, p::Tuple{Any, Any, Vararg})
if @generated
N = length(p.parameters)
a = :(p[end])
Expand All @@ -141,15 +144,18 @@ function evalpoly(z::Complex, p::Tuple)
_evalpoly(z, p)
end
end

evalpoly(z::Complex, p::Tuple{<:Any}) = p[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't unnecessarily increase diff size. Leave this line as it is.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry for that. I have fixed it in the latest commit.


evalpoly(z::Complex, p::Tuple{}) = zero(z)

evalpoly(z::Complex, p::AbstractVector) = _evalpoly(z, p)

function _evalpoly(z::Complex, p)
Base.require_one_based_indexing(p)
length(p) == 1 && return p[1]
N = length(p)
N == 0 && return zero(z)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
N == 0 && return zero(z)
N == 0 && return zero(z) * zero(eltype(p))

for improved type-stability? Although that will throw if zero(eltype(p)) is undefined, e.g. if p == Any[], so not sure this is better.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your suggestion, but I personally think this change should not be made due to the possible undefined error in zero(eltype(p)) as you pointed out.

N == 1 && return p[1]
a = p[end]
b = p[end-1]

Expand Down
8 changes: 8 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,14 @@ end
@test evalpoly(1+im, [2,]) == 2
end

@testset "evalpoly no coefficients" begin
for x in (1,1.0,1.0+1.0im)
for p in ((),[])
@test evalpoly(x,p) == zero(x)
end
end
end

@testset "cis" begin
for z in (1.234, 1.234 + 5.678im)
@test cis(z) ≈ exp(im*z)
Expand Down