Skip to content

Gradients for polynomials in Polynomials.jl #533

@manuelbb-upb

Description

@manuelbb-upb

Zygote does not work with polynomials made with Polynomials.jl.
This is probably due to a missing Base.similar function and an index-shift in evalpoly.
I found this issue in discourse, see my comment here.

mwe:

using Zygote
using Polynomials
p = Polynomial( [0.0, 1.0] )  # 0 * x^0 + 1 * x^1 = x_1
gradient(p, 1.0)

errors with

MethodError: no method matching similar(::Polynomial{Float64, :x}, ::Type{Float64}, ::Int64)

After defining

Base.similar( :: Polynomial, etype = eltype(p), len = length(p.coeffs)) = similar( p.coeffs, etype, len )

the error is gone, but the result is wrong, gradient(p,1.0) evaluates to 0.0 instead of 1.0.
Also, for q = Polynomial( [0.0,] ) errors with

BoundsError: attempt to access 0-element Vector{Float64} at index [1]

which leads me to believe the error is due to the fact, that _evalpoly_intermediate(x,p) assumes p to be a Tuple with indexing starting at 1 whilst Polynomials.jl starts at 0 (as the first coefficient is for x^0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions