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

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

Revision 5001 - (download) (as text) (annotate)
Tue Jun 12 04:05:56 2007 UTC (5 years, 11 months ago) by dpvc
File size: 2215 byte(s)
More fixes for creating items in the corret context.  Also added a
method for looking up the package associated with a particular Parser
class (for consistency with the Value->Package call).


    1 #########################################################################
2 #
3 #  Implements the Matrix class.
4 #
5 package Parser::List::Matrix;
6 use strict;
7 our @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$TeX = '\begin{array}{'.($self->{array_template} || ('c'x$d)).'}'."\n".$TeX.'\end{array}'; 44 return$TeX unless $open ||$close;
45   $open = "." if$close && !$open; 46$close = "." if $open && !$close;
47   return '\left'.$open.$TeX.'\right'.$close; 48 } 49 50 # 51 # Recursively handle the rows, but only enclose in brackets 52 # to form reference to array of (references to arrays of ...) entries 53 # 54 sub perl { 55 my$self = shift; my $parens = shift; my$matrix = shift;
56   my $perl; my @p = (); 57 foreach my$x (@{$self->{coords}}) {push(@p,$x->perl(0,1))}
58   if ($matrix) { 59$perl = '['.join(',',@p).']';
60   } else {
61     $perl =$self->Package($self->type).'->new('.join(',',@p).')'; 62$perl = '('.$perl.')' if$parens;
63   }
64   return \$perl;
65 }
66
67 #########################################################################
68
69 1;


 aubreyja at gmail dot com ViewVC Help Powered by ViewVC 1.0.9