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

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

Thu Mar 31 19:44:49 2005 UTC (14 years, 8 months ago) by dpvc
File size: 1753 byte(s)
```Added a check that the coordinates are numeric.  This avoids an error
were <i+j+k> was accepted as OK.
```

```    1 #########################################################################
2 #
3 #  Implements the Vector class
4 #
5 package Parser::List::Vector;
6 use strict; use vars qw(@ISA);
7 @ISA = qw(Parser::List);
8
9 #
10 #  The basic List class does nearly everything.
11 #
12
13 #
14 #  Check that the coordinates are numbers (avoid <i+j+k>)
15 #
16 sub _check {
17   my \$self = shift;
18   foreach my \$x (@{\$self->{coords}}) {
19     \$self->{equation}->Error("Coordinates of Vector must be Numbers")
20       unless \$x->isNumber;
21   }
22 }
23
24 my \$ijk_string = ['i','j','k','0'];
25 my \$ijk_TeX = ['\boldsymbol{i}','\boldsymbol{j}','\boldsymbol{k}','\boldsymbol(0)'];
26
27 sub ijk {
28   my \$self = shift;
29   my \$method = shift || 'TeX'; my \$ijk = shift || \$ijk_TeX;
30   my @coords = @{\$self->{coords}};
31   \$self->Error("Method 'ijk' can only be used on vectors in three-space")
32     unless (scalar(@coords) <= 3);
33   my \$prec = \$self->{equation}{context}->operators->get('*')->{precedence};
34   my \$string = ''; my \$n; my \$term;
35   foreach \$n (0..scalar(@coords)-1) {
36     \$term = \$coords[\$n]->\$method(\$prec);
37     if (\$term ne '0') {
38       \$term = '' if \$term eq '1'; \$term = '-' if \$term eq '-1';
39       \$term = '+' . \$term unless \$string eq '' or \$term =~ m/^-/;
40       \$string .= \$term . \$ijk->[\$n];
41     }
42   }
43   \$string = \$ijk->[3] if \$string eq '';
44   return \$string;
45 }
46
47 sub TeX {
48   my \$self = shift;
49   if (\$self->{ijk} || \$self->{equation}{ijk} || \$self->{equation}{context}->flag("ijk"))
50     {return \$self->ijk}
51   return \$self->SUPER::TeX;
52 }
53
54 sub string {
55   my \$self = shift;
56   if (\$self->{ijk} || \$self->{equation}{ijk} || \$self->{equation}{context}->flag("ijk"))
57     {return \$self->ijk('string',\$ijk_string)}
58   return \$self->SUPER::string;
59 }
60
61 #########################################################################
62
63 1;
64
```