# HG changeset patch
# User Jean-Pierre Flori <flori@enst.fr>
# Date 1299854441 -3600
# Node ID 474b56e1b430940d79fd0c60964f2a6e892cf859
# Parent 60b96545371000ca24cd8a7310fc83caeea6fa31
Doctests.
diff -r 60b965453710 -r 474b56e1b430 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 60b965453710 -r 474b56e1b430 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 60b965453710 -r 474b56e1b430 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 interface |
| 30 | |
| 31 | This is the interface used by the maxima object:: |
| 32 | |
| 33 | sage: type(maxima) |
| 34 | <class 'sage.interfaces.maxima.Maxima'> |
| 35 | |
29 | 36 | If the string "error" (case insensitive) occurs in the output of |
30 | 37 | anything from Maxima, a RuntimeError exception is raised. |
31 | 38 | |
… |
… |
|
468 | 475 | class Maxima(MaximaAbstract, Expect): |
469 | 476 | """ |
470 | 477 | Interface to the Maxima interpreter. |
| 478 | |
| 479 | EXAMPLES:: |
| 480 | |
| 481 | sage: m = Maxima() |
| 482 | sage: m == maxima |
| 483 | False |
471 | 484 | """ |
472 | 485 | def __init__(self, script_subdirectory=None, logfile=None, server=None, |
473 | 486 | init_code = None): |
… |
… |
|
567 | 580 | |
568 | 581 | def __reduce__(self): |
569 | 582 | """ |
| 583 | Implementation of __reduce__ for ``Maxima``. |
| 584 | |
570 | 585 | EXAMPLES:: |
571 | 586 | |
572 | 587 | sage: maxima.__reduce__() |
… |
… |
|
575 | 590 | return reduce_load_Maxima, tuple([]) |
576 | 591 | |
577 | 592 | def _sendline(self, str): |
| 593 | """ |
| 594 | Send a string followed by a newline character. |
| 595 | |
| 596 | EXAMPLES:: |
| 597 | |
| 598 | sage: maxima._sendline('t : 9;') |
| 599 | sage: maxima.get('t') |
| 600 | '9' |
| 601 | """ |
578 | 602 | self._sendstr(str) |
579 | 603 | os.write(self._expect.child_fd, os.linesep) |
580 | 604 | |
… |
… |
|
671 | 695 | def _eval_line(self, line, allow_use_file=False, |
672 | 696 | wait_for_prompt=True, reformat=True, error_check=True): |
673 | 697 | """ |
| 698 | Return result of line evaluation. |
| 699 | |
674 | 700 | EXAMPLES: |
675 | 701 | |
676 | 702 | We check that errors are correctly checked:: |
… |
… |
|
794 | 820 | self.quit() |
795 | 821 | |
796 | 822 | def _batch(self, s, batchload=True): |
| 823 | """ |
| 824 | Call Maxima's batch or batchload command with a file containing the given string as argument. |
| 825 | |
| 826 | EXAMPLES:: |
| 827 | |
| 828 | sage: maxima._batch('10003;') |
| 829 | '...batchload...' |
| 830 | sage: maxima._batch('10003;',batchload=False) |
| 831 | '...batch...10003...' |
| 832 | """ |
797 | 833 | filename = '%s-%s'%(self._local_tmpfile(),randrange(2147483647)) |
798 | 834 | F = open(filename, 'w') |
799 | 835 | F.write(s) |
… |
… |
|
815 | 851 | self._sendline(cmd) |
816 | 852 | self._expect_expr(s) |
817 | 853 | out = self._before() |
818 | | self._error_check(str, out) |
| 854 | self._error_check(cmd, out) |
819 | 855 | os.unlink(filename) |
820 | 856 | return out |
821 | 857 | |
822 | 858 | def _quit_string(self): |
823 | 859 | """ |
| 860 | Return string representation of quit command. |
| 861 | |
824 | 862 | EXAMPLES:: |
825 | 863 | |
826 | 864 | sage: maxima._quit_string() |
… |
… |
|
830 | 868 | |
831 | 869 | def _crash_msg(self): |
832 | 870 | """ |
| 871 | Return string representation of crash message. |
| 872 | |
833 | 873 | EXAMPLES:: |
834 | 874 | |
835 | 875 | sage: maxima._crash_msg() |
… |
… |
|
837 | 877 | """ |
838 | 878 | print "Maxima crashed -- automatically restarting." |
839 | 879 | |
840 | | def _error_check(self, str, out): |
| 880 | def _error_check(self, cmd, out): |
| 881 | """ |
| 882 | Check string for errors. |
| 883 | |
| 884 | EXAMPLES:: |
| 885 | |
| 886 | sage: maxima._error_check("1+1;","Principal Value") |
| 887 | Traceback (most recent call last): |
| 888 | ... |
| 889 | TypeError: Error executing code in Maxima |
| 890 | CODE: |
| 891 | 1+1; |
| 892 | Maxima ERROR: |
| 893 | Principal Value |
| 894 | """ |
841 | 895 | r = self._error_re |
842 | 896 | m = r.search(out) |
843 | 897 | if not m is None: |
844 | | self._error_msg(str, out) |
| 898 | self._error_msg(cmd, out) |
845 | 899 | |
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);','')) |
| 900 | def _error_msg(self, cmd, out): |
| 901 | """ |
| 902 | Raise error with formated description. |
| 903 | |
| 904 | EXAMPLES:: |
| 905 | |
| 906 | sage: maxima._error_msg("1+1;","Principal Value") |
| 907 | Traceback (most recent call last): |
| 908 | ... |
| 909 | TypeError: Error executing code in Maxima |
| 910 | CODE: |
| 911 | 1+1; |
| 912 | Maxima ERROR: |
| 913 | Principal Value |
| 914 | """ |
| 915 | raise TypeError, "Error executing code in Maxima\nCODE:\n %s\nMaxima ERROR:\n %s"%(cmd, out.replace('-- an error. To debug this try debugmode(true);','')) |
848 | 916 | |
849 | 917 | ########################################### |
850 | 918 | # Direct access to underlying lisp interpreter. |
851 | 919 | ########################################### |
852 | 920 | def lisp(self, cmd): |
853 | 921 | """ |
854 | | Send a lisp command to maxima. |
| 922 | Send a lisp command to Maxima. |
855 | 923 | |
856 | 924 | .. note:: |
857 | 925 | |
… |
… |
|
878 | 946 | |
879 | 947 | INPUT: |
880 | 948 | |
881 | | |
882 | 949 | - ``var`` - string |
883 | 950 | |
884 | 951 | - ``value`` - string |
… |
… |
|
935 | 1002 | |
936 | 1003 | def _function_class(self): |
937 | 1004 | """ |
| 1005 | Return the Python class of Maxima functions. |
| 1006 | |
938 | 1007 | EXAMPLES:: |
939 | 1008 | |
940 | 1009 | sage: maxima._function_class() |
… |
… |
|
954 | 1023 | return MaximaElement |
955 | 1024 | |
956 | 1025 | def _function_element_class(self): |
957 | | """ |
| 1026 | """ |
| 1027 | Return the Python class of Maxima functions of elements. |
| 1028 | |
958 | 1029 | EXAMPLES:: |
959 | 1030 | |
960 | 1031 | sage: maxima._function_element_class() |
… |
… |
|
964 | 1035 | |
965 | 1036 | def _object_function_class(self): |
966 | 1037 | """ |
| 1038 | Return the Python class of Maxima user-defined functions. |
| 1039 | |
967 | 1040 | EXAMPLES:: |
968 | 1041 | |
969 | 1042 | sage: maxima._object_function_class() |
… |
… |
|
971 | 1044 | """ |
972 | 1045 | return MaximaElementFunction |
973 | 1046 | |
974 | | ##some old helper functions to wrap the calculus use of the maxima interface. |
| 1047 | ##some old helper functions to wrap the calculus use of the Maxima interface. |
975 | 1048 | ##these routines expect arguments living in the symbolic ring and return something |
976 | 1049 | ##that is hopefully coercible into the symbolic ring again. |
977 | 1050 | ## |
… |
… |
|
1006 | 1079 | """ |
1007 | 1080 | return isinstance(x, MaximaElement) |
1008 | 1081 | |
1009 | | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
| 1082 | # Thanks to the MRO for multiple inheritance used by the Sage's Python, this should work as expected |
1010 | 1083 | class MaximaElement(MaximaAbstractElement, ExpectElement): |
| 1084 | """ |
| 1085 | Maxima elements. |
| 1086 | |
| 1087 | EXAMPLES: |
| 1088 | |
| 1089 | Elements of this class should not be created directly. |
| 1090 | The targeted parent should be used instead:: |
| 1091 | |
| 1092 | sage: maxima(3) |
| 1093 | 3 |
| 1094 | sage: maxima(cos(x)+e^234) |
| 1095 | cos(x)+%e^234 |
| 1096 | """ |
1011 | 1097 | def __init__(self, parent, value, is_name=False, name=None): |
| 1098 | """ |
| 1099 | Create a Maxima element. |
| 1100 | See ``MaximaElement`` for full documentation. |
| 1101 | |
| 1102 | EXAMPLES:: |
| 1103 | |
| 1104 | sage: maxima(zeta(7)) |
| 1105 | zeta(7) |
| 1106 | """ |
1012 | 1107 | ExpectElement.__init__(self, parent, value, is_name=False, name=None) |
1013 | 1108 | |
1014 | 1109 | def display2d(self, onscreen=True): |
1015 | 1110 | """ |
| 1111 | Return the 2d string representation of this Maxima object. |
| 1112 | |
1016 | 1113 | EXAMPLES:: |
1017 | 1114 | |
1018 | 1115 | sage: F = maxima('x^5 - y^5').factor() |
1019 | | sage: F.display2d () |
| 1116 | sage: F.display2d() |
1020 | 1117 | 4 3 2 2 3 4 |
1021 | 1118 | - (y - x) (y + x y + x y + x y + x ) |
1022 | 1119 | """ |
… |
… |
|
1055 | 1152 | |
1056 | 1153 | # Thanks to the MRO for multiple inheritance used by the Sage's Python , this should work as expected |
1057 | 1154 | class MaximaElementFunction(MaximaElement, MaximaAbstractElementFunction): |
| 1155 | """ |
| 1156 | Maxima user-defined functions. |
| 1157 | |
| 1158 | EXAMPLES: |
| 1159 | |
| 1160 | Elements of this class should not be created directly. |
| 1161 | The method ``function`` of the targeted parent should be used instead:: |
| 1162 | |
| 1163 | sage: maxima.function('x,y','h(x)*y') |
| 1164 | h(x)*y |
| 1165 | """ |
| 1166 | |
1058 | 1167 | def __init__(self, parent, name, defn, args, latex): |
| 1168 | """ |
| 1169 | Create a Maxima function. |
| 1170 | See ``MaximaElementFunction`` for full documentation. |
| 1171 | |
| 1172 | EXAMPLES:: |
| 1173 | |
| 1174 | sage: maxima.function('x,y','cos(x)+y') |
| 1175 | cos(x)+y |
| 1176 | """ |
1059 | 1177 | MaximaElement.__init__(self, parent, name, is_name=True) |
1060 | 1178 | MaximaAbstractElementFunction.__init__(self, parent, name, defn, args, latex) |
1061 | 1179 | |
… |
… |
|
1067 | 1185 | |
1068 | 1186 | def reduce_load_Maxima(): |
1069 | 1187 | """ |
| 1188 | Unpickle a Maxima Pexpect interface. |
| 1189 | |
1070 | 1190 | EXAMPLES:: |
1071 | 1191 | |
1072 | 1192 | sage: from sage.interfaces.maxima import reduce_load_Maxima |
… |
… |
|
1075 | 1195 | """ |
1076 | 1196 | return maxima |
1077 | 1197 | |
| 1198 | # This is defined for compatibility with the old Maxima interface. |
1078 | 1199 | def reduce_load_Maxima_function(parent, defn, args, latex): |
| 1200 | """ |
| 1201 | Unpickle a Maxima function. |
| 1202 | |
| 1203 | EXAMPLES:: |
| 1204 | |
| 1205 | sage: from sage.interfaces.maxima import reduce_load_Maxima_function |
| 1206 | sage: f = maxima.function('x,y','sin(x+y)') |
| 1207 | sage: _,args = f.__reduce__() |
| 1208 | sage: g = reduce_load_Maxima_function(*args) |
| 1209 | sage: g == f |
| 1210 | True |
| 1211 | """ |
1079 | 1212 | return parent.function(args, defn, defn, latex) |
1080 | 1213 | |
1081 | 1214 | def __doctest_cleanup(): |
| 1215 | """ |
| 1216 | Kill all Pexpect interfaces. |
| 1217 | |
| 1218 | EXAMPLES:: |
| 1219 | |
| 1220 | sage: from sage.interfaces.maxima import __doctest_cleanup |
| 1221 | sage: maxima(1) |
| 1222 | 1 |
| 1223 | sage: maxima.is_running() |
| 1224 | True |
| 1225 | sage: __doctest_cleanup() |
| 1226 | sage: maxima.is_running() |
| 1227 | False |
| 1228 | """ |
1082 | 1229 | import sage.interfaces.quit |
1083 | 1230 | sage.interfaces.quit.expect_quitall() |
diff -r 60b965453710 -r 474b56e1b430 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 interface |
| 30 | |
| 31 | This is an abstract class implementing the functions shared between the Pexpect and library interfaces to Maxima. |
31 | 32 | """ |
32 | 33 | |
33 | 34 | #***************************************************************************** |
… |
… |
|
65 | 66 | # documentation from the system -- this could also be useful. |
66 | 67 | |
67 | 68 | class MaximaAbstract(Interface): |
| 69 | r""" |
| 70 | Abstract interface to Maxima. |
| 71 | |
| 72 | INPUT: |
| 73 | |
| 74 | - ``name`` - string |
| 75 | |
| 76 | OUTPUT: |
| 77 | |
| 78 | - the interface |
| 79 | |
| 80 | EXAMPLES: |
| 81 | |
| 82 | This class should not be instantiated directly, |
| 83 | but through its subclasses Maxima (Pexpect interface) |
| 84 | or MaximaLib (library interface):: |
| 85 | |
| 86 | sage: m = Maxima() |
| 87 | sage: from sage.interfaces.maxima_abstract import MaximaAbstract |
| 88 | sage: isinstance(m,MaximaAbstract) |
| 89 | True |
68 | 90 | """ |
69 | | Interface to the Maxima interpreter. |
70 | | """ |
| 91 | |
71 | 92 | def __init__(self, name): |
72 | | """ |
| 93 | r""" |
73 | 94 | Create an instance of an abstract interface to Maxima. |
| 95 | See ``MaximaAbstract`` for full documentation. |
| 96 | |
| 97 | EXAMPLES:: |
| 98 | |
| 99 | sage: from sage.interfaces.maxima_abstract import MaximaAbstract |
| 100 | sage: isinstance(maxima,MaximaAbstract) |
| 101 | True |
74 | 102 | """ |
75 | 103 | Interface.__init__(self,name) |
76 | 104 | |
… |
… |
|
78 | 106 | # System -- change directory, etc |
79 | 107 | ########################################### |
80 | 108 | def chdir(self, dir): |
81 | | """ |
| 109 | r""" |
82 | 110 | Change Maxima's current working directory. |
83 | 111 | |
| 112 | INPUT: |
| 113 | |
| 114 | - ``dir`` - string |
| 115 | |
| 116 | OUTPUT: none |
| 117 | |
84 | 118 | EXAMPLES:: |
85 | 119 | |
86 | 120 | sage: maxima.chdir('/') |
… |
… |
|
91 | 125 | # Interactive help |
92 | 126 | ########################################### |
93 | 127 | def _command_runner(self, command, s, redirect=True): |
94 | | """ |
| 128 | r""" |
95 | 129 | Run ``command`` in a new Maxima session and return its |
96 | 130 | output as an ``AsciiArtString``. |
| 131 | |
| 132 | INPUT: |
97 | 133 | |
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? |
| 134 | - ``command`` - string; function to call |
| 135 | |
| 136 | - ``s`` - string; argument to the function |
| 137 | |
| 138 | - ``redirect`` - boolean (default: True); if redirect is set to False, then the output of the command is not |
| 139 | returned as a string. Instead, it behaves like os.system. This is |
| 140 | used for interactive things like Maxima's demos. See maxima.demo? |
| 141 | |
| 142 | OUTPUT: |
| 143 | |
| 144 | Output of ``command(s)`` as an ``AsciiArtString`` if ``redirect`` is set to False; |
| 145 | None otherwise. |
101 | 146 | |
102 | 147 | EXAMPLES:: |
103 | 148 | |
… |
… |
|
125 | 170 | subprocess.Popen(cmd, shell=True) |
126 | 171 | |
127 | 172 | def help(self, s): |
128 | | """ |
| 173 | r""" |
| 174 | Return Maxima's help for ``s``. |
| 175 | |
| 176 | INPUT: |
| 177 | |
| 178 | - ``s`` - string |
| 179 | |
| 180 | OUTPUT: |
| 181 | |
| 182 | Maxima's help for ``s`` |
| 183 | |
129 | 184 | EXAMPLES:: |
130 | 185 | |
131 | 186 | sage: maxima.help('gcd') |
132 | 187 | -- Function: gcd (<p_1>, <p_2>, <x_1>, ...) |
133 | 188 | ... |
134 | 189 | """ |
| 190 | # Should this be implemented without launching a new Maxima session |
| 191 | # i.e. using eval_line ? |
135 | 192 | return self._command_runner("describe", s) |
136 | 193 | |
137 | 194 | def example(self, s): |
138 | | """ |
| 195 | r""" |
| 196 | Return Maxima's examples for ``s``. |
| 197 | |
| 198 | INPUT: |
| 199 | |
| 200 | - ``s`` - string |
| 201 | |
| 202 | OUTPUT: |
| 203 | |
| 204 | Maxima's examples for ``s`` |
| 205 | |
139 | 206 | EXAMPLES:: |
140 | 207 | |
141 | 208 | sage: maxima.example('arrays') |
… |
… |
|
152 | 219 | 24 |
153 | 220 | done |
154 | 221 | """ |
| 222 | # Should this be implemented without launching a new Maxima session |
| 223 | # i.e. using eval_line ? |
155 | 224 | return self._command_runner("example", s) |
156 | 225 | |
157 | 226 | describe = help |
158 | 227 | |
159 | 228 | def demo(self, s): |
160 | | """ |
| 229 | r""" |
| 230 | Run Maxima's demo for ``s``. |
| 231 | |
| 232 | INPUT: |
| 233 | |
| 234 | - ``s`` - string |
| 235 | |
| 236 | OUTPUT: none |
| 237 | |
161 | 238 | EXAMPLES:: |
162 | 239 | |
163 | 240 | sage: maxima.demo('array') # not tested |
… |
… |
|
166 | 243 | At the _ prompt, type ';' followed by enter to get next demo |
167 | 244 | subscrmap : true _ |
168 | 245 | """ |
| 246 | # Should this be implemented without launching a new Maxima session |
| 247 | # i.e. using eval_line ? |
169 | 248 | return self._command_runner("demo", s, redirect=False) |
170 | 249 | |
171 | 250 | def completions(self, s, verbose=True): |
172 | | """ |
| 251 | r""" |
173 | 252 | Return all commands that complete the command starting with the |
174 | | string s. This is like typing s[tab] in the Maxima interpreter. |
| 253 | string ``s``. This is like typing s[tab] in the Maxima interpreter. |
| 254 | |
| 255 | INPUT: |
| 256 | |
| 257 | - ``s`` - string |
| 258 | |
| 259 | - ``verbose`` - boolean (default: True) |
| 260 | |
| 261 | OUTPUT: array of strings |
175 | 262 | |
176 | 263 | EXAMPLES:: |
177 | 264 | |
… |
… |
|
191 | 278 | def _commands(self, verbose=True): |
192 | 279 | """ |
193 | 280 | Return list of all commands defined in Maxima. |
| 281 | |
| 282 | INPUT: |
| 283 | |
| 284 | - ``verbose`` - boolean (default: True) |
| 285 | |
| 286 | OUTPUT: array of strings |
194 | 287 | |
195 | 288 | EXAMPLES:: |
196 | 289 | |
| 290 | # The output is kind of random |
197 | 291 | sage: sorted(maxima._commands(verbose=False)) |
198 | | ['Alpha', |
199 | | 'Beta', |
| 292 | [... |
| 293 | 'display', |
200 | 294 | ... |
201 | | 'zunderflow'] |
| 295 | 'gcd', |
| 296 | ... |
| 297 | 'verbose', |
| 298 | ...] |
202 | 299 | """ |
203 | 300 | try: |
204 | 301 | return self.__commands |
… |
… |
|
210 | 307 | return self.__commands |
211 | 308 | |
212 | 309 | def trait_names(self, verbose=True, use_disk_cache=True): |
213 | | """ |
| 310 | r""" |
214 | 311 | Return all Maxima commands, which is useful for tab completion. |
| 312 | |
| 313 | INPUT: |
| 314 | |
| 315 | - ``verbose`` - boolean (default: True) |
| 316 | |
| 317 | - ``use_disk_cache`` - boolean (default: True); if set to True, try to read cached result from disk |
| 318 | |
| 319 | OUTPUT: array of strings |
215 | 320 | |
216 | 321 | EXAMPLES:: |
217 | 322 | |
… |
… |
|
247 | 352 | Start the interactive Maxima console. This is a completely separate |
248 | 353 | maxima session from this interface. To interact with this session, |
249 | 354 | you should instead use ``maxima.interact()``. |
| 355 | |
| 356 | INPUT: none |
250 | 357 | |
| 358 | OUTPUT: none |
| 359 | |
251 | 360 | EXAMPLES:: |
252 | 361 | |
253 | 362 | sage: maxima.console() # not tested (since we can't) |
… |
… |
|
273 | 382 | def cputime(self, t=None): |
274 | 383 | r""" |
275 | 384 | 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}. |
| 385 | |
| 386 | INPUT: |
| 387 | |
| 388 | - ``t`` - float (default: None); If \var{t} is not None, then it returns the difference between |
| 389 | the current CPU time and \var{t}. |
| 390 | |
| 391 | OUTPUT: float |
278 | 392 | |
279 | 393 | EXAMPLES: |
280 | 394 | sage: t = maxima.cputime() |
… |
… |
|
288 | 402 | return float(self.eval('elapsed_run_time()')) |
289 | 403 | |
290 | 404 | def version(self): |
291 | | """ |
| 405 | r""" |
292 | 406 | Return the version of Maxima that Sage includes. |
| 407 | |
| 408 | INPUT: none |
| 409 | |
| 410 | OUTPUT: none |
293 | 411 | |
294 | 412 | EXAMPLES:: |
295 | 413 | |
… |
… |
|
303 | 421 | ### |
304 | 422 | |
305 | 423 | def _assign_symbol(self): |
| 424 | r""" |
| 425 | Return the assign symbol in Maxima. |
| 426 | |
| 427 | INPUT: none |
| 428 | |
| 429 | OUTPUT: string |
| 430 | |
| 431 | EXAMPLES:: |
| 432 | |
| 433 | sage: maxima._assign_symbol() |
| 434 | ':' |
| 435 | sage: maxima.eval('t : 8') |
| 436 | '8' |
| 437 | sage: maxima.eval('t') |
| 438 | '8' |
| 439 | """ |
306 | 440 | return ":" |
307 | 441 | |
308 | 442 | def _true_symbol(self): |
309 | 443 | """ |
310 | 444 | Return the true symbol in Maxima. |
| 445 | |
| 446 | INPUT: none |
| 447 | |
| 448 | OUTPUT: string |
311 | 449 | |
312 | 450 | EXAMPLES:: |
313 | 451 | |
… |
… |
|
321 | 459 | def _false_symbol(self): |
322 | 460 | """ |
323 | 461 | Return the false symbol in Maxima. |
| 462 | |
| 463 | INPUT: none |
| 464 | |
| 465 | OUTPUT: string |
324 | 466 | |
325 | 467 | EXAMPLES:: |
326 | 468 | |
… |
… |
|
335 | 477 | """ |
336 | 478 | Returns the equality symbol in Maxima. |
337 | 479 | |
| 480 | INPUT: none |
| 481 | |
| 482 | OUTPUT: string |
| 483 | |
338 | 484 | EXAMPLES:: |
339 | 485 | |
340 | 486 | sage: maxima._equality_symbol() |
341 | 487 | '=' |
| 488 | sage: var('x y') |
| 489 | (x, y) |
| 490 | sage: maxima(x == y) |
| 491 | x=y |
342 | 492 | """ |
343 | 493 | return '=' |
344 | 494 | |
345 | 495 | def _inequality_symbol(self): |
346 | 496 | """ |
347 | | Returns the equality symbol in Maxima. |
| 497 | Returns the inequality symbol in Maxima. |
| 498 | |
| 499 | INPUT: none |
| 500 | |
| 501 | OUTPUT: string |
348 | 502 | |
349 | 503 | EXAMPLES:: |
350 | 504 | |
… |
… |
|
357 | 511 | |
358 | 512 | def _function_class(self): |
359 | 513 | """ |
| 514 | Return the Python class of Maxima functions. |
| 515 | |
| 516 | INPUT: none |
| 517 | |
| 518 | OUTPUT: type |
| 519 | |
360 | 520 | EXAMPLES:: |
361 | 521 | |
362 | 522 | sage: maxima._function_class() |
… |
… |
|
367 | 527 | def _object_class(self): |
368 | 528 | """ |
369 | 529 | Return the Python class of Maxima elements. |
| 530 | |
| 531 | INPUT: none |
| 532 | |
| 533 | OUTPUT: type |
370 | 534 | |
371 | 535 | EXAMPLES:: |
372 | 536 | |
… |
… |
|
376 | 540 | return MaximaAbstractElement |
377 | 541 | |
378 | 542 | def _function_element_class(self): |
379 | | """ |
| 543 | """ |
| 544 | Return the Python class of Maxima functions of elements. |
| 545 | |
| 546 | INPUT: none |
| 547 | |
| 548 | OUTPUT: type |
| 549 | |
380 | 550 | EXAMPLES:: |
381 | 551 | |
382 | 552 | sage: maxima._function_element_class() |
… |
… |
|
386 | 556 | |
387 | 557 | def _object_function_class(self): |
388 | 558 | """ |
| 559 | Return the Python class of Maxima user-defined functions. |
| 560 | |
| 561 | INPUT: none |
| 562 | |
| 563 | OUTPUT: type |
| 564 | |
389 | 565 | EXAMPLES:: |
390 | 566 | |
391 | 567 | sage: maxima._object_function_class() |
… |
… |
|
393 | 569 | """ |
394 | 570 | return MaximaAbstractElementFunction |
395 | 571 | |
396 | | #### |
397 | | # |
398 | | #### |
| 572 | #################### |
| 573 | # Maxima functions # |
| 574 | #################### |
399 | 575 | |
400 | 576 | def function(self, args, defn, rep=None, latex=None): |
401 | 577 | """ |
… |
… |
|
403 | 579 | |
404 | 580 | INPUT: |
405 | 581 | |
406 | | |
407 | 582 | - ``args`` - a string with variable names separated by |
408 | 583 | commas |
409 | 584 | |
… |
… |
|
413 | 588 | - ``rep`` - an optional string; if given, this is how |
414 | 589 | the function will print. |
415 | 590 | |
| 591 | OUTPUT: Maxima function |
416 | 592 | |
417 | 593 | EXAMPLES:: |
418 | 594 | |
… |
… |
|
433 | 609 | sage: g(1,2,3) |
434 | 610 | 3*(cos(2)+sin(1)) |
435 | 611 | |
436 | | The function definition can be a maxima object:: |
| 612 | The function definition can be a Maxima object:: |
437 | 613 | |
438 | 614 | sage: an_expr = maxima('sin(x)*gamma(x)') |
439 | 615 | sage: t = maxima.function('x', an_expr) |
… |
… |
|
461 | 637 | rep = defn |
462 | 638 | f = self._object_function_class()(self, name, rep, args, latex) |
463 | 639 | return f |
464 | | |
465 | | ##### |
466 | | # Maxima functions |
467 | | ##### |
468 | 640 | |
469 | 641 | ## def display2d(self, flag=True): |
470 | 642 | ## """ |
… |
… |
|
496 | 668 | |
497 | 669 | INPUT: |
498 | 670 | |
499 | | |
500 | 671 | - ``f`` - a string representing a function (such as |
501 | 672 | f="sin(x)") [var, xmin, xmax] |
502 | 673 | |
503 | 674 | - ``options`` - an optional string representing plot2d |
504 | 675 | options in gnuplot format |
505 | 676 | |
506 | | |
507 | 677 | EXAMPLES:: |
508 | 678 | |
509 | 679 | sage: maxima.plot2d('sin(x)','[x,-5,5]') # not tested |
… |
… |
|
516 | 686 | |
517 | 687 | def plot2d_parametric(self, r, var, trange, nticks=50, options=None): |
518 | 688 | r""" |
519 | | Plots r = [x(t), y(t)] for t = tmin...tmax using gnuplot with |
520 | | options |
| 689 | Plot r = [x(t), y(t)] for t = tmin...tmax using gnuplot with |
| 690 | options. |
521 | 691 | |
522 | 692 | INPUT: |
523 | | |
524 | | |
| 693 | |
525 | 694 | - ``r`` - a string representing a function (such as |
526 | 695 | r="[x(t),y(t)]") |
527 | 696 | |
… |
… |
|
535 | 704 | - ``options`` - an optional string representing plot2d |
536 | 705 | options in gnuplot format |
537 | 706 | |
538 | | |
539 | 707 | EXAMPLES:: |
540 | 708 | |
541 | 709 | sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t",[-3.1,3.1]) # not tested |
… |
… |
|
570 | 738 | |
571 | 739 | INPUT: |
572 | 740 | |
573 | | |
574 | 741 | - ``f`` - a string representing a function (such as |
575 | 742 | f="sin(x)") [var, min, max] |
576 | 743 | |
577 | | |
| 744 | - ``args`` should be of the form '[x, xmin, xmax]', '[y, ymin, ymax]', '[grid, nx, ny]', options |
| 745 | |
578 | 746 | EXAMPLES:: |
579 | 747 | |
580 | 748 | sage: maxima.plot3d('1 + x^3 - y^2', '[x,-2,2]', '[y,-2,2]', '[grid,12,12]') # not tested |
… |
… |
|
594 | 762 | |
595 | 763 | INPUT: |
596 | 764 | |
597 | | |
598 | 765 | - ``x, y, z`` - a string representing a function (such |
599 | 766 | as ``x="u2+v2"``, ...) vars is a list or two strings |
600 | 767 | representing variables (such as vars = ["u","v"]) |
… |
… |
|
607 | 774 | - ``options`` - optional string representing plot2d |
608 | 775 | options in gnuplot format |
609 | 776 | |
610 | | |
611 | 777 | OUTPUT: displays a plot on screen or saves to a file |
612 | 778 | |
613 | 779 | EXAMPLES:: |
… |
… |
|
649 | 815 | |
650 | 816 | INPUT: |
651 | 817 | |
652 | | |
653 | 818 | - ``de`` - a string representing the ODE |
654 | 819 | |
655 | 820 | - ``vars`` - a list of strings representing the two |
… |
… |
|
658 | 823 | - ``ics`` - a triple of numbers [a,b1,b2] representing |
659 | 824 | y(a)=b1, y'(a)=b2 |
660 | 825 | |
661 | | |
662 | 826 | EXAMPLES:: |
663 | 827 | |
664 | 828 | sage: maxima.de_solve('diff(y,x,2) + 3*x = y', ['x','y'], [1,1,1]) |
… |
… |
|
692 | 856 | |
693 | 857 | INPUT: |
694 | 858 | |
695 | | |
696 | 859 | - ``de`` - a string representing the ODE (e.g., de = |
697 | 860 | "diff(f(x),x,2)=diff(f(x),x)+sin(x)") |
698 | 861 | |
… |
… |
|
703 | 866 | conditions, with symbols allowed which are represented by strings |
704 | 867 | (eg, f(0)=1, f'(0)=2 is ics = [0,1,2]) |
705 | 868 | |
706 | | |
707 | 869 | EXAMPLES:: |
708 | 870 | |
709 | 871 | sage: maxima.clear('x'); maxima.clear('f') |
… |
… |
|
742 | 904 | """ |
743 | 905 | Wraps maxima's linsolve. |
744 | 906 | |
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 |
| 907 | INPUT: |
| 908 | |
| 909 | - ``eqns`` - a list of m strings; each representing a linear |
| 910 | question in m = n variables |
| 911 | - ``vars`` - a list of n strings; each |
747 | 912 | representing a variable |
748 | 913 | |
749 | 914 | EXAMPLES:: |
… |
… |
|
772 | 937 | Finds a unit of the ring of integers of the quadratic number field |
773 | 938 | `\QQ(\sqrt{n})`, `n>1`, using the qunit maxima |
774 | 939 | command. |
| 940 | |
| 941 | INPUT: |
| 942 | |
| 943 | - ``n`` - an integer |
775 | 944 | |
776 | 945 | EXAMPLES:: |
777 | 946 | |
… |
… |
|
804 | 973 | |
805 | 974 | INPUT: |
806 | 975 | |
807 | | |
808 | 976 | - ``ptsx`` - [x1,...,xn], where the xi and yi are |
809 | 977 | real, |
810 | 978 | |
… |
… |
|
813 | 981 | - ``options`` - a string representing maxima plot2d |
814 | 982 | options. |
815 | 983 | |
816 | | |
817 | 984 | The points are (x1,y1), (x2,y2), etc. |
818 | 985 | |
819 | 986 | This function requires maxima 5.9.2 or newer. |
… |
… |
|
845 | 1012 | where each ptsi is of the form [[x1,y1],...,[xn,yn]] x's must be |
846 | 1013 | integers and y's reals options is a string representing maxima |
847 | 1014 | plot2d options. |
| 1015 | |
| 1016 | INPUT: |
| 1017 | |
| 1018 | - ``pts_lst`` - list of points; each point must be of the form [x,y] |
| 1019 | where ``x`` is an integer and ``y`` is a real |
| 1020 | - ``var`` - string; representing Maxima's plot2d options |
848 | 1021 | |
849 | 1022 | Requires maxima 5.9.2 at least. |
850 | 1023 | |
… |
… |
|
885 | 1058 | class MaximaAbstractElement(InterfaceElement): |
886 | 1059 | def __str__(self): |
887 | 1060 | """ |
888 | | Printing an object explicitly gives ASCII art: |
| 1061 | Printing an object explicitly gives ASCII art. |
| 1062 | |
| 1063 | INPUT: none |
| 1064 | |
| 1065 | OUTPUT: string |
889 | 1066 | |
890 | 1067 | EXAMPLES:: |
891 | 1068 | |
… |
… |
|
901 | 1078 | |
902 | 1079 | def bool(self): |
903 | 1080 | """ |
| 1081 | Convert ``self`` into a boolean. |
| 1082 | |
| 1083 | INPUT: none |
| 1084 | |
| 1085 | OUTPUT: boolean |
| 1086 | |
904 | 1087 | EXAMPLES:: |
905 | 1088 | |
906 | 1089 | sage: maxima(0).bool() |
… |
… |
|
913 | 1096 | |
914 | 1097 | def __cmp__(self, other): |
915 | 1098 | """ |
| 1099 | Compare this Maxima object with ``other``. |
| 1100 | |
| 1101 | INPUT: |
| 1102 | |
| 1103 | - ``other`` - an object to compare to |
| 1104 | |
| 1105 | OUTPUT: integer |
| 1106 | |
916 | 1107 | EXAMPLES:: |
917 | 1108 | |
918 | 1109 | sage: a = maxima(1); b = maxima(2) |
… |
… |
|
952 | 1143 | |
953 | 1144 | def _sage_(self): |
954 | 1145 | """ |
955 | | Attempt to make a native Sage object out of this maxima object. |
| 1146 | Attempt to make a native Sage object out of this Maxima object. |
956 | 1147 | This is useful for automatic coercions in addition to other |
957 | 1148 | things. |
| 1149 | |
| 1150 | INPUT: none |
| 1151 | |
| 1152 | OUTPUT: Sage object |
958 | 1153 | |
959 | 1154 | EXAMPLES:: |
960 | 1155 | |
… |
… |
|
1000 | 1195 | |
1001 | 1196 | def _symbolic_(self, R): |
1002 | 1197 | """ |
1003 | | Return a symbolic expression equivalent to this maxima object. |
| 1198 | Return a symbolic expression equivalent to this Maxima object. |
| 1199 | |
| 1200 | INPUT: |
| 1201 | |
| 1202 | - ``R`` - symbolic ring to convert into |
| 1203 | |
| 1204 | OUTPUT: symbolic expression |
1004 | 1205 | |
1005 | 1206 | EXAMPLES:: |
1006 | 1207 | |
… |
… |
|
1010 | 1211 | sage: u.parent() |
1011 | 1212 | Symbolic Ring |
1012 | 1213 | |
1013 | | This is used when converting maxima objects to the Symbolic Ring:: |
| 1214 | This is used when converting Maxima objects to the Symbolic Ring:: |
1014 | 1215 | |
1015 | 1216 | sage: SR(t) |
1016 | 1217 | sqrt(2) |
… |
… |
|
1019 | 1220 | |
1020 | 1221 | def __complex__(self): |
1021 | 1222 | """ |
| 1223 | Return a complex number equivalent to this Maxima object. |
| 1224 | |
| 1225 | INPUT: none |
| 1226 | |
| 1227 | OUTPUT: complex |
| 1228 | |
1022 | 1229 | EXAMPLES:: |
1023 | 1230 | |
1024 | 1231 | sage: complex(maxima('sqrt(-2)+1')) |
… |
… |
|
1028 | 1235 | |
1029 | 1236 | def _complex_mpfr_field_(self, C): |
1030 | 1237 | """ |
| 1238 | Return a mpfr complex number equivalent to this Maxima object. |
| 1239 | |
| 1240 | INPUT: |
| 1241 | |
| 1242 | - ``C`` - complex numbers field to convert into |
| 1243 | |
| 1244 | OUTPUT: complex |
| 1245 | |
1031 | 1246 | EXAMPLES:: |
1032 | 1247 | |
1033 | 1248 | sage: CC(maxima('1+%i')) |
… |
… |
|
1047 | 1262 | |
1048 | 1263 | def _mpfr_(self, R): |
1049 | 1264 | """ |
| 1265 | Return a mpfr real number equivalent to this Maxima object. |
| 1266 | |
| 1267 | INPUT: |
| 1268 | |
| 1269 | - ``R`` - real numbers field to convert into |
| 1270 | |
| 1271 | OUTPUT: real |
| 1272 | |
1050 | 1273 | EXAMPLES:: |
1051 | 1274 | |
1052 | 1275 | sage: RealField(100)(maxima('sqrt(2)+1')) |
… |
… |
|
1056 | 1279 | |
1057 | 1280 | def _complex_double_(self, C): |
1058 | 1281 | """ |
| 1282 | Return a double precision complex number equivalent to this Maxima object. |
| 1283 | |
| 1284 | INPUT: |
| 1285 | |
| 1286 | - ``C`` - double precision complex numbers field to convert into |
| 1287 | |
| 1288 | OUTPUT: complex |
| 1289 | |
1059 | 1290 | EXAMPLES:: |
1060 | 1291 | |
1061 | 1292 | sage: CDF(maxima('sqrt(2)+1')) |
… |
… |
|
1065 | 1296 | |
1066 | 1297 | def _real_double_(self, R): |
1067 | 1298 | """ |
| 1299 | Return a double precision real number equivalent to this Maxima object. |
| 1300 | |
| 1301 | INPUT: |
| 1302 | |
| 1303 | - ``R`` - double precision real numbers field to convert into |
| 1304 | |
| 1305 | OUTPUT: real |
| 1306 | |
1068 | 1307 | EXAMPLES:: |
1069 | 1308 | |
1070 | 1309 | sage: RDF(maxima('sqrt(2)+1')) |
… |
… |
|
1074 | 1313 | |
1075 | 1314 | def real(self): |
1076 | 1315 | """ |
1077 | | Return the real part of this maxima element. |
| 1316 | Return the real part of this Maxima element. |
1078 | 1317 | |
| 1318 | INPUT: none |
| 1319 | |
| 1320 | OUTPUT: Maxima real |
| 1321 | |
1079 | 1322 | EXAMPLES:: |
1080 | 1323 | |
1081 | 1324 | sage: maxima('2 + (2/3)*%i').real() |
… |
… |
|
1085 | 1328 | |
1086 | 1329 | def imag(self): |
1087 | 1330 | """ |
1088 | | Return the imaginary part of this maxima element. |
| 1331 | Return the imaginary part of this Maxima element. |
| 1332 | |
| 1333 | INPUT: none |
| 1334 | |
| 1335 | OUTPUT: Maxima real |
1089 | 1336 | |
1090 | 1337 | EXAMPLES:: |
1091 | 1338 | |
… |
… |
|
1098 | 1345 | """ |
1099 | 1346 | Return numerical approximation to self as a Maxima object. |
1100 | 1347 | |
| 1348 | INPUT: none |
| 1349 | |
| 1350 | OUTPUT: Maxima object |
| 1351 | |
1101 | 1352 | EXAMPLES:: |
1102 | 1353 | |
1103 | 1354 | sage: a = maxima('sqrt(2)').numer(); a |
… |
… |
|
1109 | 1360 | |
1110 | 1361 | def str(self): |
1111 | 1362 | """ |
1112 | | Return string representation of this maxima object. |
| 1363 | Return string representation of this Maxima object. |
| 1364 | |
| 1365 | INPUT: none |
| 1366 | |
| 1367 | OUTPUT: string |
1113 | 1368 | |
1114 | 1369 | EXAMPLES:: |
1115 | 1370 | |
… |
… |
|
1121 | 1376 | |
1122 | 1377 | def __repr__(self): |
1123 | 1378 | """ |
1124 | | Return print representation of this object. |
| 1379 | Return print representation of this Maxima object. |
| 1380 | |
| 1381 | INPUT: none |
| 1382 | |
| 1383 | OUTPUT: string |
| 1384 | |
| 1385 | The result is cached. |
1125 | 1386 | |
1126 | 1387 | EXAMPLES:: |
1127 | 1388 | |
… |
… |
|
1143 | 1404 | |
1144 | 1405 | INPUT: |
1145 | 1406 | |
1146 | | |
1147 | 1407 | - ``var`` - variable (default: 'x') |
1148 | 1408 | |
1149 | 1409 | - ``n`` - integer (default: 1) |
1150 | 1410 | |
1151 | | |
1152 | 1411 | OUTPUT: n-th derivative of self with respect to the variable var |
1153 | 1412 | |
1154 | 1413 | EXAMPLES:: |
… |
… |
|
1184 | 1443 | |
1185 | 1444 | INPUT: |
1186 | 1445 | |
1187 | | |
1188 | 1446 | - ``var`` - variable to integrate with respect to |
1189 | 1447 | |
1190 | 1448 | - ``a`` - lower endpoint of integration |
… |
… |
|
1196 | 1454 | |
1197 | 1455 | - ``maximum_num_subintervals`` - (default: 200) |
1198 | 1456 | maxima number of subintervals |
1199 | | |
1200 | | |
| 1457 | |
1201 | 1458 | OUTPUT: |
1202 | 1459 | |
1203 | | |
1204 | 1460 | - approximation to the integral |
1205 | 1461 | |
1206 | 1462 | - estimated absolute error of the |
… |
… |
|
1224 | 1480 | |
1225 | 1481 | - ``6`` - the input is invalid |
1226 | 1482 | |
1227 | | |
1228 | 1483 | EXAMPLES:: |
1229 | 1484 | |
1230 | 1485 | sage: maxima('exp(-sqrt(x))').nintegral('x',0,1) |
… |
… |
|
1251 | 1506 | |
1252 | 1507 | INPUT: |
1253 | 1508 | |
1254 | | |
1255 | 1509 | - ``var`` - variable |
1256 | 1510 | |
1257 | 1511 | - ``min`` - default: None |
1258 | 1512 | |
1259 | 1513 | - ``max`` - default: None |
1260 | 1514 | |
| 1515 | OUTPUT: |
1261 | 1516 | |
1262 | | Returns the definite integral if xmin is not None, otherwise |
1263 | | returns an indefinite integral. |
| 1517 | - the definite integral if xmin is not None |
| 1518 | |
| 1519 | - an indefinite integral otherwise |
1264 | 1520 | |
1265 | 1521 | EXAMPLES:: |
1266 | 1522 | |
… |
… |
|
1296 | 1552 | |
1297 | 1553 | def __float__(self): |
1298 | 1554 | """ |
1299 | | Return floating point version of this maxima element. |
| 1555 | Return floating point version of this Maxima element. |
| 1556 | |
| 1557 | INPUT: none |
| 1558 | |
| 1559 | OUTPUT: real |
1300 | 1560 | |
1301 | 1561 | EXAMPLES:: |
1302 | 1562 | |
… |
… |
|
1315 | 1575 | def __len__(self): |
1316 | 1576 | """ |
1317 | 1577 | Return the length of a list. |
| 1578 | |
| 1579 | INPUT: none |
| 1580 | |
| 1581 | OUTPUT: integer |
1318 | 1582 | |
1319 | 1583 | EXAMPLES:: |
1320 | 1584 | |
… |
… |
|
1328 | 1592 | def dot(self, other): |
1329 | 1593 | """ |
1330 | 1594 | Implements the notation self . other. |
| 1595 | |
| 1596 | INPUT: |
| 1597 | |
| 1598 | - ``other`` - matrix; argument to dot. |
| 1599 | |
| 1600 | OUTPUT: Maxima matrix |
1331 | 1601 | |
1332 | 1602 | EXAMPLES:: |
1333 | 1603 | |
… |
… |
|
1343 | 1613 | def __getitem__(self, n): |
1344 | 1614 | r""" |
1345 | 1615 | Return the n-th element of this list. |
| 1616 | |
| 1617 | INPUT: |
| 1618 | |
| 1619 | - ``n`` - integer |
| 1620 | |
| 1621 | OUTPUT: Maxima object |
1346 | 1622 | |
1347 | 1623 | .. note:: |
1348 | 1624 | |
… |
… |
|
1370 | 1646 | |
1371 | 1647 | def __iter__(self): |
1372 | 1648 | """ |
1373 | | EXAMPLE:: |
| 1649 | Return an iterator for self. |
| 1650 | |
| 1651 | INPUT: none |
| 1652 | |
| 1653 | OUTPUT: iterator |
| 1654 | |
| 1655 | EXAMPLES:: |
1374 | 1656 | |
1375 | 1657 | sage: v = maxima('create_list(i*x^i,i,0,5)') |
1376 | 1658 | sage: L = list(v) |
… |
… |
|
1383 | 1665 | def subst(self, val): |
1384 | 1666 | """ |
1385 | 1667 | Substitute a value or several values into this Maxima object. |
| 1668 | |
| 1669 | INPUT: |
| 1670 | |
| 1671 | - ``val`` - string representing substitution(s) to perform |
| 1672 | |
| 1673 | OUTPUT: Maxima object |
1386 | 1674 | |
1387 | 1675 | EXAMPLES:: |
1388 | 1676 | |
… |
… |
|
1398 | 1686 | def comma(self, args): |
1399 | 1687 | """ |
1400 | 1688 | Form the expression that would be written 'self, args' in Maxima. |
| 1689 | |
| 1690 | INPUT: |
| 1691 | |
| 1692 | - ``args`` - string |
| 1693 | |
| 1694 | OUTPUT: Maxima object |
1401 | 1695 | |
1402 | 1696 | EXAMPLES:: |
1403 | 1697 | |
… |
… |
|
1413 | 1707 | def _latex_(self): |
1414 | 1708 | """ |
1415 | 1709 | Return Latex representation of this Maxima object. |
| 1710 | |
| 1711 | INPUT: none |
| 1712 | |
| 1713 | OUTPUT: string |
1416 | 1714 | |
1417 | 1715 | This calls the tex command in Maxima, then does a little |
1418 | 1716 | post-processing to fix bugs in the resulting Maxima output. |
… |
… |
|
1438 | 1736 | P = self.parent() |
1439 | 1737 | s = P._eval_line('tex(%s);'%self.name(), reformat=False) |
1440 | 1738 | if not '$$' in s: |
1441 | | raise RuntimeError, "Error texing maxima object." |
| 1739 | raise RuntimeError, "Error texing Maxima object." |
1442 | 1740 | i = s.find('$$') |
1443 | 1741 | j = s.rfind('$$') |
1444 | 1742 | s = s[i+2:j] |
… |
… |
|
1462 | 1760 | def trait_names(self, verbose=False): |
1463 | 1761 | """ |
1464 | 1762 | Return all Maxima commands, which is useful for tab completion. |
| 1763 | |
| 1764 | INPUT: |
| 1765 | |
| 1766 | - ``verbose`` - boolean |
| 1767 | |
| 1768 | OUTPUT: list of strings |
1465 | 1769 | |
1466 | 1770 | EXAMPLES:: |
1467 | 1771 | |
… |
… |
|
1476 | 1780 | If self is a Maxima matrix, return the corresponding Sage matrix |
1477 | 1781 | over the Sage ring `R`. |
1478 | 1782 | |
| 1783 | INPUT: |
| 1784 | |
| 1785 | - ``R`` - ring to coerce into |
| 1786 | |
| 1787 | OUTPUT: matrix |
| 1788 | |
1479 | 1789 | This may or may not work depending in how complicated the entries |
1480 | 1790 | of self are! It only works if the entries of self can be coerced as |
1481 | 1791 | strings to produce meaningful elements of `R`. |
… |
… |
|
1517 | 1827 | """ |
1518 | 1828 | Return the partial fraction decomposition of self with respect to |
1519 | 1829 | the variable var. |
| 1830 | |
| 1831 | INPUT: |
| 1832 | |
| 1833 | - ``var`` - string |
| 1834 | |
| 1835 | OUTPUT: Maxima object |
1520 | 1836 | |
1521 | 1837 | EXAMPLES:: |
1522 | 1838 | |
… |
… |
|
1532 | 1848 | |
1533 | 1849 | def _operation(self, operation, right): |
1534 | 1850 | r""" |
| 1851 | Return the result of "self operation right" in Maxima. |
| 1852 | |
| 1853 | INPUT: |
| 1854 | |
| 1855 | - ``operation`` - string; operator |
| 1856 | |
| 1857 | - ``right`` - Maxima object; right operand |
| 1858 | |
| 1859 | OUTPUT: Maxima object |
| 1860 | |
1535 | 1861 | Note that right's parent should already be Maxima since this should |
1536 | 1862 | be called after coercion has been performed. |
1537 | 1863 | |
… |
… |
|
1568 | 1894 | |
1569 | 1895 | |
1570 | 1896 | class MaximaAbstractElementFunction(MaximaAbstractElement): |
| 1897 | r""" |
| 1898 | Create a Maxima function with the parent ``parent``, |
| 1899 | name ``name``, definition ``defn``, arguments ``args`` |
| 1900 | and latex representation ``latex``. |
| 1901 | |
| 1902 | INPUT: |
| 1903 | |
| 1904 | - ``parent`` - an instance of a concrete Maxima interface |
| 1905 | |
| 1906 | - ``name`` - string |
| 1907 | |
| 1908 | - ``defn`` - string |
| 1909 | |
| 1910 | - ``args`` - string; comma separated names of arguments |
| 1911 | |
| 1912 | - ``latex`` - string |
| 1913 | |
| 1914 | OUTPUT: Maxima function |
| 1915 | |
| 1916 | EXAMPLES:: |
| 1917 | |
| 1918 | sage: f = maxima.function('x,y','sin(x+y)') |
| 1919 | sage: f == loads(dumps(f)) |
| 1920 | True |
| 1921 | """ |
| 1922 | |
1571 | 1923 | def __init__(self, parent, name, defn, args, latex): |
1572 | 1924 | """ |
| 1925 | Create a Maxima function. |
| 1926 | See ``MaximaAbstractElementFunction`` for full documentation. |
| 1927 | |
1573 | 1928 | EXAMPLES:: |
1574 | 1929 | |
1575 | 1930 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1583 | 1938 | |
1584 | 1939 | def __reduce__(self): |
1585 | 1940 | """ |
| 1941 | Implement __reduce__ for ``MaximaAbstractElementFunction``. |
| 1942 | |
| 1943 | INPUT: none |
| 1944 | |
| 1945 | OUTPUT: |
| 1946 | |
| 1947 | A couple consisting of: |
| 1948 | |
| 1949 | - the function to call for unpickling |
| 1950 | |
| 1951 | - a tuple of arguments for the function |
| 1952 | |
1586 | 1953 | EXAMPLES:: |
1587 | 1954 | |
1588 | 1955 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1594 | 1961 | |
1595 | 1962 | def __call__(self, *x): |
1596 | 1963 | """ |
| 1964 | Return the result of calling this Maxima function with arguments ``*x``. |
| 1965 | |
| 1966 | INPUT: |
| 1967 | |
| 1968 | - ``x`` - a variable number of arguments |
| 1969 | |
| 1970 | OUTPUT: Maxima object |
| 1971 | |
1597 | 1972 | EXAMPLES:: |
1598 | 1973 | |
1599 | 1974 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1609 | 1984 | |
1610 | 1985 | def __repr__(self): |
1611 | 1986 | """ |
| 1987 | Return print representation of this Maxima function. |
| 1988 | |
| 1989 | INPUT: none |
| 1990 | |
| 1991 | OUTPUT: string |
| 1992 | |
1612 | 1993 | EXAMPLES:: |
1613 | 1994 | |
1614 | 1995 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1619 | 2000 | |
1620 | 2001 | def _latex_(self): |
1621 | 2002 | """ |
| 2003 | Return latex representation of this Maxima function. |
| 2004 | |
| 2005 | INPUT: none |
| 2006 | |
| 2007 | OUTPUT: string |
| 2008 | |
1622 | 2009 | EXAMPLES:: |
1623 | 2010 | |
1624 | 2011 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1633 | 2020 | def arguments(self, split=True): |
1634 | 2021 | r""" |
1635 | 2022 | Returns the arguments of this Maxima function. |
| 2023 | |
| 2024 | INPUT: |
| 2025 | |
| 2026 | - ``split`` - boolean; if True return a tuple of strings, |
| 2027 | otherwise return a string of comma-separated arguments |
| 2028 | |
| 2029 | OUTPUT: |
| 2030 | |
| 2031 | - a string if ``split`` is False |
| 2032 | |
| 2033 | - a list of strings if ``split`` is True |
1636 | 2034 | |
1637 | 2035 | EXAMPLES:: |
1638 | 2036 | |
… |
… |
|
1653 | 2051 | def definition(self): |
1654 | 2052 | """ |
1655 | 2053 | Returns the definition of this Maxima function as a string. |
| 2054 | |
| 2055 | INPUT: none |
| 2056 | |
| 2057 | OUTPUT: string |
1656 | 2058 | |
1657 | 2059 | EXAMPLES:: |
1658 | 2060 | |
… |
… |
|
1665 | 2067 | def integral(self, var): |
1666 | 2068 | """ |
1667 | 2069 | Returns the integral of self with respect to the variable var. |
| 2070 | |
| 2071 | INPUT: |
| 2072 | |
| 2073 | - ``var`` - a variable |
| 2074 | |
| 2075 | OUTPUT: Maxima function |
1668 | 2076 | |
1669 | 2077 | Note that integrate is an alias of integral. |
1670 | 2078 | |
… |
… |
|
1692 | 2100 | r""" |
1693 | 2101 | This is a utility function which factors out much of the |
1694 | 2102 | commonality used in the arithmetic operations for |
1695 | | ``MaximaFunctions``. |
| 2103 | ``MaximaAbstractElementFunction``. |
1696 | 2104 | |
1697 | 2105 | INPUT: |
1698 | 2106 | |
1699 | | |
1700 | 2107 | - ``operation`` - A string representing the operation |
1701 | 2108 | being performed. For example, '\*', or '1/'. |
1702 | 2109 | |
1703 | 2110 | - ``f`` - The other operand. If f is |
1704 | | ``None``, than the operation is assumed to be unary |
| 2111 | ``None``, then the operation is assumed to be unary |
1705 | 2112 | rather than binary. |
1706 | 2113 | |
1707 | | |
1708 | 2114 | EXAMPLES:: |
1709 | 2115 | |
1710 | 2116 | sage: f = maxima.function('x,y','sin(x+y)') |
… |
… |
|
1733 | 2139 | |
1734 | 2140 | def _add_(self, f): |
1735 | 2141 | """ |
1736 | | MaximaFunction as left summand. |
| 2142 | This Maxima function as left summand. |
1737 | 2143 | |
1738 | 2144 | EXAMPLES:: |
1739 | 2145 | |
… |
… |
|
1769 | 2175 | |
1770 | 2176 | def _sub_(self, f): |
1771 | 2177 | r""" |
1772 | | ``MaximaFunction`` as minuend. |
| 2178 | This Maxima function as minuend. |
1773 | 2179 | |
1774 | 2180 | EXAMPLES:: |
1775 | 2181 | |
… |
… |
|
1794 | 2200 | |
1795 | 2201 | def _mul_(self, f): |
1796 | 2202 | r""" |
1797 | | ``MaximaFunction`` as left factor. |
| 2203 | This Maxima function as left factor. |
1798 | 2204 | |
1799 | 2205 | EXAMPLES:: |
1800 | 2206 | |
… |
… |
|
1820 | 2226 | |
1821 | 2227 | def _div_(self, f): |
1822 | 2228 | r""" |
1823 | | ``MaximaFunction`` as dividend. |
| 2229 | This Maxima function as dividend. |
1824 | 2230 | |
1825 | 2231 | EXAMPLES:: |
1826 | 2232 | |
… |
… |
|
1846 | 2252 | |
1847 | 2253 | def __neg__(self): |
1848 | 2254 | r""" |
1849 | | Additive inverse of a ``MaximaFunction``. |
| 2255 | Additive inverse of this Maxima function. |
1850 | 2256 | |
1851 | 2257 | EXAMPLES:: |
1852 | 2258 | |
… |
… |
|
1858 | 2264 | |
1859 | 2265 | def __inv__(self): |
1860 | 2266 | r""" |
1861 | | Multiplicative inverse of a ``MaximaFunction``. |
| 2267 | Multiplicative inverse of this Maxima function. |
1862 | 2268 | |
1863 | 2269 | EXAMPLES:: |
1864 | 2270 | |
… |
… |
|
1870 | 2276 | |
1871 | 2277 | def __pow__(self,f): |
1872 | 2278 | r""" |
1873 | | ``MaximaFunction`` raised to some power. |
| 2279 | This Maxima function raised to some power. |
1874 | 2280 | |
1875 | 2281 | EXAMPLES:: |
1876 | 2282 | |
… |
… |
|
1890 | 2296 | |
1891 | 2297 | |
1892 | 2298 | def reduce_load_MaximaAbstract_function(parent, defn, args, latex): |
| 2299 | r""" |
| 2300 | Unpickle a Maxima function. |
| 2301 | |
| 2302 | EXAMPLES:: |
| 2303 | |
| 2304 | sage: from sage.interfaces.maxima_abstract import reduce_load_MaximaAbstract_function |
| 2305 | sage: f = maxima.function('x,y','sin(x+y)') |
| 2306 | sage: _,args = f.__reduce__() |
| 2307 | sage: g = reduce_load_MaximaAbstract_function(*args) |
| 2308 | sage: g == f |
| 2309 | True |
| 2310 | """ |
1893 | 2311 | return parent.function(args, defn, defn, latex) |
1894 | 2312 | |
1895 | 2313 | def maxima_version(): |
1896 | 2314 | """ |
| 2315 | Return Maxima version. |
| 2316 | |
| 2317 | Currently this calls a new copy of Maxima. |
| 2318 | |
1897 | 2319 | EXAMPLES:: |
1898 | 2320 | |
1899 | 2321 | sage: from sage.interfaces.maxima_abstract import maxima_version |
diff -r 60b965453710 -r 474b56e1b430 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 interface |
| 30 | |
| 31 | For this interface, Maxima is loaded into ECL which is itself loaded as a C library in Sage. |
| 32 | Translations between Sage and Maxima objects (which are nothing but wrappers to ECL objects) |
| 33 | is made as much as possible directly, but falls back to the string based conversion |
| 34 | used by the classical Maxima Pexpect interface in case no new implementation has been made. |
| 35 | |
| 36 | This interface is the one used for calculus by Sage and is accessible as maxima_calculus:: |
| 37 | |
| 38 | sage: maxima_calculus |
| 39 | Maxima_lib |
| 40 | |
| 41 | Only one instance of this interface can be instantiated, |
| 42 | so the user should not try to instantiate another one, |
| 43 | which is anyway set to raise an error:: |
| 44 | |
| 45 | sage: from sage.interfaces.maxima_lib import MaximaLib |
| 46 | sage: MaximaLib() |
| 47 | Traceback (most recent call last): |
| 48 | ... |
| 49 | RuntimeError: Maxima interface in library mode can only be instantiated once |
31 | 50 | """ |
32 | 51 | |
33 | 52 | #***************************************************************************** |
… |
… |
|
51 | 70 | |
52 | 71 | from maxima_abstract import MaximaAbstract, MaximaAbstractFunction, MaximaAbstractElement, MaximaAbstractFunctionElement, MaximaAbstractElementFunction |
53 | 72 | |
54 | | ## We begin here by initializing maxima in library mode |
| 73 | ## We begin here by initializing Maxima in library mode |
| 74 | ## i.e. loading it into ECL |
55 | 75 | ecl_eval("(setf *load-verbose* NIL)") |
56 | 76 | ecl_eval("(require 'maxima)") |
57 | 77 | ecl_eval("(in-package :maxima)") |
… |
… |
|
60 | 80 | ecl_eval("(set-locale-subdir)") |
61 | 81 | ecl_eval("(set-pathnames)") |
62 | 82 | ecl_eval("(defun add-lineinfo (x) x)") |
| 83 | |
63 | 84 | #the following is a direct adaption of the definition of "retrieve" in the Maxima file |
64 | 85 | #macsys.lisp. This routine is normally responsible for displaying a question and |
65 | 86 | #returning the answer. We change it to throw an error in which the text of the question |
… |
… |
|
100 | 121 | ) |
101 | 122 | """) |
102 | 123 | |
| 124 | ## Redirection of ECL and Maxima stdout to /dev/null |
103 | 125 | 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 | | |
120 | 126 | ecl_eval("(setf original-standard-output *standard-output*)") |
121 | 127 | ecl_eval("(setf *standard-output* *dev-null*)") |
122 | 128 | #ecl_eval("(setf *error-output* *dev-null*)") |
123 | 129 | |
| 130 | ## Default options set in Maxima |
124 | 131 | # display2d -- no ascii art output |
125 | 132 | # keepfloat -- don't automatically convert floats to rationals |
126 | 133 | init_code = ['display2d : false', 'domain : complex', 'keepfloat : true', 'load(to_poly_solver)', 'load(simplify_sum)'] |
… |
… |
|
137 | 144 | ## should allow to do this through a method |
138 | 145 | #ecl_eval("(setf *standard-output* original-standard-output)") |
139 | 146 | |
| 147 | ## This is the main function (ECL object) used for evalutation |
140 | 148 | # This returns an EclObject |
141 | 149 | maxima_eval=ecl_eval(""" |
142 | 150 | (defun maxima-eval( form ) |
… |
… |
|
166 | 174 | ) |
167 | 175 | """) |
168 | 176 | |
| 177 | ## Number of instances of this interface |
169 | 178 | maxima_lib_instances = 0 |
170 | 179 | |
| 180 | ## Here we define several useful ECL/Maxima objects |
171 | 181 | # The Maxima string function can change the structure of its input |
172 | 182 | #maxprint=EclObject("$STRING") |
173 | 183 | maxprint=EclObject("(defun mstring-for-sage (form) (coerce (mstring form) 'string))").eval() |
… |
… |
|
189 | 199 | max_to_poly_solve=EclObject("$TO_POLY_SOLVE") |
190 | 200 | |
191 | 201 | def stdout_to_string(s): |
| 202 | r""" |
| 203 | Evaluate command ``s`` and catch Maxima stdout (not the result of the command!) into a string. |
| 204 | |
| 205 | INPUT: |
| 206 | |
| 207 | - ``s`` - string; command to evaluate |
| 208 | |
| 209 | OUTPUT: string |
| 210 | |
| 211 | This is currently used to implement display2d. |
| 212 | |
| 213 | EXAMPLES:: |
| 214 | |
| 215 | sage: from sage.interfaces.maxima_lib import stdout_to_string |
| 216 | sage: stdout_to_string('1+1') |
| 217 | '' |
| 218 | sage: stdout_to_string('disp(1+1)') |
| 219 | '2\n\n' |
| 220 | """ |
192 | 221 | return ecl_eval("(with-output-to-string (*standard-output*) (maxima-eval #$%s$))"%s).python()[1:-1] |
193 | 222 | |
194 | 223 | def max_to_string(s): |
195 | | return maxprint(s).python()[1:-1] |
| 224 | r""" |
| 225 | Return the Maxima string corresponding to this ECL object. |
| 226 | |
| 227 | INPUT: |
| 228 | |
| 229 | - ``s`` - ECL object |
| 230 | |
| 231 | OUTPUT: string |
| 232 | |
| 233 | EXAMPLES:: |
| 234 | |
| 235 | sage: from sage.interfaces.maxima_lib import maxima_lib, max_to_string |
| 236 | sage: ecl = maxima_lib(cos(x)).ecl() |
| 237 | sage: max_to_string(ecl) |
| 238 | 'cos(x)' |
| 239 | """ |
| 240 | return maxprint(s).python()[1:-1] |
196 | 241 | |
197 | 242 | my_mread=ecl_eval(""" |
198 | 243 | (defun my-mread (cmd) |
199 | 244 | (caddr (mread (make-string-input-stream cmd)))) |
200 | 245 | """) |
201 | 246 | |
202 | | def parse_max_string(l): |
203 | | return my_mread('"%s;"'%l) |
| 247 | def parse_max_string(s): |
| 248 | r""" |
| 249 | Evaluate string in Maxima without *any* further simplification. |
| 250 | |
| 251 | INPUT: |
| 252 | |
| 253 | - ``s`` - string |
| 254 | |
| 255 | OUTPUT: ECL object |
| 256 | |
| 257 | EXAMPLES:: |
| 258 | |
| 259 | sage: from sage.interfaces.maxima_lib import parse_max_string |
| 260 | sage: parse_max_string('1+1') |
| 261 | <ECL: ((MPLUS) 1 1)> |
| 262 | """ |
| 263 | return my_mread('"%s;"'%s) |
204 | 264 | |
205 | 265 | class MaximaLib(MaximaAbstract): |
206 | 266 | """ |
207 | 267 | Interface to Maxima as a Library. |
| 268 | |
| 269 | INPUT: none |
| 270 | |
| 271 | OUTPUT: Maxima interface as a Library |
| 272 | |
| 273 | EXAMPLES:: |
| 274 | |
| 275 | sage: from sage.interfaces.maxima_lib import MaximaLib, maxima_lib |
| 276 | sage: isinstance(maxima_lib,MaximaLib) |
| 277 | True |
| 278 | |
| 279 | Only one such interface can be instantiated:: |
| 280 | |
| 281 | sage: MaximaLib() |
| 282 | Traceback (most recent call last): |
| 283 | ... |
| 284 | RuntimeError: Maxima interface in library mode can only be instantiated once |
208 | 285 | """ |
209 | 286 | def __init__(self): |
210 | 287 | """ |
211 | 288 | Create an instance of the Maxima interpreter. |
| 289 | See ``MaximaLib`` for full documentation. |
212 | 290 | |
213 | 291 | TESTS:: |
214 | 292 | |
… |
… |
|
229 | 307 | global init_code |
230 | 308 | self.__init_code = init_code |
231 | 309 | |
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 | 310 | MaximaAbstract.__init__(self,"maxima_lib") |
235 | 311 | self.__seq = 0 |
236 | 312 | |
237 | 313 | def _coerce_from_special_method(self, x): |
| 314 | r""" |
| 315 | Coerce ``x`` into self trying to call a special underscore method. |
| 316 | |
| 317 | INPUT: |
| 318 | |
| 319 | - ``x`` - object to coerce into self |
| 320 | |
| 321 | OUTPUT: Maxima element equivalent to ``x`` |
| 322 | |
| 323 | EXAMPLES:: |
| 324 | |
| 325 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 326 | sage: xmax = maxima_lib._coerce_from_special_method(x) |
| 327 | sage: type(xmax) |
| 328 | <class 'sage.interfaces.maxima_lib.MaximaLibElement'> |
| 329 | """ |
238 | 330 | if isinstance(x, EclObject): |
239 | 331 | return MaximaLibElement(self,self._create(x)) |
240 | 332 | else: |
241 | 333 | return MaximaAbstract._coerce_from_special_method(self,x) |
242 | 334 | |
243 | 335 | def __reduce__(self): |
244 | | """ |
| 336 | r""" |
| 337 | Implement __reduce__ for ``MaximaLib``. |
| 338 | |
| 339 | INPUT: none |
| 340 | |
| 341 | OUTPUT: |
| 342 | |
| 343 | A couple consisting of: |
| 344 | |
| 345 | - the function to call for unpickling |
| 346 | |
| 347 | - a tuple of arguments for the function |
| 348 | |
245 | 349 | EXAMPLES:: |
246 | 350 | |
247 | 351 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
251 | 355 | return reduce_load_MaximaLib, tuple([]) |
252 | 356 | |
253 | 357 | # This outputs a string |
254 | | def eval(self, line, locals=None, reformat=True, **kwds): |
| 358 | def _eval_line(self, line, locals=None, reformat=True, **kwds): |
| 359 | r""" |
| 360 | Evaluate the line in Maxima. |
| 361 | |
| 362 | INPUT: |
| 363 | |
| 364 | - ``line`` - string; text to evaluate |
| 365 | |
| 366 | - ``locals`` - None (ignored); this is used for compatibility with the |
| 367 | Sage notebook's generic system interface. |
| 368 | |
| 369 | - ``reformat`` - boolean; whether to strip output or not |
| 370 | |
| 371 | - ``**kwds`` - All other arguments are currently ignored. |
| 372 | |
| 373 | OUTPUT: string representing Maxima output |
| 374 | |
| 375 | EXAMPLES:: |
| 376 | |
| 377 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 378 | sage: maxima_lib._eval_line('1+1') |
| 379 | '2' |
| 380 | sage: maxima_lib._eval_line('1+1;') |
| 381 | '2' |
| 382 | sage: maxima_lib._eval_line('1+1$') |
| 383 | '' |
| 384 | sage: maxima_lib._eval_line('randvar : cos(x)+sin(y)$') |
| 385 | '' |
| 386 | sage: maxima_lib._eval_line('randvar') |
| 387 | 'sin(y)+cos(x)' |
| 388 | """ |
255 | 389 | result = '' |
256 | 390 | while line: |
257 | 391 | ind_dollar=line.find("$") |
… |
… |
|
272 | 406 | return result |
273 | 407 | return ''.join([x.strip() for x in result.split()]) |
274 | 408 | |
275 | | _eval_line = eval |
| 409 | eval = _eval_line |
276 | 410 | |
277 | 411 | ########################################### |
278 | 412 | # Direct access to underlying lisp interpreter. |
… |
… |
|
281 | 415 | """ |
282 | 416 | Send a lisp command to maxima. |
283 | 417 | |
| 418 | INPUT: |
| 419 | |
| 420 | - ``cmd`` - string |
| 421 | |
| 422 | OUTPUT: ECL object |
| 423 | |
284 | 424 | .. note:: |
285 | 425 | |
286 | 426 | The output of this command is very raw - not pretty. |
… |
… |
|
299 | 439 | |
300 | 440 | INPUT: |
301 | 441 | |
302 | | |
303 | 442 | - ``var`` - string |
304 | 443 | |
305 | 444 | - ``value`` - string |
306 | | |
| 445 | |
| 446 | OUTPUT: none |
307 | 447 | |
308 | 448 | EXAMPLES:: |
309 | 449 | |
… |
… |
|
321 | 461 | """ |
322 | 462 | Clear the variable named var. |
323 | 463 | |
| 464 | INPUT: |
| 465 | |
| 466 | - ``var`` - string |
| 467 | |
| 468 | OUTPUT: none |
| 469 | |
324 | 470 | EXAMPLES:: |
325 | 471 | |
326 | 472 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
339 | 485 | def get(self, var): |
340 | 486 | """ |
341 | 487 | Get the string value of the variable var. |
| 488 | |
| 489 | INPUT: |
| 490 | |
| 491 | - ``var`` - string |
| 492 | |
| 493 | OUTPUT: string |
342 | 494 | |
343 | 495 | EXAMPLES:: |
344 | 496 | |
… |
… |
|
351 | 503 | return s |
352 | 504 | |
353 | 505 | def _create(self, value, name=None): |
| 506 | r""" |
| 507 | Create a variable with given value and name. |
| 508 | |
| 509 | INPUT: |
| 510 | |
| 511 | - ``value`` - string or ECL object |
| 512 | |
| 513 | - ``name`` - string (default: None); name to use for the variable, |
| 514 | an automatically generated name is used if this is none |
| 515 | |
| 516 | OUTPUT: |
| 517 | |
| 518 | - string; the name of the created variable |
| 519 | |
| 520 | EXAMPLES: |
| 521 | |
| 522 | Creation from strings:: |
| 523 | |
| 524 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 525 | sage: maxima_lib._create('3','var3') |
| 526 | 'var3' |
| 527 | sage: maxima_lib.get('var3') |
| 528 | '3' |
| 529 | sage: s = maxima_lib._create('3') |
| 530 | sage: s # random output |
| 531 | 'sage9' |
| 532 | sage: s[:4] == 'sage' |
| 533 | True |
| 534 | |
| 535 | And from ECL object:: |
| 536 | |
| 537 | sage: c = maxima_lib(x+cos(19)).ecl() |
| 538 | sage: maxima_lib._create(c,'m') |
| 539 | 'm' |
| 540 | sage: maxima_lib.get('m') |
| 541 | 'x+cos(19)' |
| 542 | """ |
354 | 543 | name = self._next_var_name() if name is None else name |
355 | 544 | if isinstance(value,EclObject): |
356 | 545 | maxima_eval([[msetq],cadadr("#$%s$#$"%name),value]) |
… |
… |
|
359 | 548 | return name |
360 | 549 | |
361 | 550 | def _function_class(self): |
362 | | """ |
| 551 | r""" |
| 552 | Return the Python class of Maxima functions. |
| 553 | |
| 554 | INPUT: none |
| 555 | |
| 556 | OUTPUT: type |
| 557 | |
363 | 558 | EXAMPLES:: |
364 | 559 | |
365 | 560 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
369 | 564 | return MaximaLibFunction |
370 | 565 | |
371 | 566 | def _object_class(self): |
372 | | """ |
| 567 | r""" |
373 | 568 | Return the Python class of Maxima elements. |
| 569 | |
| 570 | INPUT: none |
| 571 | |
| 572 | OUTPUT: type |
374 | 573 | |
375 | 574 | EXAMPLES:: |
376 | 575 | |
… |
… |
|
381 | 580 | return MaximaLibElement |
382 | 581 | |
383 | 582 | def _function_element_class(self): |
384 | | """ |
| 583 | r""" |
| 584 | Return the Python class of Maxima functions of elements. |
| 585 | |
| 586 | INPUT: none |
| 587 | |
| 588 | OUTPUT: type |
| 589 | |
385 | 590 | EXAMPLES:: |
386 | 591 | |
387 | 592 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
391 | 596 | return MaximaLibFunctionElement |
392 | 597 | |
393 | 598 | def _object_function_class(self): |
394 | | """ |
| 599 | r""" |
| 600 | Return the Python class of Maxima user-defined functions. |
| 601 | |
| 602 | INPUT: none |
| 603 | |
| 604 | OUTPUT: type |
| 605 | |
395 | 606 | EXAMPLES:: |
396 | 607 | |
397 | 608 | sage: from sage.interfaces.maxima_lib import maxima_lib |
… |
… |
|
562 | 773 | |
563 | 774 | |
564 | 775 | def is_MaximaLibElement(x): |
565 | | """ |
| 776 | r""" |
566 | 777 | Returns True if x is of type MaximaLibElement. |
567 | 778 | |
568 | 779 | EXAMPLES:: |
… |
… |
|
576 | 787 | """ |
577 | 788 | return isinstance(x, MaximaLibElement) |
578 | 789 | |
579 | | class MaximaLibElement(MaximaAbstractElement): |
580 | | """ |
581 | | """ |
| 790 | class MaximaLibElement(MaximaAbstractElement): |
582 | 791 | def ecl(self): |
| 792 | r""" |
| 793 | Return the underlying ECL object of this MaximaLib object. |
| 794 | |
| 795 | INPUT: none |
| 796 | |
| 797 | OUTPUT: ECL object |
| 798 | |
| 799 | EXAMPLES:: |
| 800 | |
| 801 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 802 | sage: maxima_lib(x+cos(19)).ecl() |
| 803 | <ECL: ((MPLUS SIMP) ((%COS SIMP) 19) $X)> |
| 804 | """ |
583 | 805 | try: |
584 | 806 | return self._ecl |
585 | 807 | except AttributeError: |
… |
… |
|
587 | 809 | return self._ecl |
588 | 810 | |
589 | 811 | def to_poly_solve(self,vars,options=""): |
| 812 | r""" |
| 813 | Use Maxima's to_poly_solver package. |
| 814 | |
| 815 | INPUT: |
| 816 | |
| 817 | - ``vars`` - symbolic expressions |
| 818 | |
| 819 | - ``options`` - string (default="") |
| 820 | |
| 821 | OUTPUT: Maxima object |
| 822 | |
| 823 | EXAMPLES:: |
| 824 | |
| 825 | sage: from sage.interfaces.maxima_lib import maxima_lib |
| 826 | sage: sol = maxima_lib(sin(x) == 0).to_poly_solve(x) |
| 827 | sage: sol.sage() |
| 828 | [[x == pi + 2*pi*z6], [x == 2*pi*z8]] |
| 829 | """ |
590 | 830 | if options.find("use_grobner=true") != -1: |
591 | 831 | cmd=EclObject([[max_to_poly_solve], self.ecl(), sr_to_max(vars), |
592 | 832 | [[mequal],max_use_grobner,True]]) |
… |
… |
|
595 | 835 | return self.parent()(maxima_eval(cmd)) |
596 | 836 | |
597 | 837 | def display2d(self, onscreen=True): |
598 | | """ |
| 838 | r""" |
| 839 | Return the 2d representation of this Maxima object. |
| 840 | |
| 841 | INPUT: |
| 842 | |
| 843 | - ``onscreen`` - boolean (default: True); whether to print or return |
| 844 | |
| 845 | OUTPUT: |
| 846 | |
| 847 | The representation is printed if onscreen is set to True |
| 848 | and returned as a string otherwise. |
| 849 | |
599 | 850 | EXAMPLES:: |
600 | 851 | |
601 | 852 | sage: from sage.interfaces.maxima_lib import maxima_lib |
602 | 853 | sage: F = maxima_lib('x^5 - y^5').factor() |
603 | | sage: F.display2d () |
| 854 | sage: F.display2d() |
604 | 855 | 4 3 2 2 3 4 |
605 | 856 | - (y - x) (y + x y + x y + x y + x ) |
606 | 857 | """ |
… |
… |
|
638 | 889 | |
639 | 890 | |
640 | 891 | def reduce_load_MaximaLib(): |
641 | | """ |
| 892 | r""" |
| 893 | Unpickle the Maxima library interface. |
| 894 | |
642 | 895 | EXAMPLES:: |
643 | 896 | |
644 | 897 | sage: from sage.interfaces.maxima_lib import reduce_load_MaximaLib |
… |
… |
|
647 | 900 | """ |
648 | 901 | return maxima_lib |
649 | 902 | |
650 | | #********************************** |
651 | | # ??? |
652 | 903 | |
| 904 | ############################################# |
| 905 | # Smart translations between SR and Maxima |
| 906 | ############################################# |
| 907 | |
| 908 | import sage.rings.real_double |
653 | 909 | import sage.symbolic.expression |
654 | 910 | import sage.functions.trig |
655 | 911 | import sage.functions.log |
… |
… |
|
667 | 923 | cadadr=EclObject("cadadr") |
668 | 924 | meval=EclObject("meval") |
669 | 925 | NIL=EclObject("NIL") |
670 | | ratdisrep=EclObject("ratdisrep") |
671 | 926 | |
| 927 | ## Dictionaries for standard operators |
672 | 928 | sage_op_dict = { |
673 | 929 | sage.symbolic.expression.operator.abs : "MABS", |
674 | 930 | sage.symbolic.expression.operator.add : "MPLUS", |
… |
… |
|
707 | 963 | sage_op_dict = dict([(k,EclObject(sage_op_dict[k])) for k in sage_op_dict]) |
708 | 964 | max_op_dict = dict([(sage_op_dict[k],k) for k in sage_op_dict]) |
709 | 965 | |
| 966 | |
| 967 | ## Here we correct the dictionaries for some simple operators |
710 | 968 | def add_vararg(*args): |
| 969 | r""" |
| 970 | Addition of a variable number of arguments. |
| 971 | |
| 972 | INPUT: |
| 973 | |
| 974 | - ``args`` - arguments to add |
| 975 | |
| 976 | OUTPUT: sum of arguments |
| 977 | |
| 978 | EXAMPLES:: |
| 979 | |
| 980 | sage: from sage.interfaces.maxima_lib import add_vararg |
| 981 | sage: add_vararg(1,2,3,4,5,6,7) |
| 982 | 28 |
| 983 | """ |
711 | 984 | S=0 |
712 | 985 | for a in args: |
713 | 986 | S=S+a |
714 | 987 | return S |
715 | 988 | |
716 | 989 | def mul_vararg(*args): |
| 990 | r""" |
| 991 | Multiplication of a variable number of arguments. |
| 992 | |
| 993 | INPUT: |
| 994 | |
| 995 | - ``args`` - arguments to multiply |
| 996 | |
| 997 | OUTPUT: product of arguments |
| 998 | |
| 999 | EXAMPLES:: |
| 1000 | |
| 1001 | sage: from sage.interfaces.maxima_lib import mul_vararg |
| 1002 | sage: mul_vararg(9,8,7,6,5,4) |
| 1003 | 60480 |
| 1004 | """ |
717 | 1005 | P=1 |
718 | 1006 | for a in args: |
719 | 1007 | P=P*a |
720 | 1008 | return P |
721 | 1009 | |
722 | 1010 | def sage_rat(x,y): |
| 1011 | r""" |
| 1012 | Return quotient x/y. |
| 1013 | |
| 1014 | INPUT: |
| 1015 | |
| 1016 | - ``x`` - integer |
| 1017 | |
| 1018 | - ``y`` - integer |
| 1019 | |
| 1020 | OUTPUT: rational |
| 1021 | |
| 1022 | EXAMPLES:: |
| 1023 | |
| 1024 | sage: from sage.interfaces.maxima_lib import sage_rat |
| 1025 | sage: sage_rat(1,7) |
| 1026 | 1/7 |
| 1027 | """ |
723 | 1028 | return x/y |
724 | 1029 | |
725 | 1030 | mplus=EclObject("MPLUS") |
726 | 1031 | mtimes=EclObject("MTIMES") |
727 | | mdiff=EclObject("%DERIVATIVE") |
728 | 1032 | rat=EclObject("RAT") |
729 | | max_i=EclObject("$%I") |
730 | 1033 | max_op_dict[mplus]=add_vararg |
731 | 1034 | max_op_dict[mtimes]=mul_vararg |
732 | 1035 | max_op_dict[rat]=sage_rat |
| 1036 | |
| 1037 | |
| 1038 | ## Here we build dictionaries for operators needing special conversions. |
| 1039 | ratdisrep=EclObject("ratdisrep") |
| 1040 | mrat=EclObject("MRAT") |
733 | 1041 | mqapply=EclObject("MQAPPLY") |
734 | 1042 | max_li=EclObject("$LI") |
735 | 1043 | max_psi=EclObject("$PSI") |
736 | 1044 | max_array=EclObject("ARRAY") |
| 1045 | mdiff=EclObject("%DERIVATIVE") |
737 | 1046 | max_gamma_incomplete=sage_op_dict[sage.functions.other.gamma_inc] |
738 | 1047 | |
739 | 1048 | def mrat_to_sage(expr): |
740 | 1049 | r""" |
741 | | Convert a maxima MRAT expression to Sage SR |
| 1050 | Convert a Maxima MRAT expression to Sage SR. |
742 | 1051 | |
| 1052 | INPUT: |
| 1053 | |
| 1054 | - ``expr`` - ECL object; a Maxima MRAT expression |
| 1055 | |
| 1056 | OUTPUT: symbolic expression |
| 1057 | |
743 | 1058 | 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 |
| 1059 | The easiest way to translate those to SR is by first asking Maxima to give |
745 | 1060 | the generic representation of the object. That is what RATDISREP does in |
746 | | maxima. |
| 1061 | Maxima. |
| 1062 | |
| 1063 | EXAMPLES:: |
| 1064 | |
| 1065 | sage: from sage.interfaces.maxima_lib import maxima_lib, mrat_to_sage |
| 1066 | sage: var('x y z') |
| 1067 | (x, y, z) |
| 1068 | sage: c = maxima_lib((x+y^2+z^9)/x^6+z^8/y).rat() |
| 1069 | sage: c |
| 1070 | (y*z^9+x^6*z^8+y^3+x*y)/(x^6*y) |
| 1071 | sage: c.ecl() |
| 1072 | <ECL: ((MRAT SIMP ($X $Y $Z) |
| 1073 | ...> |
| 1074 | sage: mrat_to_sage(c.ecl()) |
| 1075 | (x^6*z^8 + y*z^9 + y^3 + x*y)/(x^6*y) |
747 | 1076 | """ |
748 | 1077 | return max_to_sr(meval(EclObject([[ratdisrep],expr]))) |
749 | 1078 | |
750 | 1079 | def mqapply_to_sage(expr): |
751 | 1080 | r""" |
752 | | Special conversion rule for MQAPPLY expressions |
| 1081 | Special conversion rule for MQAPPLY expressions. |
| 1082 | |
| 1083 | INPUT: |
| 1084 | |
| 1085 | - ``expr`` - ECL object; a Maxima MQAPPLY expression |
| 1086 | |
| 1087 | OUTPUT: symbolic expression |
| 1088 | |
| 1089 | MQAPPLY is used for function as li[x](y) and psi[x](y). |
| 1090 | |
| 1091 | EXAMPLES:: |
| 1092 | |
| 1093 | sage: from sage.interfaces.maxima_lib import maxima_lib, mqapply_to_sage |
| 1094 | sage: c = maxima_lib('li[2](3)') |
| 1095 | sage: c.ecl() |
| 1096 | <ECL: ((MQAPPLY SIMP) (($LI SIMP ARRAY) 2) 3)> |
| 1097 | sage: mqapply_to_sage(c.ecl()) |
| 1098 | polylog(2, 3) |
753 | 1099 | """ |
754 | 1100 | if caaadr(expr) == max_li: |
755 | 1101 | return sage.functions.log.polylog(max_to_sr(cadadr(expr)),max_to_sr(caddr(expr))) |
… |
… |
|
761 | 1107 | args=[max_to_sr(a) for a in max_args] |
762 | 1108 | return op(*args) |
763 | 1109 | |
| 1110 | def mdiff_to_sage(expr): |
| 1111 | r""" |
| 1112 | Special conversion rule for %DERIVATIVE expressions. |
| 1113 | |
| 1114 | INPUT: |
| 1115 | |
| 1116 | - ``expr`` - ECL object; a Maxima %DERIVATIVE expression |
| 1117 | |
| 1118 | OUTPUT: symbolic expression |
| 1119 | |
| 1120 | EXAMPLES:: |
| 1121 | |
| 1122 | sage: from sage.interfaces.maxima_lib import maxima_lib, mdiff_to_sage |
| 1123 | sage: f = maxima_lib('f(x)').diff('x',4) |
| 1124 | sage: f.ecl() |
| 1125 | <ECL: ((%DERIVATIVE SIMP) (($F SIMP) $X) $X 4)> |
| 1126 | sage: mdiff_to_sage(f.ecl()) |
| 1127 | D[0, 0, 0, 0](f)(x) |
| 1128 | """ |
| 1129 | return max_to_sr(expr.cadr()).diff(*[max_to_sr(e) for e in expr.cddr()]) |
| 1130 | |
764 | 1131 | def dummy_integrate(expr): |
765 | 1132 | 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. |
| 1133 | 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. |
| 1134 | |
| 1135 | INPUT: |
| 1136 | |
| 1137 | - ``expr`` - ECL object; a Maxima %INTEGRATE expression |
| 1138 | |
| 1139 | OUTPUT: symbolic expression |
| 1140 | |
| 1141 | EXAMPLES:: |
| 1142 | sage: from sage.interfaces.maxima_lib import maxima_lib, dummy_integrate |
| 1143 | sage: f = maxima_lib('f(x)').integrate('x') |
| 1144 | sage: f.ecl() |
| 1145 | <ECL: ((%INTEGRATE SIMP) (($F SIMP) $X) $X)> |
| 1146 | sage: dummy_integrate(f.ecl()) |
| 1147 | integrate(f(x), x) |
| 1148 | |
| 1149 | :: |
| 1150 | sage: f = maxima_lib('f(x)').integrate('x',0,10) |
| 1151 | sage: f.ecl() |
| 1152 | <ECL: ((%INTEGRATE SIMP) (($F SIMP) $X) $X 0 10)> |
| 1153 | sage: dummy_integrate(f.ecl()) |
| 1154 | integrate(f(x), x, 0, 10) |
767 | 1155 | """ |
768 | 1156 | args=[max_to_sr(a) for a in cdr(expr)] |
769 | 1157 | if len(args) == 4 : |
… |
… |
|
771 | 1159 | else: |
772 | 1160 | return sage.symbolic.integration.integral.indefinite_integral(*args, hold=True) |
773 | 1161 | |
774 | | def mdiff_to_sage(expr): |
775 | | return max_to_sr(expr.cadr()).diff(*[max_to_sr(e) for e in expr.cddr()]) |
776 | | |
| 1162 | ## The dictionaries |
777 | 1163 | special_max_to_sage={ |
778 | | EclObject("MRAT") : mrat_to_sage, |
| 1164 | mrat : mrat_to_sage, |
779 | 1165 | mqapply : mqapply_to_sage, |
780 | | EclObject("%INTEGRATE") : dummy_integrate, |
781 | | mdiff : mdiff_to_sage |
| 1166 | mdiff : mdiff_to_sage, |
| 1167 | EclObject("%INTEGRATE") : dummy_integrate |
782 | 1168 | } |
783 | 1169 | |
784 | 1170 | special_sage_to_max={ |
… |
… |
|
788 | 1174 | sage.functions.other.Ei : lambda X : [[max_gamma_incomplete], 0, X] |
789 | 1175 | } |
790 | 1176 | |
| 1177 | |
| 1178 | ## Dictionaries for symbols |
791 | 1179 | sage_sym_dict={} |
792 | 1180 | max_sym_dict={} |
793 | 1181 | |
| 1182 | |
| 1183 | ## Generic conversion functions |
| 1184 | |
| 1185 | max_i=EclObject("$%I") |
794 | 1186 | def pyobject_to_max(obj): |
| 1187 | r""" |
| 1188 | Convert a (simple) Python object into a Maxima object. |
| 1189 | |
| 1190 | INPUT: |
| 1191 | |
| 1192 | - ``expr`` - Python object |
| 1193 | |
| 1194 | OUTPUT: ECL object |
| 1195 | |
| 1196 | .. note:: |
| 1197 | This uses functions defined in sage.libs.ecl. |
| 1198 | |
| 1199 | EXAMPLES:: |
| 1200 | sage: from sage.interfaces.maxima_lib import pyobject_to_max |
| 1201 | sage: pyobject_to_max(4) |
| 1202 | <ECL: 4> |
| 1203 | sage: pyobject_to_max('z') |
| 1204 | <ECL: Z> |
| 1205 | sage: var('x') |
| 1206 | x |
| 1207 | sage: pyobject_to_max(x) |
| 1208 | Traceback (most recent call last): |
| 1209 | ... |
| 1210 | TypeError: Unimplemented type for python_to_ecl |
| 1211 | """ |
795 | 1212 | if isinstance(obj,sage.rings.rational.Rational): |
796 | 1213 | return EclObject(obj) if (obj.denom().is_one()) else EclObject([[rat], obj.numer(),obj.denom()]) |
797 | 1214 | elif isinstance(obj,sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic) and obj.parent().defining_polynomial().list() == [1,0,1]: |
… |
… |
|
802 | 1219 | # This goes from SR to EclObject |
803 | 1220 | def sr_to_max(expr): |
804 | 1221 | r""" |
| 1222 | Convert a symbolic expression into a Maxima object. |
| 1223 | |
| 1224 | INPUT: |
| 1225 | |
| 1226 | - ``expr`` - symbolic expression |
| 1227 | |
| 1228 | OUTPUT: ECL object |
| 1229 | |
| 1230 | EXAMPLES:: |
| 1231 | sage: from sage.interfaces.maxima_lib import sr_to_max |
| 1232 | sage: var('x') |
| 1233 | x |
| 1234 | sage: sr_to_max(x) |
| 1235 | <ECL: $X> |
| 1236 | sage: sr_to_max(cos(x)) |
| 1237 | <ECL: ((%COS) $X)> |
| 1238 | sage: f = function('f',x) |
| 1239 | sage: sr_to_max(f.diff()) |
| 1240 | <ECL: ((%DERIVATIVE) (($F) $X) $X 1)> |
805 | 1241 | """ |
806 | 1242 | global sage_op_dict, max_op_dict |
807 | 1243 | global sage_sym_dict, max_sym_dict |
… |
… |
|
822 | 1258 | params = op.parameter_set() |
823 | 1259 | deriv_max = [] |
824 | 1260 | [deriv_max.extend([sr_to_max(args[i]), EclObject(params.count(i))]) for i in set(params)] |
825 | | l = [mdiff,f] |
| 1261 | l = [[mdiff],f] |
826 | 1262 | l.extend(deriv_max) |
827 | 1263 | return EclObject(l) |
828 | 1264 | elif (op in special_sage_to_max): |
… |
… |
|
850 | 1286 | return maxima(expr).ecl() |
851 | 1287 | |
852 | 1288 | # This goes from EclObject to SR |
853 | | import sage.rings.real_double |
| 1289 | def max_to_sr(expr): |
| 1290 | r""" |
| 1291 | Convert a Maxima object into a symbolic expression. |
854 | 1292 | |
855 | | def max_to_sr(expr): |
| 1293 | INPUT: |
| 1294 | |
| 1295 | - ``expr`` - ECL object |
| 1296 | |
| 1297 | OUTPUT: symbolic expression |
| 1298 | |
| 1299 | EXAMPLES:: |
| 1300 | |
| 1301 | sage: from sage.interfaces.maxima_lib import maxima_lib, max_to_sr |
| 1302 | sage: f = maxima_lib('f(x)') |
| 1303 | sage: f.ecl() |
| 1304 | <ECL: (($F SIMP) $X)> |
| 1305 | sage: max_to_sr(f.ecl()) |
| 1306 | f(x) |
| 1307 | |
| 1308 | TESTS:: |
| 1309 | |
| 1310 | sage: from sage.interfaces.maxima_lib import sr_to_max, max_to_sr |
| 1311 | sage: f = function('f',x).diff() |
| 1312 | sage: bool(max_to_sr(sr_to_max(f)) == f) |
| 1313 | True |
| 1314 | """ |
856 | 1315 | if expr.consp(): |
857 | 1316 | op_max=caar(expr) |
858 | 1317 | if op_max in special_max_to_sage: |
859 | 1318 | return special_max_to_sage[op_max](expr) |
860 | 1319 | if not(op_max in max_op_dict): |
861 | | # This could be unsafe if the conversion to SR chenges the structure of expr |
| 1320 | # This could be unsafe if the conversion to SR changes the structure of expr |
862 | 1321 | sage_expr=SR(maxima(expr)) |
863 | 1322 | max_op_dict[op_max]=sage_expr.operator() |
864 | 1323 | sage_op_dict[sage_expr.operator()]=op_max |