WeBWorK Problems

vectors input in different formats, custom comparisons

Re: vectors input in different formats, custom comparisons

by Paul Pearson -
Number of replies: 0
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[1]");
$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[0], $Ucomp[1]
$BR
V components are $Vcomp[0], $Vcomp[1]
$BR
W components are $Wcomp[0], $Wcomp[1]
$PAR
The derivative of the second component of V is \( $Vyderiv \)
END_TEXT
Context()->normalStrings;


############################
# Answer evaluation

$showPartialCorrectAnswers = 1;

ENDDOCUMENT();