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)
 
(8 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
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 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.
+
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/macros/parserVectorUtils.html POD documentation] for details.
   
   
Line 64: Line 64:
 
$f = Formula("(1,-3,5) + t<3,1,-1>");
 
$f = Formula("(1,-3,5) + t<3,1,-1>");
   
  +
Please note that the operator <code>x</code> is also used as a repetition operator in the Perl language; for example, <code>(1,2,3) x 2</code> produces the array <code>(1,2,3,1,2,3)</code>. For this reason, it can sometimes happen that cross products are not interpreted correctly; for example, <code>norm(($a - $b) x $c)</code> may produce an error. If this happens, a workaround is to ensure that the cross product is computed in a scalar context: <code>norm(scalar(($a - $b) x $c))</code>.
   
 
=== Answer Checker ===
 
=== Answer Checker ===
Line 96: Line 97:
 
| style="text-align:center" | <code>0</code>
 
| style="text-align:center" | <code>0</code>
 
|}
 
|}
  +
  +
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 <code>LimitedVector</code> Context available in the <code>pg/macros/contextLimitedVector.pl</code> file; see the [http://webwork.maa.org/pod/pg/macros/contextLimitedVector.html POD documentation] for details.
  +
  +
Alternatively, you can use the Vector's <code>ans_array()</code> method rather than PG's <code>ans_rule()</code> 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 ===
 
=== Methods ===
   
The Vector class supports the [[MathObject_Methods_and_Properties| common MathObject methods]], and the following additional methods:
+
The Vector class supports the [[Common MathObject Methods]], and the following additional methods:
   
 
{| class="wikitable"
 
{| class="wikitable"
! Option !! Description
+
! Method !! Description
 
|- style="vertical-align: top"
 
|- 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; white-space: nowrap" | <code>$v->length</code>
  +
| style="padding: 5px" | Returns the number of entries in <code>$v</code>
  +
|- 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="padding: 5px" | Returns the dot product of the two vectors as a Real.
 
|- style="vertical-align: top"
 
|- 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; 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="padding: 5px" | Returns the cross product of the two vectors (in <math>{\bf R}^3</math>) as a Vector.
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
| style="padding: 5px; white-space: nowrap" | <code>norm($v)</code> &nbsp; or &nbsp; <code>$v->norm</code>
+
| style="padding: 5px; white-space: nowrap" | <code>$v->neg</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="padding: 5px" | Returns <code>-$v</code>
 
|- style="vertical-align: top"
 
|- style="vertical-align: top"
| style="padding: 5px; white-space: nowrap" | <code>unit($v)</code> &nbsp; or &nbsp; <code>$v->unit</code>
+
| 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>absb($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="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="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; 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.
+
| style="padding: 5px" | Returns 1 if the two vectors are parallel, and 0 otherwise.<br />
  +
Note: it will return 1 if both <code>$v</code> and <code>$w</code> are zero vectors
  +
|- style="vertical-align: top"
  +
| style="padding: 5px; white-space: nowrap" | <code>$v->compare ($w)</code>
  +
| style="padding: 5px" | Returns -1, 0, or 1, depending on whether <code>$v</code> is less than, equal to, or greater than, <code>$w</code>.<br />
  +
If <code>$v</code> and <code>$w</code> have different lengths, the smaller one is the one with fewer entries.<br />
  +
Otherwise, comparison is done with lexicographic order.
 
|}
 
|}
 
   
 
=== Properties ===
 
=== Properties ===
   
The Vector class supports the [[MathObject_Methods_and_Properties| common MathObject properties]]. There are no additional properties for this class.
+
The Vector class supports the [[Common MathObject Properties]], and the following additional ones:
  +
  +
{| class="wikitable"
  +
! Property !! Description !! style="padding:5px" | Default
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$r->{ColumnVector}</code>
  +
| style="padding: 5px" | When set, the vector will display as vertically in TeX output, horizontally otherwise
  +
| style="text-align:center" | <code>undef</code>
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$r->{open}</code>
  +
| style="padding: 5px" | The symbol to use for the open angle bracket
  +
| style="text-align:center" | <code>&lt;</code>
  +
  +
|- style="vertical-align: baseline"
  +
| style="padding: 5px; white-space: nowrap" | <code>$r->{close}</code>
  +
| style="padding: 5px" | The symbol to use for the close angle bracket
  +
| style="text-align:center" | <code>&gt;</code>
  +
|}
   
   

Latest revision as of 17:57, 7 April 2021

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

Please note that the operator x is also used as a repetition operator in the Perl language; for example, (1,2,3) x 2 produces the array (1,2,3,1,2,3). For this reason, it can sometimes happen that cross products are not interpreted correctly; for example, norm(($a - $b) x $c) may produce an error. If this happens, a workaround is to ensure that the cross product is computed in a scalar context: norm(scalar(($a - $b) x $c)).

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:

Method Description
$v->length Returns the number of entries in $v
$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.
$v->neg Returns -$v
norm ($v)   or   $v->norm Returns the magnitude of $c as a Real. Also available as $v->abs or absb($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 0 otherwise.

Note: it will return 1 if both $v and $w are zero vectors

$v->compare ($w) Returns -1, 0, or 1, depending on whether $v is less than, equal to, or greater than, $w.

If $v and $w have different lengths, the smaller one is the one with fewer entries.
Otherwise, comparison is done with lexicographic order.

Properties

The Vector class supports the Common MathObject Properties, and the following additional ones:

Property Description Default
$r->{ColumnVector} When set, the vector will display as vertically in TeX output, horizontally otherwise undef
$r->{open} The symbol to use for the open angle bracket <
$r->{close} The symbol to use for the close angle bracket >