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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5556 - (download) (as text) (annotate)
Thu Oct 4 16:40:49 2007 UTC (12 years, 3 months ago) by sh002i
File size: 5218 byte(s)
added standard copyright/license header

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork2/lib/WeBWorK.pm,v 1.100 2007/08/13 22:59:53 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 parserVectorUtils.pl - Utility macros that are useful in vector problems.
   20 
   21 =head1 DESCRIPTION
   22 
   23 Some utility routines that are useful in vector problems
   24 
   25 =cut
   26 
   27 sub _parserVectorUtils_init {}; # don't reload this file
   28 
   29 =head1 MACROS
   30 
   31 =head2 Overline
   32 
   33   Overline($vectorName)
   34 
   35 formats a vector name (should be used in math mode)
   36 
   37 Vectors will be in bold italics in HTML modes, and
   38 will be overlined in TeX modes.  (Bold italic could also work in
   39 TeX modes, but the low resolution on screen made it less easy
   40 to distinguish the difference between bold and regular letters.)
   41 
   42 =cut
   43 
   44 sub Overline {
   45   my $v = shift;
   46   my $HTML = '<B><I>'.$v.'</B></I>';
   47   MODES(
   48     TeX => "\\overline{$v}",
   49     HTML => $HTML,
   50     HTML_tth => '\begin{rawhtml}'.$HTML.'\end{rawhtml}',
   51     HTML_dpng => "\\overline{$v}",
   52   );
   53 }
   54 
   55 =head2 BoldMath
   56 
   57   BoldMath($vectorName)
   58 
   59 This gets a bold letter in TeX as well as HTML modes.
   60 Although \boldsymbol{} works fine on screen in latex2html mode,
   61 the PDF file produces non-bold letters.  I haven't been able to
   62 track this down, so used \mathbf{} in TeX mode, which produces
   63 roman bold, not math-italic bold.
   64 
   65 =cut
   66 
   67 sub BoldMath {
   68   my $v = shift;
   69   my $HTML = '<B><I>'.$v.'</B></I>';
   70   MODES(
   71     TeX => "\\boldsymbol{$v}", #  doesn't seem to work in TeX mode
   72 #    TeX => "\\mathbf{$v}",      #  gives non-italic bold in TeX mode
   73     Latex2HTML => "\\boldsymbol{$v}",
   74     HTML => $HTML,
   75     HTML_tth => '\begin{rawhtml}'.$HTML.'\end{rawhtml}',
   76     HTML_dpng => "\\boldsymbol{$v}",
   77   );
   78 }
   79 
   80 =head2 $GRAD
   81 
   82   TEXT($GRAD)
   83 
   84   BEGIN_TEXT
   85   $GRAD
   86   END_TEXT
   87 
   88 Grad symbol.
   89 
   90 =cut
   91 
   92 $GRAD = '\nabla ';
   93 
   94 =head2 non_zero_point
   95 
   96   non_zero_point($Dim,$L_bound,$U_bound,$step)
   97 
   98 Create a non-zero point with the given number of coordinates
   99 with the given random range (which defaults to (-5,5,1)).
  100 
  101 non_zero_point(n,a,b,c)
  102 non_zero_point_2D(a,b,c)
  103 non_zero_point_3D(a,b,c)
  104 
  105 non_zero_point2D and 3D automatically set Dimension to 2 and 3 respectively.
  106 
  107 =cut
  108 
  109 sub non_zero_point {
  110   my $n = shift; my $k = $n; my @v = ();
  111   my $a = shift || -5; my $b = shift || $a + 10; my $c = shift || 1;
  112   while ($k--) {push(@v,random($a,$b,$c))}
  113   if (norm(Point(@v)) == 0) {$v[random(0,$n-1,1)] = non_zero_random($a,$b,$c)}
  114   return Point(@v);
  115 }
  116 sub non_zero_point2D {non_zero_point(2,@_)}
  117 sub non_zero_point3D {non_zero_point(3,@_)}
  118 
  119 =head2 non_zero_vector, non_zero_vector2D, non_zero_vector3D
  120 
  121   non_zero_vector($Dim,$L_bound,$U_bound,$step)
  122 
  123   non_zero_vector2D($L_bound,$U_bound,$step)
  124 
  125   non_zero_vector3D($L_bound,$U_bound,$step)
  126 
  127 Functions the same as non_zero_point but for Vectors. non_zero_vector2D and
  128 non_zero_vector3D automatically set Dimension to 2 and 3 respectively.
  129 
  130 =cut
  131 
  132 sub non_zero_vector   {Vector(non_zero_point(@_))}
  133 sub non_zero_vector2D {non_zero_vector(2,@_)}
  134 sub non_zero_vector3D {non_zero_vector(3,@_)}
  135 
  136 =head2 Line
  137 
  138   $P = Point(@coords1);
  139   $V = Vector(@coords2);
  140   $t = 't';
  141   Line($P,$V);
  142   Line($P,$V,$t);
  143 
  144 Form the vector-parametric form for a line given its point and vector, where $P
  145 is the point and $V the direction vector for the line, and $t is the variable to
  146 use (default is 't').
  147 
  148 For example:
  149 
  150   Line([1,-3],[2,1]);            # produces Vector("1+2t","-3+t").
  151   Line(Point(1,-3),Vector(2,1)); # produces Vector("1+2t","-3+t").
  152 
  153 (It may be better to use the ParametricLine class from
  154 parserParametricLine.pl).
  155 
  156 =cut
  157 
  158 sub Line {
  159   my @p = Point(shift)->value; my @v = Vector(shift)->value;
  160   my $t = shift; $t = 't' unless $t; $t = Formula($t);
  161   my @coords = ();
  162   die "Dimensions of point and vector don't match" unless $#p == $#v;
  163   foreach my $i (0..$#p) {push(@coords,($p[$i]+$v[$i]*$t)->reduce)}
  164   return Vector(@coords);
  165 }
  166 
  167 =head2 Plane
  168 
  169   Plane($point,$NormalVector)
  170 
  171 Creates a displayable string for a plane given its
  172 normal vector and a point on the plane.  (Better to use
  173 the ImplicitPlane class from parserImplicitPlane.pl).
  174 
  175 =cut
  176 
  177 sub Plane {
  178   my $P = Point(shift); my $N = Vector(shift); my @N = $N->value;
  179   my $xyz = shift; $xyz = ['x','y','z'] unless defined($xyz);
  180   die "Number of variables doesn't match dimension of normal vector"
  181     unless scalar(@N) == scalar(@{$xyz});
  182   my @terms = ();
  183   foreach my $i (0..$#N) {push(@terms,$N[$i]->TeX.$xyz->[$i])}
  184   Formula(join(' + ',@terms))->reduce->TeX . " = " . ($N.$P)->TeX;
  185 }
  186 
  187 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9