WeBWorK Main Forum

Combine MathObjects in Answers

Combine MathObjects in Answers

by Yuncong Chen -
Number of replies: 3
Hi, I have a question with using PGML. I have the following script:

loadMacros(
...
"contextIntegerFunctions.pl"
)
...
$a = Compute("C(4,2)");
$b = Compute("C(3,2)");
BEGIN_PGML
The answer is [_____]{$a*$b}
END_PGML

I expect the answer string to be C(4,2)*C(3,2), but it is actually just C(4,2). What would be the correct way to achieve the expected effect? Thanks.
In reply to Yuncong Chen

Re: Combine MathObjects in Answers

by Hedley Pinsent -
Just a little workaround - hp

# DESCRIPTION

DOCUMENT();
loadMacros(
"contextIntegerFunctions.pl",
"MathObjects.pl",
"PGstandard.pl",
"PGML.pl",
);

$showPartialCorrectAnswers = 1;

$d = Compute("C(4,2)*C(3,2)");

$a = Compute("C(4,2)");
$b = Compute("C(3,2)");

BEGIN_PGML
The answer is [_____]{$a*$b}

The answer is [_____]{$d}
END_PGML

ENDDOCUMENT();

Attachment Untitled.png
In reply to Hedley Pinsent

Re: Combine MathObjects in Answers

by Yuncong Chen -
I understand this workaround would work but I would like to reuse previously defined MathObjects. If those are long strings involving many C functions, retyping them at each variable that uses them is a pain. Also it seems to defy the object-oriented design of having MathObjects at the first place.
In reply to Yuncong Chen

Re: Combine MathObjects in Answers

by Davide Cervone -
The problem is that $a is actually just the real number 6 (equivalent to Real(6)), not a Formula object, but with its correct_ans field set to the string C(4,2) (and its correct_ans_latex_string set to the appropriate thing). When you do $a*$b, you end up with the equivalent of Real(18), again just a Real MathObject. The Real inherits properties from the first factor ($a), but some properties where supposed to be removed, including the correct_ans, since it no longer applies. The correct_ans did get removed, but the correct_ans_latex_string did not (this value was added rather recently, so hasn't had a lot of testing), which is why it shows up a C(4,2) in the correct answer rather than 18 as it should have. I will need to fix that bug.

One way to handle this is the following:

    Context()->flags->set(reduceConstantFunctions=>0,reduceConstants=>0);
    
    $a = Formula("C(4,2)");
    $b = Formula("C(3,2)");

    BEGIN_PGML
    The answer is [______________]{$a*$b} 
    END_PGML
This works, but the type of answer will be a Formula, so you will get error messages that say you are looking for a Formula, and it will not give error messages if you include x in your answer.

If you really want to get it right, you need to do something like

    Context()->flags->set(reduceConstantFunctions=>0,reduceConstants=>0);
    
    $a = Formula("C(4,2)");
    $b = Formula("C(3,2)");
    
    $d = $a*$b;
    $d = $d->eval()->with(
      correct_ans => $d->string,
      correct_ans_latex_string=> $d->TeX
    );
    
    BEGIN_PGML
    The answer is [______________]{$d} 
    END_PGML
This will produce the proper error messages (since $d is a Real), while showing the proper correct answer (since the correct_ans is set explicitly). A bit unpleasant, but it works.