WeBWorK Main Forum

absolute value in matrix answer entry

absolute value in matrix answer entry

by Andras Balogh -
Number of replies: 3

The following code accepts |h+2| for the scalar as correct answer, but for the matrix entry it accepts only abs(h+2). If I use |h+2| in the matrix I get error message
"The domain of your function doesn't match that of the correct answer"
And of course then even abs() does not work if I use MathQuill for live formula rendering.

This happens with ww_version: 2.17, but I think similar things worked a year ago with ww_version: 2.16.


DOCUMENT();
loadMacros(
   "PGstandard.pl",   
   "MathObjects.pl",
   "PGML.pl",   
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

Context('Matrix')->variables->are(h=>'Real');
$mf = Formula("abs(h+2)");
$myMTX=Matrix([[1,$mf], [2, 3] ]);

BEGIN_PGML
    [``[$mf]=``][__]{$mf}{20}
    
    [``[$myMTX]=``][__]*{$myMTX}{20}
END_PGML
Context()->normalStrings;
ENDDOCUMENT();

In reply to Andras Balogh

Re: absolute value in matrix answer entry

by Andras Balogh -

The absolute value obviously came up with a variable, but the problem is the matrix and not the variable. 

If I enter |3| instead of 3, the answer is also marked incorrect. In that case the error message is:
Matrix row entries must be numbers: [3]

In reply to Andras Balogh

Re: absolute value in matrix answer entry

by Davide Cervone -

Sorry for not looking into this earlier. I no longer get notifications of new message, and never saw it.

It turns out that this is due to the fact that absolute values are treated internally by the MathObject parser as a special kind of list (lists have opening and closing delimiters, like absolute values). But some of the code that builds matrices from their entries turn lists into sub-matrices, and that was being done to the absolute values, so the matrix was ending up looking like it was $$ \begin{bmatrix} 1 & [h+2] \\ 2 & 3 \end{bmatrix} $$ instead. That lead to the error about row entries must be numbers, since one of the entries was a (1 x 1) matrix.

The fix is the following:

diff --git a/lib/Parser/List/AbsoluteValue.pm b/lib/Parser/List/AbsoluteValue.pm
index 6e2d552b..ab922627 100644
--- a/lib/Parser/List/AbsoluteValue.pm
+++ b/lib/Parser/List/AbsoluteValue.pm
@@ -23,6 +23,8 @@ sub _check {
 
 sub class {'AbsoluteValue'}; # don't report List
 
+sub makeMatrix {} # override Parser::List method
+
 #
 #  Compute using abs()
 #
diff --git a/lib/Parser/List/Matrix.pm b/lib/Parser/List/Matrix.pm
index c14f6fa9..6374458d 100644
--- a/lib/Parser/List/Matrix.pm
+++ b/lib/Parser/List/Matrix.pm
@@ -15,7 +15,7 @@ sub _check {
   my $self = shift;
   my $matrix = $self->{equation}{context}{lists}{Matrix};
   $self->{open} = $matrix->{open}; $self->{close} = $matrix->{close};
-  if ($self->{entryType}{name} ne 'Matrix') {
+  if ($self->entryType->{name} ne 'Matrix') {
     foreach my $x (@{$self->{coords}})
       {$x->makeMatrix($self->{type}{name},$self->{open},$self->{close})}
   }
In reply to Davide Cervone

Re: absolute value in matrix answer entry

by Andras Balogh -

Thank you. For now I converted the problem into asking for the matrix entries one-by-one as scalars, but I will definitely try these changes at some point.