Figure 4.3 presents an example of the transformation of source code using ADIFOR. Figure 4.3 (a) shows a segment of a sample Fortran program for which derivatives are desired. The dependent variable is y and the independent variables are x(1) through x(10). Figure 4.3 (b) shows the derivative code produced through the application of ADIFOR. Using the resultant code, the partial derivatives of y with respect to independent variable x(i) can be obtained through the corresponding element dy(i) from the array dy.
This example illustrates some key advantages of using ADIFOR over other methods: ADIFOR steps through the conditional execution loops and iterative loops (e.g., the if and the do statements in Figure 4.3 (a)), and ADIFOR can calculate derivatives for assignment and iteration statements (e.g., the statement y =x(i) * y *y). Further, the iterative loops and conditional loops are common in computer codes, while they do not appear in algebraic or differential equations. It must be noted that the value of the variable y depends to a great extent the ``sign'' of the various x(i)'s, specifically of x(n); such situations are difficult to express in algebraic forms, whereas they can be readily expressed in computer code. Since ADIFOR methodically follows the model code, all the complex logic of the model code is followed exactly by the derivative code.
In principle, this method can be easily extended to compute derivatives for computer code of arbitrary length and complexity. Furthermore, in the application of ADIFOR, the user has to only (a) specify the information about the dependent and independent variables, and (b) add statements to the derivative code to output the derivatives.
![]() |