[system] / trunk / pg / macros / PGmatrixmacros.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/PGmatrixmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4997 - (download) (as text) (annotate)
Mon Jun 11 18:16:40 2007 UTC (12 years, 8 months ago) by gage
File size: 31537 byte(s)
Fixing docementation so that it can be read from the web.

    1 
    2 ###########
    3 #use Carp;
    4 
    5 =head1 NAME
    6 
    7         Matrix macros for the PG language
    8 
    9 =head1 SYNPOSIS
   10 
   11 
   12 
   13 =head1 DESCRIPTION
   14 
   15 Almost all of the macros in the file are very rough at best.  The most useful is display_matrix.
   16 Many of the other macros work with vectors and matrices stored as anonymous arrays.
   17 
   18 Frequently it may be
   19 more useful to use the Matrix objects defined RealMatrix.pm and Matrix.pm and the constructs listed there.
   20 
   21 
   22 =cut
   23 
   24 BEGIN {
   25         be_strict();
   26 }
   27 
   28 sub _PGmatrixmacros_init {
   29 }
   30 
   31 # this subroutine zero_check is not very well designed below -- if it is used much it should receive
   32 # more work -- particularly for checking relative tolerance.  More work needs to be done if this is
   33 # actually used.
   34 
   35 sub zero_check{
   36     my $array = shift;
   37     my %options = @_;
   38         my $num = @$array;
   39         my $i;
   40         my $max = 0; my $mm;
   41         for ($i=0; $i< $num; $i++) {
   42                 $mm = $array->[$i] ;
   43                 $max = abs($mm) if abs($mm) > $max;
   44         }
   45     my $tol = $options{tol};
   46     $tol = 0.01*$options{reltol}*$options{avg} if defined($options{reltol}) and defined $options{avg};
   47     $tol = .000001 unless defined($tol);
   48         ($max <$tol) ? 1: 0;       # 1 if the array is close to zero;
   49 }
   50 sub vec_dot{
   51         my $vec1 = shift;
   52         my $vec2 = shift;
   53         warn "vectors must have the same length" unless @$vec1 == @$vec2;  # the vectors must have the same length.
   54         my @vec1=@$vec1;
   55         my @vec2=@$vec2;
   56         my $sum = 0;
   57 
   58         while(@vec1) {
   59                 $sum += shift(@vec1)*shift(@vec2);
   60         }
   61         $sum;
   62 }
   63 sub proj_vec {
   64         my $vec = shift;
   65         warn "First input must be a column matrix" unless ref($vec) eq 'Matrix' and ${$vec->dim()}[1] == 1;
   66         my $matrix = shift;    # the matrix represents a set of vectors spanning the linear space
   67                                # onto which we want to project the vector.
   68         warn "Second input must be a matrix" unless ref($matrix) eq 'Matrix' and ${$matrix->dim()}[1] == ${$vec->dim()}[0];
   69         $matrix * transpose($matrix) * $vec;
   70 }
   71 
   72 sub vec_cmp{    #check to see that the submitted vector is a non-zero multiple of the correct vector
   73     my $correct_vector = shift;
   74     my %options = @_;
   75         my $ans_eval = sub {
   76                 my $in =  shift @_;
   77 
   78                 my $ans_hash = new AnswerHash;
   79                 my @in = spli