@@ -315,3 +315,81 @@ appropriate library functions:
315315 // The parameters include variables t, z, and a and their derivative variables dt, dz, and da.
316316 call ADDiv(t, dt, z, dz, a, da)
317317```
318+
319+ The library functions, ADAdd and ADDiv, use the chain rule to define the
320+ Add and Div differential expressions, respectively. This is illustrated
321+ in Code ` lst:diff ` .
322+
323+ ** lst: diff **
324+ ```
325+ def ADAdd(x, dx, y, dy, z, dz):
326+ z = x + y
327+ dz = dy + dx
328+
329+ def ADDiv(x, dx, y, dy, z, dz):
330+ z = x / y
331+ dz = dx / y + (x / (y * y)) * dy
332+ ```
333+
334+ Elemental libraries constitute a simple and straightforward way of
335+ implementing automatic differentiation for programming languages.
336+ However, this approach requires users to manually decompose a program
337+ into elementary expressions before calling library functions for
338+ programming. Furthermore, it is not possible to use the native
339+ expressions found in programming languages.
340+
341+ ### Operator Overloading
342+
343+ Leveraging the polymorphism characteristic inherent in modern
344+ programming languages, the Operator Overloading design pattern redefines
345+ the semantics of elementary operations and successfully encapsulates
346+ their differentiation rules. During the execution phase, it methodically
347+ documents the type, inputs, and outputs of every elementary operation
348+ within a data structure known as a 'tape'. These tapes have the ability
349+ to generate a trace, serving as a pathway for applying the chain rule.
350+ This makes it possible to aggregate elementary operations either in a
351+ forward or backward direction to facilitate differentiation. As depicted
352+ in Code ` lst:OO ` ,
353+ we utilize the AutoDiff code from automatic differentiation libraries as
354+ a case in point to overload the basic arithmetic operators in
355+ programming languages.
356+
357+ ** lst: OO **
358+ ``` cpp
359+ namespace AutoDiff
360+ {
361+ public abstract class Term
362+ {
363+ // To overload and call operators (`+`, `*`, and `/`),
364+ // TermBuilder records the types, inputs, and outputs of operations in tapes.
365+ public static Term operator+(Term left, Term right)
366+ {
367+ return TermBuilder.Sum(left, right);
368+ }
369+ public static Term operator *(Term left, Term right)
370+ {
371+ return TermBuilder.Product(left, right);
372+ }
373+ public static Term operator/(Term numerator, Term denominator)
374+ {
375+ return TermBuilder.Product(numerator, TermBuilder.Power(denominator, -1));
376+ }
377+ }
378+
379+ // Tape data structures include the following basic elements:
380+ // 1) Arithmetic results of operations
381+ // 2) Derivative evaluation results corresponding to arithmetic results of operations
382+ // 3) Inputs of operations
383+ // In addition, functions Eval and Diff are used to define the computation and differentiation rules of the arithmetic operations.
384+ internal abstract class TapeElement
385+ {
386+ public double Value;
387+ public double Adjoint;
388+ public InputEdges Inputs;
389+
390+ public abstract void Eval();
391+ public abstract void Diff();
392+ }
393+ }
394+ ```
395+
0 commit comments