WeBWorK Problems

mixing matrices and complex numbers in multianswer

mixing matrices and complex numbers in multianswer

by Richard Lynch -
Number of replies: 2
Hi Everyone, I am trying to write a problem that takes a square matrix in the first answer blank and a complex number in the second, and then I would like to check that the eigenvalue given is in fact one of the given matrix. My attempt at such a problem is:

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

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


###########################################################################
# setup contexts and variables 
###########################################################################
Context("Complex");
$a = Complex("i");

Context("Matrix")->strings->add("true"=>{}, "false"=>{});
$A = Matrix([0,1],[-1,0]);
Context()->{cmpDefaults}{Matrix}{showDimensionHints} = 0;
$answer = MultiAnswer($A,$a)->with(
  singleResult => 0,
  checker => sub {
    my ($c,$s,$self) = @_;
    my ($s1,$s2) = @{$s};
    return [0,0] if !($s1->isSquare);
    @d = $s1->dimensions;
    $test1 = 0;
    my $T = $s1 - $s2*Value::Matrix->I($d[0]);
    $test1 = 1 if $T->det == 0;
    $test2 = 0;
    $test2 = 1 if $a->Im != 0;
    return [1,1] if $test1 == 1 && $test2 == 1;
    return [0,0];
  }
);


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


###########################################################################
# state the problem 
###########################################################################
Context()->texStrings;
BEGIN_TEXT
Determine if the following statement is true or false:
$PAR
$SPACE $SPACE A square matrix \(A\) having real entries also has real eigenvalues.
$PAR
If the answer is true, then type ${BBOLD}true${EBOLD} in one of the blanks. If the answer is false, then provide a square matrix \(A\) with real entries and one of its complex eigenvalues. $PAR
\(A = \) \{$answer->ans_rule(35)\} $BR
Exactly one complex eigenvalue of \(A\): \{$answer->ans_rule(20)\}
$PAR
${BBOLD}Help:${EBOLD} To enter a matrix use $BBOLD [[ ],[ ]] $EBOLD. For example, to enter the \(2\times 3\) matrix
$PAR\[
\left[\begin{array}{ccc}
1 & 2 & 3 \\ 6 & 5 & 4
\end{array}\right]
\]$PAR you would type [[1,2,3],[6,5,4]], so each inside set of [ ] represents a row.
END_TEXT
Context()->normalStrings;

The issue I am having is that it gives the following errors:
  • Operator or semicolon missing before *Value::Matrix at line 51 of (eval 2279)
  • Ambiguous use of * resolved as operator * at line 51 of (eval 2279)
However, it seems to actually work and take answers:
wdx5obC.png

I think the error basically means that there is no operator * defined for a complex object times a matrix? Any ideas how how I can fix this? Thank you so much in advance!
In reply to Richard Lynch

Re: mixing matrices and complex numbers in multianswer

by Davide Cervone -
No, multiplication between a complex and a matrix is defined. The error message is complaining about the fact that *Value::Matrix has another potential meaning, and so Perl is not sure how to interpret the expression (and is making the assumption you mean multiplication).

The easiest solution is to put spaces around the * as in

   my $T = $s1 - $s2 * Value::Matrix->I($d[0]);
This will disambiguate the expression, making Perl happy.

Davide

In reply to Davide Cervone

Re: mixing matrices and complex numbers in multianswer

by Richard Lynch -
Hi Davide, this worked, so thanks! I should have thought to put parenthesis around it or something... As always, I appreciate your help!!