TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Dual Numbers and Automatic Differentiation (2014)

78 pointsby Kristine1975about 8 years ago

14 comments

Atrix256about 8 years ago
BTW this older article of mine is extended with a new one that shows how to handle multiple variables (: <a href="http:&#x2F;&#x2F;blog.demofox.org&#x2F;2017&#x2F;02&#x2F;20&#x2F;multivariable-dual-numbers-automatic-differentiation&#x2F;" rel="nofollow">http:&#x2F;&#x2F;blog.demofox.org&#x2F;2017&#x2F;02&#x2F;20&#x2F;multivariable-dual-number...</a>
评论 #13697634 未加载
one-more-minuteabout 8 years ago
Check out ForwardDiff.jl [1], which is a really impressive implementation of this exact idea. The technique gets pretty magical results; you can apply it to any function, complete with loops, linalg etc, and it will compute a derivative with something like 10% overhead. The standard approach is finite differencing, which involves many-times overhead, isn&#x27;t exact, and can easily blow up for common pathological cases like step functions.<p>Fede_V&#x27;s points about the drawbacks of the technique are valid in C++, but Julia&#x27;s duck-typing makes being generic the default (including in the standard library). ForwardDiff works out of the box, for free, in a huge number of cases.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;JuliaDiff&#x2F;ForwardDiff.jl" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JuliaDiff&#x2F;ForwardDiff.jl</a>
评论 #13698231 未加载
Fede_Vabout 8 years ago
This is a great post. However, it didn&#x27;t touch on the two main problems of AD:<p>- Using Dual Numbers requires that all functions that you call into accept templated parameters. If you want to use GSL, BLAS, or any other mature math library, you are probably out of luck.<p>- Even if you are willing to port the code and modify the functions to accept templated parameters, very highly optimized math libraries make assumption not just about the behaviour of numbers (their API, defined by how they behave under addition&#x2F;subtraction, etc) but also about their ABI. For example, a well tuned LAPACK like OpenBlas or MKL has very well tuned loop sizes to optimize cache behaviour assuming that floats are of a particular size.
评论 #13696673 未加载
pettersabout 8 years ago
Using dual numbers and modern C++ you can write a library that can do this:<p><pre><code> auto lambda = [](auto x, auto y) { &#x2F;&#x2F; The Rosenbrock function. auto d0 = y[0] - x[0]*x[0]; auto d1 = 1 - x[0]; return 100 * d0*d0 + d1*d1; }; &#x2F;&#x2F; The lambda function will be copied and &#x2F;&#x2F; automatically differentiated. The derivatives &#x2F;&#x2F; are computed using templates, not numerically. &#x2F;&#x2F; &#x2F;&#x2F; No need to derive or compute derivatives &#x2F;&#x2F; manually! auto func = make_differentiable&lt;1, 1&gt;(lambda); </code></pre> &quot;func&quot; now has code for first-order, second-order derivatives all generated and heavily optimized at compile-time. This is one reason C++ is so good for (mathematical) optimization.
doublerebelabout 8 years ago
One of the libraries I&#x27;m using for computer vision depends on using the inner dual number class Jet [0] from Google&#x27;s ceres-solver to do automatic differentiation.<p>It was worthwhile research reading through the implementation to understand the applications.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;ceres-solver&#x2F;ceres-solver&#x2F;blob&#x2F;master&#x2F;include&#x2F;ceres&#x2F;jet.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ceres-solver&#x2F;ceres-solver&#x2F;blob&#x2F;master&#x2F;inc...</a>
divbitabout 8 years ago
Super cool. In this example, it&#x27;s differentiation of a one-dimensional curve, but one can use the dual numbers to compute tangent spaces of more interesting algebraic objects as well. See, e.g. remark 5.38 here: <a href="http:&#x2F;&#x2F;www.jmilne.org&#x2F;math&#x2F;CourseNotes&#x2F;AG500.pdf" rel="nofollow">http:&#x2F;&#x2F;www.jmilne.org&#x2F;math&#x2F;CourseNotes&#x2F;AG500.pdf</a>
jmountabout 8 years ago
Dual numbers are a blast, especially with type-templated languages. I wrote a Scala implementation some time ago that I would like to share: <a href="http:&#x2F;&#x2F;www.win-vector.com&#x2F;blog&#x2F;2010&#x2F;06&#x2F;automatic-differentiation-with-scala&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.win-vector.com&#x2F;blog&#x2F;2010&#x2F;06&#x2F;automatic-differentia...</a>
BoppreHabout 8 years ago
I&#x27;ve implemented this in Python a while ago, also (ab)using operator overload: <a href="https:&#x2F;&#x2F;github.com&#x2F;boppreh&#x2F;derivative" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;boppreh&#x2F;derivative</a><p>Not remarkable, but works.<p><pre><code> f = lambda x: x * 5 + x ** 2 - 2 &#x2F; x + 3 &#x2F; x ** 2 print(derive(f, 6))</code></pre>
评论 #13699667 未加载
zardoabout 8 years ago
An extension to multiple dimensions is mentioned. This would be exactly geometric algebra, wouldn&#x27;t it?
评论 #13696344 未加载
inlineintabout 8 years ago
I don&#x27;t like this use of dual numbers notation for differentiation because division by a dual number is not defined. For example, how would one calculate ε^2&#x2F;ε? If ε is a matrix [0,1;0,0] then it doesn&#x27;t have inverse and thus the expression could not be evaluated.<p>On the other hand, little-o notation [1] was invented for exactly this purpose. It is easy to evaluate derivatives using it, for example (x+ε)^3 = x^3+3<i>x^2</i>ε+o(ε), and so ((x+ε)^3 - x^3)&#x2F;ε = 3*x^2 + o(1), and o(1) tends to 0 for ε tends to 0.<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Big_O_notation#Little-o_notation" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Big_O_notation#Little-o_notati...</a>
评论 #13696373 未加载
评论 #13696683 未加载
frankohnabout 8 years ago
The article is interesting but one have to be aware that dual number are not a solution to automatic differentiation.<p>The reason is that to have automatic differentiation one needs to keep in principle the epsilon^n for any order and the dual number just take epsilon^2 = 0 which is an strong limitation.<p>For example with dual number you can say that:<p>limit(x-&gt;0) (sin(x) - 1) &#x2F; x = 1<p>just by doing x = epsilon but they will fail with:<p>limit(x-&gt;0) (cos(x) - 1) &#x2F; x^2 = -1&#x2F;2<p>because the quadratic terms you need will go to zero for x = epsilon.
评论 #13698543 未加载
评论 #13699020 未加载
tehsauceabout 8 years ago
Anyone else know this guy from his shadertoy profile?
评论 #13697959 未加载
erichoceanabout 8 years ago
I&#x27;m not a math guy, but I&#x27;ve gotten incredible usage out of Dual numbers over the years. Highly recommended.
评论 #13696093 未加载
empath75about 8 years ago
How would this work with second, third, etc, derivatives?
评论 #13699490 未加载
评论 #13704271 未加载