WeBWorK Main Forum

Answer array in PGML with custom answer checker

Answer array in PGML with custom answer checker

by Sean Fitzpatrick -
Number of replies: 4

I was interested in including the problem Library/NAU/setLinearAlgebra/span.pg in a PreTeXt book, but it doesn't give valid PreTeXt XML.

So I decided I would try to re-write it using PGML. This was mostly successful, in that the problem still seems to work, and I now get valild PTX.

But the question uses a custom answer checker for the matrices to be entered, and the recommended syntax (put a * between the answer blank and the answer) does not produce an answer array for me.

My attempt at modifying this question is below.

(Related question: this problem uses a rank.pl macro that is not part of PG. When I make a local copy, is there a way to make sure this macro gets included? The only way I was able to figure out was to SSH into the server, browse through the OPL to find the file, save it to my computer, and then upload to the macros folder in my course. But if I was the server admin I wouldn't have been able to do this.)

New source:

## DBsubject(Linear algebra)

## DBchapter(Abstract vector spaces)

## DBsection(Span)

## Date(10/5/2013)

## Institution(NAU)

## Author(Nandor Sieben)

## Level(2)

## MO(1)

## KEYWORDS('linear algebra','span')


DOCUMENT();

loadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "parserMultiAnswer.pl",

  "AnswerFormatHelp.pl",

  "MatrixReduce.pl",

  "rank.pl",

  "PGcourse.pl",

  "PGML.pl"

);

$showPartialCorrectAnswers = 1;

TEXT(beginproblem()); 


Context("Fraction");

Context() -> parens -> set ("[" => {formMatrix => 1});

{

$a11= random(-5,5,1);

$a12= random(-5,5,1);

$a22= random(-5,5,1);

$b11= random(-5,5,1);

$b12= random(-5,5,1);

$b22= random(-5,5,1);

Context() -> parens -> set ("[" => {formMatrix => 1});

$A = Matrix([[$a11,$a12],[$a12,$a22]]);

$B = Matrix([[$b11,$b12],[$b12,$b22]]);

redo if(rank(Matrix([[$a11,$a12,$a22],[$b11,$b12,$b22],[0,0,0]]))<2);

}

$answer = $A+$B;

$answer = $A-$B if (rank($answer) == 0);


{

$c1 = Compute(random(-2,2,1));

$c2 = Compute(random(-2,2,1));

$c3 = Compute(1);

Context() -> parens -> set ("[" => {formMatrix => 1});

$answer2=Matrix([[$c1,$c3],[$c3,$c2]]);

redo if (rank(Matrix([$a11,$a12,$a22],[$b11,$b12,$b22],

      [$answer2->element(1,1),$answer2->element(1,2),$answer2->element(2,2)])) == 2);

}


$A1 = $answer->cmp(

  checker => sub {

  my ($correct, $student, $ansHash) = @_;

  if ( ! $student->is_symmetric )

    { return 0; } 

  if ( rank(Matrix($student)) == 0 )

    { return 0; } 

  if (rank(Matrix([$a11,$a12,$a22],[$b11,$b12,$b22],

      [$student->element(1,1),$student->element(1,2),$student->element(2,2)])) == 2) {

    return 1;

  }

  return 0;

  }

);


$A2 = $answer2->cmp(

  checker => sub {

  my ($correct, $student, $ansHash) = @_;

  if ( ! $student->is_symmetric )

    { return 0; } 

  if (rank(Matrix([$a11,$a12,$a22],[$b11,$b12,$b22],

      [$student->element(1,1),$student->element(1,2),$student->element(2,2)]))==3) {

    return 1;

  }

  return 0;

  }

);


Context()->texStrings;

BEGIN_PGML

Let [`V`] be the vector space of symmetric [`2\times 2`] matrices and

[`W`] be the subspace 

[```W=\operatorname{span} \left\{ [$A],[$B] \right\} .```]


a. Find a nonzero element [`X`] in [` W `].


[` X =`] [__________]*{$A1}



b. Find an element [`Y`] in [` V `] that is not in  [` W `].


[` Y = `] [__________]*{$A2}


END_PGML





ENDDOCUMENT();

In reply to Sean Fitzpatrick

Re: Answer array in PGML with custom answer checker

by Glenn Rice -
To make this work with PGML you will need to do this a little differently. You won't be able to use the PGML "[_]*{$A1}" method. Instead use
BEGIN_PGML
...
[`X =`] [@ $answer->ans_array @]*

[`X =`] [@ $answer->ans_array @]*
END_PGML
ANS($A1);
ANS($A2);

For the macro, I notice that it only has one method in it that is 11 lines long. I recommend that you remove the inclusion of the macro from the problem, and instead just copy the method directly into the problem.  You should probably delete the Context('Fraction') call in it though since that is called in your problem.  You should also remove the loading of parserMultiAnswer.pl and AnswerFormatHelp.pl from the problem as those are not used.  Furthermore, AnswerFormatHelp.pl is deprecated (as of the next release of PG).  Just use helpLink from PGbasicmacros.pl that works the same (except opens help in a knowl instead of a new window).

I have attached a version of this problem with these modifications.  You will have to test it for PreTeXt output though.

In reply to Glenn Rice

Re: Answer array in PGML with custom answer checker

by Sean Fitzpatrick -
This is great, thank you! I'll test it tomorrow (today is a holiday and I have kids to keep entertained).

Despite being our WeBWorK admin for almost a decade, it's been awhile since I've had to get my hands dirty work problem authoring :-)
I couldn't remember if having the 'ANS' lines at the end of the file also worked in PGML.

All that I did was to modify the original filler from the OPL to use PGML, and I moved the custom answer checkers into the preamble (originally they came after the problem text; not sure if this matters)
In reply to Sean Fitzpatrick

Re: Answer array in PGML with custom answer checker

by Sean Fitzpatrick -

Your version doesn't work with PreTeXt. I'll do some playing around to see if I can fix that.

If not, I think my version (after incorporating some of your suggestions) will do: I'm putting this problem in a book where students will be regularly using the [[a,b],[c,d]] syntax, because it's the same syntax used to enter a matrix in Python.

In reply to Sean Fitzpatrick

Re: Answer array in PGML with custom answer checker

by Sean Fitzpatrick -

I just realized that I figured this out and never posted an update. I was able to get the attached version to work.