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

View of /trunk/pg/macros/parserParametricLine.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: 4226 byte(s)
Fixing docementation so that it can be read from the web.

    1 loadMacros('Parser.pl');
    2 
    3 sub _parserParametricLine_init {}; # don't reload this file
    4 
    5 =head1 DESCRIPTION
    6 
    7 ######################################################################
    8 #
    9 #  This is a Parser class that implements parametric lines as
   10 #  a subclass of the Formula class.  The standard ->cmp routine
   11 #  will work for this, provided we define the compare() function
   12 #  needed by the overloaded ==.  We assign the special precedence
   13 #  so that overloaded operations will be promoted to the ones below.
   14 #
   15 #  Use ParametricLine(point,vector) or ParametricLine(formula)
   16 #  to create a ParametricLine object.  You can pass an optional
   17 #  additional parameter that indicated the variable to use for the
   18 #  parameter for the line.
   19 #
   20 #  Usage examples:
   21 #
   22 #      $L = ParametricLine(Point(3,-1,2),Vector(1,1,3));
   23 #      $L = ParametricLine([3,-1,2],[1,1,3]);
   24 #      $L = ParametricLine("<t,1-t,2t-3>");
   25 #
   26 #      $p = Point(3,-1,2); $v = Vector(1,1,3);
   27 #      $L = ParametricLine($p,$v);
   28 #
   29 #      $t = Formula('t'); $p = Point(3,-1,2); $v = Vector(1,1,3);
   30 #      $L = ParametricLine($p+$t*$v);
   31 #
   32 #      Context()->constants->are(a=>1+pi^2); # won't guess this value
   33 #      $L = ParametricLine("(a,2a,-1) + t <1,a,a^2>");
   34 #
   35 #  Then use
   36 #
   37 #     ANS($L->cmp);
   38 #
   39 #  to get the answer checker for $L.
   40 #
   41 
   42 =cut
   43 
   44 #
   45 #  Define a new context for lines
   46 #
   47 $context{ParametricLine} = Context("Vector")->copy();
   48 $context{ParametricLine}->variables->are(t=>'Real');
   49 $context{ParametricLine}->{precedence}{ParametricLine} =
   50   $context{ParametricLine}->{precedence}{special};
   51 $context{ParametricLine}->reduction->set('(-x)-y'=>0);
   52 #
   53 #  Make it active
   54 #
   55 Context("ParametricLine");
   56 
   57 #
   58 #  Syntactic sugar
   59 #
   60 sub ParametricLine {ParametricLine->new(@_)}
   61 
   62 #
   63 #  Define the subclass of Formula
   64 #
   65 package ParametricLine;
   66 our @ISA = qw(Value::Formula);
   67 
   68 sub new {
   69   my $self = shift; my $class = ref($self) || $self;
   70   my ($p,$v,$line,$t);
   71   return shift if scalar(@_) == 1 && ref($_[0]) eq $class;
   72   $_[0] = Value::Point->new($_[0]) if ref($_[0]) eq 'ARRAY';
   73   $_[1] = Value::Vector->new($_[1]) if ref($_[1]) eq 'ARRAY';
   74   if (scalar(@_) >= 2 && Value::class($_[0]) eq 'Point' &&
   75                          Value::class($_[1]) eq 'Vector') {
   76     $p = shift; $v = shift;
   77     $t = shift || Value::Formula->new('t');
   78     $line = $p + $t*$v;
   79   } else {
   80     $line = Value::Formula->new(shift);
   81     Value::Error("Your formula doesn't look like a parametric line")
   82       unless $line->type eq 'Vector';
   83     $t = shift || (keys %{$line->{variables}})[0];
   84     Value::Error("A line can't be just a constant vector") unless $t;
   85     $p = Value::Point->new($line->eval($t=>0));
   86     $v = Value::Vector->new($line->eval($t=>1) - $p);
   87     Value::Error("Your formula isn't linear in the variable %s",$t)
   88       unless $line == $p + Value::Formula->new($t) * $v;
   89   }
   90   Value::Error("The direction vector for a parametric line can't be the zero vector")
   91     if ($v->norm == 0);
   92   $line->{p} = $p; $line->{v} = $v;
   93   $line->{isValue} = $line->{isFormula} = 1;
   94   return bless $line, $class;
   95 }
   96 
   97 =head3 compare($lhs,$rhs)
   98 
   99 #
  100 #  Two parametric lines are equal if they have
  101 #  parallel direction vectors and either the same
  102 #  points or the vector between the points is
  103 #  parallel to the (common) direction vector.
  104 #
  105 
  106 =cut
  107 
  108 sub compare {
  109   my ($l,$r,$flag) = @_;
  110   if ($l->promotePrecedence($r)) {return $r->compare($l,!$flag)}
  111   $r = ParametricLine->new($r);
  112   if ($flag) {my $tmp = $l; $l = $r; $r = $tmp}
  113   my ($lp,$lv) = ($l->{p},$l->{v});
  114   my ($rp,$rv) = ($r->{p},$r->{v});
  115   return $lv <=> $rv unless ($lv->isParallel($rv));
  116   return 0 if $lp == $rp || $lv->isParallel($rp-$lp);
  117   return $lp <=> $rp;
  118 }
  119 
  120 sub cmp_class {'a Parametric Line'};
  121 sub showClass {shift->cmp_class};
  122 
  123 sub cmp_defaults {(
  124   shift->SUPER::cmp_defaults,
  125   showEqualErrors => 0,  # don't show problems evaluating student answer
  126   ignoreInfinity => 0,   # report infinity as an error
  127 )}
  128 
  129 #
  130 #  Report some errors that were stopped by the showEqualErrors=>0 above.
  131 #
  132 sub cmp_postprocess {
  133   my $self = shift; my $ans = shift;
  134   my $error = $$Value::context->{error}{message};
  135   $self->cmp_error($ans)
  136     if $error =~ m/^(Your formula (isn't linear|doesn't look)|A line can't|The direction vector)/;
  137 }
  138 
  139 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9