File:
[LON-CAPA] /
loncom /
homework /
lonmaxima.pm
Revision
1.29:
download - view:
text,
annotated -
select for diffs
Mon Jun 14 11:34:18 2010 UTC (14 years, 3 months ago) by
www
Branches:
MAIN
CVS tags:
version_2_10_1,
version_2_10_0_RC2,
version_2_10_0_RC1,
version_2_10_0,
language_hyphenation_merge,
language_hyphenation,
HEAD,
BZ4492-merge,
BZ4492-feature_horizontal_radioresponse,
BZ4492-feature_Support_horizontal_radioresponse,
BZ4492-Support_horizontal_radioresponse
Deal with pi->%pi
1: # The LearningOnline Network with CAPA
2: # Interface routines to MAXIMA CAS
3: #
4: # $Id: lonmaxima.pm,v 1.29 2010/06/14 11:34:18 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonmaxima;
30:
31: use strict;
32: use IO::Socket;
33: use Apache::lonnet;
34: use Apache::response();
35: use LONCAPA;
36:
37: sub connect {
38: return IO::Socket::UNIX->new(Peer => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock',
39: Type => SOCK_STREAM,
40: Timeout => 10);
41: }
42:
43: sub disconnect {
44: my ($socket)=@_;
45: if ($socket) { close($socket); }
46: }
47:
48: sub maximareply {
49: my ($socket,$cmd)=@_;
50: if ($socket) {
51: print $socket &escape($cmd)."\n";
52: my $reply=<$socket>;
53: chomp($reply);
54: if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
55: return &unescape($reply);
56: } else {
57: return 'Error: no connection.';
58: }
59: }
60:
61: sub blacklisted {
62: my ($cmd)=@_;
63: foreach my $forbidden (
64: '\? ','\?','%i\d+','%o','batch','block'
65: ,'compil','concat','describe','display2d','file','inchar'
66: ,'includ','lisp','load','outchar','plot','quit'
67: ,'read','reset','save','stin','stout','stringout'
68: ,'system','translat','ttyoff','with_stdout','writefile'
69: ) {
70: if ($cmd=~/$forbidden/s) { return 1; }
71: }
72: return 0;
73: }
74:
75: sub maxima_allowed_libraries {
76: return (
77: "absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag",
78: "eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack",
79: "lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly",
80: "quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units",
81: "vect","zeilberger");
82: }
83:
84: sub maxima_is_allowed_library {
85: my ($library)=@_;
86: foreach my $allowed_library (&maxima_allowed_libraries()) {
87: if ($library eq $allowed_library) { return 1; }
88: }
89: return 0;
90: }
91:
92: sub runscript {
93: my ($socket,$fullscript,$libraries)=@_;
94: if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
95: my $reply;
96: $fullscript=~s/[\n\r\l]//gs;
97: if ($libraries) {
98: foreach my $library (split(/\s*\,\s*/,$libraries)) {
99: unless ($library=~/\w/) { next; }
100: if (&maxima_is_allowed_library($library)) {
101: $reply=&maximareply($socket,'load('.$library.')$'."\n");
102: if ($reply=~/^Error\:/) { return $reply; }
103: } else {
104: return 'Error: blacklisted';
105: }
106: }
107: }
108: foreach my $line (split(/\;/s,$fullscript)) {
109: if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
110: if ($reply=~/^Error\:/) { return $reply; }
111: }
112: $reply=~s/^\s*//gs;
113: $reply=~s/\s*$//gs;
114: &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
115: return $reply;
116: }
117:
118: sub maxima_cas_formula_fix {
119: my ($expression)=@_;
120: $expression=&Apache::response::implicit_multiplication($expression);
121: $expression=~s/([^\w\%])pi/$1\%pi/gs;
122: return $expression;
123: }
124:
125: sub maxima_run {
126: my ($script,$submission,$argument,$libraries) = @_;
127: my $socket=&connect();
128: my @submissionarray=split(/\s*\,\s*/,$submission);
129: for (my $i=0;$i<=$#submissionarray;$i++) {
130: my $n=$i+1;
131: my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
132: $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
133: }
134: my @argumentarray=@{$argument};
135: for (my $i=0;$i<=$#argumentarray;$i++) {
136: my $n=$i+1;
137: my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
138: $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
139: }
140: my $reply=&runscript($socket,$script,$libraries);
141: &disconnect($socket);
142: if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
143: if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; }
144: return 'BAD_FORMULA';
145: }
146:
147: sub maxima_eval {
148: my ($script,$libraries) = @_;
149: my $socket=&connect();
150: my $reply=&runscript($socket,$script,$libraries);
151: &disconnect($socket);
152: return $reply;
153: }
154:
155:
156: sub compareterms {
157: my ($socket,$terma,$termb)=@_;
158: my $difference=$terma.'-('.$termb.')';
159: if (&blacklisted($difference)) { return 'Error: blacklisted'; }
160: my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
161: if ($reply=~/^\s*0\s*$/) { return 'true'; }
162: if ($reply=~/^Error\:/) { return $reply; }
163: return 'false';
164: }
165:
166: sub maxima_check {
167: my ($response,$answer,$reterror) = @_;
168: my $socket=&connect();
169: my $reply=&compareterms($socket,$response,$answer);
170: &disconnect($socket);
171: # integer to string mappings come from capaParser.h
172: # 1 maps to 'EXACT_ANS'
173: if ($reply eq 'true') { return 1; }
174: # 11 maps to 'BAD_FORMULA'
175: if ($reply=~/^Error\:/) { return 11; }
176: # 7 maps to 'INCORRECT'
177: return 7;
178: }
179:
180: 1;
181: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>