Prev Next a_double_cond_assign_xam.pm Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Perl: a_double Conditional Assignment: Example and Test
package a_double_cond_assign_xam;
sub a_double_cond_assign_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 = 4;
     my $n_dep = 1;
     #
     # create ax (value of independent variables does not matter)
     my $x = new pm_cppad::vec_double($n_ind);
     $x->set(0, 0.0);
     $x->set(1, 1.0);
     $x->set(2, 2.0);
     $x->set(3, 3.0);
     my $ax = pm_cppad::independent($x);
     #
     # arguments to conditional assignment
     my $left = $ax->get(0);
     my $right = $ax->get(1);
     my $if_true = $ax->get(2);
     my $if_false = $ax->get(3);
     #
     # assignment
     my $target = new pm_cppad::a_double();
     $target->cond_assign(
          "<",
          $left,
          $right,
          $if_true,
          $if_false
     );
     #
     # f(x) = taget
     my $ay = new pm_cppad::vec_a_double($n_dep);
     $ay->set(0, $target);
     my $af = new pm_cppad::a_fun($ax, $ay);
     #
     # assignment with different independent variable values
     $x->set(0, 9.0); # left
     $x->set(1, 8.0); # right
     $x->set(2, 7.0); # if_true
     $x->set(3, 6.0); # if_false
     my $p = 0;
     my $y = $af->forward($p, $x);
     $ok = $ok && $y->get(0) == 6.0;
     #
     return( $ok  );
}

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