forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
isympy
executable file
·305 lines (228 loc) · 9.53 KB
/
isympy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# XXX: Don't put a newline here, or it will add an extra line with
# isympy --help
# |
# v
"""Python shell for SymPy.
This is just a normal Python shell (IPython shell if you have the
IPython package installed), that executes the following commands for
the user:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
So starting 'isympy' is equivalent to starting Python (or IPython) and
executing the above commands by hand. It is intended for easy and quick
experimentation with SymPy. isympy is a good way to use SymPy as an
interactive calculator
COMMAND LINE OPTIONS
--------------------
-c CONSOLE, --console=CONSOLE
Use the specified shell (Python or IPython) shell as the console
backend instead of the default one (IPython if present, Python
otherwise), e.g.:
$isympy -c python
CONSOLE must be one of 'ipython' or 'python'
-p PRETTY, --pretty PRETTY
Setup pretty printing in SymPy. By default the most pretty, unicode
printing is enabled (if the terminal supports it). You can use less
pretty ASCII printing instead or no pretty printing at all, e.g.:
$isympy -p no
PRETTY must be one of 'unicode', 'ascii', or 'no'
-t TYPES, --types=TYPES
Setup the ground types for the polys. By default, gmpy ground types
are used if gmpy is installed, otherwise it falls back to python
ground types, which are a little bit slower. You can manually
choose python ground types even if gmpy is installed (e.g., for
testing purposes), e.g.:
$isympy -t python
TYPES must be one of 'gmpy', 'python', or 'sympy'
Note that sympy ground types are not supported, and should be used
only for experimental purposes.
This is the same as setting the environment variable
SYMPY_GROUND_TYPES to the given ground type (e.g.,
SYMPY_GROUND_TYPES='gmpy')
The ground types can be determined interactively from the variable
sympy.polys.domains.GROUND_TYPES.
-o ORDER, --order ORDER
Setup the ordering of terms for printing. The default is lex, which
orders terms lexicographically (e.g., x**2 + x + 1). You can choose
other orderings, such as rev-lex, which will use reverse
lexicographic ordering (e.g., 1 + x + x**2), e.g.:
$isympy -o rev-lex
ORDER must be one of 'lex', 'rev-lex', 'grlex', 'rev-grlex',
'grevlex', 'rev-grevlex', 'old', or 'none'.
Note that for very large expressions, ORDER='none' may speed up
printing considerably, with the tradeoff that the order of the terms
in the printed expression will have no canonical order.
-q, --quiet
Print only Python's and SymPy's versions to stdout at startup.
-d, --doctest
Use the same format that should be used for doctests. This is
equivalent to -c python -p no.
-C, --no-cache
Disable the caching mechanism. Disabling the cache may slow certain
operations down considerably. This is useful for testing the cache,
or for benchmarking, as the cache can result in deceptive timings.
This is the same as setting the environment variable SYMPY_USE_CACHE
to 'no'.
This is equivalent to setting the environment variable
SYMPY_USE_CACHE='no'.
-a, --auto
Automatically create missing symbols. Normally, typing a name of a
Symbol that has not been instantiated first would raise NameError,
but with this option enabled, any undefined name will be
automatically created as a Symbol. This only works in IPython 0.11.
Note that this is intended only for interactive, calculator style
usage. In a script that uses SymPy, Symbols should be instantiated
at the top, so that it's clear what they are.
This will not override any names that are already defined, which
includes the single character letters represented by the mnemonic
QCOSINE (see the "Gotchas and Pitfalls" document in the
documentation). You can delete existing names by executing "del
name". You can see if a name is defined by typing "'name' in
globals()".
The Symbols that are created using this have default assumptions.
If you want to place assumptions on symbols, you should create them
using symbols() or var().
Finally, this only works in the top level namespace. So, for
example, if you define a function in isympy with an undefined
Symbol, it will not work.
-D, --debug
Enable debugging output. This is the same as setting the
environment variable SYMPY_DEBUG to 'True'. The debug status is set
in the variable SYMPY_DEBUG within isympy.
-- IPython options
Additionally you can pass command line options directly to the
IPython interpreter (the standard Python shell is not supported).
However you need to add '--' separator between two types of options.
To run SymPy without startup banner and colors, for example, issue in IPython 0.11 or higher:
$isympy -q -- --colors=NoColor
Or in an older version of IPython:
$isympy -q -- -colors NoColor
See also isympy --help.
"""
import os, sys, warnings
# hook in-tree SymPy into Python path, if possible
isympy_path = os.path.abspath(__file__)
isympy_dir = os.path.dirname(isympy_path)
sympy_top = os.path.split(isympy_dir)[0]
sympy_dir = os.path.join(sympy_top, 'sympy')
if os.path.isdir(sympy_dir):
sys.path.insert(0, sympy_top)
def main():
from optparse import OptionParser
if '-h' in sys.argv or '--help' in sys.argv:
# XXX: We can't use description=__doc__ in the OptionParser call
# below because optparse line wraps it weird. The argparse module
# allows you to disable this, though, but it's only available in
# Python 2.7+.
print __doc__ # the docstring of this module above
usage = 'usage: isympy [options] -- [ipython options]'
parser = OptionParser(
usage=usage,
version='0.7.1-git',
# XXX: We need a more centralized place to store the version.
# It is currently stored in sympy.__version__, but we can't yet
# import sympy at this point.
)
parser.add_option(
'-c', '--console',
dest='console',
action='store',
default=None,
choices=['ipython', 'python'],
help='select type of interactive session: ipython | python; defaults '
'to ipython if IPython is installed, otherwise python')
parser.add_option(
'-p', '--pretty',
dest='pretty',
action='store',
default=None,
choices=['unicode', 'ascii', 'no'],
help='setup pretty printing: unicode | ascii | no; defaults to '
'unicode printing if the terminal supports it, otherwise ascii')
parser.add_option(
'-t', '--types',
dest='types',
action='store',
default=None,
choices=['gmpy', 'python', 'sympy'],
help='setup ground types: gmpy | python | sympy; defaults to gmpy if '
'gmpy is installed, otherwise python (note that sympy ground types are '
'not supported and should be used for experimental purposes only)')
parser.add_option(
'-o', '--order',
dest='order',
action='store',
default=None,
choices=['lex', 'grlex', 'grevlex', 'rev-lex', 'rev-grlex', 'rev-grevlex', 'old', 'none'],
help='setup ordering of terms: [rev-]lex | [rev-]grlex | [rev-]grevlex | old | none; defaults to lex')
parser.add_option(
'-q', '--quiet',
dest='quiet',
action='store_true',
default=False,
help='print only version information at startup')
parser.add_option(
'-d', '--doctest',
dest='doctest',
action='store_true',
default=False,
help='use the doctest format for output (you can just copy and paste it)')
parser.add_option(
'-C', '--no-cache',
dest='cache',
action='store_false',
default=True,
help='disable caching mechanism')
parser.add_option(
'-a', '--auto',
dest='auto',
action='store_true',
default=False,
help='automatically construct missing symbols')
parser.add_option(
'-D', '--debug',
dest='debug',
action='store_true',
default=False,
help='enable debugging output')
(options, ipy_args) = parser.parse_args()
if not options.cache:
os.environ['SYMPY_USE_CACHE'] = 'no'
if options.types:
os.environ['SYMPY_GROUND_TYPES'] = options.types
if options.debug:
os.environ['SYMPY_DEBUG'] = str(options.debug)
if options.doctest:
options.pretty = 'no'
options.console = 'python'
session = options.console
if session is not None:
ipython = session == 'ipython'
else:
ipython = None
args = {
'pretty_print' : True,
'use_unicode' : None,
'order' : None,
'argv' : ipy_args,
}
if options.pretty == 'unicode':
args['use_unicode'] = True
elif options.pretty == 'ascii':
args['use_unicode'] = False
elif options.pretty == 'no':
args['pretty_print'] = False
if options.order is not None:
args['order'] = options.order
args['quiet'] = options.quiet
args['auto'] = options.auto
from sympy.utilities.exceptions import SymPyDeprecationWarning
warnings.simplefilter("always", SymPyDeprecationWarning)
from sympy.interactive import init_session
init_session(ipython, **args)
if __name__ == "__main__":
main()