# HG changeset patch
# User Jean-Pierre Flori <flori@enst.fr>
# Date 1299854441 -3600
# Node ID c45d947b51794b23113f735b2adca96acefc5eda
# Parent c86e54a6bef8e0625f47bac2ec1128c30d0f235a
Doctests.
diff -r c86e54a6bef8 -r c45d947b5179 sage/calculus/calculus.py
a
|
b
|
|
57 | 57 | sage: f = x + y + z/(2*sin(y*z/55)) |
58 | 58 | sage: g = f^f; g |
59 | 59 | (x + y + 1/2*z/sin(1/55*y*z))^(x + y + 1/2*z/sin(1/55*y*z)) |
60 | | |
| 60 | |
61 | 61 | Differentiation and integration are available, but behind the |
62 | 62 | scenes through Maxima:: |
63 | 63 | |
… |
… |
|
108 | 108 | sage: diff(T) # Jacobian matrix |
109 | 109 | [ (r, theta) |--> cos(theta) (r, theta) |--> -r*sin(theta)] |
110 | 110 | [ (r, theta) |--> sin(theta) (r, theta) |--> r*cos(theta)] |
111 | | sage: T.diff().det() # Jacobian |
| 111 | sage: T.diff().det() # Jacobian |
112 | 112 | (r, theta) |--> r*sin(theta)^2 + r*cos(theta)^2 |
113 | | |
114 | | When the order of variables is ambiguous, Sage will raise an exception when differentiating:: |
| 113 | |
| 114 | When the order of variables is ambiguous, Sage will raise an |
| 115 | exception when differentiating:: |
115 | 116 | |
116 | 117 | sage: f = sin(x+y); f.derivative() |
117 | 118 | Traceback (most recent call last): |
… |
… |
|
146 | 147 | |
147 | 148 | sage: f = sin(x) |
148 | 149 | sage: f(y) |
149 | | doctest:...: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) |
| 150 | doctest:...: DeprecationWarning: Substitution using function-call |
| 151 | syntax and unnamed arguments is deprecated and will be removed |
| 152 | from a future release of Sage; you can use named arguments instead, |
| 153 | like EXPR(x=..., y=...) |
150 | 154 | sin(y) |
151 | 155 | sage: f(pi) |
152 | 156 | 0 |
… |
… |
|
388 | 392 | # This is not the same instance of Maxima as the general purpose one |
389 | 393 | #from sage.interfaces.maxima import Maxima |
390 | 394 | #maxima = Maxima(init_code = ['display2d : false', 'domain : complex', |
391 | | # 'keepfloat : true', 'load(to_poly_solver)', 'load(simplify_sum)'], |
| 395 | # 'keepfloat : true', 'load(to_poly_solver)', |
| 396 | # 'load(simplify_sum)'], |
392 | 397 | # script_subdirectory=None) |
393 | 398 | |
394 | 399 | ######################################################## |
… |
… |
|
406 | 411 | - ``a`` - lower endpoint of the sum |
407 | 412 | |
408 | 413 | - ``b`` - upper endpoint of the sum |
409 | | |
| 414 | |
410 | 415 | - ``algorithm`` - (default: 'maxima') one of |
411 | 416 | - 'maxima' - use Maxima (the default) |
412 | 417 | - 'maple' - (optional) use Maple |
… |
… |
|
483 | 488 | sage: symbolic_sum(a*q^k, k, 0, n) |
484 | 489 | (a*q^(n + 1) - a)/(q - 1) |
485 | 490 | |
486 | | For the geometric series, we will have to assume |
| 491 | For the geometric series, we will have to assume |
487 | 492 | the right values for the sum to converge:: |
488 | 493 | |
489 | 494 | sage: assume(abs(q) < 1) |
… |
… |
|
518 | 523 | Trac #10564 is fixed:: |
519 | 524 | |
520 | 525 | sage: sum (n^3 * x^n, n, 0, infinity) |
521 | | (x^3 + 4*x^2 + x)/(x^4 - 4*x^3 + 6*x^2 - 4*x + 1) |
| 526 | (x^3 + 4*x^2 + x)/(x^4 - 4*x^3 + 6*x^2 - 4*x + 1) |
522 | 527 | |
523 | 528 | .. note:: |
524 | 529 | |
525 | | #. Sage can currently only understand a subset of the output of Maxima, Maple and |
526 | | Mathematica, so even if the chosen backend can perform the summation the |
527 | | result might not be convertable into a Sage expression. |
| 530 | #. Sage can currently only understand a subset of the output of Maxima, |
528 | 531 | |
| 532 | Maple and Mathematica, so even if the chosen backend can perform |
| 533 | |
| 534 | the summation the result might not be convertable into a Sage |
| 535 | |
| 536 | expression. |
529 | 537 | """ |
530 | 538 | if not is_SymbolicVariable(v): |
531 | 539 | if isinstance(v, str): |
… |
… |
|
573 | 581 | |
574 | 582 | INPUT: |
575 | 583 | |
| 584 | - ``x`` - variable to integrate with respect to |
576 | 585 | |
577 | | - ``x`` - variable to integrate with respect to |
| 586 | - ``a`` - lower endpoint of integration |
578 | 587 | |
579 | | - ``a`` - lower endpoint of integration |
| 588 | - ``b`` - upper endpoint of integration |
580 | 589 | |
581 | | - ``b`` - upper endpoint of integration |
| 590 | - ``desired_relative_error`` - (default: '1e-8') the |
| 591 | desired relative error |
582 | 592 | |
583 | | - ``desired_relative_error`` - (default: '1e-8') the |
584 | | desired relative error |
585 | | |
586 | | - ``maximum_num_subintervals`` - (default: 200) |
587 | | maxima number of subintervals |
588 | | |
| 593 | - ``maximum_num_subintervals`` - (default: 200) |
| 594 | maxima number of subintervals |
589 | 595 | |
590 | 596 | OUTPUT: |
591 | 597 | |
| 598 | - float: approximation to the integral |
592 | 599 | |
593 | | - float: approximation to the integral |
| 600 | - float: estimated absolute error of the |
| 601 | approximation |
594 | 602 | |
595 | | - float: estimated absolute error of the |
596 | | approximation |
| 603 | - the number of integrand evaluations |
597 | 604 | |
598 | | - the number of integrand evaluations |
| 605 | - an error code: |
599 | 606 | |
600 | | - an error code: |
| 607 | - ``0`` - no problems were encountered |
601 | 608 | |
602 | | - ``0`` - no problems were encountered |
| 609 | - ``1`` - too many subintervals were done |
603 | 610 | |
604 | | - ``1`` - too many subintervals were done |
| 611 | - ``2`` - excessive roundoff error |
605 | 612 | |
606 | | - ``2`` - excessive roundoff error |
| 613 | - ``3`` - extremely bad integrand behavior |
607 | 614 | |
608 | | - ``3`` - extremely bad integrand behavior |
| 615 | - ``4`` - failed to converge |
609 | 616 | |
610 | | - ``4`` - failed to converge |
| 617 | - ``5`` - integral is probably divergent or slowly |
| 618 | convergent |
611 | 619 | |
612 | | - ``5`` - integral is probably divergent or slowly |
613 | | convergent |
614 | | |
615 | | - ``6`` - the input is invalid |
616 | | |
| 620 | - ``6`` - the input is invalid |
617 | 621 | |
618 | 622 | ALIAS: nintegrate is the same as nintegral |
619 | 623 | |
… |
… |
|
631 | 635 | sage: f = f.nintegral(x,0,1,1e-14) |
632 | 636 | Traceback (most recent call last): |
633 | 637 | ... |
634 | | ValueError: Maxima (via quadpack) cannot compute the integral to that precision |
| 638 | ValueError: Maxima (via quadpack) cannot compute the integral |
| 639 | to that precision |
635 | 640 | |
636 | 641 | EXAMPLES:: |
637 | 642 | |
… |
… |
|
701 | 706 | else: |
702 | 707 | raise TypeError, err |
703 | 708 | |
704 | | #This is just a work around until there is a response to |
| 709 | #This is just a work around until there is a response to |
705 | 710 | #http://www.math.utexas.edu/pipermail/maxima/2008/012975.html |
706 | 711 | if 'quad_qags' in str(v): |
707 | 712 | raise ValueError, "Maxima (via quadpack) cannot compute the integral to that precision" |
… |
… |
|
716 | 721 | |
717 | 722 | INPUT: |
718 | 723 | |
719 | | - ``var`` - polynomial variable name (default 'x') |
| 724 | - ``var`` - polynomial variable name (default 'x') |
720 | 725 | |
721 | | - ``algorithm`` - 'algebraic' or 'numerical' (default |
722 | | both, but with numerical first) |
| 726 | - ``algorithm`` - 'algebraic' or 'numerical' (default |
| 727 | both, but with numerical first) |
723 | 728 | |
724 | | - ``bits`` - the number of bits to use in numerical |
725 | | approx |
| 729 | - ``bits`` - the number of bits to use in numerical |
| 730 | approx |
726 | 731 | |
727 | | - ``degree`` - the expected algebraic degree |
| 732 | - ``degree`` - the expected algebraic degree |
728 | 733 | |
729 | | - ``epsilon`` - return without error as long as |
730 | | f(self) epsilon, in the case that the result cannot be proven. |
| 734 | - ``epsilon`` - return without error as long as |
| 735 | f(self) epsilon, in the case that the result cannot be proven. |
731 | 736 | |
732 | | All of the above parameters are optional, with epsilon=0, bits and |
733 | | degree tested up to 1000 and 24 by default respectively. The |
734 | | numerical algorithm will be faster if bits and/or degree are given |
735 | | explicitly. The algebraic algorithm ignores the last three |
736 | | parameters. |
| 737 | All of the above parameters are optional, with epsilon=0, bits and |
| 738 | degree tested up to 1000 and 24 by default respectively. The |
| 739 | numerical algorithm will be faster if bits and/or degree are given |
| 740 | explicitly. The algebraic algorithm ignores the last three |
| 741 | parameters. |
737 | 742 | |
738 | 743 | |
739 | 744 | OUTPUT: The minimal polynomial of self. If the numerical algorithm |
… |
… |
|
806 | 811 | |
807 | 812 | The minpoly function is used implicitly when creating |
808 | 813 | number fields:: |
809 | | |
| 814 | |
810 | 815 | sage: x = var('x') |
811 | 816 | sage: eqn = x^3 + sqrt(2)*x + 5 == 0 |
812 | 817 | sage: a = solve(eqn, x)[0].rhs() |
… |
… |
|
898 | 903 | |
899 | 904 | for degree in degree_list: |
900 | 905 | |
901 | | f = QQ[var](algdep(a, degree)) # TODO: use the known_bits parameter? |
902 | | # If indeed we have found a minimal polynomial, |
903 | | # it should be accurate to a much higher precision. |
| 906 | f = QQ[var](algdep(a, degree)) # TODO: use the known_bits parameter? |
| 907 | # If indeed we have found a minimal polynomial, |
| 908 | # it should be accurate to a much higher precision. |
904 | 909 | error = abs(f(aa)) |
905 | 910 | dx = ~RR(Integer(1) << (check_bits - degree - 2)) |
906 | 911 | expected_error = abs(f.derivative()(CC(aa))) * dx |
907 | 912 | |
908 | 913 | if error < expected_error: |
909 | | # Degree might have been an over-estimate, factor because we want (irreducible) minpoly. |
| 914 | # Degree might have been an over-estimate, |
| 915 | # factor because we want (irreducible) minpoly. |
910 | 916 | ff = f.factor() |
911 | 917 | for g, e in ff: |
912 | 918 | lead = g.leading_coefficient() |
… |
… |
|
930 | 936 | if algorithm is None or algorithm == 'algebraic': |
931 | 937 | from sage.rings.all import QQbar |
932 | 938 | return QQ[var](QQbar(ex).minpoly()) |
933 | | |
| 939 | |
934 | 940 | raise ValueError, "Unknown algorithm: %s" % algorithm |
935 | 941 | |
936 | 942 | |
… |
… |
|
943 | 949 | from the given direction. |
944 | 950 | |
945 | 951 | :: |
946 | | |
| 952 | |
947 | 953 | expr.limit(x = a) |
948 | 954 | expr.limit(x = a, dir='above') |
949 | 955 | |
950 | 956 | INPUT: |
951 | 957 | |
952 | | - ``dir`` - (default: None); dir may have the value |
953 | | 'plus' (or '+' or 'right') for a limit from above, |
954 | | 'minus' (or '-' or 'left') for a limit from below, or may be omitted |
955 | | (implying a two-sided limit is to be computed). |
| 958 | - ``dir`` - (default: None); dir may have the value |
| 959 | 'plus' (or '+' or 'right') for a limit from above, |
| 960 | 'minus' (or '-' or 'left') for a limit from below, or may be omitted |
| 961 | (implying a two-sided limit is to be computed). |
956 | 962 | |
957 | | - ``taylor`` - (default: False); if True, use Taylor |
958 | | series, which allows more limits to be computed (but may also |
959 | | crash in some obscure cases due to bugs in Maxima). |
| 963 | - ``taylor`` - (default: False); if True, use Taylor |
| 964 | series, which allows more limits to be computed (but may also |
| 965 | crash in some obscure cases due to bugs in Maxima). |
960 | 966 | |
961 | | - ``**argv`` - 1 named parameter |
| 967 | - ``**argv`` - 1 named parameter |
962 | 968 | |
963 | 969 | .. note:: |
964 | 970 | |
… |
… |
|
989 | 995 | |
990 | 996 | Notice that Maxima may ask for more information:: |
991 | 997 | |
992 | | sage: var('a') |
| 998 | sage: var('a') |
993 | 999 | a |
994 | 1000 | sage: limit(x^a,x=0) |
995 | 1001 | Traceback (most recent call last): |
996 | 1002 | ... |
997 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details) |
| 1003 | ValueError: Computation failed since Maxima requested additional |
| 1004 | constraints; using the 'assume' command before limit evaluation |
| 1005 | *may* help (see `assume?` for more details) |
998 | 1006 | Is a positive, negative, or zero? |
999 | 1007 | |
1000 | 1008 | With this example, Maxima is looking for a LOT of information:: |
… |
… |
|
1003 | 1011 | sage: limit(x^a,x=0) |
1004 | 1012 | Traceback (most recent call last): |
1005 | 1013 | ... |
1006 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details) |
1007 | | Is a an integer? |
| 1014 | ValueError: Computation failed since Maxima requested additional |
| 1015 | constraints; using the 'assume' command before limit evaluation |
| 1016 | *may* help (see `assume?` for more details) |
| 1017 | Is a an integer? |
1008 | 1018 | sage: assume(a,'integer') |
1009 | 1019 | sage: limit(x^a,x=0) |
1010 | 1020 | Traceback (most recent call last): |
1011 | 1021 | ... |
1012 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details) |
| 1022 | ValueError: Computation failed since Maxima requested additional |
| 1023 | constraints; using the 'assume' command before limit evaluation |
| 1024 | *may* help (see `assume?` for more details) |
1013 | 1025 | Is a an even number? |
1014 | 1026 | sage: assume(a,'even') |
1015 | 1027 | sage: limit(x^a,x=0) |
… |
… |
|
1059 | 1071 | |
1060 | 1072 | sage: lim(x^2, x=2, dir='nugget') |
1061 | 1073 | Traceback (most recent call last): |
1062 | | ... |
1063 | | ValueError: dir must be one of None, 'plus', '+', 'right', 'minus', '-', 'left' |
| 1074 | ... |
| 1075 | ValueError: dir must be one of None, 'plus', '+', 'right', |
| 1076 | 'minus', '-', 'left' |
1064 | 1077 | |
1065 | 1078 | We check that Trac ticket 3718 is fixed, so that |
1066 | 1079 | Maxima gives correct limits for the floor function:: |
… |
… |
|
1093 | 1106 | Check that Trac 8942 is fixed:: |
1094 | 1107 | |
1095 | 1108 | sage: f(x) = (cos(pi/4-x) - tan(x)) / (1 - sin(pi/4+x)) |
1096 | | sage: limit(f(x), x = pi/4, dir='minus') |
| 1109 | sage: limit(f(x), x = pi/4, dir='minus') |
1097 | 1110 | +Infinity |
1098 | | sage: limit(f(x), x = pi/4, dir='plus') |
| 1111 | sage: limit(f(x), x = pi/4, dir='plus') |
1099 | 1112 | -Infinity |
1100 | | sage: limit(f(x), x = pi/4) |
| 1113 | sage: limit(f(x), x = pi/4) |
1101 | 1114 | Infinity |
1102 | 1115 | |
1103 | 1116 | Check that we give deprecation warnings for 'above' and 'below' #9200:: |
1104 | 1117 | |
1105 | 1118 | sage: limit(1/x, x=0, dir='above') |
1106 | | doctest:...: DeprecationWarning: (Since Sage version 4.6) the keyword 'above' is deprecated. Please use 'right' or '+' instead. |
| 1119 | doctest:...: DeprecationWarning: (Since Sage version 4.6) the keyword |
| 1120 | 'above' is deprecated. Please use 'right' or '+' instead. |
1107 | 1121 | +Infinity |
1108 | 1122 | sage: limit(1/x, x=0, dir='below') |
1109 | | doctest:...: DeprecationWarning: (Since Sage version 4.6) the keyword 'below' is deprecated. Please use 'left' or '-' instead. |
| 1123 | doctest:...: DeprecationWarning: (Since Sage version 4.6) the keyword |
| 1124 | 'below' is deprecated. Please use 'left' or '-' instead. |
1110 | 1125 | -Infinity |
1111 | 1126 | """ |
1112 | 1127 | if not isinstance(ex, Expression): |
… |
… |
|
1156 | 1171 | #return l.sage() |
1157 | 1172 | return ex.parent()(l) |
1158 | 1173 | |
1159 | | # lim is alias for limit |
| 1174 | # lim is alias for limit |
1160 | 1175 | lim = limit |
1161 | 1176 | |
1162 | 1177 | ################################################################### |
… |
… |
|
1172 | 1187 | The function that is returned may be be viewed as a function of |
1173 | 1188 | `s`. |
1174 | 1189 | |
1175 | | DEFINITION: The Laplace transform of a function `f(t)`, |
| 1190 | DEFINITION: |
| 1191 | |
| 1192 | The Laplace transform of a function `f(t)`, |
1176 | 1193 | defined for all real numbers `t \geq 0`, is the function |
1177 | 1194 | `F(s)` defined by |
1178 | 1195 | |
1179 | 1196 | .. math:: |
1180 | 1197 | |
1181 | | F(s) = \int_{0}^{\infty} e^{-st} f(t) dt. |
| 1198 | F(s) = \int_{0}^{\infty} e^{-st} f(t) dt. |
1182 | 1199 | |
| 1200 | EXAMPLES: |
1183 | 1201 | |
1184 | | |
1185 | | EXAMPLES: We compute a few Laplace transforms:: |
| 1202 | We compute a few Laplace transforms:: |
1186 | 1203 | |
1187 | 1204 | sage: var('x, s, z, t, t0') |
1188 | 1205 | (x, s, z, t, t0) |
… |
… |
|
1192 | 1209 | z/s + 1/(s - 1) |
1193 | 1210 | sage: log(t/t0).laplace(t, s) |
1194 | 1211 | -(euler_gamma + log(s) - log(1/t0))/s |
1195 | | |
| 1212 | |
1196 | 1213 | We do a formal calculation:: |
1197 | 1214 | |
1198 | 1215 | sage: f = function('f', x) |
… |
… |
|
1201 | 1218 | sage: g.laplace(x, s) |
1202 | 1219 | s*laplace(f(x), x, s) - f(0) |
1203 | 1220 | |
1204 | | EXAMPLE: A BATTLE BETWEEN the X-women and the Y-men (by David |
| 1221 | EXAMPLES: |
| 1222 | |
| 1223 | A BATTLE BETWEEN the X-women and the Y-men (by David |
1205 | 1224 | Joyner): Solve |
1206 | 1225 | |
1207 | 1226 | .. math:: |
1208 | 1227 | |
1209 | | x' = -16y, x(0)=270, y' = -x + 1, y(0) = 90. |
1210 | | |
| 1228 | x' = -16y, x(0)=270, y' = -x + 1, y(0) = 90. |
1211 | 1229 | |
1212 | 1230 | This models a fight between two sides, the "X-women" and the |
1213 | 1231 | "Y-men", where the X-women have 270 initially and the Y-men have |
… |
… |
|
1228 | 1246 | s*laplace(x(t), t, s) + 16*laplace(y(t), t, s) - x(0) |
1229 | 1247 | sage: de2.laplace(t, s) |
1230 | 1248 | s*laplace(y(t), t, s) - 1/s + laplace(x(t), t, s) - y(0) |
1231 | | |
| 1249 | |
1232 | 1250 | Next we form the augmented matrix of the above system:: |
1233 | 1251 | |
1234 | | sage: A = matrix([[s, 16, 270],[1, s, 90+1/s]]) |
| 1252 | sage: A = matrix([[s, 16, 270],[1, s, 90+1/s]]) |
1235 | 1253 | sage: E = A.echelon_form() |
1236 | 1254 | sage: xt = E[0,2].inverse_laplace(s,t) |
1237 | 1255 | sage: yt = E[1,2].inverse_laplace(s,t) |
… |
… |
|
1247 | 1265 | |
1248 | 1266 | sage: var('a,s,t') |
1249 | 1267 | (a, s, t) |
1250 | | sage: f = exp (2*t + a) * sin(t) * t; f |
| 1268 | sage: f = exp (2*t + a) * sin(t) * t; f |
1251 | 1269 | t*e^(a + 2*t)*sin(t) |
1252 | 1270 | sage: L = laplace(f, t, s); L |
1253 | 1271 | 2*(s - 2)*e^a/(s^2 - 4*s + 5)^2 |
1254 | 1272 | sage: inverse_laplace(L, s, t) |
1255 | 1273 | t*e^(a + 2*t)*sin(t) |
1256 | | |
| 1274 | |
1257 | 1275 | Unable to compute solution:: |
1258 | | |
| 1276 | |
1259 | 1277 | sage: laplace(1/s, s, t) |
1260 | 1278 | laplace(1/s, s, t) |
1261 | | |
1262 | 1279 | """ |
1263 | 1280 | if not isinstance(ex, (Expression, Function)): |
1264 | 1281 | ex = SR(ex) |
… |
… |
|
1279 | 1296 | |
1280 | 1297 | .. math:: |
1281 | 1298 | |
1282 | | F(s) = \frac{1}{2\pi i} \int_{\gamma-i\infty}^{\gamma + i\infty} e^{st} F(s) dt, |
1283 | | |
| 1299 | F(s) = \frac{1}{2\pi i} \int_{\gamma-i\infty}^{\gamma + i\infty} e^{st} F(s) dt, |
1284 | 1300 | |
1285 | 1301 | where `\gamma` is chosen so that the contour path of |
1286 | 1302 | integration is in the region of convergence of `F(s)`. |
… |
… |
|
1302 | 1318 | t |--> t*cos(t) |
1303 | 1319 | sage: inverse_laplace(1/(s^3+1), s, t) |
1304 | 1320 | 1/3*(sqrt(3)*sin(1/2*sqrt(3)*t) - cos(1/2*sqrt(3)*t))*e^(1/2*t) + 1/3*e^(-t) |
1305 | | |
| 1321 | |
1306 | 1322 | No explicit inverse Laplace transform, so one is returned formally |
1307 | 1323 | as a function ``ilt``:: |
1308 | | |
| 1324 | |
1309 | 1325 | sage: inverse_laplace(cos(s), s, t) |
1310 | 1326 | ilt(cos(s), s, t) |
1311 | | |
1312 | 1327 | """ |
1313 | 1328 | if not isinstance(ex, Expression): |
1314 | 1329 | ex = SR(ex) |
… |
… |
|
1367 | 1382 | Return comparison of the two variables x and y, which is just the |
1368 | 1383 | comparison of the underlying string representations of the |
1369 | 1384 | variables. This is used internally by the Calculus package. |
1370 | | |
| 1385 | |
1371 | 1386 | INPUT: |
1372 | | |
1373 | | - ``x, y`` - symbolic variables |
1374 | | |
| 1387 | |
| 1388 | - ``x, y`` - symbolic variables |
| 1389 | |
1375 | 1390 | OUTPUT: Python integer; either -1, 0, or 1. |
1376 | | |
| 1391 | |
1377 | 1392 | EXAMPLES:: |
1378 | | |
| 1393 | |
1379 | 1394 | sage: sage.calculus.calculus.var_cmp(x,x) |
1380 | 1395 | 0 |
1381 | 1396 | sage: sage.calculus.calculus.var_cmp(x,var('z')) |
… |
… |
|
1389 | 1404 | """ |
1390 | 1405 | This function is called to create formal wrappers of limits that |
1391 | 1406 | Maxima can't compute: |
1392 | | |
| 1407 | |
1393 | 1408 | EXAMPLES:: |
1394 | | |
| 1409 | |
1395 | 1410 | sage: a = lim(exp(x^2)*(1-erf(x)), x=infinity); a |
1396 | 1411 | limit(-e^(x^2)*erf(x) + e^(x^2), x, +Infinity) |
1397 | | sage: a = sage.calculus.calculus.dummy_limit(sin(x)/x, x, 0);a |
| 1412 | sage: a = sage.calculus.calculus.dummy_limit(sin(x)/x, x, 0);a |
1398 | 1413 | limit(sin(x)/x, x, 0) |
1399 | 1414 | """ |
1400 | 1415 | return _limit(args[0], var(repr(args[1])), SR(args[2])) |
… |
… |
|
1422 | 1437 | for i in range(1, len(args), 2): |
1423 | 1438 | args[i] = Integer(args[i]) |
1424 | 1439 | return f.diff(*args) |
1425 | | |
| 1440 | |
1426 | 1441 | def dummy_integrate(*args): |
1427 | 1442 | """ |
1428 | 1443 | This function is called to create formal wrappers of integrals that |
1429 | 1444 | Maxima can't compute: |
1430 | | |
| 1445 | |
1431 | 1446 | EXAMPLES:: |
1432 | | |
| 1447 | |
1433 | 1448 | sage: from sage.calculus.calculus import dummy_integrate |
1434 | 1449 | sage: f(x) = function('f',x) |
1435 | 1450 | sage: dummy_integrate(f(x), x) |
… |
… |
|
1437 | 1452 | sage: a,b = var('a,b') |
1438 | 1453 | sage: dummy_integrate(f(x), x, a, b) |
1439 | 1454 | integrate(f(x), x, a, b) |
1440 | | |
1441 | 1455 | """ |
1442 | 1456 | if len(args) == 4: |
1443 | 1457 | return definite_integral(*args, hold=True) |
… |
… |
|
1448 | 1462 | """ |
1449 | 1463 | This function is called to create formal wrappers of laplace transforms |
1450 | 1464 | that Maxima can't compute: |
1451 | | |
| 1465 | |
1452 | 1466 | EXAMPLES:: |
1453 | | |
| 1467 | |
1454 | 1468 | sage: from sage.calculus.calculus import dummy_laplace |
1455 | 1469 | sage: s,t = var('s,t') |
1456 | 1470 | sage: f(t) = function('f',t) |
… |
… |
|
1463 | 1477 | """ |
1464 | 1478 | This function is called to create formal wrappers of inverse laplace |
1465 | 1479 | transforms that Maxima can't compute: |
1466 | | |
| 1480 | |
1467 | 1481 | EXAMPLES:: |
1468 | | |
| 1482 | |
1469 | 1483 | sage: from sage.calculus.calculus import dummy_inverse_laplace |
1470 | 1484 | sage: s,t = var('s,t') |
1471 | 1485 | sage: F(s) = function('F',s) |
… |
… |
|
1476 | 1490 | |
1477 | 1491 | ####################################################### |
1478 | 1492 | # |
1479 | | # Helper functions for printing latex expression |
| 1493 | # Helper functions for printing latex expression |
1480 | 1494 | # |
1481 | 1495 | ####################################################### |
1482 | 1496 | |
1483 | 1497 | def _limit_latex_(self, f, x, a): |
1484 | 1498 | r""" |
1485 | 1499 | Return latex expression for limit of a symbolic function. |
1486 | | |
| 1500 | |
1487 | 1501 | EXAMPLES:: |
1488 | | |
| 1502 | |
1489 | 1503 | sage: from sage.calculus.calculus import _limit_latex_ |
1490 | 1504 | sage: var('x,a') |
1491 | 1505 | (x, a) |
… |
… |
|
1494 | 1508 | '\\lim_{x \\to a}\\, f\\left(x\\right)' |
1495 | 1509 | sage: latex(limit(f, x=oo)) |
1496 | 1510 | \lim_{x \to +\infty}\, f\left(x\right) |
1497 | | |
| 1511 | |
1498 | 1512 | """ |
1499 | 1513 | return "\\lim_{%s \\to %s}\\, %s"%(latex(x), latex(a), latex(f)) |
1500 | 1514 | |
1501 | 1515 | def _laplace_latex_(self, *args): |
1502 | 1516 | r""" |
1503 | 1517 | Return LaTeX expression for Laplace transform of a symbolic function. |
1504 | | |
| 1518 | |
1505 | 1519 | EXAMPLES:: |
1506 | | |
| 1520 | |
1507 | 1521 | sage: from sage.calculus.calculus import _laplace_latex_ |
1508 | 1522 | sage: var('s,t') |
1509 | 1523 | (s, t) |
… |
… |
|
1512 | 1526 | '\\mathcal{L}\\left(f\\left(t\\right), t, s\\right)' |
1513 | 1527 | sage: latex(laplace(f, t, s)) |
1514 | 1528 | \mathcal{L}\left(f\left(t\right), t, s\right) |
1515 | | |
| 1529 | |
1516 | 1530 | """ |
1517 | 1531 | return "\\mathcal{L}\\left(%s\\right)"%(', '.join([latex(x) for x in args])) |
1518 | 1532 | |
1519 | 1533 | def _inverse_laplace_latex_(self, *args): |
1520 | 1534 | r""" |
1521 | | Return LaTeX expression for inverse Laplace transform of a symbolic function. |
1522 | | |
| 1535 | Return LaTeX expression for inverse Laplace transform |
| 1536 | of a symbolic function. |
| 1537 | |
1523 | 1538 | EXAMPLES:: |
1524 | | |
| 1539 | |
1525 | 1540 | sage: from sage.calculus.calculus import _inverse_laplace_latex_ |
1526 | 1541 | sage: var('s,t') |
1527 | 1542 | (s, t) |
… |
… |
|
1530 | 1545 | '\\mathcal{L}^{-1}\\left(F\\left(s\\right), s, t\\right)' |
1531 | 1546 | sage: latex(inverse_laplace(F,s,t)) |
1532 | 1547 | \mathcal{L}^{-1}\left(F\left(s\right), s, t\right) |
1533 | | |
1534 | 1548 | """ |
1535 | 1549 | return "\\mathcal{L}^{-1}\\left(%s\\right)"%(', '.join([latex(x) for x in args])) |
1536 | 1550 | |
… |
… |
|
1568 | 1582 | """ |
1569 | 1583 | Given a string representation of a Maxima expression, parse it and |
1570 | 1584 | return the corresponding Sage symbolic expression. |
1571 | | |
| 1585 | |
1572 | 1586 | INPUT: |
1573 | | |
1574 | | - ``x`` - a string |
1575 | | |
1576 | | - ``equals_sub`` - (default: False) if True, replace |
1577 | | '=' by '==' in self |
1578 | | |
1579 | | - ``maxima`` - (default: the calculus package's |
1580 | | Maxima) the Maxima interpreter to use. |
1581 | | |
1582 | | |
| 1587 | |
| 1588 | - ``x`` - a string |
| 1589 | |
| 1590 | - ``equals_sub`` - (default: False) if True, replace |
| 1591 | '=' by '==' in self |
| 1592 | |
| 1593 | - ``maxima`` - (default: the calculus package's |
| 1594 | Maxima) the Maxima interpreter to use. |
| 1595 | |
1583 | 1596 | EXAMPLES:: |
1584 | | |
| 1597 | |
1585 | 1598 | sage: from sage.calculus.calculus import symbolic_expression_from_maxima_string as sefms |
1586 | 1599 | sage: sefms('x^%e + %e^%pi + %i + sin(0)') |
1587 | 1600 | x^e + e^pi + I |
… |
… |
|
1594 | 1607 | x != 0 |
1595 | 1608 | |
1596 | 1609 | TESTS: |
1597 | | |
| 1610 | |
1598 | 1611 | Trac #8459 fixed:: |
1599 | 1612 | |
1600 | 1613 | sage: maxima('3*li[2](u)+8*li[33](exp(u))').sage() |
1601 | 1614 | 3*polylog(2, u) + 8*polylog(33, e^u) |
1602 | | |
1603 | 1615 | """ |
1604 | 1616 | syms = sage.symbolic.pynac.symbol_table.get('maxima', {}).copy() |
1605 | 1617 | |
… |
… |
|
1609 | 1621 | |
1610 | 1622 | # This is inefficient since it so rarely is needed: |
1611 | 1623 | #r = maxima._eval_line('listofvars(_tmp_);')[1:-1] |
1612 | | |
| 1624 | |
1613 | 1625 | s = maxima._eval_line('_tmp_;') |
1614 | | |
| 1626 | |
1615 | 1627 | formal_functions = maxima_tick.findall(s) |
1616 | 1628 | if len(formal_functions) > 0: |
1617 | 1629 | for X in formal_functions: |
… |
… |
|
1619 | 1631 | # You might think there is a potential very subtle bug if 'foo |
1620 | 1632 | # is in a string literal -- but string literals should *never* |
1621 | 1633 | # ever be part of a symbolic expression. |
1622 | | s = s.replace("'","") |
| 1634 | s = s.replace("'","") |
1623 | 1635 | |
1624 | 1636 | delayed_functions = maxima_qp.findall(s) |
1625 | 1637 | if len(delayed_functions) > 0: |
… |
… |
|
1630 | 1642 | syms[X[2:]] = function_factory(X[2:]) |
1631 | 1643 | s = s.replace("?%","") |
1632 | 1644 | |
1633 | | s = polylog_ex.sub('polylog(\\1,',s) |
| 1645 | s = polylog_ex.sub('polylog(\\1,',s) |
1634 | 1646 | s = multiple_replace(symtable, s) |
1635 | 1647 | s = s.replace("%","") |
1636 | 1648 | |
1637 | 1649 | s = s.replace("#","!=") # a lot of this code should be refactored somewhere... |
1638 | 1650 | |
1639 | 1651 | s = maxima_polygamma.sub('psi(\g<1>,',s) # this replaces psi[n](foo) with psi(n,foo), ensuring that derivatives of the digamma function are parsed properly below |
1640 | | |
| 1652 | |
1641 | 1653 | if equals_sub: |
1642 | 1654 | s = s.replace('=','==') |
1643 | 1655 | |
… |
… |
|
1660 | 1672 | r = create_RealNumber(s[start:end]).str(no_sci=2, truncate=True) |
1661 | 1673 | s = s.replace(s[start:end], r) |
1662 | 1674 | search = sci_not.search(s) |
1663 | | |
| 1675 | |
1664 | 1676 | # have to do this here, otherwise maxima_tick catches it |
1665 | 1677 | syms['limit'] = dummy_limit |
1666 | 1678 | syms['diff'] = dummy_diff |
… |
… |
|
1687 | 1699 | """ |
1688 | 1700 | Used internally when creating a string of options to pass to |
1689 | 1701 | Maxima. |
1690 | | |
| 1702 | |
1691 | 1703 | INPUT: |
1692 | | |
1693 | | - ``v`` - an object |
1694 | | |
| 1704 | |
| 1705 | - ``v`` - an object |
| 1706 | |
1695 | 1707 | OUTPUT: a string. |
1696 | | |
| 1708 | |
1697 | 1709 | The main use of this is to turn Python bools into lower case |
1698 | 1710 | strings. |
1699 | | |
| 1711 | |
1700 | 1712 | EXAMPLES:: |
1701 | | |
| 1713 | |
1702 | 1714 | sage: sage.calculus.calculus.mapped_opts(True) |
1703 | 1715 | 'true' |
1704 | 1716 | sage: sage.calculus.calculus.mapped_opts(False) |
… |
… |
|
1713 | 1725 | def maxima_options(**kwds): |
1714 | 1726 | """ |
1715 | 1727 | Used internally to create a string of options to pass to Maxima. |
1716 | | |
| 1728 | |
1717 | 1729 | EXAMPLES:: |
1718 | | |
| 1730 | |
1719 | 1731 | sage: sage.calculus.calculus.maxima_options(an_option=True, another=False, foo='bar') |
1720 | 1732 | 'an_option=true,foo=bar,another=false' |
1721 | 1733 | """ |
… |
… |
|
1736 | 1748 | syms_default = dict(syms_cur) |
1737 | 1749 | |
1738 | 1750 | # This dictionary is used to pass a lookup table other than the system registry |
1739 | | # to the parser. A global variable is necessary since the parser calls the |
| 1751 | # to the parser. A global variable is necessary since the parser calls the |
1740 | 1752 | # _find_var() and _find_func() functions below without extra arguments. |
1741 | 1753 | _augmented_syms = {} |
1742 | 1754 | |
… |
… |
|
1744 | 1756 | |
1745 | 1757 | def _find_var(name): |
1746 | 1758 | """ |
1747 | | Function to pass to Parser for constructing |
| 1759 | Function to pass to Parser for constructing |
1748 | 1760 | variables from strings. For internal use. |
1749 | 1761 | |
1750 | 1762 | EXAMPLES:: |
1751 | | |
| 1763 | |
1752 | 1764 | sage: y = var('y') |
1753 | 1765 | sage: sage.calculus.calculus._find_var('y') |
1754 | 1766 | y |
… |
… |
|
1772 | 1784 | return SR(sage.all.__dict__[name]) |
1773 | 1785 | except (KeyError, TypeError): |
1774 | 1786 | return var(name) |
1775 | | |
| 1787 | |
1776 | 1788 | def _find_func(name): |
1777 | 1789 | """ |
1778 | | Function to pass to Parser for constructing |
| 1790 | Function to pass to Parser for constructing |
1779 | 1791 | functions from strings. For internal use. |
1780 | 1792 | |
1781 | 1793 | EXAMPLES:: |
1782 | | |
| 1794 | |
1783 | 1795 | sage: sage.calculus.calculus._find_func('limit') |
1784 | 1796 | limit |
1785 | 1797 | sage: sage.calculus.calculus._find_func('zeta_zeros') |
… |
… |
|
1810 | 1822 | make_float = lambda x: SR(RealDoubleElement(x)), |
1811 | 1823 | make_var = _find_var, |
1812 | 1824 | make_function = _find_func) |
1813 | | |
| 1825 | |
1814 | 1826 | def symbolic_expression_from_string(s, syms=None, accept_sequence=False): |
1815 | 1827 | """ |
1816 | | Given a string, (attempt to) parse it and return the |
| 1828 | Given a string, (attempt to) parse it and return the |
1817 | 1829 | corresponding Sage symbolic expression. Normally used |
1818 | 1830 | to return Maxima output to the user. |
1819 | | |
| 1831 | |
1820 | 1832 | INPUT: |
1821 | | |
1822 | | - ``s`` - a string |
1823 | | |
1824 | | - ``syms`` - (default: None) dictionary of |
1825 | | strings to be regarded as symbols or functions |
1826 | 1833 | |
1827 | | - ``accept_sequence`` - (default: False) controls whether |
1828 | | to allow a (possibly nested) set of lists and tuples |
1829 | | as input |
1830 | | |
| 1834 | - ``s`` - a string |
| 1835 | |
| 1836 | - ``syms`` - (default: None) dictionary of |
| 1837 | strings to be regarded as symbols or functions |
| 1838 | |
| 1839 | - ``accept_sequence`` - (default: False) controls whether |
| 1840 | to allow a (possibly nested) set of lists and tuples |
| 1841 | as input |
| 1842 | |
1831 | 1843 | EXAMPLES:: |
1832 | | |
| 1844 | |
1833 | 1845 | sage: y = var('y') |
1834 | 1846 | sage: sage.calculus.calculus.symbolic_expression_from_string('[sin(0)*x^2,3*spam+e^pi]',syms={'spam':y},accept_sequence=True) |
1835 | 1847 | [0, 3*y + e^pi] |
diff -r c86e54a6bef8 -r c45d947b5179 sage/calculus/functional.py
a
|
b
|
|
203 | 203 | sage: (x,y) = (t^4,t) |
204 | 204 | sage: (dx,dy) = (diff(x,t), diff(y,t)) |
205 | 205 | sage: integral(sin(x)*dx, t,-1, 1) |
206 | | 0 |
| 206 | 0 |
207 | 207 | sage: restore('x,y') # restore the symbolic variables x and y |
208 | 208 | |
209 | 209 | Sage is unable to do anything with the following integral:: |
… |
… |
|
242 | 242 | sage: integral(abs(x)*x, x, 0, a) |
243 | 243 | Traceback (most recent call last): |
244 | 244 | ... |
245 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details) |
| 245 | ValueError: Computation failed since Maxima requested additional |
| 246 | constraints; using the 'assume' command before integral evaluation |
| 247 | *may* help (example of legal syntax is 'assume(a>0)', |
| 248 | see `assume?` for more details) |
246 | 249 | Is a positive, negative, or zero? |
247 | 250 | sage: assume(a>0) |
248 | 251 | sage: integral(abs(x)*x, x, 0, a) |
… |
… |
|
266 | 269 | -6.9388939039072284e-17] |
267 | 270 | sage: h.factor() |
268 | 271 | 0 |
269 | | sage: bool(h == 0) |
| 272 | sage: bool(h == 0) |
270 | 273 | True |
271 | 274 | """ |
272 | 275 | try: |
… |
… |
|
293 | 296 | |
294 | 297 | INPUT: |
295 | 298 | |
296 | | |
297 | | - ``dir`` - (default: None); dir may have the value |
| 299 | - ``dir`` - (default: None); dir may have the value |
298 | 300 | 'plus' (or 'above') for a limit from above, 'minus' (or 'below') |
299 | 301 | for a limit from below, or may be omitted (implying a two-sided |
300 | 302 | limit is to be computed). |
301 | 303 | |
302 | | - ``taylor`` - (default: False); if True, use Taylor |
| 304 | - ``taylor`` - (default: False); if True, use Taylor |
303 | 305 | series, which allows more limits to be computed (but may also |
304 | 306 | crash in some obscure cases due to bugs in Maxima). |
305 | 307 | |
306 | | - ``\*\*argv`` - 1 named parameter |
307 | | |
| 308 | - ``\*\*argv`` - 1 named parameter |
308 | 309 | |
309 | 310 | ALIAS: You can also use lim instead of limit. |
310 | 311 | |
… |
… |
|
316 | 317 | +Infinity |
317 | 318 | sage: lim(exp(x), x=-oo) |
318 | 319 | 0 |
319 | | sage: lim(1/x, x=0) |
| 320 | sage: lim(1/x, x=0) |
320 | 321 | Infinity |
321 | 322 | sage: limit(sqrt(x^2+x+1)+x, taylor=True, x=-oo) |
322 | 323 | -1/2 |
… |
… |
|
341 | 342 | variable `v` around the point `a`, containing terms |
342 | 343 | through `(x - a)^n`. Functions in more variables are also |
343 | 344 | supported. |
344 | | |
| 345 | |
345 | 346 | INPUT: |
346 | | |
347 | | - ``*args`` - the following notation is supported |
348 | | |
349 | | - ``x, a, n`` - variable, point, degree |
350 | | |
351 | | - ``(x, a), (y, b), ..., n`` - variables with points, degree of polynomial |
352 | | |
| 347 | |
| 348 | - ``*args`` - the following notation is supported |
| 349 | |
| 350 | - ``x, a, n`` - variable, point, degree |
| 351 | |
| 352 | - ``(x, a), (y, b), ..., n`` - variables with points, degree of polynomial |
| 353 | |
353 | 354 | EXAMPLES:: |
354 | 355 | |
355 | 356 | sage: var('x,k,n') |
… |
… |
|
366 | 367 | |
367 | 368 | sage: taylor ((x + 1)^n, x, 0, 4) |
368 | 369 | 1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1 |
369 | | |
370 | | Taylor polynomial in two variables:: |
| 370 | |
| 371 | Taylor polynomial in two variables:: |
371 | 372 | |
372 | | sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,-1),4) |
| 373 | sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,-1),4) |
373 | 374 | (y + 1)^3*(x - 1) + (y + 1)^3 - 3*(y + 1)^2*(x - 1) - 3*(y + 1)^2 + 3*(y + 1)*(x - 1) - x + 3*y + 3 |
374 | 375 | """ |
375 | 376 | if not isinstance(f, Expression): |
… |
… |
|
425 | 426 | 0.28867513459481287 |
426 | 427 | sage: float(expand(t1 + t2)) |
427 | 428 | -0.288675134594812... |
428 | | sage: float(expand(tt1 + t2)) |
| 429 | sage: float(expand(tt1 + t2)) |
429 | 430 | -0.288675134594812... |
430 | 431 | """ |
431 | 432 | try: |
432 | 433 | return x.expand(*args, **kwds) |
433 | 434 | except AttributeError: |
434 | 435 | return x |
435 | | |
diff -r c86e54a6bef8 -r c45d947b5179 sage/interfaces/expect.py
a
|
b
|
|
1 | 1 | """ |
2 | | Common Interface Functionality |
| 2 | Common Interface Functionality through Pexpect |
3 | 3 | |
4 | 4 | See the examples in the other sections for how to use specific |
5 | 5 | interfaces. The interface classes all derive from the generic |
… |
… |
|
17 | 17 | - Simon King (2010-09-25): Expect._local_tmpfile() depends on |
18 | 18 | Expect.pid() and is cached; Expect.quit() clears that cache, |
19 | 19 | which is important for forking. |
| 20 | |
| 21 | - Jean-Pierre Flori (2010,2011): Split non Pexpect stuff into a parent class. |
20 | 22 | """ |
21 | 23 | |
22 | 24 | #***************************************************************************** |
diff -r c86e54a6bef8 -r c45d947b5179 sage/interfaces/interface.py
a
|
b
|
|
17 | 17 | - Simon King (2010-09-25): Expect._local_tmpfile() depends on |
18 | 18 | Expect.pid() and is cached; Expect.quit() clears that cache, |
19 | 19 | which is important for forking. |
| 20 | |
| 21 | - Jean-Pierre Flori (2010,2011): Split non Pexpect stuff into a parent class. |
20 | 22 | """ |
21 | 23 | |
22 | 24 | #***************************************************************************** |
diff -r c86e54a6bef8 -r c45d947b5179 sage/interfaces/maxima.py
a
|
b
|
|
1 | 1 | r""" |
2 | | Interface to Maxima |
| 2 | Pexpect interface to Maxima |
3 | 3 | |
4 | 4 | Maxima is a free GPL'd general purpose computer algebra system |
5 | 5 | whose development started in 1968 at MIT. It contains symbolic |
… |
… |
|
26 | 26 | - William Stein (2006-02-24): *greatly* improved robustness by adding |
27 | 27 | sequence numbers to IO bracketing in _eval_line |
28 | 28 | |
| 29 | - Robert Bradshaw, Nils Bruin, Jean-Pierre Flori (2010,2011): Binary library |
| 30 | interface |
| 31 | |
| 32 | This is the interface used by the maxima object:: |
| 33 | |
| 34 | sage: type(maxima) |
| 35 | <class 'sage.interfaces.maxima.Maxima'> |
| 36 | |
29 | 37 | If the string "error" (case insensitive) occurs in the output of |
30 | 38 | anything from Maxima, a RuntimeError exception is raised. |
31 | 39 | |
… |
… |
|
119 | 127 | :: |
120 | 128 | |
121 | 129 | sage: a = maxima('(1 + sqrt(2))^5') |
122 | | sage: float(a) |
| 130 | sage: float(a) |
123 | 131 | 82.012193308819747 |
124 | 132 | sage: a.numer() |
125 | 133 | 82.01219330881975 |
… |
… |
|
460 | 468 | ##import sage.rings.all |
461 | 469 | import sage.rings.complex_number |
462 | 470 | |
463 | | from expect import Expect, ExpectElement, FunctionElement, ExpectFunction, gc_disabled, AsciiArtString |
| 471 | from expect import (Expect, ExpectElement, FunctionElement, |
| 472 | ExpectFunction, gc_disabled, AsciiArtString) |
464 | 473 | |
465 | | from maxima_abstract import MaximaAbstract, MaximaAbstractFunction, MaximaAbstractElement, MaximaAbstractFunctionElement, MaximaAbstractElementFunction |
| 474 | from maxima_abstract import (MaximaAbstract, MaximaAbstractFunction, |
| 475 | MaximaAbstractElement, |
| 476 | MaximaAbstractFunctionElement, |
| 477 | MaximaAbstractElementFunction) |
466 | 478 | |
467 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 479 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, |
| 480 | # this should work as expected |
468 | 481 | class Maxima(MaximaAbstract, Expect): |
469 | 482 | """ |
470 | 483 | Interface to the Maxima interpreter. |
| 484 | |
| 485 | EXAMPLES:: |
| 486 | |
| 487 | sage: m = Maxima() |
| 488 | sage: m == maxima |
| 489 | False |
471 | 490 | """ |
472 | 491 | def __init__(self, script_subdirectory=None, logfile=None, server=None, |
473 | 492 | init_code = None): |
… |
… |
|
476 | 495 | |
477 | 496 | TESTS:: |
478 | 497 | |
| 498 | sage: Maxima == loads(dumps(Maxima)) |
| 499 | True |
479 | 500 | sage: maxima == loads(dumps(maxima)) |
480 | 501 | True |
481 | 502 | |
| 503 | Unpickling a Maxima Pexpect interface gives the default interface:: |
| 504 | |
| 505 | sage: m = Maxima() |
| 506 | sage: maxima == loads(dumps(m)) |
| 507 | True |
| 508 | |
482 | 509 | We make sure labels are turned off (see trac 6816):: |
483 | 510 | |
484 | 511 | sage: 'nolabels : true' in maxima._Expect__init_code |
… |
… |
|
501 | 528 | |
502 | 529 | if not os.path.exists(STARTUP): |
503 | 530 | raise RuntimeError, 'You must get the file local/bin/sage-maxima.lisp' |
| 531 | |
| 532 | #self.__init_code = init_code |
504 | 533 | if init_code is None: |
505 | 534 | # display2d -- no ascii art output |
506 | 535 | # keepfloat -- don't automatically convert floats to rationals |
507 | 536 | init_code = ['display2d : false', 'keepfloat : true'] |
508 | | |
| 537 | |
509 | 538 | # Turn off the prompt labels, since computing them *very |
510 | 539 | # dramatically* slows down the maxima interpret after a while. |
511 | 540 | # See the function makelabel in suprv1.lisp. |
512 | 541 | # Many thanks to andrej.vodopivec@gmail.com and also |
513 | 542 | # Robert Dodier for figuring this out! |
514 | | # See trac # 6818. |
| 543 | # See trac # 6818. |
515 | 544 | init_code.append('nolabels : true') |
516 | | |
| 545 | |
517 | 546 | MaximaAbstract.__init__(self,"maxima") |
518 | 547 | Expect.__init__(self, |
519 | 548 | name = 'maxima', |
520 | 549 | prompt = '\(\%i[0-9]+\)', |
521 | 550 | command = 'maxima-noreadline --userdir="%s" -p "%s"'%(SAGE_MAXIMA_DIR,STARTUP), |
522 | | maxread = 10000, |
| 551 | maxread = 10000, |
523 | 552 | script_subdirectory = script_subdirectory, |
524 | 553 | restart_on_ctrlc = False, |
525 | 554 | verbose_start = False, |
… |
… |
|
561 | 590 | self._sendline(r":lisp (defun tex-derivative (x l r) (tex (if $derivabbrev (tex-dabbrev x) (tex-d x '\\partial)) l r lop rop ))") |
562 | 591 | |
563 | 592 | # Remove limit on the max heapsize (since otherwise it defaults |
564 | | # to 256MB with ECL). |
| 593 | # to 256MB with ECL). |
565 | 594 | self._sendline(":lisp (ext:set-limit 'ext:heap-size 0)") |
566 | 595 | self._eval_line('0;') |
567 | 596 | |
568 | 597 | def __reduce__(self): |
569 | 598 | """ |
| 599 | Implementation of __reduce__ for ``Maxima``. |
| 600 | |
570 | 601 | EXAMPLES:: |
571 | 602 | |
572 | 603 | sage: maxima.__reduce__() |
573 | 604 | (<function reduce_load_Maxima at 0x...>, ()) |
574 | 605 | """ |
575 | | return reduce_load_Maxima, tuple([]) |
| 606 | return reduce_load_Maxima, tuple([]) #(self.__init_code,) |
576 | 607 | |
577 | 608 | def _sendline(self, str): |
| 609 | """ |
| 610 | Send a string followed by a newline character. |
| 611 | |
| 612 | EXAMPLES:: |
| 613 | |
| 614 | sage: maxima._sendline('t : 9;') |
| 615 | sage: maxima.get('t') |
| 616 | '9' |
| 617 | """ |
578 | 618 | self._sendstr(str) |
579 | 619 | os.write(self._expect.child_fd, os.linesep) |
580 | 620 | |
… |
… |
|
582 | 622 | """ |
583 | 623 | Wait for a given expression expr (which could be a regular |
584 | 624 | expression or list of regular expressions) to appear in the output |
585 | | for at most timeout seconds. |
| 625 | for at most timeout seconds. |
586 | 626 | |
587 | 627 | See `sage.interfaces.expect.Expect._expect_expr` for full details |
588 | 628 | on its use and functionality. |
… |
… |
|
597 | 637 | sage: maxima('integrate(1/(x^3*(a+b*x)^(1/3)),x)') |
598 | 638 | Traceback (most recent call last): |
599 | 639 | ... |
600 | | TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('a>0')" before integral or limit evaluation, for example): |
| 640 | TypeError: Computation failed since Maxima requested additional |
| 641 | constraints (try the command "maxima.assume('a>0')" |
| 642 | before integral or limit evaluation, for example): |
601 | 643 | Is a positive or negative? |
602 | 644 | sage: maxima.assume('a>0') |
603 | 645 | [a>0] |
… |
… |
|
606 | 648 | sage: maxima('integrate(x^n,x)') |
607 | 649 | Traceback (most recent call last): |
608 | 650 | ... |
609 | | TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('n+1>0')" before integral or limit evaluation, for example): |
| 651 | TypeError: Computation failed since Maxima requested additional |
| 652 | constraints (try the command "maxima.assume('n+1>0')" before |
| 653 | integral or limit evaluation, for example): |
610 | 654 | Is n+1 zero or nonzero? |
611 | 655 | sage: maxima.assume('n+1>0') |
612 | 656 | [n>-1] |
… |
… |
|
616 | 660 | [[a>0,n>-1]] |
617 | 661 | sage: maxima.facts() |
618 | 662 | [] |
619 | | sage: var('a') |
| 663 | sage: var('a') |
620 | 664 | a |
621 | 665 | sage: maxima('limit(x^a,x,0)') |
622 | 666 | Traceback (most recent call last): |
623 | 667 | ... |
624 | | TypeError: Computation failed since Maxima requested additional constraints (try the command "maxima.assume('a>0')" before integral or limit evaluation, for example): |
| 668 | TypeError: Computation failed since Maxima requested additional |
| 669 | constraints (try the command "maxima.assume('a>0')" before |
| 670 | integral or limit evaluation, for example): |
625 | 671 | Is a positive, negative, or zero? |
626 | 672 | """ |
627 | 673 | if expr is None: |
… |
… |
|
671 | 717 | def _eval_line(self, line, allow_use_file=False, |
672 | 718 | wait_for_prompt=True, reformat=True, error_check=True): |
673 | 719 | """ |
| 720 | Return result of line evaluation. |
| 721 | |
674 | 722 | EXAMPLES: |
675 | 723 | |
676 | 724 | We check that errors are correctly checked:: |
… |
… |
|
693 | 741 | self._synchronize() |
694 | 742 | |
695 | 743 | if len(line) > self.__eval_using_file_cutoff: |
696 | | # This implicitly uses the set method, then displays the result of the thing that was set. |
697 | | # This only works when the input line is an expression. But this is our only choice, since |
698 | | # batchmode doesn't display expressions to screen. |
| 744 | # This implicitly uses the set method, then displays |
| 745 | # the result of the thing that was set. |
| 746 | # This only works when the input line is an expression. |
| 747 | # But this is our only choice, since |
| 748 | # batchmode doesn't display expressions to screen. |
699 | 749 | a = self(line) |
700 | 750 | return repr(a) |
701 | 751 | else: |
… |
… |
|
706 | 756 | return |
707 | 757 | assert line_echo.strip() == line.strip() |
708 | 758 | |
709 | | # This broke in maxima-5.22.1 as discussed in http://trac.sagemath.org/sage_trac/ticket/10187 |
| 759 | # This broke in maxima-5.22.1 as discussed in |
| 760 | # http://trac.sagemath.org/sage_trac/ticket/10187 |
710 | 761 | #self._expect_expr(self._display_prompt) |
711 | 762 | #pre_out = self._before() |
712 | 763 | #self._expect_expr() |
… |
… |
|
731 | 782 | # i = o.rfind('(%o') |
732 | 783 | # return o[:i] |
733 | 784 | |
734 | | self._expect_expr(self._display_prompt) |
| 785 | self._expect_expr(self._display_prompt) |
735 | 786 | out = self._before() # input echo + output prompt + output |
736 | 787 | if error_check: |
737 | 788 | self._error_check(line, out) |
… |
… |
|
778 | 829 | r = randrange(2147483647) |
779 | 830 | s = marker + str(r+1) |
780 | 831 | |
781 | | # The 0; *is* necessary... it comes up in certain rare cases |
782 | | # that are revealed by extensive testing. Don't delete it. -- william stein |
| 832 | # The 0; *is* necessary... it comes up in certain rare cases |
| 833 | # that are revealed by extensive testing. |
| 834 | # Don't delete it. -- william stein |
783 | 835 | cmd = '''0;sconcat("%s",(%s+1));\n'''%(marker,r) |
784 | 836 | self._sendstr(cmd) |
785 | 837 | try: |
… |
… |
|
794 | 846 | self.quit() |
795 | 847 | |
796 | 848 | def _batch(self, s, batchload=True): |
| 849 | """ |
| 850 | Call Maxima's batch or batchload command with a file |
| 851 | containing the given string as argument. |
| 852 | |
| 853 | EXAMPLES:: |
| 854 | |
| 855 | sage: maxima._batch('10003;') |
| 856 | '...batchload...' |
| 857 | sage: maxima._batch('10003;',batchload=False) |
| 858 | '...batch...10003...' |
| 859 | """ |
797 | 860 | filename = '%s-%s'%(self._local_tmpfile(),randrange(2147483647)) |
798 | 861 | F = open(filename, 'w') |
799 | 862 | F.write(s) |
… |
… |
|
815 | 878 | self._sendline(cmd) |
816 | 879 | self._expect_expr(s) |
817 | 880 | out = self._before() |
818 | | self._error_check(str, out) |
| 881 | self._error_check(cmd, out) |
819 | 882 | os.unlink(filename) |
820 | 883 | return out |
821 | 884 | |
822 | 885 | def _quit_string(self): |
823 | 886 | """ |
| 887 | Return string representation of quit command. |
| 888 | |
824 | 889 | EXAMPLES:: |
825 | 890 | |
826 | 891 | sage: maxima._quit_string() |
… |
… |
|
830 | 895 | |
831 | 896 | def _crash_msg(self): |
832 | 897 | """ |
| 898 | Return string representation of crash message. |
| 899 | |
833 | 900 | EXAMPLES:: |
834 | 901 | |
835 | 902 | sage: maxima._crash_msg() |
… |
… |
|
837 | 904 | """ |
838 | 905 | print "Maxima crashed -- automatically restarting." |
839 | 906 | |
840 | | def _error_check(self, str, out): |
| 907 | def _error_check(self, cmd, out): |
| 908 | """ |
| 909 | Check string for errors. |
| 910 | |
| 911 | EXAMPLES:: |
| 912 | |
| 913 | sage: maxima._error_check("1+1;","Principal Value") |
| 914 | Traceback (most recent call last): |
| 915 | ... |
| 916 | TypeError: Error executing code in Maxima |
| 917 | CODE: |
| 918 | 1+1; |
| 919 | Maxima ERROR: |
| 920 | Principal Value |
| 921 | """ |
841 | 922 | r = self._error_re |
842 | 923 | m = r.search(out) |
843 | 924 | if not m is None: |
844 | | self._error_msg(str, out) |
845 | | |
846 | | def _error_msg(self, str, out): |
847 | | raise TypeError, "Error executing code in Maxima\nCODE:\n\t%s\nMaxima ERROR:\n\t%s"%(str, out.replace('-- an error. To debug this try debugmode(true);','')) |
| 925 | self._error_msg(cmd, out) |
| 926 | |
| 927 | def _error_msg(self, cmd, out): |
| 928 | """ |
| 929 | Raise error with formatted description. |
| 930 | |
| 931 | EXAMPLES:: |
| 932 | |
| 933 | sage: maxima._error_msg("1+1;","Principal Value") |
| 934 | Traceback (most recent call last): |
| 935 | ... |
| 936 | TypeError: Error executing code in Maxima |
| 937 | CODE: |
| 938 | 1+1; |
| 939 | Maxima ERROR: |
| 940 | Principal Value |
| 941 | """ |
| 942 | raise TypeError, "Error executing code in Maxima\nCODE:\n\t%s\nMaxima ERROR:\n\t%s"%(cmd, out.replace('-- an error. To debug this try debugmode(true);','')) |
848 | 943 | |
849 | 944 | ########################################### |
850 | | # Direct access to underlying lisp interpreter. |
| 945 | # Direct access to underlying lisp interpreter. |
851 | 946 | ########################################### |
852 | 947 | def lisp(self, cmd): |
853 | 948 | """ |
854 | | Send a lisp command to maxima. |
| 949 | Send a lisp command to Maxima. |
855 | 950 | |
856 | 951 | .. note:: |
857 | 952 | |
… |
… |
|
864 | 959 | 19 |
865 | 960 | ( |
866 | 961 | """ |
867 | | self._eval_line(':lisp %s\n""'%cmd, allow_use_file=False, wait_for_prompt=False, reformat=False, error_check=False) |
| 962 | self._eval_line(':lisp %s\n""'%cmd, allow_use_file=False, |
| 963 | wait_for_prompt=False, reformat=False, error_check=False) |
868 | 964 | self._expect_expr('(%i)') |
869 | 965 | return self._before() |
870 | 966 | |
… |
… |
|
878 | 974 | |
879 | 975 | INPUT: |
880 | 976 | |
| 977 | - ``var`` - string |
881 | 978 | |
882 | | - ``var`` - string |
883 | | |
884 | | - ``value`` - string |
885 | | |
| 979 | - ``value`` - string |
886 | 980 | |
887 | 981 | EXAMPLES:: |
888 | 982 | |
… |
… |
|
935 | 1029 | |
936 | 1030 | def _function_class(self): |
937 | 1031 | """ |
| 1032 | Return the Python class of Maxima functions. |
| 1033 | |
938 | 1034 | EXAMPLES:: |
939 | 1035 | |
940 | 1036 | sage: maxima._function_class() |
… |
… |
|
954 | 1050 | return MaximaElement |
955 | 1051 | |
956 | 1052 | def _function_element_class(self): |
957 | | """ |
| 1053 | """ |
| 1054 | Return the Python class of Maxima functions of elements. |
| 1055 | |
958 | 1056 | EXAMPLES:: |
959 | 1057 | |
960 | 1058 | sage: maxima._function_element_class() |
… |
… |
|
964 | 1062 | |
965 | 1063 | def _object_function_class(self): |
966 | 1064 | """ |
| 1065 | Return the Python class of Maxima user-defined functions. |
| 1066 | |
967 | 1067 | EXAMPLES:: |
968 | 1068 | |
969 | 1069 | sage: maxima._object_function_class() |
… |
… |
|
971 | 1071 | """ |
972 | 1072 | return MaximaElementFunction |
973 | 1073 | |
974 | | ##some old helper functions to wrap the calculus use of the maxima interface. |
975 | | ##these routines expect arguments living in the symbolic ring and return something |
976 | | ##that is hopefully coercible into the symbolic ring again. |
| 1074 | ## some old helper functions to wrap the calculus use |
| 1075 | ## of the Maxima interface. these routines expect arguments |
| 1076 | ## living in the symbolic ring and return something |
| 1077 | ## that is hopefully coercible into the symbolic ring again. |
977 | 1078 | ## |
978 | 1079 | ## def sr_integral(self,*args): |
979 | 1080 | ## return args[0]._maxima_().integrate(*args[1:]) |
… |
… |
|
1006 | 1107 | """ |
1007 | 1108 | return isinstance(x, MaximaElement) |
1008 | 1109 | |
1009 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 1110 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, |
| 1111 | # this should work as expected |
1010 | 1112 | class MaximaElement(MaximaAbstractElement, ExpectElement): |
| 1113 | """ |
| 1114 | Element of Maxima through Pexpect interface. |
| 1115 | |
| 1116 | EXAMPLES: |
| 1117 | |
| 1118 | Elements of this class should not be created directly. |
| 1119 | The targeted parent should be used instead:: |
| 1120 | |
| 1121 | sage: maxima(3) |
| 1122 | 3 |
| 1123 | sage: maxima(cos(x)+e^234) |
| 1124 | cos(x)+%e^234 |
| 1125 | """ |
| 1126 | |
1011 | 1127 | def __init__(self, parent, value, is_name=False, name=None): |
| 1128 | """ |
| 1129 | Create a Maxima element. |
| 1130 | See ``MaximaElement`` for full documentation. |
| 1131 | |
| 1132 | EXAMPLES:: |
| 1133 | |
| 1134 | sage: maxima(zeta(7)) |
| 1135 | zeta(7) |
| 1136 | |
| 1137 | TESTS:: |
| 1138 | |
| 1139 | sage: from sage.interfaces.maxima import MaximaElement |
| 1140 | sage: loads(dumps(MaximaElement))==MaximaElement |
| 1141 | True |
| 1142 | sage: a = maxima(5) |
| 1143 | sage: type(a) |
| 1144 | <class 'sage.interfaces.maxima.MaximaElement'> |
| 1145 | sage: loads(dumps(a))==a |
| 1146 | True |
| 1147 | """ |
1012 | 1148 | ExpectElement.__init__(self, parent, value, is_name=False, name=None) |
1013 | 1149 | |
1014 | 1150 | def display2d(self, onscreen=True): |
1015 | 1151 | """ |
| 1152 | Return the 2d string representation of this Maxima object. |
| 1153 | |
1016 | 1154 | EXAMPLES:: |
1017 | 1155 | |
1018 | | sage: F = maxima('x^5 - y^5').factor() |
1019 | | sage: F.display2d () |
| 1156 | sage: F = maxima('x^5 - y^5').factor() |
| 1157 | sage: F.display2d() |
1020 | 1158 | 4 3 2 2 3 4 |
1021 | 1159 | - (y - x) (y + x y + x y + x y + x ) |
1022 | 1160 | """ |
… |
… |
|
1026 | 1164 | P._eval_line('display2d : true$') |
1027 | 1165 | s = P._eval_line('disp(%s)$'%self.name(), reformat=False) |
1028 | 1166 | P._eval_line('display2d : false$') |
1029 | | |
1030 | 1167 | s = s.strip('\r\n') |
1031 | 1168 | |
1032 | 1169 | # if ever want to dedent, see |
… |
… |
|
1037 | 1174 | return s |
1038 | 1175 | |
1039 | 1176 | |
1040 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 1177 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, |
| 1178 | # this should work as expected |
1041 | 1179 | class MaximaFunctionElement(MaximaAbstractFunctionElement, FunctionElement): |
1042 | 1180 | pass |
1043 | 1181 | # def __init__(self, obj, name): |
… |
… |
|
1045 | 1183 | # FunctionElement.__init__(self, obj, name) |
1046 | 1184 | |
1047 | 1185 | |
1048 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 1186 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, |
| 1187 | # this should work as expected |
1049 | 1188 | class MaximaFunction(MaximaAbstractFunction, ExpectFunction): |
1050 | 1189 | pass |
1051 | 1190 | # def __init__(self, parent, name): |
… |
… |
|
1053 | 1192 | # ExpectFunction.__init__(self, parent, name) |
1054 | 1193 | |
1055 | 1194 | |
1056 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 1195 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, |
| 1196 | # this should work as expected |
1057 | 1197 | class MaximaElementFunction(MaximaElement, MaximaAbstractElementFunction): |
| 1198 | """ |
| 1199 | Maxima user-defined functions. |
| 1200 | |
| 1201 | EXAMPLES: |
| 1202 | |
| 1203 | Elements of this class should not be created directly. |
| 1204 | The method ``function`` of the targeted parent should be used instead:: |
| 1205 | |
| 1206 | sage: maxima.function('x,y','h(x)*y') |
| 1207 | h(x)*y |
| 1208 | """ |
| 1209 | |
1058 | 1210 | def __init__(self, parent, name, defn, args, latex): |
| 1211 | """ |
| 1212 | Create a Maxima function. |
| 1213 | See ``MaximaElementFunction`` for full documentation. |
| 1214 | |
| 1215 | EXAMPLES:: |
| 1216 | |
| 1217 | sage: maxima.function('x,y','cos(x)+y') |
| 1218 | cos(x)+y |
| 1219 | |
| 1220 | TESTS:: |
| 1221 | |
| 1222 | sage: f = maxima.function('x,y','x+y^9') |
| 1223 | sage: f == loads(dumps(f)) |
| 1224 | True |
| 1225 | |
| 1226 | Unpickling a Maxima Pexpect interface gives the default interface:: |
| 1227 | |
| 1228 | sage: m = Maxima() |
| 1229 | sage: g = m.function('x,y','x+y^9') |
| 1230 | sage: h = loads(dumps(g)) |
| 1231 | sage: g.parent() == h.parent() |
| 1232 | False |
| 1233 | """ |
1059 | 1234 | MaximaElement.__init__(self, parent, name, is_name=True) |
1060 | | MaximaAbstractElementFunction.__init__(self, parent, name, defn, args, latex) |
| 1235 | MaximaAbstractElementFunction.__init__(self, parent, |
| 1236 | name, defn, args, latex) |
1061 | 1237 | |
1062 | 1238 | |
1063 | 1239 | # An instance |
1064 | | maxima = Maxima(init_code = ['display2d : false; domain : complex; keepfloat : true'], |
| 1240 | maxima = Maxima(init_code = ['display2d : false', |
| 1241 | 'domain : complex', 'keepfloat : true'], |
1065 | 1242 | script_subdirectory=None) |
1066 | 1243 | |
1067 | 1244 | |
1068 | | def reduce_load_Maxima(): |
| 1245 | def reduce_load_Maxima(): #(init_code=None): |
1069 | 1246 | """ |
| 1247 | Unpickle a Maxima Pexpect interface. |
| 1248 | |
1070 | 1249 | EXAMPLES:: |
1071 | 1250 | |
1072 | 1251 | sage: from sage.interfaces.maxima import reduce_load_Maxima |
1073 | 1252 | sage: reduce_load_Maxima() |
1074 | 1253 | Maxima |
1075 | 1254 | """ |
1076 | | return maxima |
| 1255 | return maxima #Maxima(init_code=init_code) |
1077 | 1256 | |
| 1257 | # This is defined for compatibility with the old Maxima interface. |
1078 | 1258 | def reduce_load_Maxima_function(parent, defn, args, latex): |
| 1259 | """ |
| 1260 | Unpickle a Maxima function. |
| 1261 | |
| 1262 | EXAMPLES:: |
| 1263 | |
| 1264 | sage: from sage.interfaces.maxima import reduce_load_Maxima_function |
| 1265 | sage: f = maxima.function('x,y','sin(x+y)') |
| 1266 | sage: _,args = f.__reduce__() |
| 1267 | sage: g = reduce_load_Maxima_function(*args) |
| 1268 | sage: g == f |
| 1269 | True |
| 1270 | """ |
1079 | 1271 | return parent.function(args, defn, defn, latex) |
1080 | 1272 | |
1081 | 1273 | def __doctest_cleanup(): |
| 1274 | """ |
| 1275 | Kill all Pexpect interfaces. |
| 1276 | |
| 1277 | EXAMPLES:: |
| 1278 | |
| 1279 | sage: from sage.interfaces.maxima import __doctest_cleanup |
| 1280 | sage: maxima(1) |
| 1281 | 1 |
| 1282 | sage: maxima.is_running() |
| 1283 | True |
| 1284 | sage: __doctest_cleanup() |
| 1285 | sage: maxima.is_running() |
| 1286 | False |
| 1287 | """ |
1082 | 1288 | import sage.interfaces.quit |
1083 | 1289 | sage.interfaces.quit.expect_quitall() |
diff -r c86e54a6bef8 -r c45d947b5179 sage/interfaces/maxima_abstract.py
a
|
b
|
|
1 | 1 | r""" |
2 | | Interface to Maxima |
| 2 | Abstract interface to Maxima |
3 | 3 | |
4 | 4 | Maxima is a free GPL'd general purpose computer algebra system |
5 | 5 | whose development started in 1968 at MIT. It contains symbolic |
… |
… |
|
26 | 26 | - William Stein (2006-02-24): *greatly* improved robustness by adding |
27 | 27 | sequence numbers to IO bracketing in _eval_line |
28 | 28 | |
29 | | If the string "error" (case insensitive) occurs in the output of |
30 | | anything from Maxima, a RuntimeError exception is raised. |
| 29 | - Robert Bradshaw, Nils Bruin, Jean-Pierre Flori (2010,2011): Binary library |
| 30 | interface |
| 31 | |
| 32 | This is an abstract class implementing the functions shared between the Pexpect |
| 33 | and library interfaces to Maxima. |
31 | 34 | """ |
32 | 35 | |
33 | 36 | #***************************************************************************** |
… |
… |
|
57 | 60 | ##import sage.rings.all |
58 | 61 | import sage.rings.complex_number |
59 | 62 | |
60 | | from interface import Interface, InterfaceElement, InterfaceFunctionElement, InterfaceFunction, AsciiArtString |
| 63 | from interface import (Interface, InterfaceElement, InterfaceFunctionElement, |
| 64 | InterfaceFunction, AsciiArtString) |
61 | 65 | |
62 | 66 | # The Maxima "apropos" command, e.g., apropos(det) gives a list |
63 | 67 | # of all identifiers that begin in a certain way. This could |
… |
… |
|
65 | 69 | # documentation from the system -- this could also be useful. |
66 | 70 | |
67 | 71 | class MaximaAbstract(Interface): |
| 72 | r""" |
| 73 | Abstract interface to Maxima. |
| 74 | |
| 75 | INPUT: |
| 76 | |
| 77 | - ``name`` - string |
| 78 | |
| 79 | OUTPUT: the interface |
| 80 | |
| 81 | EXAMPLES: |
| 82 | |
| 83 | This class should not be instantiated directly, |
| 84 | but through its subclasses Maxima (Pexpect interface) |
| 85 | or MaximaLib (library interface):: |
| 86 | |
| 87 | sage: m = Maxima() |
| 88 | sage: from sage.interfaces.maxima_abstract import MaximaAbstract |
| 89 | sage: isinstance(m,MaximaAbstract) |
| 90 | True |
68 | 91 | """ |
69 | | Interface to the Maxima interpreter. |
70 | | """ |
71 | | def __init__(self, name): |
72 | | """ |
| 92 | |
| 93 | def __init__(self, name='maxima_abstract'): |
| 94 | r""" |
73 | 95 | Create an instance of an abstract interface to Maxima. |
| 96 | See ``MaximaAbstract`` for full documentation. |
| 97 | |
| 98 | EXAMPLES:: |
| 99 | |
| 100 | sage: from sage.interfaces.maxima_abstract import MaximaAbstract |
| 101 | sage: isinstance(maxima,MaximaAbstract) |
| 102 | True |
| 103 | |
| 104 | TESTS:: |
| 105 | |
| 106 | sage: from sage.interfaces.maxima_abstract import MaximaAbstract |
| 107 | sage: loads(dumps(MaximaAbstract)) == MaximaAbstract |
| 108 | True |
74 | 109 | """ |
75 | 110 | Interface.__init__(self,name) |
76 | 111 | |
… |
… |
|
78 | 113 | # System -- change directory, etc |
79 | 114 | ########################################### |
80 | 115 | def chdir(self, dir): |
81 | | """ |
| 116 | r""" |
82 | 117 | Change Maxima's current working directory. |
83 | 118 | |
| 119 | INPUT: |
| 120 | |
| 121 | - ``dir`` - string |
| 122 | |
| 123 | OUTPUT: none |
| 124 | |
84 | 125 | EXAMPLES:: |
85 | 126 | |
86 | 127 | sage: maxima.chdir('/') |
… |
… |
|
91 | 132 | # Interactive help |
92 | 133 | ########################################### |
93 | 134 | def _command_runner(self, command, s, redirect=True): |
94 | | """ |
| 135 | r""" |
95 | 136 | Run ``command`` in a new Maxima session and return its |
96 | 137 | output as an ``AsciiArtString``. |
| 138 | |
| 139 | INPUT: |
97 | 140 | |
98 | | If redirect is set to False, then the output of the command is not |
99 | | returned as a string. Instead, it behaves like os.system. This is |
100 | | used for interactive things like Maxima's demos. See maxima.demo? |
| 141 | - ``command`` - string; function to call |
| 142 | |
| 143 | - ``s`` - string; argument to the function |
| 144 | |
| 145 | - ``redirect`` - boolean (default: True); if redirect is set to False, |
| 146 | then the output of the command is not returned as a string. |
| 147 | Instead, it behaves like os.system. This is used for interactive |
| 148 | things like Maxima's demos. See maxima.demo? |
| 149 | |
| 150 | OUTPUT: |
| 151 | |
| 152 | Output of ``command(s)`` as an ``AsciiArtString`` if ``redirect`` is set |
| 153 | to False; None otherwise. |
101 | 154 | |
102 | 155 | EXAMPLES:: |
103 | 156 | |
… |
… |
|
125 | 178 | subprocess.Popen(cmd, shell=True) |
126 | 179 | |
127 | 180 | def help(self, s): |
128 | | """ |
| 181 | r""" |
| 182 | Return Maxima's help for ``s``. |
| 183 | |
| 184 | INPUT: |
| 185 | |
| 186 | - ``s`` - string |
| 187 | |
| 188 | OUTPUT: |
| 189 | |
| 190 | Maxima's help for ``s`` |
| 191 | |
129 | 192 | EXAMPLES:: |
130 | 193 | |
131 | 194 | sage: maxima.help('gcd') |
132 | 195 | -- Function: gcd (<p_1>, <p_2>, <x_1>, ...) |
133 | 196 | ... |
134 | 197 | """ |
| 198 | # Should this be implemented without launching a new Maxima session |
| 199 | # i.e. using eval_line ? |
135 | 200 | return self._command_runner("describe", s) |
136 | 201 | |
137 | 202 | def example(self, s): |
138 | | """ |
| 203 | r""" |
| 204 | Return Maxima's examples for ``s``. |
| 205 | |
| 206 | INPUT: |
| 207 | |
| 208 | - ``s`` - string |
| 209 | |
| 210 | OUTPUT: |
| 211 | |
| 212 | Maxima's examples for ``s`` |
| 213 | |
139 | 214 | EXAMPLES:: |
140 | 215 | |
141 | 216 | sage: maxima.example('arrays') |
… |
… |
|
152 | 227 | 24 |
153 | 228 | done |
154 | 229 | """ |
| 230 | # Should this be implemented without launching a new Maxima session |
| 231 | # i.e. using eval_line ? |
155 | 232 | return self._command_runner("example", s) |
156 | 233 | |
157 | 234 | describe = help |
158 | 235 | |
159 | 236 | def demo(self, s): |
160 | | """ |
| 237 | r""" |
| 238 | Run Maxima's demo for ``s``. |
| 239 | |
| 240 | INPUT: |
| 241 | |
| 242 | - ``s`` - string |
| 243 | |
| 244 | OUTPUT: none |
| 245 | |
161 | 246 | EXAMPLES:: |
162 | 247 | |
163 | 248 | sage: maxima.demo('array') # not tested |
… |
… |
|
166 | 251 | At the _ prompt, type ';' followed by enter to get next demo |
167 | 252 | subscrmap : true _ |
168 | 253 | """ |
| 254 | # Should this be implemented without launching a new Maxima session |
| 255 | # i.e. using eval_line ? |
169 | 256 | return self._command_runner("demo", s, redirect=False) |
170 | 257 | |
171 | 258 | def completions(self, s, verbose=True): |
172 | | """ |
| 259 | r""" |
173 | 260 | Return all commands that complete the command starting with the |
174 | | string s. This is like typing s[tab] in the Maxima interpreter. |
| 261 | string ``s``. This is like typing s[tab] in the Maxima interpreter. |
| 262 | |
| 263 | INPUT: |
| 264 | |
| 265 | - ``s`` - string |
| 266 | |
| 267 | - ``verbose`` - boolean (default: True) |
| 268 | |
| 269 | OUTPUT: array of strings |
175 | 270 | |
176 | 271 | EXAMPLES:: |
177 | 272 | |
… |
… |
|
191 | 286 | def _commands(self, verbose=True): |
192 | 287 | """ |
193 | 288 | Return list of all commands defined in Maxima. |
| 289 | |
| 290 | INPUT: |
| 291 | |
| 292 | - ``verbose`` - boolean (default: True) |
| 293 | |
| 294 | OUTPUT: array of strings |
194 | 295 | |
195 | 296 | EXAMPLES:: |
196 | 297 | |
| 298 | # The output is kind of random |
197 | 299 | sage: sorted(maxima._commands(verbose=False)) |
198 | | ['Alpha', |
199 | | 'Beta', |
| 300 | [... |
| 301 | 'display', |
200 | 302 | ... |
201 | | 'zunderflow'] |
| 303 | 'gcd', |
| 304 | ... |
| 305 | 'verbose', |
| 306 | ...] |
202 | 307 | """ |
203 | 308 | try: |
204 | 309 | return self.__commands |
… |
… |
|
210 | 315 | return self.__commands |
211 | 316 | |
212 | 317 | def trait_names(self, verbose=True, use_disk_cache=True): |
213 | | """ |
| 318 | r""" |
214 | 319 | Return all Maxima commands, which is useful for tab completion. |
| 320 | |
| 321 | INPUT: |
| 322 | |
| 323 | - ``verbose`` - boolean (default: True) |
| 324 | |
| 325 | - ``use_disk_cache`` - boolean (default: True); if set to True, |
| 326 | try to read cached result from disk |
| 327 | |
| 328 | OUTPUT: array of strings |
215 | 329 | |
216 | 330 | EXAMPLES:: |
217 | 331 | |
… |
… |
|
247 | 361 | Start the interactive Maxima console. This is a completely separate |
248 | 362 | maxima session from this interface. To interact with this session, |
249 | 363 | you should instead use ``maxima.interact()``. |
| 364 | |
| 365 | INPUT: none |
250 | 366 | |
| 367 | OUTPUT: none |
| 368 | |
251 | 369 | EXAMPLES:: |
252 | 370 | |
253 | 371 | sage: maxima.console() # not tested (since we can't) |
… |
… |
|
273 | 391 | def cputime(self, t=None): |
274 | 392 | r""" |
275 | 393 | Returns the amount of CPU time that this Maxima session has used. |
276 | | If \var{t} is not None, then it returns the difference between |
277 | | the current CPU time and \var{t}. |
| 394 | |
| 395 | INPUT: |
| 396 | |
| 397 | - ``t`` - float (default: None); If \var{t} is not None, then |
| 398 | it returns the difference between the current CPU time and \var{t}. |
| 399 | |
| 400 | OUTPUT: float |
278 | 401 | |
279 | 402 | EXAMPLES: |
280 | 403 | sage: t = maxima.cputime() |
… |
… |
|
288 | 411 | return float(self.eval('elapsed_run_time()')) |
289 | 412 | |
290 | 413 | def version(self): |
291 | | """ |
| 414 | r""" |
292 | 415 | Return the version of Maxima that Sage includes. |
| 416 | |
| 417 | INPUT: none |
| 418 | |
| 419 | OUTPUT: none |
293 | 420 | |
294 | 421 | EXAMPLES:: |
295 | 422 | |
… |
… |
|
303 | 430 | ### |
304 | 431 | |
305 | 432 | def _assign_symbol(self): |
| 433 | r""" |
| 434 | Return the assign symbol in Maxima. |
| 435 | |
| 436 | INPUT: none |
| 437 | |
| 438 | OUTPUT: string |
| 439 | |
| 440 | EXAMPLES:: |
| 441 | |
| 442 | sage: maxima._assign_symbol() |
| 443 | ':' |
| 444 | sage: maxima.eval('t : 8') |
| 445 | '8' |
| 446 | sage: maxima.eval('t') |
| 447 | '8' |
| 448 | """ |
306 | 449 | return ":" |
307 | 450 | |
308 | 451 | def _true_symbol(self): |
309 | 452 | """ |
310 | 453 | Return the true symbol in Maxima. |
| 454 | |
| 455 | INPUT: none |
| 456 | |
| 457 | OUTPUT: string |
311 | 458 | |
312 | 459 | EXAMPLES:: |
313 | 460 | |
… |
… |
|
321 | 468 | def _false_symbol(self): |
322 | 469 | """ |
323 | 470 | Return the false symbol in Maxima. |
| 471 | |
| 472 | INPUT: none |
| 473 | |
| 474 | OUTPUT: string |
324 | 475 | |
325 | 476 | EXAMPLES:: |
326 | 477 | |
… |
… |
|
335 | 486 | """ |
336 | 487 | Returns the equality symbol in Maxima. |
337 | 488 | |
| 489 | INPUT: none |
| 490 | |
| 491 | OUTPUT: string |
| 492 | |
338 | 493 | EXAMPLES:: |
339 | 494 | |
340 | 495 | sage: maxima._equality_symbol() |
341 | 496 | '=' |
| 497 | sage: var('x y') |
| 498 | (x, y) |
| 499 | sage: maxima(x == y) |
| 500 | x=y |
342 | 501 | """ |
343 | 502 | return '=' |
344 | 503 | |
345 | 504 | def _inequality_symbol(self): |
346 | 505 | """ |
347 | | Returns the equality symbol in Maxima. |
| 506 | Returns the inequality symbol in Maxima. |
| 507 | |
| 508 | INPUT: none |
| 509 | |
| 510 | OUTPUT: string |
348 | 511 | |
349 | 512 | EXAMPLES:: |
350 | 513 | |
… |
… |
|
357 | 520 | |
358 | 521 | def _function_class(self): |
359 | 522 | """ |
| 523 | Return the Python class of Maxima functions. |
| 524 | |
| 525 | INPUT: none |
| 526 | |
| 527 | OUTPUT: type |
| 528 | |
360 | 529 | EXAMPLES:: |
361 | 530 | |
362 | 531 | sage: maxima._function_class() |
… |
… |
|
367 | 536 | def _object_class(self): |
368 | 537 | """ |
369 | 538 | Return the Python class of Maxima elements. |
| 539 | |
| 540 | INPUT: none |
| 541 | |
| 542 | OUTPUT: type |
370 | 543 | |
371 | 544 | EXAMPLES:: |
372 | 545 | |
… |
… |
|
376 | 549 | return MaximaAbstractElement |
377 | 550 | |
378 | 551 | def _function_element_class(self): |
379 | | """ |
| 552 | """ |
| 553 | Return the Python class of Maxima functions of elements. |
| 554 | |
| 555 | INPUT: none |
| 556 | |
| 557 | OUTPUT: type |
| 558 | |
380 | 559 | EXAMPLES:: |
381 | 560 | |
382 | 561 | sage: maxima._function_element_class() |
… |
… |
|
386 | 565 | |
387 | 566 | def _object_function_class(self): |
388 | 567 | """ |
| 568 | Return the Python class of Maxima user-defined functions. |
| 569 | |
| 570 | INPUT: none |
| 571 | |
| 572 | OUTPUT: type |
| 573 | |
389 | 574 | EXAMPLES:: |
390 | 575 | |
391 | 576 | sage: maxima._object_function_class() |
… |
… |
|
393 | 578 | """ |
394 | 579 | return MaximaAbstractElementFunction |
395 | 580 | |
396 | | #### |
397 | | # |
398 | | #### |
| 581 | #################### |
| 582 | # Maxima functions # |
| 583 | #################### |
399 | 584 | |
400 | 585 | def function(self, args, defn, rep=None, latex=None): |
401 | 586 | """ |
… |
… |
|
403 | 588 | |
404 | 589 | INPUT: |
405 | 590 | |
406 | | |
407 | | - ``args`` - a string with variable names separated by |
| 591 | - ``args`` - a string with variable names separated by |
408 | 592 | commas |
409 | 593 | |
410 | | - ``defn`` - a string (or Maxima expression) that |
| 594 | - ``defn`` - a string (or Maxima expression) that |
411 | 595 | defines a function of the arguments in Maxima. |
412 | 596 | |
413 | | - ``rep`` - an optional string; if given, this is how |
| 597 | - ``rep`` - an optional string; if given, this is how |
414 | 598 | the function will print. |
415 | 599 | |
| 600 | OUTPUT: Maxima function |
416 | 601 | |
417 | 602 | EXAMPLES:: |
418 | 603 | |
… |
… |
|
433 | 618 | sage: g(1,2,3) |
434 | 619 | 3*(cos(2)+sin(1)) |
435 | 620 | |
436 | | The function definition can be a maxima object:: |
| 621 | The function definition can be a Maxima object:: |
437 | 622 | |
438 | 623 | sage: an_expr = maxima('sin(x)*gamma(x)') |
439 | 624 | sage: t = maxima.function('x', an_expr) |
… |
… |
|
462 | 647 | f = self._object_function_class()(self, name, rep, args, latex) |
463 | 648 | return f |
464 | 649 | |
465 | | ##### |
466 | | # Maxima functions |
467 | | ##### |
468 | | |
469 | 650 | ## def display2d(self, flag=True): |
470 | 651 | ## """ |
471 | 652 | ## Set the flag that determines whether Maxima objects are |
… |
… |
|
496 | 677 | |
497 | 678 | INPUT: |
498 | 679 | |
499 | | |
500 | | - ``f`` - a string representing a function (such as |
| 680 | - ``f`` - a string representing a function (such as |
501 | 681 | f="sin(x)") [var, xmin, xmax] |
502 | 682 | |
503 | | - ``options`` - an optional string representing plot2d |
| 683 | - ``options`` - an optional string representing plot2d |
504 | 684 | options in gnuplot format |
505 | 685 | |
506 | | |
507 | 686 | EXAMPLES:: |
508 | 687 | |
509 | 688 | sage: maxima.plot2d('sin(x)','[x,-5,5]') # not tested |
… |
… |
|
516 | 695 | |
517 | 696 | def plot2d_parametric(self, r, var, trange, nticks=50, options=None): |
518 | 697 | r""" |
519 | | Plots r = [x(t), y(t)] for t = tmin...tmax using gnuplot with |
520 | | options |
| 698 | Plot r = [x(t), y(t)] for t = tmin...tmax using gnuplot with |
| 699 | options. |
521 | 700 | |
522 | 701 | INPUT: |
523 | | |
524 | | |
525 | | - ``r`` - a string representing a function (such as |
| 702 | |
| 703 | - ``r`` - a string representing a function (such as |
526 | 704 | r="[x(t),y(t)]") |
527 | 705 | |
528 | | - ``var`` - a string representing the variable (such |
| 706 | - ``var`` - a string representing the variable (such |
529 | 707 | as var = "t") |
530 | 708 | |
531 | | - ``trange`` - [tmin, tmax] are numbers with tmintmax |
| 709 | - ``trange`` - [tmin, tmax] are numbers with tmintmax |
532 | 710 | |
533 | | - ``nticks`` - int (default: 50) |
| 711 | - ``nticks`` - int (default: 50) |
534 | 712 | |
535 | | - ``options`` - an optional string representing plot2d |
| 713 | - ``options`` - an optional string representing plot2d |
536 | 714 | options in gnuplot format |
537 | 715 | |
538 | | |
539 | 716 | EXAMPLES:: |
540 | 717 | |
541 | 718 | sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t",[-3.1,3.1]) # not tested |
… |
… |
|
570 | 747 | |
571 | 748 | INPUT: |
572 | 749 | |
573 | | |
574 | | - ``f`` - a string representing a function (such as |
| 750 | - ``f`` - a string representing a function (such as |
575 | 751 | f="sin(x)") [var, min, max] |
576 | 752 | |
577 | | |
| 753 | - ``args`` should be of the form '[x, xmin, xmax]', '[y, ymin, ymax]', |
| 754 | '[grid, nx, ny]', options |
| 755 | |
578 | 756 | EXAMPLES:: |
579 | 757 | |
580 | 758 | sage: maxima.plot3d('1 + x^3 - y^2', '[x,-2,2]', '[y,-2,2]', '[grid,12,12]') # not tested |
… |
… |
|
594 | 772 | |
595 | 773 | INPUT: |
596 | 774 | |
597 | | |
598 | | - ``x, y, z`` - a string representing a function (such |
| 775 | - ``x, y, z`` - a string representing a function (such |
599 | 776 | as ``x="u2+v2"``, ...) vars is a list or two strings |
600 | 777 | representing variables (such as vars = ["u","v"]) |
601 | 778 | |
602 | | - ``urange`` - [umin, umax] |
| 779 | - ``urange`` - [umin, umax] |
603 | 780 | |
604 | | - ``vrange`` - [vmin, vmax] are lists of numbers with |
| 781 | - ``vrange`` - [vmin, vmax] are lists of numbers with |
605 | 782 | umin umax, vmin vmax |
606 | 783 | |
607 | | - ``options`` - optional string representing plot2d |
| 784 | - ``options`` - optional string representing plot2d |
608 | 785 | options in gnuplot format |
609 | 786 | |
610 | | |
611 | 787 | OUTPUT: displays a plot on screen or saves to a file |
612 | 788 | |
613 | 789 | EXAMPLES:: |
… |
… |
|
649 | 825 | |
650 | 826 | INPUT: |
651 | 827 | |
652 | | |
653 | 828 | - ``de`` - a string representing the ODE |
654 | 829 | |
655 | | - ``vars`` - a list of strings representing the two |
| 830 | - ``vars`` - a list of strings representing the two |
656 | 831 | variables. |
657 | 832 | |
658 | | - ``ics`` - a triple of numbers [a,b1,b2] representing |
| 833 | - ``ics`` - a triple of numbers [a,b1,b2] representing |
659 | 834 | y(a)=b1, y'(a)=b2 |
660 | 835 | |
661 | | |
662 | 836 | EXAMPLES:: |
663 | 837 | |
664 | 838 | sage: maxima.de_solve('diff(y,x,2) + 3*x = y', ['x','y'], [1,1,1]) |
… |
… |
|
692 | 866 | |
693 | 867 | INPUT: |
694 | 868 | |
695 | | |
696 | | - ``de`` - a string representing the ODE (e.g., de = |
| 869 | - ``de`` - a string representing the ODE (e.g., de = |
697 | 870 | "diff(f(x),x,2)=diff(f(x),x)+sin(x)") |
698 | 871 | |
699 | | - ``vars`` - a list of strings representing the |
| 872 | - ``vars`` - a list of strings representing the |
700 | 873 | variables (e.g., vars = ["x","f"]) |
701 | 874 | |
702 | | - ``ics`` - a list of numbers representing initial |
| 875 | - ``ics`` - a list of numbers representing initial |
703 | 876 | conditions, with symbols allowed which are represented by strings |
704 | 877 | (eg, f(0)=1, f'(0)=2 is ics = [0,1,2]) |
705 | 878 | |
706 | | |
707 | 879 | EXAMPLES:: |
708 | 880 | |
709 | 881 | sage: maxima.clear('x'); maxima.clear('f') |
… |
… |
|
712 | 884 | |
713 | 885 | :: |
714 | 886 | |
715 | | sage: maxima.clear('x'); maxima.clear('f') |
| 887 | sage: maxima.clear('x'); maxima.clear('f') |
716 | 888 | sage: f = maxima.de_solve_laplace("diff(f(x),x,2) = 2*diff(f(x),x)-f(x)", ["x","f"]) |
717 | 889 | sage: f |
718 | 890 | f(x)=x*%e^x*('at('diff(f(x),x,1),x=0))-f(0)*x*%e^x+f(0)*%e^x |
… |
… |
|
742 | 914 | """ |
743 | 915 | Wraps maxima's linsolve. |
744 | 916 | |
745 | | INPUT: eqns is a list of m strings, each representing a linear |
746 | | question in m = n variables vars is a list of n strings, each |
747 | | representing a variable |
| 917 | INPUT: |
| 918 | |
| 919 | - ``eqns`` - a list of m strings; each representing a linear |
| 920 | question in m = n variables |
| 921 | |
| 922 | - ``vars`` - a list of n strings; each |
| 923 | representing a variable |
748 | 924 | |
749 | 925 | EXAMPLES:: |
750 | 926 | |
751 | | sage: eqns = ["x + z = y","2*a*x - y = 2*a^2","y - 2*z = 2"] |
752 | | sage: vars = ["x","y","z"] |
| 927 | sage: eqns = ["x + z = y","2*a*x - y = 2*a^2","y - 2*z = 2"] |
| 928 | sage: vars = ["x","y","z"] |
753 | 929 | sage: maxima.solve_linear(eqns, vars) |
754 | 930 | [x=a+1,y=2*a,z=a-1] |
755 | 931 | """ |
… |
… |
|
770 | 946 | def unit_quadratic_integer(self, n): |
771 | 947 | r""" |
772 | 948 | Finds a unit of the ring of integers of the quadratic number field |
773 | | `\QQ(\sqrt{n})`, `n>1`, using the qunit maxima |
774 | | command. |
| 949 | `\QQ(\sqrt{n})`, `n>1`, using the qunit maxima command. |
| 950 | |
| 951 | INPUT: |
| 952 | |
| 953 | - ``n`` - an integer |
775 | 954 | |
776 | 955 | EXAMPLES:: |
777 | 956 | |
778 | | sage: u = maxima.unit_quadratic_integer(101); u |
| 957 | sage: u = maxima.unit_quadratic_integer(101); u |
779 | 958 | a + 10 |
780 | | sage: u.parent() |
| 959 | sage: u.parent() |
781 | 960 | Number Field in a with defining polynomial x^2 - 101 |
782 | | sage: u = maxima.unit_quadratic_integer(13) |
783 | | sage: u |
| 961 | sage: u = maxima.unit_quadratic_integer(13) |
| 962 | sage: u |
784 | 963 | 5*a + 18 |
785 | | sage: u.parent() |
| 964 | sage: u.parent() |
786 | 965 | Number Field in a with defining polynomial x^2 - 13 |
787 | 966 | """ |
788 | 967 | from sage.rings.all import QuadraticField, Integer |
789 | | # Take square-free part so sqrt(n) doesn't get simplified further by maxima |
| 968 | # Take square-free part so sqrt(n) doesn't get simplified |
| 969 | # further by maxima |
790 | 970 | # (The original version of this function would yield wrong answers if |
791 | 971 | # n is not squarefree.) |
792 | | n = Integer(n).squarefree_part() |
| 972 | n = Integer(n).squarefree_part() |
793 | 973 | if n < 1: |
794 | 974 | raise ValueError, "n (=%s) must be >= 1"%n |
795 | 975 | s = repr(self('qunit(%s)'%n)).lower() |
… |
… |
|
804 | 984 | |
805 | 985 | INPUT: |
806 | 986 | |
807 | | |
808 | | - ``ptsx`` - [x1,...,xn], where the xi and yi are |
| 987 | - ``ptsx`` - [x1,...,xn], where the xi and yi are |
809 | 988 | real, |
810 | 989 | |
811 | | - ``ptsy`` - [y1,...,yn] |
| 990 | - ``ptsy`` - [y1,...,yn] |
812 | 991 | |
813 | | - ``options`` - a string representing maxima plot2d |
| 992 | - ``options`` - a string representing maxima plot2d |
814 | 993 | options. |
815 | 994 | |
816 | | |
817 | 995 | The points are (x1,y1), (x2,y2), etc. |
818 | 996 | |
819 | 997 | This function requires maxima 5.9.2 or newer. |
… |
… |
|
845 | 1023 | where each ptsi is of the form [[x1,y1],...,[xn,yn]] x's must be |
846 | 1024 | integers and y's reals options is a string representing maxima |
847 | 1025 | plot2d options. |
| 1026 | |
| 1027 | INPUT: |
| 1028 | |
| 1029 | - ``pts_lst`` - list of points; each point must be of the form [x,y] |
| 1030 | where ``x`` is an integer and ``y`` is a real |
| 1031 | |
| 1032 | - ``var`` - string; representing Maxima's plot2d options |
848 | 1033 | |
849 | 1034 | Requires maxima 5.9.2 at least. |
850 | 1035 | |
… |
… |
|
865 | 1050 | sage: zeta_ptsx1 = [ (pari(1/2+i*I/10).zeta().real()).precision(1) for i in range (10,150)] |
866 | 1051 | sage: zeta_ptsy1 = [ (pari(1/2+i*I/10).zeta().imag()).precision(1) for i in range (10,150)] |
867 | 1052 | sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]]) # not tested |
868 | | sage: opts='[gnuplot_preamble, "set nokey"]' |
| 1053 | sage: opts='[gnuplot_preamble, "set nokey"]' |
869 | 1054 | sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]],opts) # not tested |
870 | 1055 | """ |
871 | 1056 | n = len(pts_list) |
… |
… |
|
880 | 1065 | self('plot2d('+cmd+')') |
881 | 1066 | else: |
882 | 1067 | self('plot2d('+cmd+','+options+')') |
883 | | |
| 1068 | |
884 | 1069 | |
885 | 1070 | class MaximaAbstractElement(InterfaceElement): |
| 1071 | r""" |
| 1072 | Element of Maxima through an abstract interface. |
| 1073 | |
| 1074 | EXAMPLES: |
| 1075 | |
| 1076 | Elements of this class should not be created directly. |
| 1077 | The targeted parent of a concrete inherited class should be used instead:: |
| 1078 | |
| 1079 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 1080 | sage: xp = maxima(x) |
| 1081 | sage: type(xp) |
| 1082 | <class 'sage.interfaces.maxima.MaximaElement'> |
| 1083 | sage: xl = maxima_lib(x) |
| 1084 | sage: type(xl) |
| 1085 | <class 'sage.interfaces.maxima_lib.MaximaLibElement'> |
| 1086 | """ |
| 1087 | |
886 | 1088 | def __str__(self): |
887 | 1089 | """ |
888 | | Printing an object explicitly gives ASCII art: |
| 1090 | Printing an object explicitly gives ASCII art. |
| 1091 | |
| 1092 | INPUT: none |
| 1093 | |
| 1094 | OUTPUT: string |
889 | 1095 | |
890 | 1096 | EXAMPLES:: |
891 | 1097 | |
… |
… |
|
901 | 1107 | |
902 | 1108 | def bool(self): |
903 | 1109 | """ |
| 1110 | Convert ``self`` into a boolean. |
| 1111 | |
| 1112 | INPUT: none |
| 1113 | |
| 1114 | OUTPUT: boolean |
| 1115 | |
904 | 1116 | EXAMPLES:: |
905 | 1117 | |
906 | 1118 | sage: maxima(0).bool() |
… |
… |
|
913 | 1125 | |
914 | 1126 | def __cmp__(self, other): |
915 | 1127 | """ |
| 1128 | Compare this Maxima object with ``other``. |
| 1129 | |
| 1130 | INPUT: |
| 1131 | |
| 1132 | - ``other`` - an object to compare to |
| 1133 | |
| 1134 | OUTPUT: integer |
| 1135 | |
916 | 1136 | EXAMPLES:: |
917 | 1137 | |
918 | 1138 | sage: a = maxima(1); b = maxima(2) |
… |
… |
|
935 | 1155 | """ |
936 | 1156 | |
937 | 1157 | # thanks to David Joyner for telling me about using "is". |
938 | | # but be careful, since for relations things like is(equal(a,b)) are what Maxima needs |
| 1158 | # but be careful, since for relations things like is(equal(a,b)) |
| 1159 | # are what Maxima needs |
939 | 1160 | P = self.parent() |
940 | 1161 | try: |
941 | 1162 | if P.eval("is (%s < %s)"%(self.name(), other.name())) == P._true_symbol(): |
… |
… |
|
947 | 1168 | except TypeError: |
948 | 1169 | pass |
949 | 1170 | return cmp(repr(self),repr(other)) |
950 | | # everything is supposed to be comparable in Python, so we define |
951 | | # the comparison thus when no comparable in interfaced system. |
| 1171 | # everything is supposed to be comparable in Python, |
| 1172 | # so we define the comparison thus when no comparable |
| 1173 | # in interfaced system. |
952 | 1174 | |
953 | 1175 | def _sage_(self): |
954 | 1176 | """ |
955 | | Attempt to make a native Sage object out of this maxima object. |
| 1177 | Attempt to make a native Sage object out of this Maxima object. |
956 | 1178 | This is useful for automatic coercions in addition to other |
957 | 1179 | things. |
| 1180 | |
| 1181 | INPUT: none |
| 1182 | |
| 1183 | OUTPUT: Sage object |
958 | 1184 | |
959 | 1185 | EXAMPLES:: |
960 | 1186 | |
… |
… |
|
1000 | 1226 | |
1001 | 1227 | def _symbolic_(self, R): |
1002 | 1228 | """ |
1003 | | Return a symbolic expression equivalent to this maxima object. |
| 1229 | Return a symbolic expression equivalent to this Maxima object. |
| 1230 | |
| 1231 | INPUT: |
| 1232 | |
| 1233 | - ``R`` - symbolic ring to convert into |
| 1234 | |
| 1235 | OUTPUT: symbolic expression |
1004 | 1236 | |
1005 | 1237 | EXAMPLES:: |
1006 | 1238 | |
… |
… |
|
1010 | 1242 | sage: u.parent() |
1011 | 1243 | Symbolic Ring |
1012 | 1244 | |
1013 | | This is used when converting maxima objects to the Symbolic Ring:: |
| 1245 | This is used when converting Maxima objects to the Symbolic Ring:: |
1014 | 1246 | |
1015 | 1247 | sage: SR(t) |
1016 | 1248 | sqrt(2) |
… |
… |
|
1019 | 1251 | |
1020 | 1252 | def __complex__(self): |
1021 | 1253 | """ |
| 1254 | Return a complex number equivalent to this Maxima object. |
| 1255 | |
| 1256 | INPUT: none |
| 1257 | |
| 1258 | OUTPUT: complex |
| 1259 | |
1022 | 1260 | EXAMPLES:: |
1023 | 1261 | |
1024 | 1262 | sage: complex(maxima('sqrt(-2)+1')) |
… |
… |
|
1028 | 1266 | |
1029 | 1267 | def _complex_mpfr_field_(self, C): |
1030 | 1268 | """ |
| 1269 | Return a mpfr complex number equivalent to this Maxima object. |
| 1270 | |
| 1271 | INPUT: |
| 1272 | |
| 1273 | - ``C`` - complex numbers field to convert into |
| 1274 | |
| 1275 | OUTPUT: complex |
| 1276 | |
1031 | 1277 | EXAMPLES:: |
1032 | 1278 | |
1033 | 1279 | sage: CC(maxima('1+%i')) |
… |
… |
|
1047 | 1293 | |
1048 | 1294 | def _mpfr_(self, R): |
1049 | 1295 | """ |
| 1296 | Return a mpfr real number equivalent to this Maxima object. |
| 1297 | |
| 1298 | INPUT: |
| 1299 | |
| 1300 | - ``R`` - real numbers field to convert into |
| 1301 | |
| 1302 | OUTPUT: real |
| 1303 | |
1050 | 1304 | EXAMPLES:: |
1051 | 1305 | |
1052 | 1306 | sage: RealField(100)(maxima('sqrt(2)+1')) |
… |
… |
|
1056 | 1310 | |
1057 | 1311 | def _complex_double_(self, C): |
1058 | 1312 | """ |
| 1313 | Return a double precision complex number equivalent to this Maxima object. |
| 1314 | |
| 1315 | INPUT: |
| 1316 | |
| 1317 | - ``C`` - double precision complex numbers field to convert into |
| 1318 | |
| 1319 | OUTPUT: complex |
| 1320 | |
1059 | 1321 | EXAMPLES:: |
1060 | 1322 | |
1061 | 1323 | sage: CDF(maxima('sqrt(2)+1')) |
… |
… |
|
1065 | 1327 | |
1066 | 1328 | def _real_double_(self, R): |
1067 | 1329 | """ |
| 1330 | Return a double precision real number equivalent to this Maxima object. |
| 1331 | |
| 1332 | INPUT: |
| 1333 | |
| 1334 | - ``R`` - double precision real numbers field to convert into |
| 1335 | |
| 1336 | OUTPUT: real |
| 1337 | |
1068 | 1338 | EXAMPLES:: |
1069 | 1339 | |
1070 | 1340 | sage: RDF(maxima('sqrt(2)+1')) |
… |
… |
|
1074 | 1344 | |
1075 | 1345 | def real(self): |
1076 | 1346 | """ |
1077 | | Return the real part of this maxima element. |
| 1347 | Return the real part of this Maxima element. |
1078 | 1348 | |
| 1349 | INPUT: none |
| 1350 | |
| 1351 | OUTPUT: Maxima real |
| 1352 | |
1079 | 1353 | EXAMPLES:: |
1080 | 1354 | |
1081 | 1355 | sage: maxima('2 + (2/3)*%i').real() |
… |
… |
|
1085 | 1359 | |
1086 | 1360 | def imag(self): |
1087 | 1361 | """ |
1088 | | Return the imaginary part of this maxima element. |
| 1362 | Return the imaginary part of this Maxima element. |
| 1363 | |
| 1364 | INPUT: none |
| 1365 | |
| 1366 | OUTPUT: Maxima real |
1089 | 1367 | |
1090 | 1368 | EXAMPLES:: |
1091 | 1369 | |
… |
… |
|
1098 | 1376 | """ |
1099 | 1377 | Return numerical approximation to self as a Maxima object. |
1100 | 1378 | |
| 1379 | INPUT: none |
| 1380 | |
| 1381 | OUTPUT: Maxima object |
| 1382 | |
1101 | 1383 | EXAMPLES:: |
1102 | 1384 | |
1103 | 1385 | sage: a = maxima('sqrt(2)').numer(); a |
… |
… |
|
1109 | 1391 | |
1110 | 1392 | def str(self): |
1111 | 1393 | """ |
1112 | | Return string representation of this maxima object. |
| 1394 | Return string representation of this Maxima object. |
| 1395 | |
| 1396 | INPUT: none |
| 1397 | |
| 1398 | OUTPUT: string |
1113 | 1399 | |
1114 | 1400 | EXAMPLES:: |
1115 | 1401 | |
… |
… |
|
1121 | 1407 | |
1122 | 1408 | def __repr__(self): |
1123 | 1409 | """ |
1124 | | Return print representation of this object. |
| 1410 | Return print representation of this Maxima object. |
| 1411 | |
| 1412 | INPUT: none |
| 1413 | |
| 1414 | OUTPUT: string |
| 1415 | |
| 1416 | The result is cached. |
1125 | 1417 | |
1126 | 1418 | EXAMPLES:: |
1127 | 1419 | |
… |
… |
|
1143 | 1435 | |
1144 | 1436 | INPUT: |
1145 | 1437 | |
| 1438 | - ``var`` - variable (default: 'x') |
1146 | 1439 | |
1147 | | - ``var`` - variable (default: 'x') |
1148 | | |
1149 | | - ``n`` - integer (default: 1) |
1150 | | |
| 1440 | - ``n`` - integer (default: 1) |
1151 | 1441 | |
1152 | 1442 | OUTPUT: n-th derivative of self with respect to the variable var |
1153 | 1443 | |
1154 | 1444 | EXAMPLES:: |
1155 | 1445 | |
1156 | | sage: f = maxima('x^2') |
1157 | | sage: f.diff() |
| 1446 | sage: f = maxima('x^2') |
| 1447 | sage: f.diff() |
1158 | 1448 | 2*x |
1159 | | sage: f.diff('x') |
| 1449 | sage: f.diff('x') |
1160 | 1450 | 2*x |
1161 | | sage: f.diff('x', 2) |
| 1451 | sage: f.diff('x', 2) |
1162 | 1452 | 2 |
1163 | 1453 | sage: maxima('sin(x^2)').diff('x',4) |
1164 | 1454 | 16*x^4*sin(x^2)-12*sin(x^2)-48*x^2*cos(x^2) |
1165 | 1455 | |
1166 | 1456 | :: |
1167 | 1457 | |
1168 | | sage: f = maxima('x^2 + 17*y^2') |
| 1458 | sage: f = maxima('x^2 + 17*y^2') |
1169 | 1459 | sage: f.diff('x') |
1170 | 1460 | 34*y*'diff(y,x,1)+2*x |
1171 | | sage: f.diff('y') |
| 1461 | sage: f.diff('y') |
1172 | 1462 | 34*y |
1173 | 1463 | """ |
1174 | 1464 | return InterfaceElement.__getattr__(self, 'diff')(var, n) |
… |
… |
|
1184 | 1474 | |
1185 | 1475 | INPUT: |
1186 | 1476 | |
| 1477 | - ``var`` - variable to integrate with respect to |
1187 | 1478 | |
1188 | | - ``var`` - variable to integrate with respect to |
| 1479 | - ``a`` - lower endpoint of integration |
1189 | 1480 | |
1190 | | - ``a`` - lower endpoint of integration |
| 1481 | - ``b`` - upper endpoint of integration |
1191 | 1482 | |
1192 | | - ``b`` - upper endpoint of integration |
1193 | | |
1194 | | - ``desired_relative_error`` - (default: '1e-8') the |
| 1483 | - ``desired_relative_error`` - (default: '1e-8') the |
1195 | 1484 | desired relative error |
1196 | 1485 | |
1197 | | - ``maximum_num_subintervals`` - (default: 200) |
| 1486 | - ``maximum_num_subintervals`` - (default: 200) |
1198 | 1487 | maxima number of subintervals |
1199 | 1488 | |
1200 | | |
1201 | 1489 | OUTPUT: |
1202 | 1490 | |
| 1491 | - approximation to the integral |
1203 | 1492 | |
1204 | | - approximation to the integral |
1205 | | |
1206 | | - estimated absolute error of the |
| 1493 | - estimated absolute error of the |
1207 | 1494 | approximation |
1208 | 1495 | |
1209 | | - the number of integrand evaluations |
| 1496 | - the number of integrand evaluations |
1210 | 1497 | |
1211 | | - an error code: |
| 1498 | - an error code: |
1212 | 1499 | |
1213 | | - ``0`` - no problems were encountered |
| 1500 | - ``0`` - no problems were encountered |
1214 | 1501 | |
1215 | | - ``1`` - too many subintervals were done |
| 1502 | - ``1`` - too many subintervals were done |
1216 | 1503 | |
1217 | | - ``2`` - excessive roundoff error |
| 1504 | - ``2`` - excessive roundoff error |
1218 | 1505 | |
1219 | | - ``3`` - extremely bad integrand behavior |
| 1506 | - ``3`` - extremely bad integrand behavior |
1220 | 1507 | |
1221 | | - ``4`` - failed to converge |
| 1508 | - ``4`` - failed to converge |
1222 | 1509 | |
1223 | | - ``5`` - integral is probably divergent or slowly convergent |
| 1510 | - ``5`` - integral is probably divergent or slowly convergent |
1224 | 1511 | |
1225 | | - ``6`` - the input is invalid |
1226 | | |
| 1512 | - ``6`` - the input is invalid |
1227 | 1513 | |
1228 | 1514 | EXAMPLES:: |
1229 | 1515 | |
… |
… |
|
1233 | 1519 | Note that GP also does numerical integration, and can do so to very |
1234 | 1520 | high precision very quickly:: |
1235 | 1521 | |
1236 | | sage: gp('intnum(x=0,1,exp(-sqrt(x)))') |
| 1522 | sage: gp('intnum(x=0,1,exp(-sqrt(x)))') |
1237 | 1523 | 0.5284822353142307136179049194 # 32-bit |
1238 | 1524 | 0.52848223531423071361790491935415653022 # 64-bit |
1239 | 1525 | sage: _ = gp.set_precision(80) |
… |
… |
|
1251 | 1537 | |
1252 | 1538 | INPUT: |
1253 | 1539 | |
| 1540 | - ``var`` - variable |
1254 | 1541 | |
1255 | | - ``var`` - variable |
| 1542 | - ``min`` - default: None |
1256 | 1543 | |
1257 | | - ``min`` - default: None |
| 1544 | - ``max`` - default: None |
1258 | 1545 | |
1259 | | - ``max`` - default: None |
| 1546 | OUTPUT: |
1260 | 1547 | |
| 1548 | - the definite integral if xmin is not None |
1261 | 1549 | |
1262 | | Returns the definite integral if xmin is not None, otherwise |
1263 | | returns an indefinite integral. |
| 1550 | - an indefinite integral otherwise |
1264 | 1551 | |
1265 | 1552 | EXAMPLES:: |
1266 | 1553 | |
1267 | | sage: maxima('x^2+1').integral() |
| 1554 | sage: maxima('x^2+1').integral() |
1268 | 1555 | x^3/3+x |
1269 | | sage: maxima('x^2+ 1 + y^2').integral('y') |
| 1556 | sage: maxima('x^2+ 1 + y^2').integral('y') |
1270 | 1557 | y^3/3+x^2*y+y |
1271 | | sage: maxima('x / (x^2+1)').integral() |
| 1558 | sage: maxima('x / (x^2+1)').integral() |
1272 | 1559 | log(x^2+1)/2 |
1273 | | sage: maxima('1/(x^2+1)').integral() |
| 1560 | sage: maxima('1/(x^2+1)').integral() |
1274 | 1561 | atan(x) |
1275 | | sage: maxima('1/(x^2+1)').integral('x', 0, infinity) |
| 1562 | sage: maxima('1/(x^2+1)').integral('x', 0, infinity) |
1276 | 1563 | %pi/2 |
1277 | | sage: maxima('x/(x^2+1)').integral('x', -1, 1) |
| 1564 | sage: maxima('x/(x^2+1)').integral('x', -1, 1) |
1278 | 1565 | 0 |
1279 | 1566 | |
1280 | 1567 | :: |
1281 | 1568 | |
1282 | | sage: f = maxima('exp(x^2)').integral('x',0,1); f |
| 1569 | sage: f = maxima('exp(x^2)').integral('x',0,1); f |
1283 | 1570 | -sqrt(%pi)*%i*erf(%i)/2 |
1284 | 1571 | sage: f.numer() |
1285 | 1572 | 1.46265174590718... |
… |
… |
|
1296 | 1583 | |
1297 | 1584 | def __float__(self): |
1298 | 1585 | """ |
1299 | | Return floating point version of this maxima element. |
| 1586 | Return floating point version of this Maxima element. |
| 1587 | |
| 1588 | INPUT: none |
| 1589 | |
| 1590 | OUTPUT: real |
1300 | 1591 | |
1301 | 1592 | EXAMPLES:: |
1302 | 1593 | |
… |
… |
|
1315 | 1606 | def __len__(self): |
1316 | 1607 | """ |
1317 | 1608 | Return the length of a list. |
| 1609 | |
| 1610 | INPUT: none |
| 1611 | |
| 1612 | OUTPUT: integer |
1318 | 1613 | |
1319 | 1614 | EXAMPLES:: |
1320 | 1615 | |
1321 | | sage: v = maxima('create_list(x^i,i,0,5)') |
1322 | | sage: len(v) |
| 1616 | sage: v = maxima('create_list(x^i,i,0,5)') |
| 1617 | sage: len(v) |
1323 | 1618 | 6 |
1324 | 1619 | """ |
1325 | | P = self._check_valid() |
| 1620 | P = self._check_valid() |
1326 | 1621 | return int(P.eval('length(%s)'%self.name())) |
1327 | 1622 | |
1328 | 1623 | def dot(self, other): |
1329 | 1624 | """ |
1330 | 1625 | Implements the notation self . other. |
| 1626 | |
| 1627 | INPUT: |
| 1628 | |
| 1629 | - ``other`` - matrix; argument to dot. |
| 1630 | |
| 1631 | OUTPUT: Maxima matrix |
1331 | 1632 | |
1332 | 1633 | EXAMPLES:: |
1333 | 1634 | |
… |
… |
|
1343 | 1644 | def __getitem__(self, n): |
1344 | 1645 | r""" |
1345 | 1646 | Return the n-th element of this list. |
| 1647 | |
| 1648 | INPUT: |
| 1649 | |
| 1650 | - ``n`` - integer |
| 1651 | |
| 1652 | OUTPUT: Maxima object |
1346 | 1653 | |
1347 | 1654 | .. note:: |
1348 | 1655 | |
… |
… |
|
1351 | 1658 | |
1352 | 1659 | EXAMPLES:: |
1353 | 1660 | |
1354 | | sage: v = maxima('create_list(i*x^i,i,0,5)'); v |
| 1661 | sage: v = maxima('create_list(i*x^i,i,0,5)'); v |
1355 | 1662 | [0,x,2*x^2,3*x^3,4*x^4,5*x^5] |
1356 | | sage: v[3] |
| 1663 | sage: v[3] |
1357 | 1664 | 3*x^3 |
1358 | | sage: v[0] |
| 1665 | sage: v[0] |
1359 | 1666 | 0 |
1360 | | sage: v[10] |
| 1667 | sage: v[10] |
1361 | 1668 | Traceback (most recent call last): |
1362 | 1669 | ... |
1363 | 1670 | IndexError: n = (10) must be between 0 and 5 |
… |
… |
|
1370 | 1677 | |
1371 | 1678 | def __iter__(self): |
1372 | 1679 | """ |
1373 | | EXAMPLE:: |
| 1680 | Return an iterator for self. |
| 1681 | |
| 1682 | INPUT: none |
| 1683 | |
| 1684 | OUTPUT: iterator |
| 1685 | |
| 1686 | EXAMPLES:: |
1374 | 1687 | |
1375 | 1688 | sage: v = maxima('create_list(i*x^i,i,0,5)') |
1376 | 1689 | sage: L = list(v) |
… |
… |
|
1383 | 1696 | def subst(self, val): |
1384 | 1697 | """ |
1385 | 1698 | Substitute a value or several values into this Maxima object. |
| 1699 | |
| 1700 | INPUT: |
| 1701 | |
| 1702 | - ``val`` - string representing substitution(s) to perform |
| 1703 | |
| 1704 | OUTPUT: Maxima object |
1386 | 1705 | |
1387 | 1706 | EXAMPLES:: |
1388 | 1707 | |
… |
… |
|
1398 | 1717 | def comma(self, args): |
1399 | 1718 | """ |
1400 | 1719 | Form the expression that would be written 'self, args' in Maxima. |
| 1720 | |
| 1721 | INPUT: |
| 1722 | |
| 1723 | - ``args`` - string |
| 1724 | |
| 1725 | OUTPUT: Maxima object |
1401 | 1726 | |
1402 | 1727 | EXAMPLES:: |
1403 | 1728 | |
… |
… |
|
1413 | 1738 | def _latex_(self): |
1414 | 1739 | """ |
1415 | 1740 | Return Latex representation of this Maxima object. |
| 1741 | |
| 1742 | INPUT: none |
| 1743 | |
| 1744 | OUTPUT: string |
1416 | 1745 | |
1417 | 1746 | This calls the tex command in Maxima, then does a little |
1418 | 1747 | post-processing to fix bugs in the resulting Maxima output. |
… |
… |
|
1438 | 1767 | P = self.parent() |
1439 | 1768 | s = P._eval_line('tex(%s);'%self.name(), reformat=False) |
1440 | 1769 | if not '$$' in s: |
1441 | | raise RuntimeError, "Error texing maxima object." |
| 1770 | raise RuntimeError, "Error texing Maxima object." |
1442 | 1771 | i = s.find('$$') |
1443 | 1772 | j = s.rfind('$$') |
1444 | 1773 | s = s[i+2:j] |
1445 | 1774 | s = multiple_replace({'\r\n':' ', |
1446 | | '\\%':'', |
| 1775 | '\\%':'', |
1447 | 1776 | '\\arcsin ':'\\sin^{-1} ', |
1448 | 1777 | '\\arccos ':'\\cos^{-1} ', |
1449 | 1778 | '\\arctan ':'\\tan^{-1} '}, s) |
… |
… |
|
1462 | 1791 | def trait_names(self, verbose=False): |
1463 | 1792 | """ |
1464 | 1793 | Return all Maxima commands, which is useful for tab completion. |
| 1794 | |
| 1795 | INPUT: |
| 1796 | |
| 1797 | - ``verbose`` - boolean |
| 1798 | |
| 1799 | OUTPUT: list of strings |
1465 | 1800 | |
1466 | 1801 | EXAMPLES:: |
1467 | 1802 | |
… |
… |
|
1476 | 1811 | If self is a Maxima matrix, return the corresponding Sage matrix |
1477 | 1812 | over the Sage ring `R`. |
1478 | 1813 | |
| 1814 | INPUT: |
| 1815 | |
| 1816 | - ``R`` - ring to coerce into |
| 1817 | |
| 1818 | OUTPUT: matrix |
| 1819 | |
1479 | 1820 | This may or may not work depending in how complicated the entries |
1480 | 1821 | of self are! It only works if the entries of self can be coerced as |
1481 | 1822 | strings to produce meaningful elements of `R`. |
1482 | 1823 | |
1483 | 1824 | EXAMPLES:: |
1484 | 1825 | |
1485 | | sage: _ = maxima.eval("f[i,j] := i/j") |
1486 | | sage: A = maxima('genmatrix(f,4,4)'); A |
| 1826 | sage: _ = maxima.eval("f[i,j] := i/j") |
| 1827 | sage: A = maxima('genmatrix(f,4,4)'); A |
1487 | 1828 | matrix([1,1/2,1/3,1/4],[2,1,2/3,1/2],[3,3/2,1,3/4],[4,2,4/3,1]) |
1488 | | sage: A._matrix_(QQ) |
| 1829 | sage: A._matrix_(QQ) |
1489 | 1830 | [ 1 1/2 1/3 1/4] |
1490 | 1831 | [ 2 1 2/3 1/2] |
1491 | 1832 | [ 3 3/2 1 3/4] |
… |
… |
|
1517 | 1858 | """ |
1518 | 1859 | Return the partial fraction decomposition of self with respect to |
1519 | 1860 | the variable var. |
| 1861 | |
| 1862 | INPUT: |
| 1863 | |
| 1864 | - ``var`` - string |
| 1865 | |
| 1866 | OUTPUT: Maxima object |
1520 | 1867 | |
1521 | 1868 | EXAMPLES:: |
1522 | 1869 | |
1523 | | sage: f = maxima('1/((1+x)*(x-1))') |
1524 | | sage: f.partial_fraction_decomposition('x') |
| 1870 | sage: f = maxima('1/((1+x)*(x-1))') |
| 1871 | sage: f.partial_fraction_decomposition('x') |
1525 | 1872 | 1/(2*(x-1))-1/(2*(x+1)) |
1526 | 1873 | sage: print f.partial_fraction_decomposition('x') |
1527 | 1874 | 1 1 |
… |
… |
|
1532 | 1879 | |
1533 | 1880 | def _operation(self, operation, right): |
1534 | 1881 | r""" |
| 1882 | Return the result of "self operation right" in Maxima. |
| 1883 | |
| 1884 | INPUT: |
| 1885 | |
| 1886 | - ``operation`` - string; operator |
| 1887 | |
| 1888 | - ``right`` - Maxima object; right operand |
| 1889 | |
| 1890 | OUTPUT: Maxima object |
| 1891 | |
1535 | 1892 | Note that right's parent should already be Maxima since this should |
1536 | 1893 | be called after coercion has been performed. |
1537 | 1894 | |
… |
… |
|
1568 | 1925 | |
1569 | 1926 | |
1570 | 1927 | class MaximaAbstractElementFunction(MaximaAbstractElement): |
| 1928 | r""" |
| 1929 | Create a Maxima function with the parent ``parent``, |
| 1930 | name ``name``, definition ``defn``, arguments ``args`` |
| 1931 | and latex representation ``latex``. |
| 1932 | |
| 1933 | INPUT: |
| 1934 | |
| 1935 | - ``parent`` - an instance of a concrete Maxima interface |
| 1936 | |
| 1937 | - ``name`` - string |
| 1938 | |
| 1939 | - ``defn`` - string |
| 1940 | |
| 1941 | - ``args`` - string; comma separated names of arguments |
| 1942 | |
| 1943 | - ``latex`` - string |
| 1944 | |
| 1945 | OUTPUT: Maxima function |
| 1946 | |
| 1947 | EXAMPLES:: |
| 1948 | |
| 1949 | sage: maxima.function('x,y','e^cos(x)') |
| 1950 | e^cos(x) |
| 1951 | """ |
| 1952 | |
1571 | 1953 | def __init__(self, parent, name, defn, args, latex): |
1572 | 1954 | """ |
1573 | | EXAMPLES:: |
1574 | | |
| 1955 | Create a Maxima function. |
| 1956 | See ``MaximaAbstractElementFunction`` for full documentation. |
| 1957 | |
| 1958 | TESTS:: |
| 1959 | |
| 1960 | sage: from sage.interfaces.maxima_abstract import MaximaAbstractElementFunction |
| 1961 | sage: MaximaAbstractElementFunction == loads(dumps(MaximaAbstractElementFunction)) |
| 1962 | True |
1575 | 1963 | sage: f = maxima.function('x,y','sin(x+y)') |
1576 | 1964 | sage: f == loads(dumps(f)) |
1577 | 1965 | True |
… |
… |
|
1583 | 1971 | |
1584 | 1972 | def __reduce__(self): |
1585 | 1973 | """ |
| 1974 | Implement __reduce__ for ``MaximaAbstractElementFunction``. |
| 1975 | |
| 1976 | INPUT: none |
| 1977 | |
| 1978 | OUTPUT: |
| 1979 | |
| 1980 | A couple consisting of: |
| 1981 | |
| 1982 | - the function to call for unpickling |
| 1983 | |
| 1984 | - a tuple of arguments for the function |
| 1985 | |
1586 | 1986 | EXAMPLES:: |
1587 | 1987 | |
1588 | 1988 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1590 | 1990 | (<function reduce_load_MaximaAbstract_function at 0x...>, |
1591 | 1991 | (Maxima, 'sin(x+y)', 'x,y', None)) |
1592 | 1992 | """ |
1593 | | return reduce_load_MaximaAbstract_function, (self.parent(), self.__defn, self.__args, self.__latex) |
| 1993 | return reduce_load_MaximaAbstract_function, (self.parent(), |
| 1994 | self.__defn, self.__args, self.__latex) |
1594 | 1995 | |
1595 | | def __call__(self, *x): |
| 1996 | def __call__(self, *args): |
1596 | 1997 | """ |
| 1998 | Return the result of calling this Maxima function |
| 1999 | with the given arguments. |
| 2000 | |
| 2001 | INPUT: |
| 2002 | |
| 2003 | - ``args`` - a variable number of arguments |
| 2004 | |
| 2005 | OUTPUT: Maxima object |
| 2006 | |
1597 | 2007 | EXAMPLES:: |
1598 | 2008 | |
1599 | 2009 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1603 | 2013 | sin(2*x) |
1604 | 2014 | """ |
1605 | 2015 | P = self._check_valid() |
1606 | | if len(x) == 1: |
1607 | | x = '(%s)'%x |
1608 | | return P('%s%s'%(self.name(), x)) |
| 2016 | if len(args) == 1: |
| 2017 | args = '(%s)'%args |
| 2018 | return P('%s%s'%(self.name(), args)) |
1609 | 2019 | |
1610 | 2020 | def __repr__(self): |
1611 | 2021 | """ |
| 2022 | Return print representation of this Maxima function. |
| 2023 | |
| 2024 | INPUT: none |
| 2025 | |
| 2026 | OUTPUT: string |
| 2027 | |
1612 | 2028 | EXAMPLES:: |
1613 | 2029 | |
1614 | 2030 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1619 | 2035 | |
1620 | 2036 | def _latex_(self): |
1621 | 2037 | """ |
| 2038 | Return latex representation of this Maxima function. |
| 2039 | |
| 2040 | INPUT: none |
| 2041 | |
| 2042 | OUTPUT: string |
| 2043 | |
1622 | 2044 | EXAMPLES:: |
1623 | 2045 | |
1624 | 2046 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1633 | 2055 | def arguments(self, split=True): |
1634 | 2056 | r""" |
1635 | 2057 | Returns the arguments of this Maxima function. |
| 2058 | |
| 2059 | INPUT: |
| 2060 | |
| 2061 | - ``split`` - boolean; if True return a tuple of strings, |
| 2062 | otherwise return a string of comma-separated arguments |
| 2063 | |
| 2064 | OUTPUT: |
| 2065 | |
| 2066 | - a string if ``split`` is False |
| 2067 | |
| 2068 | - a list of strings if ``split`` is True |
1636 | 2069 | |
1637 | 2070 | EXAMPLES:: |
1638 | 2071 | |
… |
… |
|
1653 | 2086 | def definition(self): |
1654 | 2087 | """ |
1655 | 2088 | Returns the definition of this Maxima function as a string. |
| 2089 | |
| 2090 | INPUT: none |
| 2091 | |
| 2092 | OUTPUT: string |
1656 | 2093 | |
1657 | 2094 | EXAMPLES:: |
1658 | 2095 | |
… |
… |
|
1665 | 2102 | def integral(self, var): |
1666 | 2103 | """ |
1667 | 2104 | Returns the integral of self with respect to the variable var. |
| 2105 | |
| 2106 | INPUT: |
| 2107 | |
| 2108 | - ``var`` - a variable |
| 2109 | |
| 2110 | OUTPUT: Maxima function |
1668 | 2111 | |
1669 | 2112 | Note that integrate is an alias of integral. |
1670 | 2113 | |
… |
… |
|
1679 | 2122 | """ |
1680 | 2123 | var = str(var) |
1681 | 2124 | P = self._check_valid() |
1682 | | f = P('integrate(%s(%s), %s)'%(self.name(), self.arguments(split=False), var)) |
| 2125 | f = P('integrate(%s(%s), %s)'%(self.name(), |
| 2126 | self.arguments(split=False), var)) |
1683 | 2127 | |
1684 | 2128 | args = self.arguments() |
1685 | 2129 | if var not in args: |
… |
… |
|
1692 | 2136 | r""" |
1693 | 2137 | This is a utility function which factors out much of the |
1694 | 2138 | commonality used in the arithmetic operations for |
1695 | | ``MaximaFunctions``. |
| 2139 | ``MaximaAbstractElementFunction``. |
1696 | 2140 | |
1697 | 2141 | INPUT: |
1698 | 2142 | |
1699 | | |
1700 | | - ``operation`` - A string representing the operation |
| 2143 | - ``operation`` - A string representing the operation |
1701 | 2144 | being performed. For example, '\*', or '1/'. |
1702 | 2145 | |
1703 | | - ``f`` - The other operand. If f is |
1704 | | ``None``, than the operation is assumed to be unary |
| 2146 | - ``f`` - The other operand. If f is |
| 2147 | ``None``, then the operation is assumed to be unary |
1705 | 2148 | rather than binary. |
1706 | 2149 | |
1707 | | |
1708 | 2150 | EXAMPLES:: |
1709 | 2151 | |
1710 | 2152 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1733 | 2175 | |
1734 | 2176 | def _add_(self, f): |
1735 | 2177 | """ |
1736 | | MaximaFunction as left summand. |
| 2178 | This Maxima function as left summand. |
1737 | 2179 | |
1738 | 2180 | EXAMPLES:: |
1739 | 2181 | |
… |
… |
|
1750 | 2192 | sage: (f+maxima.cos(x))(2) |
1751 | 2193 | sin(2)+cos(2) |
1752 | 2194 | sage: (f+maxima.cos(y)) # This is a function with only ONE argument! |
1753 | | cos(y)+sin(x) |
| 2195 | cos(y)+sin(x) |
1754 | 2196 | sage: (f+maxima.cos(y))(2) |
1755 | 2197 | cos(y)+sin(2) |
1756 | 2198 | |
… |
… |
|
1761 | 2203 | sage: g+f |
1762 | 2204 | sin(x)-cos(x) |
1763 | 2205 | sage: (g+f)(2) # The sum IS a function |
1764 | | sin(2)-cos(2) |
| 2206 | sin(2)-cos(2) |
1765 | 2207 | sage: 2+f |
1766 | 2208 | sin(x)+2 |
1767 | 2209 | """ |
… |
… |
|
1769 | 2211 | |
1770 | 2212 | def _sub_(self, f): |
1771 | 2213 | r""" |
1772 | | ``MaximaFunction`` as minuend. |
| 2214 | This Maxima function as minuend. |
1773 | 2215 | |
1774 | 2216 | EXAMPLES:: |
1775 | 2217 | |
… |
… |
|
1779 | 2221 | sage: f-g |
1780 | 2222 | sin(x)+cos(x) |
1781 | 2223 | sage: (f-g)(2) |
1782 | | sin(2)+cos(2) |
| 2224 | sin(2)+cos(2) |
1783 | 2225 | sage: (f-maxima.cos(y)) # This function only has the argument x! |
1784 | 2226 | sin(x)-cos(y) |
1785 | 2227 | sage: _(2) |
1786 | | sin(2)-cos(y) |
| 2228 | sin(2)-cos(y) |
1787 | 2229 | |
1788 | 2230 | :: |
1789 | 2231 | |
… |
… |
|
1794 | 2236 | |
1795 | 2237 | def _mul_(self, f): |
1796 | 2238 | r""" |
1797 | | ``MaximaFunction`` as left factor. |
| 2239 | This Maxima function as left factor. |
1798 | 2240 | |
1799 | 2241 | EXAMPLES:: |
1800 | 2242 | |
… |
… |
|
1820 | 2262 | |
1821 | 2263 | def _div_(self, f): |
1822 | 2264 | r""" |
1823 | | ``MaximaFunction`` as dividend. |
| 2265 | This Maxima function as dividend. |
1824 | 2266 | |
1825 | 2267 | EXAMPLES:: |
1826 | 2268 | |
… |
… |
|
1846 | 2288 | |
1847 | 2289 | def __neg__(self): |
1848 | 2290 | r""" |
1849 | | Additive inverse of a ``MaximaFunction``. |
| 2291 | Additive inverse of this Maxima function. |
1850 | 2292 | |
1851 | 2293 | EXAMPLES:: |
1852 | 2294 | |
… |
… |
|
1858 | 2300 | |
1859 | 2301 | def __inv__(self): |
1860 | 2302 | r""" |
1861 | | Multiplicative inverse of a ``MaximaFunction``. |
| 2303 | Multiplicative inverse of this Maxima function. |
1862 | 2304 | |
1863 | 2305 | EXAMPLES:: |
1864 | 2306 | |
… |
… |
|
1870 | 2312 | |
1871 | 2313 | def __pow__(self,f): |
1872 | 2314 | r""" |
1873 | | ``MaximaFunction`` raised to some power. |
| 2315 | This Maxima function raised to some power. |
1874 | 2316 | |
1875 | 2317 | EXAMPLES:: |
1876 | 2318 | |
… |
… |
|
1890 | 2332 | |
1891 | 2333 | |
1892 | 2334 | def reduce_load_MaximaAbstract_function(parent, defn, args, latex): |
| 2335 | r""" |
| 2336 | Unpickle a Maxima function. |
| 2337 | |
| 2338 | EXAMPLES:: |
| 2339 | |
| 2340 | sage: from sage.interfaces.maxima_abstract import reduce_load_MaximaAbstract_function |
| 2341 | sage: f = maxima.function('x,y','sin(x+y)') |
| 2342 | sage: _,args = f.__reduce__() |
| 2343 | sage: g = reduce_load_MaximaAbstract_function(*args) |
| 2344 | sage: g == f |
| 2345 | True |
| 2346 | """ |
1893 | 2347 | return parent.function(args, defn, defn, latex) |
1894 | 2348 | |
1895 | 2349 | def maxima_version(): |
1896 | 2350 | """ |
| 2351 | Return Maxima version. |
| 2352 | |
| 2353 | Currently this calls a new copy of Maxima. |
| 2354 | |
1897 | 2355 | EXAMPLES:: |
1898 | 2356 | |
1899 | 2357 | sage: from sage.interfaces.maxima_abstract import maxima_version |
diff -r c86e54a6bef8 -r c45d947b5179 sage/interfaces/maxima_lib.py
a
|
b
|
|
1 | 1 | r""" |
2 | | Interface to Maxima |
| 2 | Library interface to Maxima |
3 | 3 | |
4 | 4 | Maxima is a free GPL'd general purpose computer algebra system |
5 | 5 | whose development started in 1968 at MIT. It contains symbolic |
… |
… |
|
26 | 26 | - William Stein (2006-02-24): *greatly* improved robustness by adding |
27 | 27 | sequence numbers to IO bracketing in _eval_line |
28 | 28 | |
29 | | If the string "error" (case insensitive) occurs in the output of |
30 | | anything from Maxima, a RuntimeError exception is raised. |
| 29 | - Robert Bradshaw, Nils Bruin, Jean-Pierre Flori (2010,2011): Binary library |
| 30 | interface |
| 31 | |
| 32 | For this interface, Maxima is loaded into ECL which is itself loaded |
| 33 | as a C library in Sage. Translations between Sage and Maxima objects |
| 34 | (which are nothing but wrappers to ECL objects) is made as much as possible |
| 35 | directly, but falls back to the string based conversion used by the |
| 36 | classical Maxima Pexpect interface in case no new implementation has been made. |
| 37 | |
| 38 | This interface is the one used for calculus by Sage |
| 39 | and is accessible as maxima_calculus:: |
| 40 | |
| 41 | sage: maxima_calculus |
| 42 | Maxima_lib |
| 43 | |
| 44 | Only one instance of this interface can be instantiated, |
| 45 | so the user should not try to instantiate another one, |
| 46 | which is anyway set to raise an error:: |
| 47 | |
| 48 | sage: from sage.interfaces.maxima_lib import MaximaLib |
| 49 | sage: MaximaLib() |
| 50 | Traceback (most recent call last): |
| 51 | ... |
| 52 | RuntimeError: Maxima interface in library mode can only be instantiated once |
31 | 53 | """ |
32 | 54 | |
33 | 55 | #***************************************************************************** |
… |
… |
|
49 | 71 | |
50 | 72 | from sage.libs.ecl import * |
51 | 73 | |
52 | | from maxima_abstract import MaximaAbstract, MaximaAbstractFunction, MaximaAbstractElement, MaximaAbstractFunctionElement, MaximaAbstractElementFunction |
| 74 | from maxima_abstract import (MaximaAbstract, MaximaAbstractFunction, |
| 75 | MaximaAbstractElement, MaximaAbstractFunctionElement, |
| 76 | MaximaAbstractElementFunction) |
53 | 77 | |
54 | | ## We begin here by initializing maxima in library mode |
| 78 | ## We begin here by initializing Maxima in library mode |
| 79 | ## i.e. loading it into ECL |
55 | 80 | ecl_eval("(setf *load-verbose* NIL)") |
56 | 81 | ecl_eval("(require 'maxima)") |
57 | 82 | ecl_eval("(in-package :maxima)") |
… |
… |
|
60 | 85 | ecl_eval("(set-locale-subdir)") |
61 | 86 | ecl_eval("(set-pathnames)") |
62 | 87 | ecl_eval("(defun add-lineinfo (x) x)") |
63 | | #the following is a direct adaption of the definition of "retrieve" in the Maxima file |
64 | | #macsys.lisp. This routine is normally responsible for displaying a question and |
65 | | #returning the answer. We change it to throw an error in which the text of the question |
66 | | #is included. We do this by running exactly the same code as in the original definition |
67 | | #of "retrieve", but with *standard-output* redirected to a string. |
| 88 | ecl_eval('(defun principal nil (error "Divergent Integral"))') |
| 89 | ecl_eval("(setf $errormsg nil)") |
| 90 | |
| 91 | # the following is a direct adaption of the definition of "retrieve" |
| 92 | # in the Maxima file macsys.lisp. This routine is normally responsible |
| 93 | # for displaying a question and returning the answer. We change it to |
| 94 | # throw an error in which the text of the question is included. We do |
| 95 | # this by running exactly the same code as in the original definition |
| 96 | # of "retrieve", but with *standard-output* redirected to a string. |
68 | 97 | ecl_eval(r""" |
69 | 98 | (defun retrieve (msg flag &aux (print? nil)) |
70 | 99 | (declare (special msg flag print?)) |
71 | 100 | (or (eq flag 'noprint) (setq print? t)) |
72 | | (error |
73 | | (concatenate 'string "Maxima asks: " |
74 | | (string-trim '(#\Newline) |
| 101 | (error |
| 102 | (concatenate 'string "Maxima asks: " |
| 103 | (string-trim '(#\Newline) |
75 | 104 | (with-output-to-string (*standard-output*) |
76 | 105 | (cond ((not print?) |
77 | 106 | (setq print? t) |
… |
… |
|
100 | 129 | ) |
101 | 130 | """) |
102 | 131 | |
103 | | ecl_eval('(defparameter *dev-null* (make-two-way-stream (make-concatenated-stream) (make-broadcast-stream)))') |
104 | | ecl_eval('(defun principal nil (error "Divergent Integral"))') |
105 | | ecl_eval("(setf $errormsg nil)") |
106 | | |
107 | | #ecl_eval(r"(defun tex-derivative (x l r) (tex (if $derivabbrev (tex-dabbrev x) (tex-d x '\partial)) l r lop rop ))") |
108 | | |
109 | | #ecl_eval('(defun ask-evod (x even-odd)(error "Maxima asks a question"))') |
110 | | #ecl_eval('(defun ask-integerp (x)(error "Maxima asks a question"))') |
111 | | #ecl_eval('(defun ask-declare (x property)(error "Maxima asks a question"))') |
112 | | #ecl_eval('(defun ask-prop (object property fun-or-number)(error "Maxima asks a question"))') |
113 | | #ecl_eval('(defun asksign01 (a)(error "Maxima asks a question"))') |
114 | | #ecl_eval('(defun asksign (x)(error "Maxima asks a question"))') |
115 | | #ecl_eval('(defun asksign1 ($askexp)(error "Maxima asks a question"))') |
116 | | #ecl_eval('(defun ask-greateq (x y)(error "Maxima asks a question"))') |
117 | | #ecl_eval('(defun askinver (a)(error "Maxima asks a question"))') |
118 | | #ecl_eval('(defun npask (exp)(error "Maxima asks a question"))') |
119 | | |
| 132 | ## Redirection of ECL and Maxima stdout to /dev/null |
| 133 | ecl_eval(r"""(defparameter *dev-null* (make-two-way-stream |
| 134 | (make-concatenated-stream) (make-broadcast-stream)))""") |
120 | 135 | ecl_eval("(setf original-standard-output *standard-output*)") |
121 | 136 | ecl_eval("(setf *standard-output* *dev-null*)") |
122 | 137 | #ecl_eval("(setf *error-output* *dev-null*)") |
123 | 138 | |
| 139 | ## Default options set in Maxima |
124 | 140 | # display2d -- no ascii art output |
125 | 141 | # keepfloat -- don't automatically convert floats to rationals |
126 | | init_code = ['display2d : false', 'domain : complex', 'keepfloat : true', 'load(to_poly_solver)', 'load(simplify_sum)'] |
| 142 | init_code = ['display2d : false', 'domain : complex', 'keepfloat : true', |
| 143 | 'load(to_poly_solver)', 'load(simplify_sum)'] |
127 | 144 | # Turn off the prompt labels, since computing them *very |
128 | 145 | # dramatically* slows down the maxima interpret after a while. |
129 | 146 | # See the function makelabel in suprv1.lisp. |
130 | 147 | # Many thanks to andrej.vodopivec@gmail.com and also |
131 | 148 | # Robert Dodier for figuring this out! |
132 | | # See trac # 6818. |
| 149 | # See trac # 6818. |
133 | 150 | init_code.append('nolabels : true') |
134 | 151 | for l in init_code: |
135 | 152 | ecl_eval("#$%s$"%l) |
… |
… |
|
137 | 154 | ## should allow to do this through a method |
138 | 155 | #ecl_eval("(setf *standard-output* original-standard-output)") |
139 | 156 | |
| 157 | ## This is the main function (ECL object) used for evaluation |
140 | 158 | # This returns an EclObject |
141 | 159 | maxima_eval=ecl_eval(""" |
142 | 160 | (defun maxima-eval( form ) |
… |
… |
|
150 | 168 | ((eq result 'maxima-error) |
151 | 169 | (let ((the-jig (process-error-argl (cddr $error)))) |
152 | 170 | (mapc #'set (car the-jig) (cadr the-jig)) |
153 | | (error (concatenate 'string "Error executing code in Maxima: " |
154 | | (with-output-to-string (stream) |
155 | | (apply #'mformat stream (cadr $error) (caddr the-jig))))) |
| 171 | (error (concatenate 'string |
| 172 | "Error executing code in Maxima: " |
| 173 | (with-output-to-string (stream) |
| 174 | (apply #'mformat stream (cadr $error) |
| 175 | (caddr the-jig))))) |
156 | 176 | )) |
157 | | (t |
| 177 | (t |
158 | 178 | (let ((the-jig (process-error-argl (cddr $error)))) |
159 | 179 | (mapc #'set (car the-jig) (cadr the-jig)) |
160 | | (error (concatenate 'string "Maxima condition. result:" (princ-to-string result) "$error:" |
161 | | (with-output-to-string (stream) |
162 | | (apply #'mformat stream (cadr $error) (caddr the-jig))))) |
| 180 | (error (concatenate 'string "Maxima condition. result:" |
| 181 | (princ-to-string result) "$error:" |
| 182 | (with-output-to-string (stream) |
| 183 | (apply #'mformat stream (cadr $error) |
| 184 | (caddr the-jig))))) |
163 | 185 | )) |
164 | 186 | ) |
165 | 187 | ) |
166 | 188 | ) |
167 | 189 | """) |
168 | 190 | |
| 191 | ## Number of instances of this interface |
169 | 192 | maxima_lib_instances = 0 |
170 | 193 | |
| 194 | ## Here we define several useful ECL/Maxima objects |
171 | 195 | # The Maxima string function can change the structure of its input |
172 | 196 | #maxprint=EclObject("$STRING") |
173 | | maxprint=EclObject("(defun mstring-for-sage (form) (coerce (mstring form) 'string))").eval() |
| 197 | maxprint=EclObject(r"""(defun mstring-for-sage (form) |
| 198 | (coerce (mstring form) 'string))""").eval() |
174 | 199 | meval=EclObject("MEVAL") |
175 | 200 | msetq=EclObject("MSETQ") |
176 | 201 | mlist=EclObject("MLIST") |
… |
… |
|
189 | 214 | max_to_poly_solve=EclObject("$TO_POLY_SOLVE") |
190 | 215 | |
191 | 216 | def stdout_to_string(s): |
192 | | return ecl_eval("(with-output-to-string (*standard-output*) (maxima-eval #$%s$))"%s).python()[1:-1] |
| 217 | r""" |
| 218 | Evaluate command ``s`` and catch Maxima stdout |
| 219 | (not the result of the command!) into a string. |
| 220 | |
| 221 | INPUT: |
| 222 | |
| 223 | - ``s`` - string; command to evaluate |
| 224 | |
| 225 | OUTPUT: string |
| 226 | |
| 227 | This is currently used to implement display2d. |
| 228 | |
| 229 | EXAMPLES:: |
| 230 | |
| 231 | sage: from sage.interfaces.maxima_lib import stdout_to_string |
| 232 | sage: stdout_to_string('1+1') |
| 233 | '' |
| 234 | sage: stdout_to_string('disp(1+1)') |
| 235 | '2\n\n' |
| 236 | """ |
| 237 | return ecl_eval(r"""(with-output-to-string (*standard-output*) |
| 238 | (maxima-eval #$%s$))"""%s).python()[1:-1] |
193 | 239 | |
194 | 240 | def max_to_string(s): |
195 | | return maxprint(s).python()[1:-1] |
| 241 | r""" |
| 242 | Return the Maxima string corresponding to this ECL object. |
| 243 | |
| 244 | INPUT: |
| 245 | |
| 246 | - ``s`` - ECL object |
| 247 | |
| 248 | OUTPUT: string |
| 249 | |
| 250 | EXAMPLES:: |
| 251 | |
| 252 | sage: from sage.interfaces.maxima_lib import maxima_lib, max_to_string |
| 253 | sage: ecl = maxima_lib(cos(x)).ecl() |
| 254 | sage: max_to_string(ecl) |
| 255 | 'cos(x)' |
| 256 | """ |
| 257 | return maxprint(s).python()[1:-1] |
196 | 258 | |
197 | 259 | my_mread=ecl_eval(""" |
198 | 260 | (defun my-mread (cmd) |
199 | 261 | (caddr (mread (make-string-input-stream cmd)))) |
200 | 262 | """) |
201 | 263 | |
202 | | def parse_max_string(l): |
203 | | return my_mread('"%s;"'%l) |
| 264 | def parse_max_string(s): |
| 265 | r""" |
| 266 | Evaluate string in Maxima without *any* further simplification. |
| 267 | |
| 268 | INPUT: |
| 269 | |
| 270 | - ``s`` - string |
| 271 | |
| 272 | OUTPUT: ECL object |
| 273 | |
| 274 | EXAMPLES:: |
| 275 | |
| 276 | sage: from sage.interfaces.maxima_lib import parse_max_string |
| 277 | sage: parse_max_string('1+1') |
| 278 | <ECL: ((MPLUS) 1 1)> |
| 279 | """ |
| 280 | return my_mread('"%s;"'%s) |
204 | 281 | |
205 | 282 | class MaximaLib(MaximaAbstract): |
206 | 283 | """ |
207 | 284 | Interface to Maxima as a Library. |
| 285 | |
| 286 | INPUT: none |
| 287 | |
| 288 | OUTPUT: Maxima interface as a Library |
| 289 | |
| 290 | EXAMPLES:: |
| 291 | |
| 292 | sage: from sage.interfaces.maxima_lib import MaximaLib, maxima_lib |
| 293 | sage: isinstance(maxima_lib,MaximaLib) |
| 294 | True |
| 295 | |
| 296 | Only one such interface can be instantiated:: |
| 297 | |
| 298 | sage: MaximaLib() |
| 299 | Traceback (most recent call last): |
| 300 | ... |
| 301 | RuntimeError: Maxima interface in library mode can only |
| 302 | be instantiated once |
208 | 303 | """ |
209 | 304 | def __init__(self): |
210 | 305 | """ |
211 | 306 | Create an instance of the Maxima interpreter. |
| 307 | See ``MaximaLib`` for full documentation. |
212 | 308 | |
213 | 309 | TESTS:: |
214 | 310 | |
215 | | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 311 | sage: from sage.interfaces.maxima_lib import MaximaLib, maxima_lib |
| 312 | sage: MaximaLib == loads(dumps(MaximaLib)) |
| 313 | True |
216 | 314 | sage: maxima_lib == loads(dumps(maxima_lib)) |
217 | 315 | True |
218 | 316 | |
219 | 317 | We make sure labels are turned off (see trac 6816):: |
220 | | |
| 318 | |
221 | 319 | sage: 'nolabels : true' in maxima_lib._MaximaLib__init_code |
222 | 320 | True |
223 | 321 | """ |
… |
… |
|
229 | 327 | global init_code |
230 | 328 | self.__init_code = init_code |
231 | 329 | |
232 | | ## The name should definitely be changed to maxima_lib, however much more changes are then needed elsewhere |
233 | | ## With maxima, more things are fine, but for example _maxima_init_ gets called in calculus.calculus and the classic interface gets initialized (not started, it is already initialized by default, so that is not really a big deal) |
234 | 330 | MaximaAbstract.__init__(self,"maxima_lib") |
235 | 331 | self.__seq = 0 |
236 | | |
| 332 | |
237 | 333 | def _coerce_from_special_method(self, x): |
| 334 | r""" |
| 335 | Coerce ``x`` into self trying to call a special underscore method. |
| 336 | |
| 337 | INPUT: |
| 338 | |
| 339 | - ``x`` - object to coerce into self |
| 340 | |
| 341 | OUTPUT: Maxima element equivalent to ``x`` |
| 342 | |
| 343 | EXAMPLES:: |
| 344 | |
| 345 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 346 | sage: xmax = maxima_lib._coerce_from_special_method(x) |
| 347 | sage: type(xmax) |
| 348 | <class 'sage.interfaces.maxima_lib.MaximaLibElement'> |
| 349 | """ |
238 | 350 | if isinstance(x, EclObject): |
239 | 351 | return MaximaLibElement(self,self._create(x)) |
240 | 352 | else: |
241 | 353 | return MaximaAbstract._coerce_from_special_method(self,x) |
| 354 | |
| 355 | def __reduce__(self): |
| 356 | r""" |
| 357 | Implement __reduce__ for ``MaximaLib``. |
| 358 | |
| 359 | INPUT: none |
| 360 | |
| 361 | OUTPUT: |
| 362 | |
| 363 | A couple consisting of: |
| 364 | |
| 365 | - the function to call for unpickling |
242 | 366 | |
243 | | def __reduce__(self): |
244 | | """ |
| 367 | - a tuple of arguments for the function |
| 368 | |
245 | 369 | EXAMPLES:: |
246 | 370 | |
247 | 371 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
251 | 375 | return reduce_load_MaximaLib, tuple([]) |
252 | 376 | |
253 | 377 | # This outputs a string |
254 | | def eval(self, line, locals=None, reformat=True, **kwds): |
| 378 | def _eval_line(self, line, locals=None, reformat=True, **kwds): |
| 379 | r""" |
| 380 | Evaluate the line in Maxima. |
| 381 | |
| 382 | INPUT: |
| 383 | |
| 384 | - ``line`` - string; text to evaluate |
| 385 | |
| 386 | - ``locals`` - None (ignored); this is used for compatibility with the |
| 387 | Sage notebook's generic system interface. |
| 388 | |
| 389 | - ``reformat`` - boolean; whether to strip output or not |
| 390 | |
| 391 | - ``**kwds`` - All other arguments are currently ignored. |
| 392 | |
| 393 | OUTPUT: string representing Maxima output |
| 394 | |
| 395 | EXAMPLES:: |
| 396 | |
| 397 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 398 | sage: maxima_lib._eval_line('1+1') |
| 399 | '2' |
| 400 | sage: maxima_lib._eval_line('1+1;') |
| 401 | '2' |
| 402 | sage: maxima_lib._eval_line('1+1$') |
| 403 | '' |
| 404 | sage: maxima_lib._eval_line('randvar : cos(x)+sin(y)$') |
| 405 | '' |
| 406 | sage: maxima_lib._eval_line('randvar') |
| 407 | 'sin(y)+cos(x)' |
| 408 | """ |
255 | 409 | result = '' |
256 | 410 | while line: |
257 | 411 | ind_dollar=line.find("$") |
… |
… |
|
272 | 426 | return result |
273 | 427 | return ''.join([x.strip() for x in result.split()]) |
274 | 428 | |
275 | | _eval_line = eval |
| 429 | eval = _eval_line |
276 | 430 | |
277 | 431 | ########################################### |
278 | | # Direct access to underlying lisp interpreter. |
| 432 | # Direct access to underlying lisp interpreter. |
279 | 433 | ########################################### |
280 | 434 | def lisp(self, cmd): |
281 | 435 | """ |
282 | 436 | Send a lisp command to maxima. |
283 | 437 | |
| 438 | INPUT: |
| 439 | |
| 440 | - ``cmd`` - string |
| 441 | |
| 442 | OUTPUT: ECL object |
| 443 | |
284 | 444 | .. note:: |
285 | 445 | |
286 | 446 | The output of this command is very raw - not pretty. |
… |
… |
|
299 | 459 | |
300 | 460 | INPUT: |
301 | 461 | |
302 | | |
303 | 462 | - ``var`` - string |
304 | 463 | |
305 | 464 | - ``value`` - string |
306 | | |
| 465 | |
| 466 | OUTPUT: none |
307 | 467 | |
308 | 468 | EXAMPLES:: |
309 | 469 | |
… |
… |
|
321 | 481 | """ |
322 | 482 | Clear the variable named var. |
323 | 483 | |
| 484 | INPUT: |
| 485 | |
| 486 | - ``var`` - string |
| 487 | |
| 488 | OUTPUT: none |
| 489 | |
324 | 490 | EXAMPLES:: |
325 | 491 | |
326 | 492 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
339 | 505 | def get(self, var): |
340 | 506 | """ |
341 | 507 | Get the string value of the variable var. |
| 508 | |
| 509 | INPUT: |
| 510 | |
| 511 | - ``var`` - string |
| 512 | |
| 513 | OUTPUT: string |
342 | 514 | |
343 | 515 | EXAMPLES:: |
344 | 516 | |
… |
… |
|
349 | 521 | """ |
350 | 522 | s = self.eval('%s;'%var) |
351 | 523 | return s |
352 | | |
| 524 | |
353 | 525 | def _create(self, value, name=None): |
| 526 | r""" |
| 527 | Create a variable with given value and name. |
| 528 | |
| 529 | INPUT: |
| 530 | |
| 531 | - ``value`` - string or ECL object |
| 532 | |
| 533 | - ``name`` - string (default: None); name to use for the variable, |
| 534 | an automatically generated name is used if this is none |
| 535 | |
| 536 | OUTPUT: |
| 537 | |
| 538 | - string; the name of the created variable |
| 539 | |
| 540 | EXAMPLES: |
| 541 | |
| 542 | Creation from strings:: |
| 543 | |
| 544 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 545 | sage: maxima_lib._create('3','var3') |
| 546 | 'var3' |
| 547 | sage: maxima_lib.get('var3') |
| 548 | '3' |
| 549 | sage: s = maxima_lib._create('3') |
| 550 | sage: s # random output |
| 551 | 'sage9' |
| 552 | sage: s[:4] == 'sage' |
| 553 | True |
| 554 | |
| 555 | And from ECL object:: |
| 556 | |
| 557 | sage: c = maxima_lib(x+cos(19)).ecl() |
| 558 | sage: maxima_lib._create(c,'m') |
| 559 | 'm' |
| 560 | sage: maxima_lib.get('m') |
| 561 | 'x+cos(19)' |
| 562 | """ |
354 | 563 | name = self._next_var_name() if name is None else name |
355 | 564 | if isinstance(value,EclObject): |
356 | 565 | maxima_eval([[msetq],cadadr("#$%s$#$"%name),value]) |
… |
… |
|
359 | 568 | return name |
360 | 569 | |
361 | 570 | def _function_class(self): |
362 | | """ |
| 571 | r""" |
| 572 | Return the Python class of Maxima functions. |
| 573 | |
| 574 | INPUT: none |
| 575 | |
| 576 | OUTPUT: type |
| 577 | |
363 | 578 | EXAMPLES:: |
364 | 579 | |
365 | 580 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
369 | 584 | return MaximaLibFunction |
370 | 585 | |
371 | 586 | def _object_class(self): |
372 | | """ |
| 587 | r""" |
373 | 588 | Return the Python class of Maxima elements. |
| 589 | |
| 590 | INPUT: none |
| 591 | |
| 592 | OUTPUT: type |
374 | 593 | |
375 | 594 | EXAMPLES:: |
376 | 595 | |
… |
… |
|
381 | 600 | return MaximaLibElement |
382 | 601 | |
383 | 602 | def _function_element_class(self): |
384 | | """ |
| 603 | r""" |
| 604 | Return the Python class of Maxima functions of elements. |
| 605 | |
| 606 | INPUT: none |
| 607 | |
| 608 | OUTPUT: type |
| 609 | |
385 | 610 | EXAMPLES:: |
386 | 611 | |
387 | 612 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
391 | 616 | return MaximaLibFunctionElement |
392 | 617 | |
393 | 618 | def _object_function_class(self): |
394 | | """ |
| 619 | r""" |
| 620 | Return the Python class of Maxima user-defined functions. |
| 621 | |
| 622 | INPUT: none |
| 623 | |
| 624 | OUTPUT: type |
| 625 | |
395 | 626 | EXAMPLES:: |
396 | 627 | |
397 | 628 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
400 | 631 | """ |
401 | 632 | return MaximaLibElementFunction |
402 | 633 | |
403 | | ##some helper functions to wrap the calculus use of the maxima interface. |
404 | | ##these routines expect arguments living in the symbolic ring and return something |
405 | | ##that is hopefully coercible into the symbolic ring again. |
| 634 | ## some helper functions to wrap the calculus use of the maxima interface. |
| 635 | ## these routines expect arguments living in the symbolic ring |
| 636 | ## and return something that is hopefully coercible into the symbolic |
| 637 | ## ring again. |
406 | 638 | |
407 | 639 | def sr_integral(self,*args): |
408 | 640 | """ |
… |
… |
|
414 | 646 | sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x) |
415 | 647 | Traceback (most recent call last): |
416 | 648 | ... |
417 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details) |
| 649 | ValueError: Computation failed since Maxima requested additional |
| 650 | constraints; using the 'assume' command before integral evaluation |
| 651 | *may* help (example of legal syntax is 'assume(a>0)', see |
| 652 | `assume?` for more details) |
418 | 653 | Is a positive or negative? |
419 | 654 | sage: assume(a>0) |
420 | 655 | sage: integrate(1/(x^3 *(a+b*x)^(1/3)),x) |
… |
… |
|
424 | 659 | sage: integral(x^n,x) |
425 | 660 | Traceback (most recent call last): |
426 | 661 | ... |
427 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before integral evaluation *may* help (example of legal syntax is 'assume(n+1>0)', see `assume?` for more details) |
| 662 | ValueError: Computation failed since Maxima requested additional |
| 663 | constraints; using the 'assume' command before integral evaluation |
| 664 | *may* help (example of legal syntax is 'assume(n+1>0)', |
| 665 | see `assume?` for more details) |
428 | 666 | Is n+1 zero or nonzero? |
429 | 667 | sage: assume(n+1>0) |
430 | 668 | sage: integral(x^n,x) |
… |
… |
|
462 | 700 | sage: sum(a*q^k, k, 0, oo) |
463 | 701 | Traceback (most recent call last): |
464 | 702 | ... |
465 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before summation *may* help (example of legal syntax is 'assume(abs(q)-1>0)', see `assume?` for more details) |
| 703 | ValueError: Computation failed since Maxima requested additional |
| 704 | constraints; using the 'assume' command before summation *may* help |
| 705 | (example of legal syntax is 'assume(abs(q)-1>0)', see `assume?` |
| 706 | for more details) |
466 | 707 | Is abs(q)-1 positive, negative, or zero? |
467 | 708 | sage: assume(q > 1) |
468 | 709 | sage: sum(a*q^k, k, 0, oo) |
… |
… |
|
482 | 723 | except RuntimeError, error: |
483 | 724 | s = str(error) |
484 | 725 | if "divergent" in s: |
485 | | # in pexpect interface, one looks for this - could not find an example where 'Pole encountered' occurred, though |
| 726 | # in pexpect interface, one looks for this; |
| 727 | # could not find an example where 'Pole encountered' occurred, though |
486 | 728 | # if "divergent" in s or 'Pole encountered' in s: |
487 | 729 | raise ValueError, "Sum is divergent." |
488 | 730 | elif "Is" in s: # Maxima asked for a condition |
… |
… |
|
506 | 748 | 7776/3125 |
507 | 749 | sage: limit(f,x = 1.2) |
508 | 750 | 2.06961575467... |
509 | | sage: var('a') |
| 751 | sage: var('a') |
510 | 752 | a |
511 | 753 | sage: limit(x^a,x=0) |
512 | 754 | Traceback (most recent call last): |
513 | 755 | ... |
514 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details) |
| 756 | ValueError: Computation failed since Maxima requested additional |
| 757 | constraints; using the 'assume' command before limit evaluation |
| 758 | *may* help (see `assume?` for more details) |
515 | 759 | Is a positive, negative, or zero? |
516 | 760 | sage: assume(a>0) |
517 | 761 | sage: limit(x^a,x=0) |
518 | 762 | Traceback (most recent call last): |
519 | 763 | ... |
520 | | ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before limit evaluation *may* help (see `assume?` for more details) |
521 | | Is a an integer? |
| 764 | ValueError: Computation failed since Maxima requested additional |
| 765 | constraints; using the 'assume' command before limit evaluation |
| 766 | *may* help (see `assume?` for more details) |
| 767 | Is a an integer? |
522 | 768 | sage: assume(a,'integer') |
523 | 769 | sage: assume(a,'even') # Yes, Maxima will ask this too |
524 | 770 | sage: limit(x^a,x=0) |
… |
… |
|
562 | 808 | |
563 | 809 | |
564 | 810 | def is_MaximaLibElement(x): |
565 | | """ |
| 811 | r""" |
566 | 812 | Returns True if x is of type MaximaLibElement. |
567 | 813 | |
568 | 814 | EXAMPLES:: |
… |
… |
|
577 | 823 | return isinstance(x, MaximaLibElement) |
578 | 824 | |
579 | 825 | class MaximaLibElement(MaximaAbstractElement): |
| 826 | r""" |
| 827 | Element of Maxima through library interface. |
| 828 | |
| 829 | EXAMPLES: |
| 830 | |
| 831 | Elements of this class should not be created directly. |
| 832 | The targeted parent should be used instead:: |
| 833 | |
| 834 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 835 | sage: maxima_lib(4) |
| 836 | 4 |
| 837 | sage: maxima_lib(log(x)) |
| 838 | log(x) |
580 | 839 | """ |
581 | | """ |
| 840 | |
582 | 841 | def ecl(self): |
| 842 | r""" |
| 843 | Return the underlying ECL object of this MaximaLib object. |
| 844 | |
| 845 | INPUT: none |
| 846 | |
| 847 | OUTPUT: ECL object |
| 848 | |
| 849 | EXAMPLES:: |
| 850 | |
| 851 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 852 | sage: maxima_lib(x+cos(19)).ecl() |
| 853 | <ECL: ((MPLUS SIMP) ((%COS SIMP) 19) $X)> |
| 854 | """ |
583 | 855 | try: |
584 | 856 | return self._ecl |
585 | 857 | except AttributeError: |
586 | 858 | self._ecl=maxima_eval("#$%s$"%self._name) |
587 | 859 | return self._ecl |
588 | | |
| 860 | |
589 | 861 | def to_poly_solve(self,vars,options=""): |
| 862 | r""" |
| 863 | Use Maxima's to_poly_solver package. |
| 864 | |
| 865 | INPUT: |
| 866 | |
| 867 | - ``vars`` - symbolic expressions |
| 868 | |
| 869 | - ``options`` - string (default="") |
| 870 | |
| 871 | OUTPUT: Maxima object |
| 872 | |
| 873 | EXAMPLES:: |
| 874 | |
| 875 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 876 | sage: sol = maxima_lib(sin(x) == 0).to_poly_solve(x) |
| 877 | sage: sol.sage() |
| 878 | [[x == pi + 2*pi*z6], [x == 2*pi*z8]] |
| 879 | """ |
590 | 880 | if options.find("use_grobner=true") != -1: |
591 | 881 | cmd=EclObject([[max_to_poly_solve], self.ecl(), sr_to_max(vars), |
592 | 882 | [[mequal],max_use_grobner,True]]) |
… |
… |
|
595 | 885 | return self.parent()(maxima_eval(cmd)) |
596 | 886 | |
597 | 887 | def display2d(self, onscreen=True): |
598 | | """ |
| 888 | r""" |
| 889 | Return the 2d representation of this Maxima object. |
| 890 | |
| 891 | INPUT: |
| 892 | |
| 893 | - ``onscreen`` - boolean (default: True); whether to print or return |
| 894 | |
| 895 | OUTPUT: |
| 896 | |
| 897 | The representation is printed if onscreen is set to True |
| 898 | and returned as a string otherwise. |
| 899 | |
599 | 900 | EXAMPLES:: |
600 | 901 | |
601 | 902 | sage: from sage.interfaces.maxima_lib import maxima_lib |
602 | | sage: F = maxima_lib('x^5 - y^5').factor() |
603 | | sage: F.display2d () |
| 903 | sage: F = maxima_lib('x^5 - y^5').factor() |
| 904 | sage: F.display2d() |
604 | 905 | 4 3 2 2 3 4 |
605 | 906 | - (y - x) (y + x y + x y + x y + x ) |
606 | 907 | """ |
… |
… |
|
638 | 939 | |
639 | 940 | |
640 | 941 | def reduce_load_MaximaLib(): |
641 | | """ |
| 942 | r""" |
| 943 | Unpickle the (unique) Maxima library interface. |
| 944 | |
642 | 945 | EXAMPLES:: |
643 | 946 | |
644 | 947 | sage: from sage.interfaces.maxima_lib import reduce_load_MaximaLib |
… |
… |
|
647 | 950 | """ |
648 | 951 | return maxima_lib |
649 | 952 | |
650 | | #********************************** |
651 | | # ??? |
652 | 953 | |
| 954 | ############################################# |
| 955 | # Smart translations between SR and Maxima |
| 956 | ############################################# |
| 957 | |
| 958 | import sage.rings.real_double |
653 | 959 | import sage.symbolic.expression |
654 | 960 | import sage.functions.trig |
655 | 961 | import sage.functions.log |
… |
… |
|
667 | 973 | cadadr=EclObject("cadadr") |
668 | 974 | meval=EclObject("meval") |
669 | 975 | NIL=EclObject("NIL") |
670 | | ratdisrep=EclObject("ratdisrep") |
671 | 976 | |
| 977 | ## Dictionaries for standard operators |
672 | 978 | sage_op_dict = { |
673 | 979 | sage.symbolic.expression.operator.abs : "MABS", |
674 | 980 | sage.symbolic.expression.operator.add : "MPLUS", |
… |
… |
|
707 | 1013 | sage_op_dict = dict([(k,EclObject(sage_op_dict[k])) for k in sage_op_dict]) |
708 | 1014 | max_op_dict = dict([(sage_op_dict[k],k) for k in sage_op_dict]) |
709 | 1015 | |
| 1016 | |
| 1017 | ## Here we correct the dictionaries for some simple operators |
710 | 1018 | def add_vararg(*args): |
| 1019 | r""" |
| 1020 | Addition of a variable number of arguments. |
| 1021 | |
| 1022 | INPUT: |
| 1023 | |
| 1024 | - ``args`` - arguments to add |
| 1025 | |
| 1026 | OUTPUT: sum of arguments |
| 1027 | |
| 1028 | EXAMPLES:: |
| 1029 | |
| 1030 | sage: from sage.interfaces.maxima_lib import add_vararg |
| 1031 | sage: add_vararg(1,2,3,4,5,6,7) |
| 1032 | 28 |
| 1033 | """ |
711 | 1034 | S=0 |
712 | 1035 | for a in args: |
713 | 1036 | S=S+a |
714 | 1037 | return S |
715 | 1038 | |
716 | 1039 | def mul_vararg(*args): |
| 1040 | r""" |
| 1041 | Multiplication of a variable number of arguments. |
| 1042 | |
| 1043 | INPUT: |
| 1044 | |
| 1045 | - ``args`` - arguments to multiply |
| 1046 | |
| 1047 | OUTPUT: product of arguments |
| 1048 | |
| 1049 | EXAMPLES:: |
| 1050 | |
| 1051 | sage: from sage.interfaces.maxima_lib import mul_vararg |
| 1052 | sage: mul_vararg(9,8,7,6,5,4) |
| 1053 | 60480 |
| 1054 | """ |
717 | 1055 | P=1 |
718 | 1056 | for a in args: |
719 | 1057 | P=P*a |
720 | 1058 | return P |
721 | 1059 | |
722 | 1060 | def sage_rat(x,y): |
| 1061 | r""" |
| 1062 | Return quotient x/y. |
| 1063 | |
| 1064 | INPUT: |
| 1065 | |
| 1066 | - ``x`` - integer |
| 1067 | |
| 1068 | - ``y`` - integer |
| 1069 | |
| 1070 | OUTPUT: rational |
| 1071 | |
| 1072 | EXAMPLES:: |
| 1073 | |
| 1074 | sage: from sage.interfaces.maxima_lib import sage_rat |
| 1075 | sage: sage_rat(1,7) |
| 1076 | 1/7 |
| 1077 | """ |
723 | 1078 | return x/y |
724 | 1079 | |
725 | 1080 | mplus=EclObject("MPLUS") |
726 | 1081 | mtimes=EclObject("MTIMES") |
727 | | mdiff=EclObject("%DERIVATIVE") |
728 | 1082 | rat=EclObject("RAT") |
729 | | max_i=EclObject("$%I") |
730 | 1083 | max_op_dict[mplus]=add_vararg |
731 | 1084 | max_op_dict[mtimes]=mul_vararg |
732 | 1085 | max_op_dict[rat]=sage_rat |
| 1086 | |
| 1087 | |
| 1088 | ## Here we build dictionaries for operators needing special conversions. |
| 1089 | ratdisrep=EclObject("ratdisrep") |
| 1090 | mrat=EclObject("MRAT") |
733 | 1091 | mqapply=EclObject("MQAPPLY") |
734 | 1092 | max_li=EclObject("$LI") |
735 | 1093 | max_psi=EclObject("$PSI") |
736 | 1094 | max_array=EclObject("ARRAY") |
| 1095 | mdiff=EclObject("%DERIVATIVE") |
737 | 1096 | max_gamma_incomplete=sage_op_dict[sage.functions.other.gamma_inc] |
738 | 1097 | |
739 | 1098 | def mrat_to_sage(expr): |
740 | 1099 | r""" |
741 | | Convert a maxima MRAT expression to Sage SR |
| 1100 | Convert a Maxima MRAT expression to Sage SR. |
742 | 1101 | |
743 | | Maxima has an optimised representation for multivariate rational expressions. |
744 | | The easiest way to translate those to SR is by first asking maxima to give |
745 | | the generic representation of the object. That is what RATDISREP does in |
746 | | maxima. |
| 1102 | INPUT: |
| 1103 | |
| 1104 | - ``expr`` - ECL object; a Maxima MRAT expression |
| 1105 | |
| 1106 | OUTPUT: symbolic expression |
| 1107 | |
| 1108 | Maxima has an optimised representation for multivariate |
| 1109 | rational expressions. The easiest way to translate those |
| 1110 | to SR is by first asking Maxima to give the generic representation |
| 1111 | of the object. That is what RATDISREP does in Maxima. |
| 1112 | |
| 1113 | EXAMPLES:: |
| 1114 | |
| 1115 | sage: from sage.interfaces.maxima_lib import maxima_lib, mrat_to_sage |
| 1116 | sage: var('x y z') |
| 1117 | (x, y, z) |
| 1118 | sage: c = maxima_lib((x+y^2+z^9)/x^6+z^8/y).rat() |
| 1119 | sage: c |
| 1120 | (y*z^9+x^6*z^8+y^3+x*y)/(x^6*y) |
| 1121 | sage: c.ecl() |
| 1122 | <ECL: ((MRAT SIMP ($X $Y $Z) |
| 1123 | ...> |
| 1124 | sage: mrat_to_sage(c.ecl()) |
| 1125 | (x^6*z^8 + y*z^9 + y^3 + x*y)/(x^6*y) |
747 | 1126 | """ |
748 | 1127 | return max_to_sr(meval(EclObject([[ratdisrep],expr]))) |
749 | 1128 | |
750 | 1129 | def mqapply_to_sage(expr): |
751 | 1130 | r""" |
752 | | Special conversion rule for MQAPPLY expressions |
| 1131 | Special conversion rule for MQAPPLY expressions. |
| 1132 | |
| 1133 | INPUT: |
| 1134 | |
| 1135 | - ``expr`` - ECL object; a Maxima MQAPPLY expression |
| 1136 | |
| 1137 | OUTPUT: symbolic expression |
| 1138 | |
| 1139 | MQAPPLY is used for function as li[x](y) and psi[x](y). |
| 1140 | |
| 1141 | EXAMPLES:: |
| 1142 | |
| 1143 | sage: from sage.interfaces.maxima_lib import maxima_lib, mqapply_to_sage |
| 1144 | sage: c = maxima_lib('li[2](3)') |
| 1145 | sage: c.ecl() |
| 1146 | <ECL: ((MQAPPLY SIMP) (($LI SIMP ARRAY) 2) 3)> |
| 1147 | sage: mqapply_to_sage(c.ecl()) |
| 1148 | polylog(2, 3) |
753 | 1149 | """ |
754 | 1150 | if caaadr(expr) == max_li: |
755 | | return sage.functions.log.polylog(max_to_sr(cadadr(expr)),max_to_sr(caddr(expr))) |
| 1151 | return sage.functions.log.polylog(max_to_sr(cadadr(expr)), |
| 1152 | max_to_sr(caddr(expr))) |
756 | 1153 | if caaadr(expr) == max_psi: |
757 | | return sage.functions.other.psi(max_to_sr(cadadr(expr)),max_to_sr(caddr(expr))) |
| 1154 | return sage.functions.other.psi(max_to_sr(cadadr(expr)), |
| 1155 | max_to_sr(caddr(expr))) |
758 | 1156 | else: |
759 | 1157 | op=max_to_sr(cadr(expr)) |
760 | 1158 | max_args=cddr(expr) |
761 | 1159 | args=[max_to_sr(a) for a in max_args] |
762 | 1160 | return op(*args) |
763 | 1161 | |
| 1162 | def mdiff_to_sage(expr): |
| 1163 | r""" |
| 1164 | Special conversion rule for %DERIVATIVE expressions. |
| 1165 | |
| 1166 | INPUT: |
| 1167 | |
| 1168 | - ``expr`` - ECL object; a Maxima %DERIVATIVE expression |
| 1169 | |
| 1170 | OUTPUT: symbolic expression |
| 1171 | |
| 1172 | EXAMPLES:: |
| 1173 | |
| 1174 | sage: from sage.interfaces.maxima_lib import maxima_lib, mdiff_to_sage |
| 1175 | sage: f = maxima_lib('f(x)').diff('x',4) |
| 1176 | sage: f.ecl() |
| 1177 | <ECL: ((%DERIVATIVE SIMP) (($F SIMP) $X) $X 4)> |
| 1178 | sage: mdiff_to_sage(f.ecl()) |
| 1179 | D[0, 0, 0, 0](f)(x) |
| 1180 | """ |
| 1181 | return max_to_sr(expr.cadr()).diff(*[max_to_sr(e) for e in expr.cddr()]) |
| 1182 | |
764 | 1183 | def dummy_integrate(expr): |
765 | 1184 | r""" |
766 | | 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. |
| 1185 | We would like to simply tie Maxima's integrate to |
| 1186 | sage.calculus.calculus.dummy_integrate, but we're being |
| 1187 | imported there so to avoid circularity we define it here. |
| 1188 | |
| 1189 | INPUT: |
| 1190 | |
| 1191 | - ``expr`` - ECL object; a Maxima %INTEGRATE expression |
| 1192 | |
| 1193 | OUTPUT: symbolic expression |
| 1194 | |
| 1195 | EXAMPLES:: |
| 1196 | sage: from sage.interfaces.maxima_lib import maxima_lib, dummy_integrate |
| 1197 | sage: f = maxima_lib('f(x)').integrate('x') |
| 1198 | sage: f.ecl() |
| 1199 | <ECL: ((%INTEGRATE SIMP) (($F SIMP) $X) $X)> |
| 1200 | sage: dummy_integrate(f.ecl()) |
| 1201 | integrate(f(x), x) |
| 1202 | |
| 1203 | :: |
| 1204 | sage: f = maxima_lib('f(x)').integrate('x',0,10) |
| 1205 | sage: f.ecl() |
| 1206 | <ECL: ((%INTEGRATE SIMP) (($F SIMP) $X) $X 0 10)> |
| 1207 | sage: dummy_integrate(f.ecl()) |
| 1208 | integrate(f(x), x, 0, 10) |
767 | 1209 | """ |
768 | 1210 | args=[max_to_sr(a) for a in cdr(expr)] |
769 | 1211 | if len(args) == 4 : |
770 | | return sage.symbolic.integration.integral.definite_integral(*args, hold=True) |
| 1212 | return sage.symbolic.integration.integral.definite_integral(*args, |
| 1213 | hold=True) |
771 | 1214 | else: |
772 | | return sage.symbolic.integration.integral.indefinite_integral(*args, hold=True) |
| 1215 | return sage.symbolic.integration.integral.indefinite_integral(*args, |
| 1216 | hold=True) |
773 | 1217 | |
774 | | def mdiff_to_sage(expr): |
775 | | return max_to_sr(expr.cadr()).diff(*[max_to_sr(e) for e in expr.cddr()]) |
776 | | |
| 1218 | ## The dictionaries |
777 | 1219 | special_max_to_sage={ |
778 | | EclObject("MRAT") : mrat_to_sage, |
| 1220 | mrat : mrat_to_sage, |
779 | 1221 | mqapply : mqapply_to_sage, |
780 | | EclObject("%INTEGRATE") : dummy_integrate, |
781 | | mdiff : mdiff_to_sage |
| 1222 | mdiff : mdiff_to_sage, |
| 1223 | EclObject("%INTEGRATE") : dummy_integrate |
782 | 1224 | } |
783 | 1225 | |
784 | 1226 | special_sage_to_max={ |
… |
… |
|
788 | 1230 | sage.functions.other.Ei : lambda X : [[max_gamma_incomplete], 0, X] |
789 | 1231 | } |
790 | 1232 | |
| 1233 | |
| 1234 | ## Dictionaries for symbols |
791 | 1235 | sage_sym_dict={} |
792 | 1236 | max_sym_dict={} |
793 | 1237 | |
| 1238 | |
| 1239 | ## Generic conversion functions |
| 1240 | |
| 1241 | max_i=EclObject("$%I") |
794 | 1242 | def pyobject_to_max(obj): |
| 1243 | r""" |
| 1244 | Convert a (simple) Python object into a Maxima object. |
| 1245 | |
| 1246 | INPUT: |
| 1247 | |
| 1248 | - ``expr`` - Python object |
| 1249 | |
| 1250 | OUTPUT: ECL object |
| 1251 | |
| 1252 | .. note:: |
| 1253 | This uses functions defined in sage.libs.ecl. |
| 1254 | |
| 1255 | EXAMPLES:: |
| 1256 | sage: from sage.interfaces.maxima_lib import pyobject_to_max |
| 1257 | sage: pyobject_to_max(4) |
| 1258 | <ECL: 4> |
| 1259 | sage: pyobject_to_max('z') |
| 1260 | <ECL: Z> |
| 1261 | sage: var('x') |
| 1262 | x |
| 1263 | sage: pyobject_to_max(x) |
| 1264 | Traceback (most recent call last): |
| 1265 | ... |
| 1266 | TypeError: Unimplemented type for python_to_ecl |
| 1267 | """ |
795 | 1268 | if isinstance(obj,sage.rings.rational.Rational): |
796 | 1269 | return EclObject(obj) if (obj.denom().is_one()) else EclObject([[rat], obj.numer(),obj.denom()]) |
797 | 1270 | elif isinstance(obj,sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]: |
… |
… |
|
802 | 1275 | # This goes from SR to EclObject |
803 | 1276 | def sr_to_max(expr): |
804 | 1277 | r""" |
| 1278 | Convert a symbolic expression into a Maxima object. |
| 1279 | |
| 1280 | INPUT: |
| 1281 | |
| 1282 | - ``expr`` - symbolic expression |
| 1283 | |
| 1284 | OUTPUT: ECL object |
| 1285 | |
| 1286 | EXAMPLES:: |
| 1287 | sage: from sage.interfaces.maxima_lib import sr_to_max |
| 1288 | sage: var('x') |
| 1289 | x |
| 1290 | sage: sr_to_max(x) |
| 1291 | <ECL: $X> |
| 1292 | sage: sr_to_max(cos(x)) |
| 1293 | <ECL: ((%COS) $X)> |
| 1294 | sage: f = function('f',x) |
| 1295 | sage: sr_to_max(f.diff()) |
| 1296 | <ECL: ((%DERIVATIVE) (($F) $X) $X 1)> |
805 | 1297 | """ |
806 | 1298 | global sage_op_dict, max_op_dict |
807 | 1299 | global sage_sym_dict, max_sym_dict |
… |
… |
|
811 | 1303 | if op: |
812 | 1304 | # Stolen from sage.symbolic.expression_conversion |
813 | 1305 | # Should be defined in a function and then put in special_sage_to_max |
814 | | # For that, we should change the API of the functions there (we need to have access to op, not only to expr.operands() |
| 1306 | # For that, we should change the API of the functions there |
| 1307 | # (we need to have access to op, not only to expr.operands() |
815 | 1308 | if isinstance(op, FDerivativeOperator): |
816 | 1309 | from sage.symbolic.ring import is_SymbolicVariable |
817 | 1310 | args = expr.operands() |
… |
… |
|
822 | 1315 | params = op.parameter_set() |
823 | 1316 | deriv_max = [] |
824 | 1317 | [deriv_max.extend([sr_to_max(args[i]), EclObject(params.count(i))]) for i in set(params)] |
825 | | l = [mdiff,f] |
| 1318 | l = [[mdiff],f] |
826 | 1319 | l.extend(deriv_max) |
827 | 1320 | return EclObject(l) |
828 | 1321 | elif (op in special_sage_to_max): |
… |
… |
|
848 | 1341 | return pyobject_to_max(expr.pyobject()) |
849 | 1342 | except TypeError: |
850 | 1343 | return maxima(expr).ecl() |
851 | | |
| 1344 | |
852 | 1345 | # This goes from EclObject to SR |
853 | | import sage.rings.real_double |
| 1346 | def max_to_sr(expr): |
| 1347 | r""" |
| 1348 | Convert a Maxima object into a symbolic expression. |
854 | 1349 | |
855 | | def max_to_sr(expr): |
| 1350 | INPUT: |
| 1351 | |
| 1352 | - ``expr`` - ECL object |
| 1353 | |
| 1354 | OUTPUT: symbolic expression |
| 1355 | |
| 1356 | EXAMPLES:: |
| 1357 | |
| 1358 | sage: from sage.interfaces.maxima_lib import maxima_lib, max_to_sr |
| 1359 | sage: f = maxima_lib('f(x)') |
| 1360 | sage: f.ecl() |
| 1361 | <ECL: (($F SIMP) $X)> |
| 1362 | sage: max_to_sr(f.ecl()) |
| 1363 | f(x) |
| 1364 | |
| 1365 | TESTS:: |
| 1366 | |
| 1367 | sage: from sage.interfaces.maxima_lib import sr_to_max, max_to_sr |
| 1368 | sage: f = function('f',x).diff() |
| 1369 | sage: bool(max_to_sr(sr_to_max(f)) == f) |
| 1370 | True |
| 1371 | """ |
856 | 1372 | if expr.consp(): |
857 | 1373 | op_max=caar(expr) |
858 | 1374 | if op_max in special_max_to_sage: |
859 | 1375 | return special_max_to_sage[op_max](expr) |
860 | 1376 | if not(op_max in max_op_dict): |
861 | | # This could be unsafe if the conversion to SR chenges the structure of expr |
| 1377 | # This could be unsafe if the conversion to SR |
| 1378 | # changes the structure of expr |
862 | 1379 | sage_expr=SR(maxima(expr)) |
863 | 1380 | max_op_dict[op_max]=sage_expr.operator() |
864 | 1381 | sage_op_dict[sage_expr.operator()]=op_max |
… |
… |
|
877 | 1394 | if isinstance(e,float): |
878 | 1395 | return sage.rings.real_double.RealDoubleElement(e) |
879 | 1396 | return e |
880 | | |