<div dir="ltr">Thank you all for your help with this problem. This does indeed seem to work as expected:<div><br></div><div><div><problem></div><div><br></div><div><startouttext /></div><div><p>Give an example of an idempotent matrix that is neither a zero matrix nor an identity matrix.</p></div><div><endouttext /></div><div><br></div><div><customresponse></div><div><br></div><div><answer type="loncapa/perl"></div><div># Get rid of white space</div><div>$submission=~s/\s+//gs;</div><div># It's not a matrix if it does not at have parentheses</div><div>unless ($submission=~/^\(.+\)$/) { return 'BAD_FORMULA'; }</div><div><br></div><div># Now evaluate something in MAXIMA</div><div>$idempotent=&cas('maxima','rank(matrix'.$submission.'^^2-matrix'.$submission.')');</div><div>$determinant=&cas('maxima','determinant(matrix'.$submission.')');</div><div>$rank=&cas('maxima','rank(matrix'.$submission.')');</div><div># If $idempotent is not an integer, then the matrix is in the wrong format</div><div>unless ($idempotent=~/^\s*\d+\s*$/) { return 'BAD_FORMULA'; }</div><div># Test the outcome of the MAXIMA expression</div><div>if ($idempotent == 0 && $determinant == 0 && $rank != 0) { return 'EXACT_ANS'; }</div><div>else {return 'INCORRECT';}</div><div></answer></div><div><br></div><div>    <textline readonly="no" spellcheck="none" size="80" /></div><div></customresponse></div><div><br></div><div></problem></div></div><div><br></div><div>Justin</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><br></div><div style="color:rgb(102,102,102)">Justin Gray | Senior Lecturer</div><span style="color:rgb(102,102,102)">Department of Mathematics | Simon Fraser University</span><br style="color:rgb(102,102,102)"><span style="color:rgb(102,102,102)">8888 University Drive, Burnaby | V5A 1S6 | Canada</span><br style="color:rgb(102,102,102)"><span style="color:rgb(102,102,102)">Tel: +1 778.782.4237</span><br></div><div dir="ltr"><span style="color:rgb(102,102,102)"><br></span></div><div dir="ltr"><img src="cid:0786efd7371599bc51452b5fedd54d152d0fae84@zimbra" style="color:rgb(0,0,0);font-family:verdana,helvetica,sans-serif;font-size:16px"><span style="color:rgb(102,102,102)"><br></span></div></div></div></div></div>
<br><div class="gmail_quote">On Wed, May 27, 2015 at 4:41 AM, Gerd Kortemeyer <span dir="ltr"><<a href="mailto:kortemey@msu.edu" target="_blank">kortemey@msu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<span class=""><br>
> On May 26, 2015, at 10:36 PM, Damien Guillaume <<a href="mailto:damieng@msu.edu">damieng@msu.edu</a>> wrote:<br>
><br>
>> This seems to work as expected for square matrices. However, the example<br>
>> below accepts ([2,-2,-4],[-1,3,4]) as an answer, even though<br>
>> &cas('maxima','rank(matrix'.$submission.'^^2-matrix'.$submission.')')<br>
>> should generate an error in MAXIMA:<br>
>><br>
>> (%i1) A:matrix([2,-2,-4],[-1,3,4]);                               [  2<br>
>>   - 2  - 4 ]<br>
>> (%o1)                          [               ]<br>
>>                                [ - 1   3    4  ]<br>
>> (%i2) rank(A^^2 - A);<br>
>> MULTIPLYMATRICES: attempt to multiply nonconformable matrices.<br>
>>  -- an error. To debug this try: debugmode(true);<br>
><br>
> It looks like maximareply() in <a href="http://lonmaxima.pm" target="_blank">lonmaxima.pm</a> does not recognize this error message as an error message (it is only checking if there is "incorrect" in the reply). I guess the best solution would be to fix that, but maybe there is a way to tell if the result of the cas call makes sense or not (like testing if it is an integer), as a workaround.<br>
<br>
</span>Argh! Yes, it’s really a problem that MAXIMA was designed throughout for interactive sessions. The replies are non-standardized.<br>
<br>
I am not sure if &maximareply() gives us back the whole message, since it is two lines (another problem), so I think we only get back “MULTIPLYMATRICES: attempt …” It is impossible to anticipate all the possible messages that MAXIMA returns, since there is no standard - each package spits out whatever the author imagined. In this case, probably the only remedies are to interpret the presence of “attempt” also as an error<br>
<br>
if ($determinant=~/(error|attempt)/i)  { return ‘BAD_FORMULA’; }<br>
<br>
or to see if we only get back a number<br>
<br>
unless ($determinant=~/^\s*\d+\s*$)  { return ‘BAD_FORMULA’; }<br>
<br>
- probably cleaner to do the latter, if we know that all ranks should just be numbers.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Gerd.<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
LON-CAPA-users mailing list<br>
<a href="mailto:LON-CAPA-users@mail.lon-capa.org">LON-CAPA-users@mail.lon-capa.org</a><br>
<a href="http://mail.lon-capa.org/mailman/listinfo/lon-capa-users" target="_blank">http://mail.lon-capa.org/mailman/listinfo/lon-capa-users</a><br>
</div></div></blockquote></div><br></div>