WeBWorK Problems

possible issue with the LimitedPolynomial context

possible issue with the LimitedPolynomial context

by Richard Lynch -
Number of replies: 0
Hi All,

Up until recently, this code worked perfectly fine for my factoring problems: 

###########################################################################
# initialization 
###########################################################################
DOCUMENT();
loadMacros(
  "MathObjects.pl",
  "PGstandard.pl",
  "parserMultiAnswer.pl",
  "contextLimitedPowers.pl",
  "contextLimitedPolynomial.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;


###########################################################################
# setup contexts and variables 
###########################################################################
Context("LimitedPolynomial-Strict");
$var1 = list_random('a','b','m','n','x','y','z','w','p','q','r','s','t','u','v');
do {$var2 = list_random('a','b','m','n','x','y','z','w','p','q','r','s','t','u','v')} until ($var2 ne $var1); 
Context()->variables->are($var1=>"Real", uc($var1)=>"Real", $var2=>"Real", uc($var2)=>"Real");
LimitedPowers::OnlyPositiveIntegers();

$b = random(-1,1,2)*random(1,6,1);
do {
  $d = random(-1,1,2)*random(2,6,1);
} while ($d == -$b || $d == $b);

$c2 = $b + $d;
$c3 = $b*$d;
$fac1 = Formula("$var1 + $b $var2")->reduce;
$fac2 = Formula("$var1 + $d $var2")->reduce;

$expn = "$var1^2 + $c2 $var1 $var2 + $c3 $var2^2";
if ($c2 == -1) {
  $expn = "$var1^2 - $var1 $var2 + $c3 $var2^2";
} elsif ($c2 == 1) {
  $expn = "$var1^2 + $var1 $var2 + $c3 $var2^2";
}

$answer = MultiAnswer($fac1, $fac2)->with(
  singleResult => 0,
  allowBlankAnswers => 1,
  checker => sub {
    my ( $correct, $student, $self ) = @_;
    my ( $c1, $c2 ) = @{$correct};
    my ( $s1, $s2 ) = @{$student};
    $s1 = Formula($s1) unless ($s1->type eq 'Formula' || $s1->type eq 'String');
    $s2 = Formula($s2) unless ($s2->type eq 'Formula' || $s2->type eq 'String');
    $s1 = Formula("0.1") if ($s1->type eq 'String'); #bogus answer
    $s2 = Formula("0.01") if ($s2->type eq 'String'); #bogus answer
    if (($c1 == $s1 && $c1 == $s2) || ($c2 == $s1 && $c2 == $s2)) {
      return [1,0];
    } elsif (($c1 == -$s1 && $c1 == -$s2) || ($c2 == -$s1 && $c2 == -$s2)) {
      return [1,0];
    } elsif (($c1 == $s1 && $c2 == $s2) || ($c1 == $s2 && $c2 == $s1)) {
      return [1,1];
    } elsif (($c1 == -$s1 && $c2 == -$s2) || ($c1 == -$s2 && $c2 == -$s1)) {
      return [1,1];
    } elsif (($c1 == $s1 && $c2 != $s2) || ($c2 == $s1 && $c1 != $s2)) {
      return [1,0];
    } elsif (($c1 == -$s1 && $c2 == $s2) || ($c2 == -$s1 && $c1 == $s2)) {
      return [0,1];
    } elsif (($c1 == $s2 && $c2 != $s1) || ($c2 == $s2 && $c1 != $s1)) {
      return [0,1];
    }
    return [0,0];
  }
);


###########################################################################
# state the problem 
###########################################################################
Context()->texStrings;
BEGIN_TEXT
Factor the expression and simplify your answer as much as possible:
$PAR $BCENTER 
\($expn = \big( \) \{ $answer->ans_rule(20) \} \( \big) \big( \) \{ $answer->ans_rule(20) \} \( \big) \) 
$ECENTER
END_TEXT
Context()->normalStrings;


###########################################################################
# check the answer  
###########################################################################
ANS($answer->cmp());

However, now it is not working and I get the error "You can only use '-' with monomials" so it's not even making it to the answer checking. I really don't understand why this error is appearing now. Any ideas? Is it most likely related to an update to the server? I have also attached a screen shot of the issue. Thanks in advance!

EDIT: I figured out that the negative sign in front of the $s1 and $s2 variables in the Multianswer is throwing the issue. I understand that in the Limited Polynomial context you can't put a negative in front of the entire polynomial and that's what's causing the problems, however, I don't understand why this worked before and now it doesn't. Any ideas?
Attachment Screen_shot_2013-05-13_at_4.51.07_PM.png