## WeBWorK Problems

### vectors input in different formats, custom comparisons ### vectors input in different formats, custom comparisons

by Gavin LaRose -
Number of replies: 3
Hi all,

I have a problem with the context declared as Context("Vector"), and variables set to be Context("Vector")->variables->are(s=>'Real',t=>'Real').

The correct answer is defined as $rparam = Vector( "$ppos + $dv1*s +$dv2*t" );, where $ppos, $dv1, and $dv2 are (constant) vectors. In the problem answer evaluation, I have a custom checker that starts checker=>sub { my ($c, $s,$ans ) = @_; my @stucoord = $s->value; Then, if an answer is entered with angle brackets, the result is as I expect: @stucoord is an array of three Value::Formula objects. However, if the answer is entered in ijk format (as a i + b j + c k),$s and $stucoord are Value::Formula objects, but$stucoord and $stucoord are empty. Obviously, I'm demonstrating an inability to understand MathObjects again. If someone could explain what I'm doing wrong that would be great. Thanks, Gavin In reply to Gavin LaRose ### Re: vectors input in different formats, custom comparisons by Paul Pearson - Dear Gavin, I asked Davide Cervone about things like this a while back. If I remember correctly (and I might not), it boils down to$s->value; working as expected when $s is a vector whose components are numbers, but not when$s is a vector whose components are formulas. I think this is because $s->value; converts from MathObjects to perl, but I could be misremembering. When the correct answer is a vector of formulas, you should perhaps use a custom grader that checks each component separately by using dot products to check each component separately. This technique is used in example 3 on the page http://webwork.maa.org/wiki/VectorValuedFunctions Good luck! Paul In reply to Paul Pearson ### Re: vectors input in different formats, custom comparisons by Gavin LaRose - Hi Paul, Thanks! And yet, the plot thickens: next, I want to actually work with the component of the student's vector (e.g., by differentiating it). If I take $xcomp = $s . i;$xdt = $xcomp->D('t'); I get $xcomp to be an (ostensibly unevaluated) expression with the dot product explicitly remaining. So if the student's answer is $s = (4 + 3t)i + (-1 + 5t)j + (1 - 5t)k, then $xcomp ends up being ([(4+3*t)*i+(-1-5*t)*j+(1-5*t)*k].i). Then trying to calculate $xdt triggers the error "Differentiation for 'dot' is not implemented." My first thought was that I need to force the evaluation of the dot product, but I haven't managed to make that happen. Gavin In reply to Gavin LaRose ### Re: vectors input in different formats, custom comparisons by Paul Pearson - Hi Gavin, I think I've figured out how to resolve a vector whose components are real numbers or real-valued formulas into its component functions. The idea is to take a vector object$a = random(3,6,1);
$U = Vector("<3,5>");$V = Vector("<t+3,t^2>");
$W = Vector("$a*t^2*i+2*j");

get its perl representation via $U =$U->perl; which will look like

Value::Vector->new(Value::Real->new(3),Value::Real->new(5))
Value::Vector->new($t + 3,$t ** 2)
((5 * ($t ** 2)) * i) + (2 * j) and then use Perl's string manipulation facilities to obtain arrays of perl strings like ("(3)","(5)") ("t+3","t**2") ("((5*(t**2)))", "+(2)") Then, the perl string in these arrays can be made into MathObjects via the usual methods Formula("perl string"), and then differentiated like any other MathObject. I've created a subroutine called components to covert from a MathObject vector to a perl array of strings, and I illustrate its use below. Let me know if you find any bugs. Maybe Davide Cervone could be convinced to add this (or something like it with better error handling) to Vector.pm. Best Regards, Paul Pearson ################################# # Initialization DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "parserVectorUtils.pl", ); TEXT(beginproblem()); ################################# # Setup Context("Vector2D"); #Context("Vector"); Context()->variables->are(t=>"Real"); Context()->variables->set(t=>{limits=>[0,5]}); Context()->flags->set( ijk=>1 );$a = random(3,6,1);

sub components {

my $V = shift;$V = $V->perl; if ($V =~ m/Value/ ) {

$V =~ s/Value::Vector->new~~(//g;$V = substr($V, 0, -1);$V =~ s/Value::Real->new//g;
$V =~ s/~~$//g;
$V =~ s/ //g; return split(',',$V);

} else {

$V =~ s/~~* i~~)/~~),/g;$V =~ s/~~* j~~)/~~),/g;
$V =~ s/~~* k~~)/~~),/g;$V =~ s/~~$//g;$V =~ s/ //g;
$V = substr($V, 0, -1);
return split(',',$V); } }$U = Vector("<3,5>");
@Ucomp = components($U);$U = $U->perl;$V = Vector("<t+3,t^2>");
@Vcomp = components($V);$V = $V->perl;$W = Vector("$a*t^2*i+2*j"); @Wcomp = components($W);
$W =$W->perl;

$Vy = Formula("$Vcomp");
$Vyderiv =$Vy->D('t');

#####################################
# Main text

Context()->texStrings;
BEGIN_TEXT
U is $U$BR
V is $V$BR
W is $W$PAR
U components are $Ucomp,$Ucomp
$BR V components are$Vcomp, $Vcomp$BR
W components are $Wcomp,$Wcomp
$PAR The derivative of the second component of V is $$Vyderiv$$ END_TEXT Context()->normalStrings; ############################ # Answer evaluation$showPartialCorrectAnswers = 1;

ENDDOCUMENT();