--- loncom/homework/lonmaxima.pm	2006/12/19 14:24:05	1.11
+++ loncom/homework/lonmaxima.pm	2007/08/13 09:34:35	1.19
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Interface routines to MAXIMA CAS
 #
-# $Id: lonmaxima.pm,v 1.11 2006/12/19 14:24:05 www Exp $
+# $Id: lonmaxima.pm,v 1.19 2007/08/13 09:34:35 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -60,7 +60,9 @@ 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') {
 	if ($cmd=~/$forbidden/s) { return 1; }
     } 
     return 0;
@@ -75,7 +77,9 @@ sub runscript {
 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
 	if ($reply=~/^Error\:/) { return $reply; }
     }
-    $reply=~s/\W//gs;
+    $reply=~s/^\s*//gs;
+    $reply=~s/\s*$//gs;
+    &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
     return $reply;
 }
 
@@ -87,22 +91,22 @@ sub maxima_cas_formula_fix {
 sub maxima_run {
     my ($script,$submission,$argument) = @_;
     my $socket=&connect();
-    my $fullscript='';
-    my $submission_index=1;
-    foreach my $submission_component (split(/\s*\,\s*/,$submission)) {
-	$fullscript.="RESPONSE[$submission_index]:".&maxima_cas_formula_fix($submission_component).";\n";
-	$submission_index++;
+    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 $argument_index=1;
-    foreach my $argument_component (@{$argument}) {
-	$fullscript.="LONCAPALIST[$argument_index]:".&maxima_cas_formula_fix($argument_component).";\n";
-	$argument_index++;
+    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;
     }
-    $fullscript.=$script;
-    my $reply=&runscript($socket,$fullscript);
+    my $reply=&runscript($socket,$script);
     &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';
 }
 
@@ -117,7 +121,7 @@ sub maxima_eval {
 
 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'; }
@@ -130,7 +134,10 @@ 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; }
+    # 7 maps to 'INCORRECT'
     return 7;
 }