Vector (MathObject Class)

From WeBWorK_wiki
Revision as of 16:40, 2 August 2012 by Dpvc (talk | contribs)
Jump to navigation Jump to search

The 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 (of the same dimension) can be added and subtracted, or multiplied and divided by scalars. The answer checker for Vectors can give students hints about the coordinates that are wrong, and about whether the number of coordinates is correct.

   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;
   
   $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>");

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);

In addition to the Vector context, there is also Vector2D which is specifically for vectors in the plane. Here, i and j are defined as two-dimensional vectors, and k is not defined. (In Vector context, i and j are 3-dimensional, and k is available as well).

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.

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.