Difference between revisions of "Vector (MathObject Class)"

From WeBWorK_wiki
Jump to navigation Jump to search
(Add headings, and add Answer Checker, Methods, and Properties sections)
Line 1: Line 1:
== The Vector Class ==
+
=== 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 <code>&lt;-2,4,0&gt;</code>, but that can be controlled by settings in the Context. Vectors can also be produced using the coordinate unit vectors, <code>i</code>, <code>j</code>, and <code>k</code>, 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.
+
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 <code>&lt;-2,4,0&gt;</code>, but that can be controlled by settings in the Context. Vectors can also be produced using the coordinate unit vectors, <code>i</code>, <code>j</code>, and <code>k</code>, as in the examples below. Vectors typically are created in the <code>Vector</code> or <code>Matrix</code> Contexts. There is also a <code>Vector2D</code> Context which is specifically for vectors in the plane. Here, <code>i</code> and <code>j</code> are defined as two-dimensional vectors, and <code>k</code> 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 <code>pg/macros/parserVectorUtils.pl</code> includes some useful routines for obtaining things like non-zero points and vectors, or equations of lines and planes; see the [http://webwork.maa.org/pod/pg_TRUNK/macros/parserVectorUtils.pl.html POD documentation] for details.
  +
  +
  +
=== Creation ===
  +
  +
Vectors are created via the <code>Vector()</code> function, or by <code>Compute()</code>.
   
 
Context("Vector");
 
Context("Vector");
Line 12: Line 12:
 
$v = Compute("-2i + 4j");
 
$v = Compute("-2i + 4j");
 
$v = -2*i + 4*j;
 
$v = -2*i + 4*j;
 
  +
  +
There is a special constructor, <code>ColumnVector()</code> 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 + Vector(3,1,-1); # same as Vector(1,5,-1);
 
$u = $v + [3,1,-1]; # same as Vector(1,5,-1);
 
$u = $v + [3,1,-1]; # same as Vector(1,5,-1);
Line 46: Line 55:
 
$f = Formula("(1,-3,5) + t<3,1,-1>");
 
$f = Formula("(1,-3,5) + t<3,1,-1>");
   
There is a special constructor, <code>ColumnVector()</code> 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);
 
  +
=== Answer Checker ===
  +
  +
As with all MathObjects, you obtain an answer checker for a Vector object via the <code>cmp()</code> method:
  +
  +
ANS(Compute("<1,5,-2>")->cmp);
  +
  +
The Vector class supports the [[Answer_Checker_Options_(MathObjects)| common answer-checker options]], and the following additional options:
  +
  +
{| class="wikitable"
  +
! Option !! Description !! style="padding:5px" | Default
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>showDimensionHints => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Show/don't show messages about the wrong number of coordinates.
  +
| style="text-align:center" | <code>1</code>
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>showCoordinateHints => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Show/don't show message about which coordinates are right.
  +
| style="text-align:center" | <code>1</code>
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>promotePoints => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Do/don't allow the student to enter a point rather than a vector.
  +
| style="text-align:center" | <code>1</code>
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>parallel => 1</code> or <code>0</code>
  +
| style="padding: 5px" | Mark the answer as correct if it is parallel to the professor's answer. Note that a value of <code>1</code> forces <code>showCoordinateHints</code> to be <code>0</code>.
  +
| style="text-align:center" | <code>0</code>
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>sameDirection => 1</code> or <code>0</code>
  +
| style="padding: 5px" | 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.
  +
| style="text-align:center" | <code>0</code>
  +
|}
  +
  +
  +
=== Methods ===
  +
  +
The Vector class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional methods:
  +
  +
{| class="wikitable"
  +
! Option !! Description
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>$v . $w&nbsp;</code> &nbsp; or &nbsp; <code>$v->dot($w)</code>
  +
| style="padding: 5px" | Returns the dot product of the two vectors as a Real.
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>$v x $w&nbsp;</code> &nbsp; or &nbsp; <code>$v->cross($w)</code>
  +
| style="padding: 5px" | Returns the cross product of the two vectors (in <math>{\bf R}^3</math> as a Vector.
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>norm($v)</code> &nbsp; or &nbsp; <code>$v->norm</code>
  +
| style="padding: 5px" | Returns the magnitude of <code>$c</code> as a Real. Also available as <code>$v->abs</code> or <code>abs($v)</code>.
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>unit($v)</code> &nbsp; or &nbsp; <code>$v->unit</code>
  +
| style="padding: 5px" | Returns the unit vector in the same direction as <code>$v</code> (or <code>$v</code> if <code>$v</code> is the zero vector) as a Vector.
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>areParallel($v,$w)</code> &nbsp; or &nbsp; <code>$v->isParallel($w)</code>
  +
| style="padding: 5px" | Returns 1 if the two vectors are parallel (and non-zero), and 0 otherwise.
  +
|}
  +
   
In addition to the <code>Vector</code> context, there is also <code>Vector2D</code> which is specifically for vectors in the plane. Here, <code>i</code> and <code>j</code> are defined as two-dimensional vectors, and <code>k</code> is not defined. (In <code>Vector</code> context, <code>i</code> and <code>j</code> are 3-dimensional, and <code>k</code> is available as well).
 
  +
=== Properties ===
   
The file <code>pg/macros/parserVectorUtils.pl</code> includes some useful routines for obtaining things like non-zero points and vectors, or equations of lines and planes. See the [http://webwork.maa.org/pod/pg_TRUNK/macros/parserVectorUtils.pl.html POD documentation] for details.
 
  +
The Vector class supports the [[MathObject_Methods_and_Properties| common MathObject properties]]. There are no additional properties for this class.
   
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.
 
   
 
<br>
 
<br>

Revision as of 13:12, 4 August 2012

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


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.