Difference between revisions of "Vectors"

From WeBWorK_wiki
Jump to navigation Jump to search
m
m (Document extract method defined on vectors.)
(13 intermediate revisions by 2 users not shown)
Line 13: Line 13:
 
<th> Explanation </th>
 
<th> Explanation </th>
 
</tr>
 
</tr>
  +
  +
<tr valign="top">
  +
<td style="background-color:#ddffdd;border:black 1px dashed;">
  +
<pre>
  +
loadMacros(
  +
"PGstandard.pl",
  +
"PGcourse.pl",
  +
"MathObjects.pl",
  +
"parserVectorUtils.pl",
  +
);
  +
</pre>
  +
</td>
  +
<td style="background-color:#ccffcc;padding:7px;">
  +
<p>
  +
<b>Initialization:</b>
  +
Be sure to load <code>parserVectorUtils.pl</code> and <code>MathObjects.pl</code> (since <code>parserVectorUtils.pl</code> does not automatically load <code>MathObjects.pl</code>).
  +
</p>
  +
</td>
  +
</tr>
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context('Vector');
+
Context('Vector');
## display vectors in ijk format
+
## display vectors in ijk format
# Context()->flags->set( ijk=>1 );
+
## Context()->flags->set( ijk=>1 );
  +
## set the appearance of the ijk vectors
  +
## this sets them to be overset with
  +
## vector arrows, instead of boldface
  +
# Context()->constants->set(
  +
# i => {TeX => "\mathit{\vec i}"},
  +
# j => {TeX => "\mathit{\vec j}"},
  +
# k => {TeX => "\mathit{\vec k}"},
  +
# );
   
## set the appearance of the ijk vectors
 
  +
$v1 = Vector("<1,3>");
## this sets them to be overset with
 
  +
$v2 = Compute("<-3,1>");
## vector arrows, instead of boldface
 
  +
$v3 = 3*i + 2*j - 4*k;
# Context()->constants->set(
 
  +
$v4 = Vector(1,1,0);
# i => {TeX => "\mathit{\vec i}"},
 
# j => {TeX => "\mathit{\vec j}"},
 
# k => {TeX => "\mathit{\vec k}"},
 
# );
 
   
$v1 = Vector("<1,3>");
 
  +
# create an array of the components of $v3
$v2 = Compute("<-3,1>");
 
  +
@v3comp = $v3->value;
$v3 = 3*i + 2*j - 4*k;
 
   
$a = 3*i + j;
+
$a = 3*i + j;
$b = $a + $v1;
+
$b = $a + $v1;
  +
$c = norm($v3); # vector length
  +
$v5 = unit($v3); # unit vector in same direction
  +
$d = $v1 . $v2; # dot product
  +
$v6 = $v3 x $v4; # cross product
  +
$v3->isParallel($v4); # =1 if parallel, =0 if skew
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
  +
<b>Setup:</b>
 
We indicate that we are working in a vector context by setting the <code>Context</code> to be <code>Vector</code>. If we want to have vectors displayed, by default, as a sum of <i>i,j,k</i> components, we can set the <code>ijk</code> flag in the Context. This is commented out here; uncommenting it would result in the vector <b>v1</b> here being shown as <b>v1</b> = <b>i</b> + 3<b>j</b> instead of <b>v1</b> = &lt;1,3&gt;, etc. Similarly, if we wanted to change the default display of the <b>i</b>, <b>j</b> and <b>k</b> vectors, say to have them display with overset arrows, we can redefine the TeX formatting of those constants, as shown in the second comment.
 
We indicate that we are working in a vector context by setting the <code>Context</code> to be <code>Vector</code>. If we want to have vectors displayed, by default, as a sum of <i>i,j,k</i> components, we can set the <code>ijk</code> flag in the Context. This is commented out here; uncommenting it would result in the vector <b>v1</b> here being shown as <b>v1</b> = <b>i</b> + 3<b>j</b> instead of <b>v1</b> = &lt;1,3&gt;, etc. Similarly, if we wanted to change the default display of the <b>i</b>, <b>j</b> and <b>k</b> vectors, say to have them display with overset arrows, we can redefine the TeX formatting of those constants, as shown in the second comment.
 
</p>
 
</p>
Line 49: Line 65:
 
<p>
 
<p>
 
To explicitly require that the vectors be two-dimensional rather than three-dimensional, we would use <code>Context('Vector2D')</code> instead of <code>Context('Vector')</code>.
 
To explicitly require that the vectors be two-dimensional rather than three-dimensional, we would use <code>Context('Vector2D')</code> instead of <code>Context('Vector')</code>.
  +
</p>
  +
<p>
  +
The components of MathObjects vectors are available as an array from <code>$v->value</code>; thus, we could save the three components of the vector <code>$v3</code> in the array <code>@v3comp</code>. Then, we can access the first component using $v3comp[0], the second component using $v3comp[1], etc. Better still, to get the first component of the vector <code>$v3</code> we could use <code>$v3->extract(1)</code> instead of <code>($v3->value)[0]</code>. <em>(This appears only to be the case for vectors that are initially defined using angle-bracket notation, not for vectors that are defined using i, j, and k&#8212;and this behavior may be different for vectors of numbers and vectors of formulas.)</em>
 
</p>
 
</p>
 
</td>
 
</td>
Line 55: Line 74:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
Enter the vector pointing from
+
Enter the vector pointing from
\($a\) to \($b\):
+
\($a\) to \($b\):
\{ ans_rule(25) \}
+
\{ ans_rule(25) \}
$PAR
+
$PAR
Enter a vector perpendicular to this:
+
Enter a vector perpendicular to this:
\{ ans_rule(25) \}
+
\{ ans_rule(25) \}
$PAR
+
$PAR
Enter a vector parallel to \($v3\):
+
Enter a vector parallel to \($v3\):
\{ ans_rule(25) \}
+
\{ ans_rule(25) \}
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
  +
<b>Main text:</b>
 
We can then use the vectors that we created in the text section of the problem.
 
We can then use the vectors that we created in the text section of the problem.
 
</p>
 
</p>
Line 75: Line 95:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $v1->cmp() );
+
ANS( $v1->cmp() );
   
ANS( $v2->cmp( checker=>sub {
+
ANS( $v2->cmp( checker=>sub {
my ($correct, $student, $ansHash) = @_;
+
my ($correct, $student, $ansHash) = @_;
return $student->isParallel($correct);
+
return $correct->isParallel($student);
} ) );
+
}, showCoordinateHints => 0 ) );
## or:
+
## or:
# ANS( $v1->cmp( checker=>sub {
+
# ANS( $v1->cmp( checker=>sub {
# my ($correct, $student, $ansHash) = @_;
+
# my ($correct, $student, $ansHash) = @_;
# return $student.$correct == 0; } ) );
+
# return $correct.$student == 0; } ) );
+
# make a custom answer checker as a subroutine
ANS( $v3->cmp( checker=>sub {
+
sub parallel_vector_cmp {
my ($correct, $student, $ansHash) = @_;
+
my ($correct, $student, $ansHash) = @_;
return $student->isParallel($correct);
+
return $correct->isParallel($student);
} ) );
+
}
  +
ANS( $v3->cmp( checker=>~~&parallel_vector_cmp,
  +
showCoordinateHints => 0 ) );
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
We can then use the vectors to check the answers that are given. Note that we have used [[CustomAnswerCheckers|custom answer checkers]] for the latter answer evaluators here, taking advantage of the built in dot product and <code>isParallel</code> method of vector objects.
 
  +
<b>Answer evaluation:</b>
  +
We can then use the vectors to check the answers that are given. Note that we have used [[CustomAnswerCheckers|custom answer checkers]] for the latter answer evaluators here, taking advantage of the built in dot product and <code>isParallel</code> method of vector objects. When checking if a student's vector is parallel to the correct vector, hints about which coordinates are incorrect can be misleading, so we set <code>showCoordinateHints => 0</code>.
 
</p>
 
</p>
 
<p>
 
<p>
Line 106: Line 127:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
  +
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg_TRUNK/macros/parserVectorUtils.pl.html parserVectorUtils.pl.html]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/parserVectorUtils.pl?view=log parserVectorUtils.pl]</li>
  +
</ul>

Revision as of 22:28, 3 June 2015

Vectors in Problems: PG Code Snippet

This code snippet shows the essential PG code to use vectors in WeBWorK problems. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
loadMacros(
"PGstandard.pl",
"PGcourse.pl",
"MathObjects.pl",
"parserVectorUtils.pl",
);

Initialization: Be sure to load parserVectorUtils.pl and MathObjects.pl (since parserVectorUtils.pl does not automatically load MathObjects.pl).

Context('Vector');
## display vectors in ijk format
## Context()->flags->set( ijk=>1 );
## set the appearance of the ijk vectors
##    this sets them to be overset with
##    vector arrows, instead of boldface
# Context()->constants->set(
#   i => {TeX => "\mathit{\vec i}"},
#   j => {TeX => "\mathit{\vec j}"},
#   k => {TeX => "\mathit{\vec k}"},
# );

$v1 = Vector("<1,3>");
$v2 = Compute("<-3,1>");
$v3 = 3*i + 2*j - 4*k;
$v4 = Vector(1,1,0);

# create an array of the components of $v3
@v3comp = $v3->value;

$a = 3*i + j;
$b = $a + $v1;
$c = norm($v3);    # vector length   
$v5 = unit($v3);   # unit vector in same direction
$d = $v1 . $v2;    # dot product
$v6 = $v3 x $v4;      # cross product
$v3->isParallel($v4); # =1 if parallel, =0 if skew

Setup: We indicate that we are working in a vector context by setting the Context to be Vector. If we want to have vectors displayed, by default, as a sum of i,j,k components, we can set the ijk flag in the Context. This is commented out here; uncommenting it would result in the vector v1 here being shown as v1 = i + 3j instead of v1 = <1,3>, etc. Similarly, if we wanted to change the default display of the i, j and k vectors, say to have them display with overset arrows, we can redefine the TeX formatting of those constants, as shown in the second comment.

Then, we can define vectors as we might expect: either with the Vector or Compute constructors, or by using the predefined vector constants i, j and k.

Note that if we define the vector using the constants i, j and k, as in the definition of $v3 here, then the default display of that vector will be in i,j,k format even if we don't set the corresponding Context flag.

To explicitly require that the vectors be two-dimensional rather than three-dimensional, we would use Context('Vector2D') instead of Context('Vector').

The components of MathObjects vectors are available as an array from $v->value; thus, we could save the three components of the vector $v3 in the array @v3comp. Then, we can access the first component using $v3comp[0], the second component using $v3comp[1], etc. Better still, to get the first component of the vector $v3 we could use $v3->extract(1) instead of ($v3->value)[0]. (This appears only to be the case for vectors that are initially defined using angle-bracket notation, not for vectors that are defined using i, j, and k—and this behavior may be different for vectors of numbers and vectors of formulas.)

BEGIN_TEXT
Enter the vector pointing from
\($a\) to \($b\):
\{ ans_rule(25) \}
$PAR
Enter a vector perpendicular to this:
\{ ans_rule(25) \}
$PAR
Enter a vector parallel to \($v3\):
\{ ans_rule(25) \}

Main text: We can then use the vectors that we created in the text section of the problem.

ANS( $v1->cmp() );

ANS( $v2->cmp( checker=>sub {
  my ($correct, $student, $ansHash) = @_;
  return $correct->isParallel($student);
}, showCoordinateHints => 0 ) );
## or:
# ANS( $v1->cmp( checker=>sub {
#   my ($correct, $student, $ansHash) = @_;
#   return $correct.$student == 0; } ) );
# make a custom answer checker as a subroutine
sub parallel_vector_cmp {
  my ($correct, $student, $ansHash) = @_;
  return $correct->isParallel($student);
}
ANS( $v3->cmp( checker=>~~&parallel_vector_cmp, 
               showCoordinateHints => 0 ) );

Answer evaluation: We can then use the vectors to check the answers that are given. Note that we have used custom answer checkers for the latter answer evaluators here, taking advantage of the built in dot product and isParallel method of vector objects. When checking if a student's vector is parallel to the correct vector, hints about which coordinates are incorrect can be misleading, so we set showCoordinateHints => 0.

Other properties of MathObjects vectors are given in the MathObjects reference table.

Problem Techniques Index