Prev Next a_fun_optimize_xam.pm Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Perl: Optimize an a_fun: Example and Test
package a_fun_optimize_xam;
sub a_fun_optimize_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;
     # ---------------------------------------------------------------------
     my $n_ind = 1; # number of independent variables
     my $n_dep = 1; # number of dependent variables
     my $n_var = 1; # phantom variable at address 0
     my $n_op = 1;  # special operator at beginning
     #
     # dimension some vectors
     my $x = new pm_cppad::vec_double($n_ind);
     my $ay = new pm_cppad::vec_a_double($n_dep);
     #
     # independent variables
     $x->set(0, 1.0);
     my $ax = pm_cppad::independent($x);
     $n_var = $n_var + $n_ind; # one for each indpendent
     $n_op = $n_op + $n_ind;
     #
     # accumulate summation
     my $ax0 = $ax->get(0);
     my $csum = new pm_cppad::a_double(0.0);
     $csum = $ax0 + $ax0 + $ax0 + $ax0;
     $n_var = $n_var + 3; # one per + operator
     $n_op = $n_op + 3;
     #
     # define f(x) = y_0 = csum
     $ay->set(0, $csum);
     my $af = new pm_cppad::a_fun($ax, $ay);
     $n_op = $n_op + 1; # speical operator at end
     #
     # check number of variables and operators
     $ok = $ok && $af->size_var() == $n_var;
     $ok = $ok && $af->size_op() == $n_op;
     #
     # optimize
     $af->optimize();
     #
     # number of variables and operators has decreased by two
     $ok = $ok && $af->size_var() == $n_var-2;
     $ok = $ok && $af->size_op() == $n_op-2;
     #
     return( $ok  );
}

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