Ticket #7377: trac_7377-fastcalculus-p1.patch
File trac_7377-fastcalculus-p1.patch, 22.6 KB (added by , 11 years ago) |
---|
-
sage/calculus/calculus.py
# HG changeset patch # User Nils Bruin <nbruin@sfu.ca> # Date 1265693495 28800 # Node ID 2aa53d20ef6082fac0ec6218b29a469ea7cf13ae # Parent 6d5b89f947d1a7ce14bc4ff23ad63fbd6f7dbd5a Let calculus use the maxima_lib instance and implement some preliminary routines for communicating directly (not via strings) with maxima_lib diff -r 6d5b89f947d1 -r 2aa53d20ef60 sage/calculus/calculus.py
a b 386 386 f1 387 387 """ 388 388 maxima = sage.interfaces.maxima_lib.maxima 389 #maxima = Maxima(init_code = ['display2d:false', 'domain: complex', 390 # 'keepfloat: true', 'load(to_poly_solver)', 'load(simplify_sum)'], 391 # script_subdirectory=None) 389 392 390 393 ######################################################## 391 394 def symbolic_sum(expression, v, a, b, algorithm='maxima'): … … 522 525 raise ValueError, "summation limits must not depend on the summation variable" 523 526 524 527 if algorithm == 'maxima': 525 sum = "'sum(%s, %s, %s, %s)" % tuple([repr(expr._maxima_()) for expr in (expression, v, a, b)])526 528 try: 527 result = maxima.simplify_sum(sum) 528 result = result.ratsimp() 529 return expression.parent()(result) 529 return maxima.sr_sum(expression,v,a,b) 530 530 except TypeError, error: 531 531 s = str(error) 532 532 if "divergent" in s or 'Pole encountered' in s: … … 1094 1094 1095 1095 if algorithm == 'maxima': 1096 1096 if dir is None: 1097 l = ex._maxima_().limit(v, a)1097 l = maxima.sr_limit(ex, v, a) 1098 1098 elif dir in ['plus', '+', 'right', 'above']: 1099 1099 if dir == 'above': 1100 1100 from sage.misc.misc import deprecation 1101 1101 deprecation("the keyword 'above' is deprecated. Please use 'right' or '+' instead.", 'Sage version 4.6') 1102 l = ex._maxima_().limit(v, a, 'plus')1102 l = maxima.sr_limit(ex, v, a, 'plus') 1103 1103 elif dir in ['minus', '-', 'left', 'below']: 1104 1104 if dir == 'below': 1105 1105 from sage.misc.misc import deprecation 1106 1106 deprecation("the keyword 'below' is deprecated. Please use 'left' or '-' instead.", 'Sage version 4.6') 1107 l = ex._maxima_().limit(v, a, 'minus')1107 l = maxima.sr_limit(ex, v, a, 'minus') 1108 1108 elif algorithm == 'maxima_taylor': 1109 1109 if dir is None: 1110 l = ex._maxima_().tlimit(v, a)1110 l = maxima.sr_tlimit(ex, v, a) 1111 1111 elif dir == 'plus' or dir == 'above' or dir == 'from_right': 1112 l = ex._maxima_().tlimit(v, a, 'plus')1112 l = maxima.sr_tlimit(ex, v, a, 'plus') 1113 1113 elif dir == 'minus' or dir == 'below' or dir == 'from_left': 1114 l = ex._maxima_().tlimit(v, a, 'minus')1114 l = maxima.sr_tlimit(ex, v, a, 'minus') 1115 1115 elif algorithm == 'sympy': 1116 1116 if dir is None: 1117 1117 import sympy … … 1119 1119 else: 1120 1120 raise NotImplementedError, "sympy does not support one-sided limits" 1121 1121 1122 return l.sage()1122 #return l.sage() 1123 1123 return ex.parent()(l) 1124 1124 1125 1125 # lim is alias for limit -
sage/interfaces/maxima.py
diff -r 6d5b89f947d1 -r 2aa53d20ef60 sage/interfaces/maxima.py
a b 1008 1008 """ 1009 1009 return maxima_version() 1010 1010 1011 ##some helper functions to wrap tha calculus use of the maxima interface. 1012 ##these routines expect arguments living in the symbolic ring and return something 1013 ##that is hopefully coercible into the symbolic ring again. 1011 1014 1015 def sr_integral(self,*args): 1016 return args[0]._maxima_().integrate(*args[1:]) 1017 1018 def sr_sum(self,expression,v,a,b): 1019 sum = "'sum(%s, %s, %s, %s)" % tuple([repr(expr._maxima_()) for expr in (expression, v, a, b)]) 1020 result = self.simplify_sum(sum) 1021 result = result.ratsimp() 1022 return expression.parent()(result) 1023 1024 def sr_limit(self,ex,*args): 1025 return ex._maxima_().limit(*args) 1026 1027 def sr_tlimit(self,ex,*args): 1028 return ex._maxima_().tlimit(*args) 1029 1012 1030 ## def display2d(self, flag=True): 1013 1031 ## """ 1014 1032 ## Set the flag that determines whether Maxima objects are … … 1074 1092 def __doctest_cleanup(): 1075 1093 import sage.interfaces.quit 1076 1094 sage.interfaces.quit.expect_quitall() 1077 1078 -
sage/interfaces/maxima_lib.py
diff -r 6d5b89f947d1 -r 2aa53d20ef60 sage/interfaces/maxima_lib.py
a b 423 423 sage: var('Ax,Bx,By') 424 424 (Ax, Bx, By) 425 425 sage: t = -Ax*sin(sqrt(Ax^2)/2)/(sqrt(Ax^2)*sqrt(By^2 + Bx^2)) 426 sage: t.limit(Ax=0,dir=' above')426 sage: t.limit(Ax=0,dir='+') 427 427 0 428 428 429 429 A long complicated input expression:: … … 470 470 import sage.server.support 471 471 472 472 import maxima_abstract 473 from maxima_abstract import MaximaFunctionElement, MaximaExpectFunction, Maxima Element, MaximaFunction, maxima_console473 from maxima_abstract import MaximaFunctionElement, MaximaExpectFunction, MaximaFunction, maxima_console 474 474 475 475 # The Maxima "apropos" command, e.g., apropos(det) gives a list 476 476 # of all identifiers that begin in a certain way. This could … … 485 485 ecl_eval("(in-package :maxima)") 486 486 ecl_eval("(setq $nolabels t))") 487 487 ecl_eval("(defvar *MAXIMA-LANG-SUBDIR* NIL)") 488 ecl_eval("(set-locale-subdir)") 488 489 ecl_eval("(set-pathnames)") 489 490 ecl_eval("(defun add-lineinfo (x) x)") 490 491 ecl_eval("""(defun retrieve (msg flag &aux (print? nil))(error (concatenate 'string "Maxima asks:" (meval (list '($string) msg)))))""") 491 492 ecl_eval('(defparameter *dev-null* (make-two-way-stream (make-concatenated-stream) (make-broadcast-stream)))') 492 493 ecl_eval('(defun principal nil (error "Divergent Integral"))') 493 494 495 ecl_eval("(setf $errormsg nil)") 496 494 497 maxima_eval=ecl_eval(""" 495 (defun maxima-eval( form ) 496 (let ((result (catch 'macsyma-quit (cons 'maxima_eval (meval form))))) 497 ;(princ (list "result=" result)) 498 ;(terpri) 499 ;(princ (list "$error=" $error)) 500 ;(terpri) 501 (cond 502 ((and (consp result) (eq (car result) 'maxima_eval)) (cdr result)) 503 ((eq result 'maxima-error) (error (cadr $error))) 504 (t (error (concatenate 'string "Maxima condition. result:" (princ-to-string result) "$error:" (princ-to-string $error)))) 505 ) 498 (defun maxima-eval( form ) 499 (let ((result (catch 'macsyma-quit (cons 'maxima_eval (meval form))))) 500 ;(princ (list "result=" result)) 501 ;(terpri) 502 ;(princ (list "$error=" $error)) 503 ;(terpri) 504 (cond 505 ((and (consp result) (eq (car result) 'maxima_eval)) (cdr result)) 506 ((eq result 'maxima-error) 507 (let ((the-jig (process-error-argl (cddr $error)))) 508 (mapc #'set (car the-jig) (cadr the-jig)) 509 (error (concatenate 'string "Error executing code in Maxima: " 510 (with-output-to-string (stream) 511 (apply #'mformat stream (cadr $error) (caddr the-jig))))) 512 )) 513 (t 514 (let ((the-jig (process-error-argl (cddr $error)))) 515 (mapc #'set (car the-jig) (cadr the-jig)) 516 (error (concatenate 'string "Maxima condition. result:" (princ-to-string result) "$error:" 517 (with-output-to-string (stream) 518 (apply #'mformat stream (cadr $error) (caddr the-jig))))) 519 )) 506 520 ) 507 521 ) 522 ) 508 523 """) 509 524 510 525 #ecl_eval('(defun ask-evod (x even-odd)(error "Maxima asks a question"))') … … 527 542 528 543 maxprint=EclObject("$STRING") 529 544 meval=EclObject("MEVAL") 545 msetq=EclObject("MSETQ") 546 mlist=EclObject("MLIST") 547 mequal=EclObject("MEQUAL") 548 cadadr=EclObject("CADADR") 549 550 max_integrate=EclObject("$INTEGRATE") 551 max_sum=EclObject("$SUM") 552 max_simplify_sum=EclObject("$SIMPLIFY_SUM") 553 max_ratsimp=EclObject("$RATSIMP") 554 max_limit=EclObject("$LIMIT") 555 max_tlimit=EclObject("$TLIMIT") 556 max_plus=EclObject("$PLUS") 557 max_minus=EclObject("$MINUS") 558 max_use_grobner=EclObject("$USE_GROBNER") 559 max_to_poly_solve=EclObject("$TO_POLY_SOLVE") 530 560 531 561 def max_to_string(s): 532 562 return meval(EclObject([[maxprint],s])).python()[1:-1] … … 575 605 for l in init_code: 576 606 ecl_eval("#$%s$"%l) 577 607 ecl_eval("(setf *standard-output* original-standard-output)") 608 609 def _coerce_from_special_method(self, x): 610 if isinstance(x, EclObject): 611 return MaximaElement(self,self._create(x)) 612 else: 613 return maxima_abstract.Maxima._coerce_from_special_method(self,x) 614 578 615 579 616 def _function_class(self): 580 617 """ … … 598 635 sage: m.is_running() 599 636 True 600 637 """ 601 ecl_eval(r"(defun tex-derivative (x l r) (tex (if $derivabbrev (tex-dabbrev x) (tex-d x '\partial)) l r lop rop ))") 602 638 # ecl_eval(r"(defun tex-derivative (x l r) (tex (if $derivabbrev (tex-dabbrev x) (tex-d x '\partial)) l r lop rop ))") 639 pass 640 603 641 def __reduce__(self): 604 642 """ 605 643 EXAMPLES:: … … 620 658 sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x) 621 659 Traceback (most recent call last): 622 660 ... 623 TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(a>0)' before integral or limit evaluation, for example): 624 Is a positive or negative? 661 RuntimeError: ECL says: Maxima asks:... 625 662 sage: assume(a>0) 626 663 sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x) 627 664 2/9*sqrt(3)*b^2*arctan(1/3*(2*(b*x + a)^(1/3) + a^(1/3))*sqrt(3)/a^(1/3))/a^(7/3) + 2/9*b^2*log((b*x + a)^(1/3) - a^(1/3))/a^(7/3) - 1/9*b^2*log((b*x + a)^(2/3) + (b*x + a)^(1/3)*a^(1/3) + a^(2/3))/a^(7/3) + 1/6*(4*(b*x + a)^(5/3)*b^2 - 7*(b*x + a)^(2/3)*a*b^2)/((b*x + a)^2*a^2 - 2*(b*x + a)*a^3 + a^4) … … 630 667 sage: integral(x^n,x) 631 668 Traceback (most recent call last): 632 669 ... 633 TypeError: Computation failed since Maxima requested additional constraints (try the command 'assume(n+1>0)' before integral or limit evaluation, for example): 634 Is n+1 zero or nonzero? 670 RuntimeError: ECL says: Maxima asks:... 635 671 sage: assume(n+1>0) 636 672 sage: integral(x^n,x) 637 673 x^(n + 1)/(n + 1) … … 683 719 684 720 def _eval_line(self, line, allow_use_file=False, 685 721 wait_for_prompt=True, reformat=True, error_check=True): 686 return max_to_string(maxima_eval("#$%s$"%line)) 687 722 result = '' 723 while line: 724 ind_dollar=line.find("$") 725 ind_semi=line.find(";") 726 if ind_dollar == -1 or (ind_semi >=0 and ind_dollar > ind_semi): 727 if ind_semi == -1: 728 statement = line 729 line = '' 730 else: 731 statement = line[:ind_semi] 732 line = line[ind_semi+1:] 733 if statement: result = ((result + '\n') if result else '')+ max_to_string(maxima_eval("#$%s$"%statement)) 734 else: 735 statement = line[:ind_dollar] 736 line = line[ind_dollar+1:] 737 if statement: _ = max_to_string(maxima_eval("#$%s$"%statement)) 738 return result 688 739 689 740 def _synchronize(self): 690 741 """ … … 929 980 """ 930 981 s = self._eval_line('%s;'%var) 931 982 return s 932 983 984 def _create(self, value, name=None): 985 name = self._next_var_name() if name is None else name 986 if isinstance(value,EclObject): 987 maxima_eval([[msetq],cadadr("#$%s$#$"%name),value]) 988 else: 989 self.set(name, value) 990 return name 991 933 992 def version(self): 934 993 """ 935 994 Return the version of Maxima that Sage includes. … … 937 996 EXAMPLES:: 938 997 939 998 sage: maxima.version() 940 '5. 19.1'999 '5.22.1' 941 1000 """ 942 1001 return maxima_version() 943 1002 1003 ##some helper functions to wrap tha calculus use of the maxima interface. 1004 ##these routines expect arguments living in the symbolic ring and return something 1005 ##that is hopefully coercible into the symbolic ring again. 1006 1007 def sr_integral(self,*args): 1008 try: 1009 return max_to_sr(maxima_eval(([max_integrate],[sr_to_max(SR(a)) for a in args]))) 1010 except RuntimeError, error: 1011 s = str(error) 1012 if "Divergent" in s or "divergent" in s: 1013 raise ValueError, "Integral is divergent." 1014 else: 1015 raise error 1016 1017 def sr_sum(self,*args): 1018 try: 1019 return max_to_sr(maxima_eval([[max_ratsimp],[[max_simplify_sum],([max_sum],[sr_to_max(SR(a)) for a in args])]])); 1020 except RuntimeError, error: 1021 s = str(error) 1022 if "divergent" in s: 1023 raise ValueError, "Sum is divergent." 1024 else: 1025 raise error 1026 1027 def sr_limit(self,expr,v,a,dir=None): 1028 L=[sr_to_max(SR(a)) for a in [expr,v,a]] 1029 if dir == "plus": 1030 L.append(max_plus) 1031 elif dir == "minus": 1032 L.append(max_minus) 1033 return max_to_sr(maxima_eval(([max_limit],L))) 1034 1035 def sr_tlimit(self,expr,v,a,dir=None): 1036 L=[sr_to_max(SR(a)) for a in [expr,v,a]] 1037 if dir == "plus": 1038 L.append(max_plus) 1039 elif dir == "minus": 1040 L.append(max_minus) 1041 return max_to_sr(maxima_eval(([max_tlimit],L))) 944 1042 945 1043 ## def display2d(self, flag=True): 946 1044 ## """ … … 964 1062 ## """ 965 1063 ## self._display2d = bool(flag) 966 1064 1065 class MaximaElement(maxima_abstract.MaximaElement): 1066 """ 1067 """ 1068 def ecl(self): 1069 try: 1070 return self._ecl 1071 except AttributeError: 1072 self._ecl=maxima_eval("#$%s$"%self._name) 1073 return self._ecl 1074 1075 def to_poly_solve(self,vars,options=""): 1076 if options.find("use_grobner=true") != -1: 1077 cmd=EclObject([[max_to_poly_solve], self.ecl(), sr_to_max(vars), 1078 [[mequal],max_use_grobner,True]]) 1079 else: 1080 cmd=EclObject([[max_to_poly_solve], self.ecl(), sr_to_max(vars)]) 1081 return self.parent()(maxima_eval(cmd)) 1082 967 1083 def is_MaximaElement(x): 968 1084 """ 969 1085 Returns True if x is of type MaximaElement. … … 998 1114 999 1115 sage: from sage.interfaces.maxima import maxima_version 1000 1116 sage: maxima_version() 1001 '5. 19.1'1117 '5.22.1' 1002 1118 """ 1003 1119 return os.popen('maxima --version').read().split()[-1] 1004 1120 … … 1007 1123 sage.interfaces.quit.expect_quitall() 1008 1124 1009 1125 1126 import sage.symbolic.expression 1127 from sage.symbolic.ring import SR 1128 1129 import sage.symbolic.expression 1130 import sage.functions.trig 1131 import sage.functions.log 1132 import sage.functions.other 1133 import sage.symbolic.integration.integral 1134 1135 car=EclObject("car") 1136 cdr=EclObject("cdr") 1137 caar=EclObject("caar") 1138 cadr=EclObject("cadr") 1139 cddr=EclObject("cddr") 1140 caddr=EclObject("caddr") 1141 caaadr=EclObject("caaadr") 1142 cadadr=EclObject("cadadr") 1143 meval=EclObject("meval") 1144 NIL=EclObject("NIL") 1145 ratdisrep=EclObject("ratdisrep") 1146 1147 sage_op_dict={} 1148 1149 sage_op_dict = { 1150 sage.symbolic.expression.operator.abs : "MABS", 1151 sage.symbolic.expression.operator.add : "MPLUS", 1152 sage.symbolic.expression.operator.div : "MQUOTIENT", 1153 sage.symbolic.expression.operator.eq : "MEQUAL", 1154 sage.symbolic.expression.operator.ge : "MGEQP", 1155 sage.symbolic.expression.operator.gt : "MGREATERP", 1156 sage.symbolic.expression.operator.le : "MLEQP", 1157 sage.symbolic.expression.operator.lt : "MLESSP", 1158 sage.symbolic.expression.operator.mul : "MTIMES", 1159 sage.symbolic.expression.operator.ne : "MNOTEQUAL", 1160 sage.symbolic.expression.operator.neg : "MMINUS", 1161 sage.symbolic.expression.operator.pow : "MEXPT", 1162 sage.symbolic.expression.operator.or_ : "MOR", 1163 sage.symbolic.expression.operator.and_ : "MAND", 1164 sage.functions.trig.acos : "%ACOS", 1165 sage.functions.trig.acot : "%ACOT", 1166 sage.functions.trig.acsc : "%ACSC", 1167 sage.functions.trig.asec : "%ASEC", 1168 sage.functions.trig.asin : "%ASIN", 1169 sage.functions.trig.atan : "%ATAN", 1170 sage.functions.trig.cos : "%COS", 1171 sage.functions.trig.cot : "%COT", 1172 sage.functions.trig.csc : "%CSC", 1173 sage.functions.trig.sec : "%SEC", 1174 sage.functions.trig.sin : "%SIN", 1175 sage.functions.trig.tan : "%TAN", 1176 sage.functions.log.exp : "%EXP", 1177 sage.functions.log.ln : "%LOG", 1178 sage.functions.log.log : "%LOG", 1179 sage.functions.other.factorial : "MFACTORIAL", 1180 sage.functions.other.erf : "%ERF", 1181 sage.functions.other.gamma_inc : "%GAMMA_INCOMPLETE" 1182 } 1183 1184 #we compile the dictionary 1185 sage_op_dict = dict([(k,EclObject(sage_op_dict[k])) for k in sage_op_dict]) 1186 max_op_dict = dict([(sage_op_dict[k],k) for k in sage_op_dict]) 1187 def add_vararg(*args): 1188 S=0 1189 for a in args: 1190 S=S+a 1191 return S 1192 1193 def mul_vararg(*args): 1194 P=1 1195 for a in args: 1196 P=P*a 1197 return P 1198 1199 def sage_rat(x,y): 1200 return x/y 1201 1202 mplus=EclObject("MPLUS") 1203 mtimes=EclObject("MTIMES") 1204 rat=EclObject("RAT") 1205 max_i=EclObject("$%I") 1206 max_op_dict[mplus]=add_vararg 1207 max_op_dict[mtimes]=mul_vararg 1208 max_op_dict[rat]=sage_rat 1209 mqapply=EclObject("MQAPPLY") 1210 max_li=EclObject("$LI") 1211 max_psi=EclObject("$PSI") 1212 max_array=EclObject("ARRAY") 1213 max_gamma_incomplete=sage_op_dict[sage.functions.other.gamma_inc] 1214 1215 def mrat_to_sage(expr): 1216 r""" 1217 Convert a maxima MRAT expression to Sage SR 1218 1219 Maxima has an optimised representation for multivariate rational expressions. 1220 The easiest way to translate those to SR is by first asking maxima to give 1221 the generic representation of the object. That is what RATDISREP does in 1222 maxima. 1223 """ 1224 return max_to_sr(meval(EclObject([[ratdisrep],expr]))) 1225 1226 def mqapply_to_sage(expr): 1227 r""" 1228 Special conversion rule for MQAPPLY expressions 1229 """ 1230 if caaadr(expr) == max_li: 1231 return sage.functions.log.polylog(max_to_sr(cadadr(expr)),max_to_sr(caddr(expr))) 1232 if caaadr(expr) == max_psi: 1233 return sage.functions.other.psi(max_to_sr(cadadr(expr)),max_to_sr(caddr(expr))) 1234 else: 1235 op=max_to_sr(cadr(expr)) 1236 max_args=cddr(expr) 1237 args=[max_to_sr(a) for a in max_args] 1238 return op(*args) 1239 1240 def dummy_integrate(expr): 1241 r""" 1242 we would like to simply tie maxima's integrate to sage.calculus.calculus.dummy_integrate, but we're being imported there so to avoid circularity we define it here. 1243 """ 1244 args=[max_to_sr(a) for a in cdr(expr)] 1245 if len(args) == 4 : 1246 return sage.symbolic.integration.integral.definite_integral(*args, hold=True) 1247 else: 1248 return sage.symbolic.integration.integral.indefinite_integral(*args, hold=True) 1249 1250 special_max_to_sage={ 1251 EclObject("MRAT") : mrat_to_sage, 1252 EclObject("MQAPPLY") : mqapply_to_sage, 1253 EclObject("%INTEGRATE") : dummy_integrate 1254 } 1255 1256 special_sage_to_max={ 1257 sage.functions.log.polylog : lambda N,X : [[mqapply],[[max_li, max_array],N],X], 1258 sage.functions.other.psi1 : lambda X : [[mqapply],[[max_psi, max_array],0],X], 1259 sage.functions.other.psi2 : lambda N,X : [[mqapply],[[max_psi, max_array],N],X], 1260 sage.functions.other.Ei : lambda X : [[max_gamma_incomplete], 0, X] 1261 } 1262 1263 sage_sym_dict={} 1264 max_sym_dict={} 1265 1266 def pyobject_to_max(obj): 1267 if isinstance(obj,sage.rings.rational.Rational): 1268 return EclObject(obj) if (obj.denom().is_one()) else EclObject([[rat], obj.numer(),obj.denom()]) 1269 elif isinstance(obj,sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]: 1270 re, im = obj.list() 1271 return EclObject([[mplus], pyobject_to_max(re), [[mtimes], pyobject_to_max(im), max_i]]) 1272 1273 return EclObject(obj) 1274 1275 def sr_to_max(expr): 1276 r""" 1277 """ 1278 global sage_op_dict, max_op_dict 1279 global sage_sym_dict, max_sym_dict 1280 if isinstance(expr,list) or isinstance(expr,tuple): 1281 return EclObject(([mlist],[sr_to_max(e) for e in expr])) 1282 op = expr.operator() 1283 if op: 1284 if (op in special_sage_to_max): 1285 return EclObject(special_sage_to_max[op](*[sr_to_max(o) for o in expr.operands()])) 1286 elif not (op in sage_op_dict): 1287 op_max=caar(maxima(expr).ecl()) 1288 sage_op_dict[op]=op_max 1289 max_op_dict[op_max]=op 1290 return EclObject(([sage_op_dict[op]], 1291 [sr_to_max(o) for o in expr.operands()])) 1292 elif expr._is_symbol() or expr._is_constant(): 1293 if not expr in sage_sym_dict: 1294 sym_max=maxima(expr).ecl() 1295 sage_sym_dict[expr]=sym_max 1296 max_sym_dict[sym_max]=expr 1297 return sage_sym_dict[expr] 1298 else: 1299 try: 1300 return pyobject_to_max(expr.pyobject()) 1301 except TypeError: 1302 return maxima(expr).ecl() 1303 1304 def max_to_sr(expr): 1305 if expr.consp(): 1306 op_max=caar(expr) 1307 if op_max in special_max_to_sage: 1308 return special_max_to_sage[op_max](expr) 1309 if not(op_max in max_op_dict): 1310 sage_expr=SR(maxima(expr)) 1311 max_op_dict[op_max]=sage_expr.operator() 1312 sage_op_dict[sage_expr.operator()]=op_max 1313 op=max_op_dict[op_max] 1314 max_args=cdr(expr) 1315 args=[ max_to_sr(a) for a in max_args] 1316 return op(*args) 1317 elif expr.symbolp(): 1318 if not(expr in max_sym_dict): 1319 sage_symbol=SR(maxima(expr)) 1320 sage_sym_dict[sage_symbol]=expr 1321 max_sym_dict[expr]=sage_symbol 1322 return max_sym_dict[expr] 1323 else: 1324 return expr.python() 1325 -
sage/symbolic/integration/external.py
diff -r 6d5b89f947d1 -r 2aa53d20ef60 sage/symbolic/integration/external.py
a b 12 12 sage: maxima_integrator(f(x), x) 13 13 integrate(f(x), x) 14 14 """ 15 from sage.calculus.calculus import maxima 15 16 if not isinstance(expression, Expression): 16 17 expression = SR(expression) 17 18 if a is None: 18 result = expression._maxima_().integrate(v)19 result = maxima.sr_integral(expression,v) 19 20 else: 20 21 try: 21 result = expression._maxima_().integrate(v, a, b)22 result = maxima.sr_integral(expression, v, a, b) 22 23 except TypeError, error: 23 24 s = str(error) 24 25 if "divergent" in s or 'Principal Value' in s: 25 26 raise ValueError, "Integral is divergent." 26 27 else: 27 28 raise 28 return result .sage()29 return result 29 30 30 31 def sympy_integrator(expression, v, a=None, b=None): 31 32 """