Suppose we have an extremely simple program...
...and want to apply automatic differentiation to it at the input [imath]x = \frac{\pi}{2}[/imath]. Since there is only a single line of any substance, this is essentially equivalent to differentiating [imath]f(x) = 2\cos(2x) - \sin(4x)[/imath]. 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...[math](f(x) + df)|_{x = x_0 + dx}[/math]...which in this problem is [imath](f(x) + f'(x)\epsilon)|_{x = x_0 + \epsilon} = (2\cos(2x) - \sin(4x) - 4\sin(2x)\epsilon - 4\cos(4x)\epsilon)|_{x = \frac{\pi}{2} + \epsilon} = 2\cos(\pi + 2\epsilon) - \sin(2\pi + 4\epsilon) - 4\sin(\pi + 2\epsilon)\epsilon - 4\cos(2\pi + 4\epsilon)\epsilon = -2\cos(2\epsilon) - \sin(4\epsilon) + 4\sin(2\epsilon)\epsilon - 4\cos(4\epsilon)\epsilon[/imath].
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 [imath]\epsilon[/imath]; any higher order terms are not going to be rescued from [imath]\epsilon^2[/imath] annihilation in the final answer by just addition, subtraction, and non-fractional multiplication. Thus [imath]\cos(k\epsilon) = 1[/imath] and [imath]\sin(k\epsilon) = k\epsilon[/imath] for any constant [imath]k[/imath]. Then my answer reduces to [imath]-2(1) - (4\epsilon) + 4(2\epsilon)\epsilon - 4(1)\epsilon[/imath] or...[math]-2 - 8\epsilon[/math]
However, the answer should be [imath]-2 - 4\epsilon[/imath], as computed in Mathematica...

Do I just have a mistake in the algebra, or have I reasoned or understood the setup incorrectly?
myFunction(x)
[imath]\ \ \ \ [/imath]myExpression = 2cos(2x) - sin(4x)
[imath]\ \ \ \ [/imath]return myExpression
...and want to apply automatic differentiation to it at the input [imath]x = \frac{\pi}{2}[/imath]. Since there is only a single line of any substance, this is essentially equivalent to differentiating [imath]f(x) = 2\cos(2x) - \sin(4x)[/imath]. 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...[math](f(x) + df)|_{x = x_0 + dx}[/math]...which in this problem is [imath](f(x) + f'(x)\epsilon)|_{x = x_0 + \epsilon} = (2\cos(2x) - \sin(4x) - 4\sin(2x)\epsilon - 4\cos(4x)\epsilon)|_{x = \frac{\pi}{2} + \epsilon} = 2\cos(\pi + 2\epsilon) - \sin(2\pi + 4\epsilon) - 4\sin(\pi + 2\epsilon)\epsilon - 4\cos(2\pi + 4\epsilon)\epsilon = -2\cos(2\epsilon) - \sin(4\epsilon) + 4\sin(2\epsilon)\epsilon - 4\cos(4\epsilon)\epsilon[/imath].
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 [imath]\epsilon[/imath]; any higher order terms are not going to be rescued from [imath]\epsilon^2[/imath] annihilation in the final answer by just addition, subtraction, and non-fractional multiplication. Thus [imath]\cos(k\epsilon) = 1[/imath] and [imath]\sin(k\epsilon) = k\epsilon[/imath] for any constant [imath]k[/imath]. Then my answer reduces to [imath]-2(1) - (4\epsilon) + 4(2\epsilon)\epsilon - 4(1)\epsilon[/imath] or...[math]-2 - 8\epsilon[/math]
However, the answer should be [imath]-2 - 4\epsilon[/imath], as computed in Mathematica...

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