--- loncom/homework/lonmaxima.pm	2006/12/11 22:02:13	1.7
+++ loncom/homework/lonmaxima.pm	2007/04/19 17:40:43	1.13
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Interface routines to MAXIMA CAS
 #
-# $Id: lonmaxima.pm,v 1.7 2006/12/11 22:02:13 www Exp $
+# $Id: lonmaxima.pm,v 1.13 2007/04/19 17:40:43 raeburn 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 {
@@ -78,28 +79,42 @@ sub runscript {
     return $reply;
 }
 
+sub maxima_cas_formula_fix {
+   my ($expression)=@_;
+   return &Apache::response::implicit_multiplication($expression);
+}
+
 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]:$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 (split(/\s*\,\s*/,$argument)) {
-	$fullscript.="LONCAPALIST[$argument_index]:$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';
 }
 
+sub maxima_eval {
+    my ($script) = @_;
+    my $socket=&connect();
+    my $reply=&runscript($socket,$script);
+    &disconnect($socket);
+    return $reply;
+}
+
+
 sub compareterms {
     my ($socket,$terma,$termb)=@_;
     my $difference=$terma.'-'.$termb;