Difference between revisions of "Vectors"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Vectors in Problems: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to use vectors in ...)
 
m
Line 43: Line 43:
 
<p>
 
<p>
 
Then, we can define vectors as we might expect: either with the <code>Vector</code> or <code>Compute</code> constructors, or by using the predefined vector constants <code>i</code>, <code>j</code> and <code>k</code>.
 
Then, we can define vectors as we might expect: either with the <code>Vector</code> or <code>Compute</code> constructors, or by using the predefined vector constants <code>i</code>, <code>j</code> and <code>k</code>.
  +
</p>
  +
<p>
  +
Note that if we define the vector using the constants <b>i</b>, <b>j</b> and <b>k</b>, as in the definition of <code>$v3</code> here, then the default display of that vector will be in <i>i,j,k</i> format even if we don't set the corresponding Context flag.
 
</p>
 
</p>
 
<p>
 
<p>

Revision as of 16:01, 3 June 2009

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

  $a = 3*i + j;
  $b = $a + $v1;

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').

  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) \}

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 $student->isParallel($correct);
  } ) );
  ## or:
  # ANS( $v1->cmp( checker=>sub {
  #   my ($correct, $student, $ansHash) = @_;
  #   return $student.$correct == 0; } ) );

  ANS( $v3->cmp( checker=>sub {
    my ($correct, $student, $ansHash) = @_;
    return $student->isParallel($correct);
  } ) );

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.

Problem Techniques Index