Suppose we have an extremely simple program...
...and want to apply automatic differentiation to it at the input x=2π. Since there is only a single line of any substance, this is essentially equivalent to differentiating f(x)=2cos(2x)−sin(4x). I have higher aspirations to compute programs of multiple lines and multiple variables, but I seem to be making a mistake even in the basic case.
A dual number function, as I understand it in a way that will generalize to the multivariate case (perhaps no longer called a dual number), is the sum of a function and its total differential, all evaluated at a particular point of which each coordinate is also a dual number. So what we want to find is...(f(x)+df)∣x=x0+dx...which in this problem is (f(x)+f′(x)ϵ)∣x=x0+ϵ=(2cos(2x)−sin(4x)−4sin(2x)ϵ−4cos(4x)ϵ)∣x=2π+ϵ=2cos(π+2ϵ)−sin(2π+4ϵ)−4sin(π+2ϵ)ϵ−4cos(2π+4ϵ)ϵ=−2cos(2ϵ)−sin(4ϵ)+4sin(2ϵ)ϵ−4cos(4ϵ)ϵ.
This is the part where I think I'm messing it up, but I can't see how. There is not any division or fancier operation to contend with, so I reason that I can replace each individual sinusoid with just the portion of its Maclaurin Expansion which is affine in ϵ; any higher order terms are not going to be rescued from ϵ2 annihilation in the final answer by just addition, subtraction, and non-fractional multiplication. Thus cos(kϵ)=1 and sin(kϵ)=kϵ for any constant k. Then my answer reduces to −2(1)−(4ϵ)+4(2ϵ)ϵ−4(1)ϵ or...−2−8ϵ
However, the answer should be −2−4ϵ, as computed in Mathematica...

Do I just have a mistake in the algebra, or have I reasoned or understood the setup incorrectly?
myFunction(x)
myExpression = 2cos(2x) - sin(4x)
return myExpression
...and want to apply automatic differentiation to it at the input x=2π. Since there is only a single line of any substance, this is essentially equivalent to differentiating f(x)=2cos(2x)−sin(4x). I have higher aspirations to compute programs of multiple lines and multiple variables, but I seem to be making a mistake even in the basic case.
A dual number function, as I understand it in a way that will generalize to the multivariate case (perhaps no longer called a dual number), is the sum of a function and its total differential, all evaluated at a particular point of which each coordinate is also a dual number. So what we want to find is...(f(x)+df)∣x=x0+dx...which in this problem is (f(x)+f′(x)ϵ)∣x=x0+ϵ=(2cos(2x)−sin(4x)−4sin(2x)ϵ−4cos(4x)ϵ)∣x=2π+ϵ=2cos(π+2ϵ)−sin(2π+4ϵ)−4sin(π+2ϵ)ϵ−4cos(2π+4ϵ)ϵ=−2cos(2ϵ)−sin(4ϵ)+4sin(2ϵ)ϵ−4cos(4ϵ)ϵ.
This is the part where I think I'm messing it up, but I can't see how. There is not any division or fancier operation to contend with, so I reason that I can replace each individual sinusoid with just the portion of its Maclaurin Expansion which is affine in ϵ; any higher order terms are not going to be rescued from ϵ2 annihilation in the final answer by just addition, subtraction, and non-fractional multiplication. Thus cos(kϵ)=1 and sin(kϵ)=kϵ for any constant k. Then my answer reduces to −2(1)−(4ϵ)+4(2ϵ)ϵ−4(1)ϵ or...−2−8ϵ
However, the answer should be −2−4ϵ, as computed in Mathematica...

Do I just have a mistake in the algebra, or have I reasoned or understood the setup incorrectly?