Prev Next sparse_rcv_xam.pm Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Perl: Sparsity Patterns: Example and Test
package sparse_rcv_xam;
sub sparse_rcv_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;
     # ---------------------------------------------------------------------
     #
     # create sparsity pattern for n by n identity matrix
     my $pattern = new pm_cppad::sparse_rc();
     my $n = 5;
     $pattern->resize($n, $n, $n);
     for(my $k = 0; $k < $n; $k++) {
          $pattern->put($k, $k, $k);
     }
     #
     # create n by n sparse representation of identity matrix
     my $matrix = new pm_cppad::sparse_rcv($pattern);
     for(my $k = 0; $k < $n; $k++) {
          $matrix->put($k, 1.0);
     }
     #
     # row, column indices
     my $row = $matrix->row();
     my $col = $matrix->col();
     my $val = $matrix->val();
     #
     # check results
     for(my $k = 0; $k < $n; $k++) {
          $ok = $ok && $row->get($k) == $k;
          $ok = $ok && $col->get($k) == $k;
          $ok = $ok && $val->get($k) == 1.0;
     }
     #
     # For this case, row_major and col_major order are same as original order
     my $row_major = $matrix->row_major();
     my $col_major = $matrix->col_major();
     for(my $k = 0; $k < $n; $k++) {
          $ok = $ok && $row_major->get($k) == $k;
          $ok = $ok && $col_major->get($k) == $k;
     }
     #
     return( $ok );
}

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