--- loncom/homework/lonmaxima.pm 2006/12/11 17:37:43 1.6 +++ loncom/homework/lonmaxima.pm 2008/06/14 16:34:54 1.23 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Interface routines to MAXIMA CAS # -# $Id: lonmaxima.pm,v 1.6 2006/12/11 17:37:43 www Exp $ +# $Id: lonmaxima.pm,v 1.23 2008/06/14 16:34:54 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,6 +31,7 @@ package Apache::lonmaxima; use strict; use IO::Socket; use Apache::lonnet; +use Apache::response(); use LONCAPA; sub connect { @@ -59,36 +60,98 @@ sub maximareply { sub blacklisted { my ($cmd)=@_; - foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') { + foreach my $forbidden ('save','load','plot','lisp','includ','compil', + 'file','batch','stringout','translat','stout', + 'stin','block','system','concat','read','inchar', + 'outchar','ttyoff','with_stdout','writefile', + 'reset') { if ($cmd=~/$forbidden/s) { return 1; } } return 0; } +sub maxima_allowed_libraries { + return ( + "absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag", + "eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack", + "lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly", + "quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units", + "vect","zeilberger"); +} + +sub maxima_is_allowed_library { + my ($library)=@_; + foreach my $allowed_library (&maxima_allowed_libraries()) { + if ($library eq $allowed_library) { return 1; } + } + return 0; +} + sub runscript { - my ($socket,$fullscript)=@_; + my ($socket,$fullscript,$libraries)=@_; if (&blacklisted($fullscript)) { return 'Error: blacklisted'; } - return &maximareply($socket,$fullscript); + my $reply; + $fullscript=~s/[\n\r\l]//gs; + if ($libraries) { + foreach my $library (split(/\s*\,\s*/,$libraries)) { + unless ($library=~/\w/) { next; } + if (&maxima_is_allowed_library($library)) { + $reply=&maximareply($socket,'load('.$library.')$'."\n"); + if ($reply=~/^Error\:/) { return $reply; } + } else { + return 'Error: blacklisted'; + } + } + } + foreach my $line (split(/\;/s,$fullscript)) { + if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); } + if ($reply=~/^Error\:/) { return $reply; } + } + $reply=~s/^\s*//gs; + $reply=~s/\s*$//gs; + &Apache::lonxml::debug("maxima $fullscript \n reply $reply"); + return $reply; +} + +sub maxima_cas_formula_fix { + my ($expression)=@_; + return &Apache::response::implicit_multiplication($expression); } sub maxima_run { - my ($script,$submission,$arguments) = @_; + my ($script,$submission,$argument,$libraries) = @_; my $socket=&connect(); - my $fullscript=$script; - my $reply=&runscript($socket,$fullscript); -# &Apache::lonnet::logthis('Script:'.$fullscript); -# &Apache::lonnet::logthis('Sub:'.$submission); -# &Apache::lonnet::logthis('Arg:'.$arguments); - + my @submissionarray=split(/\s*\,\s*/,$submission); + for (my $i=0;$i<=$#submissionarray;$i++) { + my $n=$i+1; + my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]); + $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs; + } + my @argumentarray=@{$argument}; + for (my $i=0;$i<=$#argumentarray;$i++) { + my $n=$i+1; + my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]); + $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs; + } + my $reply=&runscript($socket,$script,$libraries); &disconnect($socket); - if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; } - if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } + if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; } + if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } return 'BAD_FORMULA'; } +sub maxima_eval { + my ($script,$libraries) = @_; + my $socket=&connect(); + my $reply=&runscript($socket,$script,$libraries); + &disconnect($socket); + return $reply; +} + + sub compareterms { my ($socket,$terma,$termb)=@_; - my $difference=$terma.'-'.$termb; + my $difference=$terma.'-('.$termb.')'; if (&blacklisted($difference)) { return 'Error: blacklisted'; } my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));'); if ($reply=~/^\s*0\s*$/) { return 'true'; } @@ -101,7 +164,12 @@ sub maxima_check { my $socket=&connect(); my $reply=&compareterms($socket,$response,$answer); &disconnect($socket); + # integer to string mappings come from capaParser.h + # 1 maps to 'EXACT_ANS' if ($reply eq 'true') { return 1; } + # 11 maps to 'BAD_FORMULA' + if ($reply=~/^Error\:/) { return 11; } + # 7 maps to 'INCORRECT' return 7; }