Vector (MathObject Class)

From WeBWorK_wiki
Revision as of 15:05, 4 August 2012 by Dpvc (talk | contribs) (Add info on ans_array method)
Jump to navigation Jump to search

Vector Class

The Vector class implements vector quantities in [math]{\bf R}^n[/math] for arbitrary [math]n[/math]. Typically, vectors are delimited by angle brackets, as in <-2,4,0>, but that can be controlled by settings in the Context. Vectors can also be produced using the coordinate unit vectors, i, j, and k, as in the examples below. Vectors typically are created in the Vector or Matrix Contexts. There is also a Vector2D Context which is specifically for vectors in the plane. Here, i and j are defined as two-dimensional vectors, and k is not defined. It is possible to create Vectors in [math]{\bf C}^n[/math], though there is no pre-defined Context that makes this easy to do.

The answer checker for Vectors can give students hints about the coordinates that are wrong, and about whether the number of coordinates is correct.

The file pg/macros/parserVectorUtils.pl includes some useful routines for obtaining things like non-zero points and vectors, or equations of lines and planes; see the POD documentation for details.


Creation

Vectors are created via the Vector() function, or by Compute().

   Context("Vector");
   
   $v = Vector(-2,4,0);
   $v = Vector([-2,4,0]);
   $v = Vector("<-2,4,0>");
   $v = Compute("<-2,4,0>");
   $v = Vector("-2i + 4j");
   $v = Compute("-2i + 4j");
   $v = -2*i + 4*j;

There is a special constructor, ColumnVector() which will produce a Vector object that displays vertically in TeX output. This is useful for matrix-vector multiplication, or systems of equations, for example. A ColumnVector acts just like a regular vector in terms of the operations and functions described above; its only difference is that it displays vertically.

   $v = ColumnVector(5,-3,-2);


Operations on Vectors

Vectors (of the same dimension) can be added to and subtracted from each other. Vectors can be multiplied and divided by scalars.

   $u = $v + Vector(3,1,-1);     # same as Vector(1,5,-1);
   $u = $v + [3,1,-1];           # same as Vector(1,5,-1);
   $u = 3*$v;                    # same as Vector(-6,12,0);
   $u = $v/2;                    # same as Vector(-1,2,0);

The dot product can be obtained by using a period (.) between two Vectors, and the cross product in [math]{\bf R}^3[/math] is formed by an x between two Vectors in Perl code, or by >< in student answers or other strings parsed by MathObjects. The absolute value of a Vector is its length, which can also be obtained from the norm() function. A unit vector in the same direction (as a non-zero Vector) can be obtained from the unit() function.

   $a = $u . $v;                 # dot product
   $a = Compute("$u . $v");
   
   $w = $u x $v;                 # cross product
   $w = Compute("$u >< $v");
   
   $b = norm($v);                # length of a vector
   $b = abs($v);                 # same
   $b = Compute("|$v|");
   $b = Compute("abs($v)");
   $b = Compute("norm($v)");
   
   $w = unit($v);                # unit vector in direction of $v
   $w = Compute("unit($v)");

If a Vector is combined with a Point, the Point will first be converted to a Vector, and then combined to form a Vector result:

   $v = Vector(3,1,-1);
   $p = Point(1,-3,5);
   $t = Formula("t");
   
   $w = $v + $p;         # same as Vector(4,-2,4);
   
   $f = $p + $t * $v;    # a parametric line through $p in the direction of $v
   $f = Formula("(1,-3,5) + t<3,1,-1>");


Answer Checker

As with all MathObjects, you obtain an answer checker for a Vector object via the cmp() method:

   ANS(Compute("<1,5,-2>")->cmp);

The Vector class supports the common answer-checker options, and the following additional options:

Option Description Default
showDimensionHints => 1 or 0 Show/don't show messages about the wrong number of coordinates. 1
showCoordinateHints => 1 or 0 Show/don't show message about which coordinates are right. 1
promotePoints => 1 or 0 Do/don't allow the student to enter a point rather than a vector. 1
parallel => 1 or 0 Mark the answer as correct if it is parallel to the professor's answer. Note that a value of 1 forces showCoordinateHints to be 0. 0
sameDirection => 1 or 0 During a parallel check, mark the answer as correct only if it is in the same (not the opposite) direction as the professor's answer. 0

By default, the Vector answer checker asks the student to type the entire vetor, including the angle-braces and commas. This allows the student to enter vector-values expressions (like sums of vectors, or cross products, or scalar multiples of vectors). You may want to restrict the operations that are allowed in the student answer, in which case you might consider using the LimitedVector Context available in the pg/macros/contextLimitedVector.pl file; see the POD documentation for details.

Alternatively, you can use the Vector's ans_array() method rather than PG's ans_rule() function to obtain a separate answer box for each coordinate. The Vector answer checker will manage this collection of answer boxes for you, so your code doesn't have to change in any other way.

   Context("Vector");
   
   $V = Vector(random(1,10,1),random(1,10,1),-1);
   
   Context()->texStrings;
   BEGIN_TEXT
   \($V\) = \{$V->ans_array\}
   END_TEXT
   Context()->normalStrings;
   
   ANS($V->cmp);

The value of this approach is that it forces the student to enter individual coordinates, without allowing operations on Vectors.


Methods

The Vector class supports the common MathObject methods, and the following additional methods:

Option Description
$v . $w    or   $v->dot($w) Returns the dot product of the two vectors as a Real.
$v x $w    or   $v->cross($w) Returns the cross product of the two vectors (in [math]{\bf R}^3[/math]) as a Vector.
norm($v)   or   $v->norm Returns the magnitude of $c as a Real. Also available as $v->abs or abs($v).
unit($v)   or   $v->unit Returns the unit vector in the same direction as $v (or $v if $v is the zero vector) as a Vector.
areParallel($v,$w)   or   $v->isParallel($w) Returns 1 if the two vectors are parallel (and non-zero), and 0 otherwise.


Properties

The Vector class supports the common MathObject properties. There are no additional properties for this class.