Thank you all for your help with this problem. This does indeed seem to work as expected:
<problem> <startouttext /> <p>Give an example of an idempotent matrix that is neither a zero matrix nor an identity matrix.</p> <endouttext /> <customresponse> <answer type="loncapa/perl"> # Get rid of white space $submission=~s/\s+//gs; # It's not a matrix if it does not at have parentheses unless ($submission=~/^\(.+\)$/) { return 'BAD_FORMULA'; } # Now evaluate something in MAXIMA $idempotent=&cas('maxima','rank(matrix'.$submission.'^^2-matrix'.$submission.')'); $determinant=&cas('maxima','determinant(matrix'.$submission.')'); $rank=&cas('maxima','rank(matrix'.$submission.')'); # If $idempotent is not an integer, then the matrix is in the wrong format unless ($idempotent=~/^\s*\d+\s*$/) { return 'BAD_FORMULA'; } # Test the outcome of the MAXIMA expression if ($idempotent == 0 && $determinant == 0 && $rank != 0) { return 'EXACT_ANS'; } else {return 'INCORRECT';} </answer> <textline readonly="no" spellcheck="none" size="80" /> </customresponse> </problem> Justin Justin Gray | Senior Lecturer Department of Mathematics | Simon Fraser University 8888 University Drive, Burnaby | V5A 1S6 | Canada Tel: +1 778.782.4237 On Wed, May 27, 2015 at 4:41 AM, Gerd Kortemeyer <korte...@msu.edu> wrote: > Hi, > > > On May 26, 2015, at 10:36 PM, Damien Guillaume <dami...@msu.edu> wrote: > > > >> This seems to work as expected for square matrices. However, the example > >> below accepts ([2,-2,-4],[-1,3,4]) as an answer, even though > >> &cas('maxima','rank(matrix'.$submission.'^^2-matrix'.$submission.')') > >> should generate an error in MAXIMA: > >> > >> (%i1) A:matrix([2,-2,-4],[-1,3,4]); [ 2 > >> - 2 - 4 ] > >> (%o1) [ ] > >> [ - 1 3 4 ] > >> (%i2) rank(A^^2 - A); > >> MULTIPLYMATRICES: attempt to multiply nonconformable matrices. > >> -- an error. To debug this try: debugmode(true); > > > > It looks like maximareply() in lonmaxima.pm 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. > > Argh! Yes, it’s really a problem that MAXIMA was designed throughout for > interactive sessions. The replies are non-standardized. > > 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 > > if ($determinant=~/(error|attempt)/i) { return ‘BAD_FORMULA’; } > > or to see if we only get back a number > > unless ($determinant=~/^\s*\d+\s*$) { return ‘BAD_FORMULA’; } > > - probably cleaner to do the latter, if we know that all ranks should just > be numbers. > > - Gerd. > > _______________________________________________ > LON-CAPA-users mailing list > LON-CAPA-users@mail.lon-capa.org > http://mail.lon-capa.org/mailman/listinfo/lon-capa-users >
_______________________________________________ LON-CAPA-users mailing list LON-CAPA-users@mail.lon-capa.org http://mail.lon-capa.org/mailman/listinfo/lon-capa-users