WeBWorK Problems

Problems with LimitedVector-ijk

Problems with LimitedVector-ijk

by Olivia Henders -
Number of replies: 2
Hello all,

I've been trying to set up a problem that demonstrates all the different ways of using vectors in WeBWorK, which includes limiting the answer entry format. I've set up the vectors $vCoord and $vIJK as my "initial" vectors in their respective formats, then performed operations on them to create new vectors. In the problem, I want the user to be forced to enter the answers in certain formats depending on which vector is used.

The problem is that, while the LimitedVector-coordinate context is working (i.e. If you try to enter these answers in ijk format, they are marked incorrect with a message explaining that variable 'i' is not defined), LimitedVector-ijk is not. With the latter, the answer can still be entered in either format, despite the setting of the ijk flag in addition to the context setting.

Am I missing something? As far as I can see, it seems that the LimitedVector-ijk context isn't working properly.

Here is the setup code for the problem:

#############################
# Setup

$showPartialCorrectAnswers = 1;

Context("Vector");

$vCoord = Compute("<-2,4,0>");
$vIJK = Compute("-2i + 4j -6k");
$vCol = ColumnVector(5,-3,-2);
# Can't perform operations in the LimitedVector context,
# so they have to be done here.
$vAdd = Compute("$vCoord") + Compute("<3,1,-1>");
$vSub = Compute("$vCoord") - Compute("<3,1,-1>");
$vMult = Compute("$vIJK") * Compute("3");
$vDiv = Compute("$vIJK") / Compute("2");
$vDot = $vCoord . $vIJK;
$vCross = $vCoord x $vIJK;

# Reassign values in appropriate contexts now that the operations have been performed.
Context("LimitedVector-coordinate")->flags->set( ijk=>0 );
$vAdd = Compute("$vAdd"); #Compute("$vCoord") + Compute("<3,1,-1>");
$vSub = Compute("$vSub"); #Compute("$vCoord") - Compute("<3,1,-1>");

Context("LimitedVector-ijk")->flags->set(ijk=>1);
$vMult = $vMult; #Compute("$vIJK") * Compute("3");
$vDiv = $vDiv; #Compute("$vIJK") / Compute("2");

Context("LimitedVector");
$vDot = $vDot; #$vCoord . $vIJK;
$vCross = $vCross; #$vCoord x $vIJK;

In reply to Olivia Henders

Re: Problems with LimitedVector-ijk

by Davide Cervone -
You have not provided a complete problem, only a code snippet, so it is hard to diagnose. But I suspect that the problem is that you are not actually using the LimitedVector-ijk context.

When you do

Context("LimitedVector-ijk")->flags->set(ijk=>1);
$vMult = $vMult;
$vDiv = $vDiv;
this does not actually use the new context. The $vMult and $vDiv were defined in the Vector context, and they retain that context even when you change to a new on. The new only only affects newly created objects. When you say
$vMult = $vMult;
you are not creating a new object, you are not really doing anything at all, since you are just setting variable equal to itself ($vMult will point to the same object that it did originally). You would have to create a new object in the context.

Context("LimitedVector-ijk")->flags->set(ijk=>1);
$vMult = Compute("$vIJK") * Compute("3");
does create a new vector, so that one should require IJK notation (I have checked it and it does for me). But it is also more complicated than it needs to be. You could just do
Context("LimitedVector-ijk")->flags->set(ijk=>1);
$vMult = Compute($vIJK) * 3;
as Compute() will create a copy of $vIJK in the new context and then multiply that by 3. No need to use quotation marks (no need to convert $vIJK to a string first and then reparse it), and no need to convert 3 to a MathObject at all, since multiplying by a vector will cause that to happen automatically.

Note, however that

Context("LimitedVector");
$vDot = $vCoord . $vIJK;
does not create a value in the LimitedVector context. Instead, it is in the same context as the $vCoord vector (computations with a MathObject stay in the context of that MathObject).

I hope that lets you work out what you are trying to do.

In reply to Davide Cervone

Re: Problems with LimitedVector-ijk

by Olivia Henders -
Thank you! After some fiddling, it turned out that there were some issues with the variables being reparsed by the compute function combined with the LimitedVector contexts. At any rate, it's all sorted and working now, so thank you for your help!