| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Python shell for SymPy. |
| 8 |
|
| 9 |
This is just a normal Python shell (IPython shell if you have the |
| 10 |
IPython package installed), that executes the following commands for |
| 11 |
the user: |
| 12 |
|
| 13 |
>>> from __future__ import division |
| 14 |
>>> from sympy import * |
| 15 |
>>> x, y, z, t = symbols('x y z t') |
| 16 |
>>> k, m, n = symbols('k m n', integer=True) |
| 17 |
>>> f, g, h = symbols('f g h', cls=Function) |
| 18 |
|
| 19 |
So starting 'isympy' is equivalent to starting Python (or IPython) and |
| 20 |
executing the above commands by hand. It is intended for easy and quick |
| 21 |
experimentation with SymPy. isympy is a good way to use SymPy as an |
| 22 |
interactive calculator. If you have IPython and Matplotlib installed, then |
| 23 |
interactive plotting is enabled by default. |
| 24 |
|
| 25 |
COMMAND LINE OPTIONS |
| 26 |
-------------------- |
| 27 |
|
| 28 |
-c CONSOLE, --console=CONSOLE |
| 29 |
|
| 30 |
Use the specified shell (Python or IPython) shell as the console |
| 31 |
backend instead of the default one (IPython if present, Python |
| 32 |
otherwise), e.g.: |
| 33 |
|
| 34 |
$isympy -c python |
| 35 |
|
| 36 |
CONSOLE must be one of 'ipython' or 'python' |
| 37 |
|
| 38 |
-p PRETTY, --pretty PRETTY |
| 39 |
|
| 40 |
Setup pretty-printing in SymPy. When pretty-printing is enabled, |
| 41 |
expressions can be printed with Unicode or ASCII. The default is |
| 42 |
to use pretty-printing (with Unicode if the terminal supports it). |
| 43 |
When this option is 'no', expressions will not be pretty-printed |
| 44 |
and ASCII will be used: |
| 45 |
|
| 46 |
$isympy -p no |
| 47 |
|
| 48 |
PRETTY must be one of 'unicode', 'ascii', or 'no' |
| 49 |
|
| 50 |
-t TYPES, --types=TYPES |
| 51 |
|
| 52 |
Setup the ground types for the polys. By default, gmpy ground types |
| 53 |
are used if gmpy2 or gmpy is installed, otherwise it falls back to python |
| 54 |
ground types, which are a little bit slower. You can manually |
| 55 |
choose python ground types even if gmpy is installed (e.g., for |
| 56 |
testing purposes): |
| 57 |
|
| 58 |
$isympy -t python |
| 59 |
|
| 60 |
TYPES must be one of 'gmpy', 'gmpy1' or 'python' |
| 61 |
|
| 62 |
Note that the ground type gmpy1 is primarily intended for testing; it |
| 63 |
forces the use of gmpy version 1 even if gmpy2 is available. |
| 64 |
|
| 65 |
This is the same as setting the environment variable |
| 66 |
SYMPY_GROUND_TYPES to the given ground type (e.g., |
| 67 |
SYMPY_GROUND_TYPES='gmpy') |
| 68 |
|
| 69 |
The ground types can be determined interactively from the variable |
| 70 |
sympy.polys.domains.GROUND_TYPES. |
| 71 |
|
| 72 |
-o ORDER, --order ORDER |
| 73 |
|
| 74 |
Setup the ordering of terms for printing. The default is lex, which |
| 75 |
orders terms lexicographically (e.g., x**2 + x + 1). You can choose |
| 76 |
other orderings, such as rev-lex, which will use reverse |
| 77 |
lexicographic ordering (e.g., 1 + x + x**2): |
| 78 |
|
| 79 |
$isympy -o rev-lex |
| 80 |
|
| 81 |
ORDER must be one of 'lex', 'rev-lex', 'grlex', 'rev-grlex', |
| 82 |
'grevlex', 'rev-grevlex', 'old', or 'none'. |
| 83 |
|
| 84 |
Note that for very large expressions, ORDER='none' may speed up |
| 85 |
printing considerably but the terms will have no canonical order. |
| 86 |
|
| 87 |
-q, --quiet |
| 88 |
|
| 89 |
Print only Python's and SymPy's versions to stdout at startup. |
| 90 |
|
| 91 |
-d, --doctest |
| 92 |
|
| 93 |
Use the same format that should be used for doctests. This is |
| 94 |
equivalent to -c python -p no. |
| 95 |
|
| 96 |
-C, --no-cache |
| 97 |
|
| 98 |
Disable the caching mechanism. Disabling the cache may slow certain |
| 99 |
operations down considerably. This is useful for testing the cache, |
| 100 |
or for benchmarking, as the cache can result in deceptive timings. |
| 101 |
|
| 102 |
This is equivalent to setting the environment variable |
| 103 |
SYMPY_USE_CACHE to 'no'. |
| 104 |
|
| 105 |
-a, --auto-symbols (requires at least IPython 0.11) |
| 106 |
|
| 107 |
Automatically create missing symbols. Normally, typing a name of a |
| 108 |
Symbol that has not been instantiated first would raise NameError, |
| 109 |
but with this option enabled, any undefined name will be |
| 110 |
automatically created as a Symbol. |
| 111 |
|
| 112 |
Note that this is intended only for interactive, calculator style |
| 113 |
usage. In a script that uses SymPy, Symbols should be instantiated |
| 114 |
at the top, so that it's clear what they are. |
| 115 |
|
| 116 |
This will not override any names that are already defined, which |
| 117 |
includes the single character letters represented by the mnemonic |
| 118 |
QCOSINE (see the "Gotchas and Pitfalls" document in the |
| 119 |
documentation). You can delete existing names by executing "del |
| 120 |
name". If a name is defined, typing "'name' in dir()" will return True. |
| 121 |
|
| 122 |
The Symbols that are created using this have default assumptions. |
| 123 |
If you want to place assumptions on symbols, you should create them |
| 124 |
using symbols() or var(). |
| 125 |
|
| 126 |
Finally, this only works in the top level namespace. So, for |
| 127 |
example, if you define a function in isympy with an undefined |
| 128 |
Symbol, it will not work. |
| 129 |
|
| 130 |
See also the -i and -I options. |
| 131 |
|
| 132 |
-i, --int-to-Integer (requires at least IPython 0.11) |
| 133 |
|
| 134 |
Automatically wrap int literals with Integer. This makes it so that |
| 135 |
things like 1/2 will come out as Rational(1, 2), rather than 0.5. This |
| 136 |
works by preprocessing the source and wrapping all int literals with |
| 137 |
Integer. Note that this will not change the behavior of int literals |
| 138 |
assigned to variables, and it also won't change the behavior of functions |
| 139 |
that return int literals. |
| 140 |
|
| 141 |
If you want an int, you can wrap the literal in int(), e.g. int(3)/int(2) |
| 142 |
gives 1.5 (with division imported from __future__). |
| 143 |
|
| 144 |
-I, --interactive (requires at least IPython 0.11) |
| 145 |
|
| 146 |
This is equivalent to --auto-symbols --int-to-Integer. Future options |
| 147 |
designed for ease of interactive use may be added to this. |
| 148 |
|
| 149 |
-D, --debug |
| 150 |
|
| 151 |
Enable debugging output. This is the same as setting the |
| 152 |
environment variable SYMPY_DEBUG to 'True'. The debug status is set |
| 153 |
in the variable SYMPY_DEBUG within isympy. |
| 154 |
|
| 155 |
-- IPython options |
| 156 |
|
| 157 |
Additionally you can pass command line options directly to the IPython |
| 158 |
interpreter (the standard Python shell is not supported). However you |
| 159 |
need to add the '--' separator between two types of options, e.g the |
| 160 |
startup banner option and the colors option. You need to enter the |
| 161 |
options as required by the version of IPython that you are using, too: |
| 162 |
|
| 163 |
in IPython 0.11, |
| 164 |
|
| 165 |
$isympy -q -- --colors=NoColor |
| 166 |
|
| 167 |
or older versions of IPython, |
| 168 |
|
| 169 |
$isympy -q -- -colors NoColor |
| 170 |
|
| 171 |
See also isympy --help. |
| 172 |
|
| 173 |
""" |
| 174 |
|
| 175 |
import os |
| 176 |
import sys |
| 177 |
import warnings |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
isympy_path = os.path.abspath(__file__) |
| 182 |
print(isympy_path) |
| 183 |
isympy_dir = os.path.dirname(isympy_path) |
| 184 |
root_dir = os.path.split(isympy_dir)[0] |
| 185 |
sympy_top = os.path.join(root_dir, 'wxgeometrie') |
| 186 |
sympy_dir = os.path.join(sympy_top, 'sympy') |
| 187 |
|
| 188 |
if os.path.isdir(sympy_dir): |
| 189 |
sys.path.insert(0, sympy_top) |
| 190 |
sys.path.insert(0, root_dir) |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
def main(): |
| 195 |
from optparse import OptionParser |
| 196 |
|
| 197 |
if '-h' in sys.argv or '--help' in sys.argv: |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
print(__doc__) |
| 203 |
|
| 204 |
VERSION = None |
| 205 |
if '--version' in sys.argv: |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
import sympy |
| 211 |
VERSION = sympy.__version__ |
| 212 |
usage = 'usage: isympy [options] -- [ipython options]' |
| 213 |
parser = OptionParser( |
| 214 |
usage=usage, |
| 215 |
version=VERSION, |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
) |
| 220 |
|
| 221 |
parser.add_option( |
| 222 |
'-c', '--console', |
| 223 |
dest='console', |
| 224 |
action='store', |
| 225 |
default=None, |
| 226 |
choices=['ipython', 'python'], |
| 227 |
help='select type of interactive session: ipython | python; defaults ' |
| 228 |
'to ipython if IPython is installed, otherwise python') |
| 229 |
|
| 230 |
parser.add_option( |
| 231 |
'-p', '--pretty', |
| 232 |
dest='pretty', |
| 233 |
action='store', |
| 234 |
default=None, |
| 235 |
choices=['unicode', 'ascii', 'no'], |
| 236 |
help='setup pretty printing: unicode | ascii | no; defaults to ' |
| 237 |
'unicode printing if the terminal supports it, otherwise ascii') |
| 238 |
|
| 239 |
parser.add_option( |
| 240 |
'-t', '--types', |
| 241 |
dest='types', |
| 242 |
action='store', |
| 243 |
default=None, |
| 244 |
choices=['gmpy', 'gmpy1', 'python'], |
| 245 |
help='setup ground types: gmpy | gmpy1 | python; defaults to gmpy if gmpy2 ' |
| 246 |
'or gmpy is installed, otherwise python') |
| 247 |
|
| 248 |
parser.add_option( |
| 249 |
'-o', '--order', |
| 250 |
dest='order', |
| 251 |
action='store', |
| 252 |
default=None, |
| 253 |
choices=['lex', 'grlex', 'grevlex', 'rev-lex', 'rev-grlex', 'rev-grevlex', 'old', 'none'], |
| 254 |
help='setup ordering of terms: [rev-]lex | [rev-]grlex | [rev-]grevlex | old | none; defaults to lex') |
| 255 |
|
| 256 |
parser.add_option( |
| 257 |
'-q', '--quiet', |
| 258 |
dest='quiet', |
| 259 |
action='store_true', |
| 260 |
default=False, |
| 261 |
help='print only version information at startup') |
| 262 |
|
| 263 |
parser.add_option( |
| 264 |
'-d', '--doctest', |
| 265 |
dest='doctest', |
| 266 |
action='store_true', |
| 267 |
default=False, |
| 268 |
help='use the doctest format for output (you can just copy and paste it)') |
| 269 |
|
| 270 |
parser.add_option( |
| 271 |
'-C', '--no-cache', |
| 272 |
dest='cache', |
| 273 |
action='store_false', |
| 274 |
default=True, |
| 275 |
help='disable caching mechanism') |
| 276 |
|
| 277 |
parser.add_option( |
| 278 |
'-a', '--auto-symbols', |
| 279 |
dest='auto_symbols', |
| 280 |
action='store_true', |
| 281 |
default=False, |
| 282 |
help='automatically construct missing symbols') |
| 283 |
|
| 284 |
parser.add_option( |
| 285 |
'-i', '--int-to-Integer', |
| 286 |
dest='auto_int_to_Integer', |
| 287 |
action='store_true', |
| 288 |
default=False, |
| 289 |
help="automatically wrap int literals with Integer") |
| 290 |
|
| 291 |
parser.add_option( |
| 292 |
'-I', '--interactive', |
| 293 |
dest='interactive', |
| 294 |
action='store_true', |
| 295 |
default=False, |
| 296 |
help="equivalent to -a -i") |
| 297 |
|
| 298 |
parser.add_option( |
| 299 |
'-D', '--debug', |
| 300 |
dest='debug', |
| 301 |
action='store_true', |
| 302 |
default=False, |
| 303 |
help='enable debugging output') |
| 304 |
|
| 305 |
(options, ipy_args) = parser.parse_args() |
| 306 |
|
| 307 |
if not options.cache: |
| 308 |
os.environ['SYMPY_USE_CACHE'] = 'no' |
| 309 |
|
| 310 |
if options.types: |
| 311 |
os.environ['SYMPY_GROUND_TYPES'] = options.types |
| 312 |
|
| 313 |
if options.debug: |
| 314 |
os.environ['SYMPY_DEBUG'] = str(options.debug) |
| 315 |
|
| 316 |
if options.doctest: |
| 317 |
options.pretty = 'no' |
| 318 |
options.console = 'python' |
| 319 |
|
| 320 |
session = options.console |
| 321 |
|
| 322 |
if session is not None: |
| 323 |
ipython = session == 'ipython' |
| 324 |
else: |
| 325 |
try: |
| 326 |
import IPython |
| 327 |
ipython = True |
| 328 |
except ImportError: |
| 329 |
if not options.quiet: |
| 330 |
from sympy.interactive.session import no_ipython |
| 331 |
print(no_ipython) |
| 332 |
ipython = False |
| 333 |
|
| 334 |
args = { |
| 335 |
'pretty_print': True, |
| 336 |
'use_unicode': None, |
| 337 |
'use_latex': None, |
| 338 |
'order': None, |
| 339 |
'argv': ipy_args, |
| 340 |
} |
| 341 |
|
| 342 |
if options.pretty == 'unicode': |
| 343 |
args['use_unicode'] = True |
| 344 |
elif options.pretty == 'ascii': |
| 345 |
args['use_unicode'] = False |
| 346 |
elif options.pretty == 'no': |
| 347 |
args['pretty_print'] = False |
| 348 |
|
| 349 |
if options.order is not None: |
| 350 |
args['order'] = options.order |
| 351 |
|
| 352 |
args['quiet'] = options.quiet |
| 353 |
args['auto_symbols'] = options.auto_symbols or options.interactive |
| 354 |
args['auto_int_to_Integer'] = options.auto_int_to_Integer or options.interactive |
| 355 |
|
| 356 |
from sympy.utilities.exceptions import SymPyDeprecationWarning |
| 357 |
warnings.simplefilter("always", SymPyDeprecationWarning) |
| 358 |
|
| 359 |
import sympy.interactive.session as session |
| 360 |
|
| 361 |
session.preexec_source += 'from wxgeometrie import *\n' |
| 362 |
from sympy.interactive import init_session |
| 363 |
init_session(ipython, **args) |
| 364 |
|
| 365 |
if __name__ == "__main__": |
| 366 |
main() |