![]() |
Prev | Next | a_fun_hessian_xam.pm | Headings |
package a_fun_hessian_xam; sub a_fun_hessian_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); # # g(x) = w_0 * f_0 (x) = f(x) my $w = new pm_cppad::vec_double($n_dep); $w->set(0, 1.0); # # compute Hessian my $fpp = $af->hessian($x, $w); # # [ 0.0 , x_2 , x_1 ] # f''(x) = [ x_2 , 0.0 , x_0 ] # [ x_1 , x_0 , 0.0 ] $ok = $ok && $fpp->get(0 * $n_ind + 0) == 0.0 ; $ok = $ok && $fpp->get(0 * $n_ind + 1) == $x->get(2) ; $ok = $ok && $fpp->get(0 * $n_ind + 2) == $x->get(1) ; # $ok = $ok && $fpp->get(1 * $n_ind + 0) == $x->get(2) ; $ok = $ok && $fpp->get(1 * $n_ind + 1) == 0.0 ; $ok = $ok && $fpp->get(1 * $n_ind + 2) == $x->get(0) ; # $ok = $ok && $fpp->get(2 * $n_ind + 0) == $x->get(1) ; $ok = $ok && $fpp->get(2 * $n_ind + 1) == $x->get(0) ; $ok = $ok && $fpp->get(2 * $n_ind + 2) == 0.0 ; # return( $ok ); }