Difference between revisions of "Vector (MathObject Class)"
(→Operations on Vectors: Added information on the overloaded x operator) 

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 === 
Revision as of 12:51, 28 November 2017
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 twodimensional vectors, and k
is not defined. It is possible to create Vectors in [math]{\bf C}^n[/math], though there is no predefined 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 nonzero 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 matrixvector 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 nonzero 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 answerchecker 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 anglebraces and commas. This allows the student to enter vectorvalues 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>compare ($w)

Returns 1, 0, or 1, depending on whether $v is less than, equal to, or greater than, $w .If 
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  >
