# Zygote differentiation with DynamicPolynomials

**URL:** <https://discourse.julialang.org/t/zygote-differentiation-with-dynamicpolynomials/90064>\
**Category:** General Usage\
**Tags:** zygote, polynomials, autodiff\
**Created:** [November 10, 2022, 6:12pm UTC](https://discourse.julialang.org/t/zygote-differentiation-with-dynamicpolynomials/90064 "2022-11-10T18:12:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhar](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@mhar](https://discourse.julialang.org/u/mhar)\
**Post date:** [November 10, 2022, 6:12pm UTC](https://discourse.julialang.org/t/zygote-differentiation-with-dynamicpolynomials/90064/1 "2022-11-10T18:12:47Z")

</div>

My primary question is: I want to write a custom adjoint for something that has a similar structure to the following, but I keep getting errors with my attempts. I think I am getting the types wrong.

```julia
using DynamicPolynomials
using Zygote
@polyvar x y
function compute_many_diffs(f::AbstractPolynomialLike, g::AbstractPolynomialLike)
    return [differentiate(f,x,i) * differentiate(g,y,i) for i = 1:100]
end

```

In my application the output is accumulated to a scalar downstream.

In trying to sort this out I tried writing an artificial accumulation function:

```julia
g = (x + 2*y)^2
f0 = (x + y)^2;
function fake_accumulation_function(f)
    sum(coefficients(f * g))
end
gradient(fake_accumulation_function, f0)

```

But this gives the error

> ERROR: MethodError: no method matching termtype(::Tangent{Any, NamedTuple{(:a, :x), Tuple{FillArrays.Fill{Int64, 1, Tuple{Base.OneTo{Int64}}}, ZeroTangent}}})  
> Closest candidates are:  
> termtype(::Union{AbstractVector{PT}, Type{var"#s9"} where var"#s9"\<:AbstractVector{PT}}) where PT\<:(AbstractPolynomialLike) at /Users/mitchell/.julia/packages/MultivariatePolynomials/zlPtT/src/term.jl:53  
> termtype(::Union{AbstractVector{PT}, Type{var"#s9"} where var"#s9"\<:AbstractVector{PT}}, ::Type{T}) where {PT\<:(AbstractPolynomialLike), T} at /Users/mitchell/.julia/packages/MultivariatePolynomials/zlPtT/src/term.jl:54  
> termtype(::Union{Polynomial{C, T}, Term{C, T}, Type{var"#s23"} where var"#s23"\<:Union{Polynomial{C, T}, Term{C, T}}}) where {C, T} at /Users/mitchell/.julia/packages/DynamicPolynomials/32Ech/src/DynamicPolynomials.jl:33  
> …  
> Stacktrace:  
> [1] adjoint\_mult\_right(p::Polynomial{true, Int64}, Δ::Tangent{Any, NamedTuple{(:a, :x), Tuple{FillArrays.Fill{Int64, 1, Tuple{Base.OneTo{Int64}}}, ZeroTangent}}})  
> @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/zlPtT/src/chain\_rules.jl:46  
> [2] (::MultivariatePolynomials.var"#times\_pullback2#116"{Polynomial{true, Int64}, Polynomial{true, Int64}})(ΔΩ̇::Tangent{Any, NamedTuple{(:a, :x), Tuple{FillArrays.Fill{Int64, 1, Tuple{Base.OneTo{Int64}}}, ZeroTangent}}})  
> @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/zlPtT/src/chain\_rules.jl:54  
> [3] (::Zygote.ZBack{MultivariatePolynomials.var"#times\_pullback2#116"{Polynomial{true, Int64}, Polynomial{true, Int64}}})(dy::NamedTuple{(:a, :x), Tuple{FillArrays.Fill{Int64, 1, Tuple{Base.OneTo{Int64}}}, Nothing}})  
> @ Zygote ~/.julia/packages/Zygote/dABKa/src/compiler/chainrules.jl:206  
> [4] Pullback  
> @ ./REPL[57]:2 [inlined]  
> [5] (::typeof(∂(fake\_accumulation\_function)))(Δ::Int64)  
> @ Zygote ~/.julia/packages/Zygote/dABKa/src/compiler/interface2.jl:0  
> [6] (::Zygote.var"#58#59"{typeof(∂(fake\_accumulation\_function))})(Δ::Int64)  
> @ Zygote ~/.julia/packages/Zygote/dABKa/src/compiler/interface.jl:45  
> [7] gradient(f::Function, args::Polynomial{true, Int64})  
> @ Zygote ~/.julia/packages/Zygote/dABKa/src/compiler/interface.jl:97  
> [8] top-level scope  
> @ REPL[58]:1

Any help at understanding this error, or more advice towards the original goal would be appreciated!
