Ahora veremos un ejemplo del funcionamiento del modo directo, el código siguiente es simple pero suficiente para mostrar los resultados del proceso.
Se tiene el código de la función de prueba sub1, el cual incluye las variables que designamos como activas first and other.
subroutine sub1(first, other, third, out, res) real first, other, third, out, res include 'globals.inc' c start mixing all variables : out = first*other + 3.0*third res = sin(first) + cos(other) first = 2.0 * first third = 2.0/other c now overwrite some values... if (first.gt.other) then first = cos(out) other = 1.0 else c ...in one or in all cases first = exp(out) other = 1.0 endif end
código resultado de la diferenciación automática.
SUBROUTINE SUB1_D(first, firstd, other, otherd, third, thirdd, out + , outd, res, resd) REAL first, firstd, other, otherd, out, outd, res, resd, third, + thirdd REAL x(14), y COMMON /cc/ x, y INTRINSIC COS, EXP, SIN C start mixing all variables : outd = firstd * other + first * otherd out = first * other + 3.0 * third resd = firstd * COS(first) - otherd * SIN(other) res = SIN(first) + COS(other) first = 2.0 * first thirdd = -(2.0*otherd/other**2) third = 2.0 / other C now overwrite some values... IF (first .GT. other) THEN firstd = -(outd*SIN(out)) first = COS(out) other = 1.0 otherd = 0.0 ELSE C ...in one or in all cases firstd = outd * EXP(out) first = EXP(out) other = 1.0 otherd = 0.0 END IF END