Prev Next a_fun_jacobian_xam.pm Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Perl: Dense Jacobian Using AD: Example and Test
package a_fun_jacobian_xam;
sub a_fun_jacobian_xam() {
     # check for standard perl programming conventions
     use strict;
     use warnings;
     #
     # load the Cppad Swig library
     use pm_cppad;
     #
     # initilaize return variable
     my $ok = 1;
     # ---------------------------------------------------------------------
     # number of dependent and independent variables
     my $n_dep = 1;
     my $n_ind = 3;
     #
     # create the independent variables ax
     my $x = new pm_cppad::vec_double($n_ind);
     for(my $i = 0; $i < $n_ind ; $i++) {
          $x->set($i, $i + 2.0);
     }
     my $ax = pm_cppad::independent($x);
     #
     # create dependent variables ay with ay0 = ax_0 * ax_1 * ax_2
     my $ax_0 = $ax->get(0);
     my $ax_1 = $ax->get(1);
     my $ax_2 = $ax->get(2);
     my $ay = new pm_cppad::vec_a_double($n_dep);
     $ay->set(0, $ax_0 * $ax_1 * $ax_2);
     #
     # define af corresponding to f(x) = x_0 * x_1 * x_2
     my $af = new pm_cppad::a_fun($ax, $ay);
     #
     # compute the Jacobian f'(x) = ( x_1*x_2, x_0*x_2, x_0*x_1 )
     my $fp = $af->jacobian($x);
     #
     # check Jacobian
     my $x_0 = $x->get(0);
     my $x_1 = $x->get(1);
     my $x_2 = $x->get(2);
     $ok = $ok && $fp->get(0 * $n_ind + 0) == $x_1 * $x_2 ;
     $ok = $ok && $fp->get(0 * $n_ind + 1) == $x_0 * $x_2 ;
     $ok = $ok && $fp->get(0 * $n_ind + 2) == $x_0 * $x_1 ;
     #
     return( $ok );
}

Input File: build/lib/example/perl/a_fun_jacobian_xam.pm