[system] / trunk / pg / lib / Parser / List / Matrix.pm Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

# View of /trunk/pg/lib/Parser/List/Matrix.pm

Tue Jan 17 02:20:19 2006 UTC (13 years, 10 months ago) by dpvc
File size: 2067 byte(s)
Fixed problem with formula-valued matrices producing an error message
incorrectly.  (Was checking the wrong attribute of the entries.)


    1 #########################################################################
2 #
3 #  Implements the Matrix class.
4 #
5 package Parser::List::Matrix;
6 use strict; use vars qw(@ISA);
7 @ISA = qw(Parser::List);
8
9 #
10 #  The main checks are all done in the List object.
11 #  Here, we just convert the entry types from points or
12 #    vectors to matrices (hack) with appropriate parens.
13 #
14 sub _check {
15   my $self = shift; 16 my$matrix = $self->{equation}{context}{lists}{Matrix}; 17$self->{open} = $matrix->{open};$self->{close} = $matrix->{close}; 18 if ($self->{entryType}{name} ne 'Matrix') {
19     foreach my $x (@{$self->{coords}})
20       {$x->makeMatrix($self->{type}{name},$self->{open},$self->{close})}
21   }
22   foreach my $x (@{$self->{coords}}) {
23     $self->{equation}->Error("Entries in a Matrix must be Numbers or Lists of Numbers") 24 unless ($x->type =~ m/Number|Matrix/);
25   }
26 }
27
28 #
29 #  Handle a 2-dimensional matrix as a special case, using
30 #  LaTeX's \array command.
31 #
32 sub TeX {
33   my ($self,$precedence) = @_;
34   return $self->SUPER::TeX(@_) unless$self->entryType->{entryType};
35   my ($open,$close) = ($self->{open},$self->{close});
36   $open = '\{' if$open eq '{'; $close = '\}' if$close eq '}';
37   my $TeX = ''; my @entries = (); my$d;
38   foreach my $row (@{$self->coords}) {
39     foreach my $x (@{$row->coords}) {push(@entries,$x->TeX)} 40$TeX .= join(' &',@entries) . '\cr'."\n";
41     $d = scalar(@entries); @entries = (); 42 } 43 return '\left'.$open.'\begin{array}{'.('c'x$d).'}'."\n".$TeX.'\end{array}\right'.$close; 44 } 45 46 # 47 # Recursively handle the rows, but only enclose in brackets 48 # to form reference to array of (references to arrays of ...) entries 49 # 50 sub perl { 51 my$self = shift; my $parens = shift; my$matrix = shift;
52   my $perl; my @p = (); 53 foreach my$x (@{$self->{coords}}) {push(@p,$x->perl(0,1))}
54   if ($matrix) { 55$perl = '['.join(',',@p).']';
56   } else {
57     $perl = 'new Value::'.$self->type.'('.join(',',@p).')';
58     $perl = '('.$perl.')' if $parens; 59 } 60 return$perl;
61 }
62
63 #########################################################################
64
65 1;
66