WeBWorK Problems

postfilters in multians

postfilters in multians

by Nandor Sieben -
Number of replies: 1
I'd like to rewrite the problem below so that it asks for the two coefficients separately instead of a 2x1 matrix, and then uses a multians evaluator. This way the problem could be assigned before coordinates are introduced. How would postfilter work with the multians?

----------------------
## DBsubject(Linear algebra)
## DBchapter(Euclidean spaces)
## DBsection(Coordinate vectors and change of basis)
## Date('10/29/2013')
## Author('Nandor Sieben')
## Institution('NAU')
 
 
 DOCUMENT();
 
    loadMacros(
      "PGstandard.pl",
      "MathObjects.pl",
      "PGnauGraphics.pl",
      "PGgraphmacros.pl",
#      "parserMultiAnswer.pl",
      "PGcourse.pl",
    );

    $showPartialCorrectAnswers = 1;

    TEXT(beginproblem);

    #####################################################

    Context('Numeric');

    $dom = 10;
{
    $w1 = list_random(1,-1);
    $w2 = list_random(1,-1);
    $q1 = list_random(1,-1);
    $q2 = -$q1;
    $a1 = $w1*non_zero_random(2,8,.2);
    $a2 = $w2*non_zero_random(2,8,.2);
    $b1 = $q1*$w1*non_zero_random(2,8,.2);
    $b2 = $q2*$w2*non_zero_random(2,8,.2);
    redo if (abs($a1 * $b2 - $a2 * $b1) < 1);
    {
      $c1 = non_zero_random(-3,3,.2);
      $c2 = non_zero_random(-3,3,.2);
      redo if (abs($c1)<1 || abs($c2)<1);
    }
    $x1 = $a1*$c1+$b1*$c2;
    $x2 = $a2*$c1+$b2*$c2;
    redo if (abs($x1)<2 || abs($x2)<2);
    redo if ($x1 < -$dom+2 || $x1>$dom-2 || $x2 < -$dom+2 || $x2 >$dom-2);
}
    $ans1 = Formula("$c1");
    $ans2 = Formula("$c2");

    Context('Matrix');
    $multians = Matrix([[$c1],[$c2]]);
    Context('Numeric');
 
 
    @opts = (-$dom, -$dom, $dom, $dom, axes=>[0,0], grid=>[$dom, $dom], size=>[250,250]);
    $grapho = init_graph(@opts);
 
    $grapho->new_color('red',200,0,0);
    $grapho->new_color('black',0,0,0);
    $grapho->new_color('blue1',10,0,110);
    $grapho->new_color('blue2',0,0,250);
    $grapho->new_color('green',0,200,0);
 
    $grapho->moveTo(0,0);
    $grapho->arrowTo($a1,$a2,'blue1');
 
    $grapho->moveTo(0,0);
    $grapho->arrowTo($b1,$b2,'blue2');
 
    $grapho->moveTo(0,0);
    $grapho->arrowTo($x1,$x2,'green',2);

    $labela = new Label(1.1*$a1,1.1*$a2, 'a', 'blue1' , 'center', 'center');
    $grapho->lb($labela);
    $labelb = new Label(1.1*$b1,1.1*$b2, 'b', 'blue2' , 'center', 'center');
    $grapho->lb($labelb);
    $labelx = new Label(1.1*$x1,1.1*$x2, 'x', 'green' , 'center', 'center');
    $grapho->lb($labelx);
 
 
    #####################################################
 
    BEGIN_TEXT
    \{ Plot($grapho) \}
    $PAR
    Find the coordinates of \(x\) in the ordered basis \( B=( a, b ) \).
    $PAR
    \([x]_B=\) \{ $multians->ans_array(5) \}
    $PAR
    Hint: The preview shows the picture of \( x \) with the coordinates you provide.
    END_TEXT

    #####################################################
 
    ANS($multians->cmp->withPostFilter(sub {
      my $ans = shift;
      my ($correct,$student) = ($ans->{correct_value},$ans->{student_value});
      if (($student && $correct->typeMatch($student))) {
        my $s = Matrix($student);
 
        my $graph = init_graph(@opts);
        $graph->new_color('red',200,0,0);
        $graph->new_color('green',0,102,0);
 
        $graph->moveTo(0,0);
        $graph->arrowTo($x1,$x2,'green',3);
 
        $graph->moveTo(0,0);
 
        my $xx1 = $a1*$s->element(1,1)+$b1*$s->element(2,1);
        my $xx2 = $a2*$s->element(1,1)+$b2*$s->element(2,1);
 
        $graph->arrowTo($xx1,$xx2,'red');
 
        $graph->gifName($graph->gifName()."-$student");
        $ans->{student_ans} = Plot($graph);
      }
      return $ans;
    }));

    #####################################################

COMMENT('Plots the student generated x in the answer preview');

    ENDDOCUMENT();

In reply to Nandor Sieben

Re: postfilters in multians

by Nandor Sieben -
Any other tricky thing would be nice like:

$ans = Matrix([[2],[3]]);

$ans->element(1,1)->ans_rule
$ans->element(2,1)->ans_rule

or
$ans->ans_array(1,1)
$ans->ans_array_extension(1,1)

or

some sort of ans_rule_extension