automatic_differentiation(n) 1.0 "flibs"

NAME

automatic_differentiation - Implement the "automatic differentation" technique

TABLE OF CONTENTS

    TABLE OF CONTENTS
    SYNOPSIS
    DESCRIPTION
    EXAMPLE
    DATA TYPES AND ROUTINES
    COPYRIGHT

SYNOPSIS

use automatic_differentiation
type(AUTODERIV)
x = derivvar( value )

DESCRIPTION

The automatic_differentiation module defines a derived type that enables you (via overloading common mathematical functions and operators) to:

without having to program that first derivative.

The module uses real numbers of the kind "wp" as defined in the auxiliary module select_precision.

(I was pointed to this technique by Simon Geard, a couple of years ago, in the context of one of the many language shootouts on the Internet.)

EXAMPLE

In numerical methods like Newton-Raphson for finding a root of the equation "f(x) = 0", you need the first derivative:

 
    x(k+1)  =  x(k)  - f(x(k)) / f'(x(k))

Rather than implementing the function and its first derivatives as separate functions, using this module you can dispense with manually determining the first derivative:

 
program root
    use automatic_differentation

    type(AUTODERIV) :: x
    type(AUTODERIV) :: res
    integer         :: i

    !
    ! First estimate
    !
    x = derivvar( 1.0_wp )

    do i = 1,10
        res = f(x)
        x = x - res.v / res.dv
    enddo

    write(*,*) 'Root: ', x.v
contains

type(AUTODERIV) function f(x)
    type(AUTODERIV) :: x

    f = x + cos(x)

end function f
end program

DATA TYPES AND ROUTINES

The module defines a single data type, AUTODERIV, and one specific function, derivvar().

use automatic_differentiation
The name of the module

type(AUTODERIV)
The type has two fields:

real(wp) v
The value of the variable/function (or any intermediate result)

real(wp) dv
The first derivative


x = derivvar( value )
Use this function to properly initialise the program variable x as a "mathematical" variable. (It sets the derivative of x to 1, because otherwise it would be regarded as a constant).

real(wp) value
The value of the "mathematical" variable.

COPYRIGHT

Copyright © 2006 Arjen Markus <arjenmarkus@sourceforge.net>