Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/all.py
4045 views
1
"""
2
all.py -- much of sage is imported into this module, so you don't
3
have to import everything individually.
4
5
TESTS:
6
7
This is to test #10570. If the number of stackframes at startup
8
changes due to a patch you made, please check that this was an
9
intended effect of your patch.
10
11
::
12
13
sage: import gc
14
sage: import inspect
15
sage: from sage import *
16
sage: frames=[x for x in gc.get_objects() if inspect.isframe(x)]
17
sage: len(frames)
18
11
19
20
"""
21
22
from __future__ import with_statement
23
24
###############################################################################
25
#
26
# SAGE: System for Algebra and Geometry Experimentation
27
#
28
# Copyright (C) 2005, 2006 William Stein <[email protected]>
29
#
30
# Distributed under the terms of the GNU General Public License (GPL)
31
#
32
# This code is distributed in the hope that it will be useful,
33
# but WITHOUT ANY WARRANTY; without even the implied warranty of
34
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35
# General Public License for more details.
36
#
37
# The full text of the GPL is available at:
38
#
39
# http://www.gnu.org/licenses/
40
###############################################################################
41
42
# Error message that matches the Sage/IPython defaults
43
quit = "Use Ctrl-D (i.e. EOF), %Exit, or %Quit to exit without confirmation."
44
exit = quit
45
46
import os, sys
47
48
if 'SAGE_ROOT' not in os.environ:
49
raise RuntimeError("To use the Sage libraries, set the environment variable SAGE_ROOT to the Sage build directory and LD_LIBRARY_PATH to $SAGE_ROOT/local/lib")
50
51
if sys.version_info[:2] < (2, 5):
52
print >>sys.stderr, "Sage requires Python 2.5 or newer"
53
sys.exit(1)
54
55
###################################################################
56
57
# We have to set this here so urllib, etc. can detect it.
58
import sage.server.notebook.gnutls_socket_ssl
59
sage.server.notebook.gnutls_socket_ssl.require_SSL()
60
61
###################################################################
62
63
from sage.ext.c_lib import _init_csage, sig_on_count
64
_init_csage()
65
66
from time import sleep
67
68
from sage.misc.all import * # takes a while
69
70
from sage.misc.sh import sh
71
72
from sage.libs.all import *
73
74
from sage.rings.all import *
75
from sage.matrix.all import *
76
77
# This must come before Calculus -- it initializes the Pynac library.
78
import sage.symbolic.pynac
79
80
from sage.modules.all import *
81
from sage.monoids.all import *
82
from sage.algebras.all import *
83
from sage.modular.all import *
84
from sage.schemes.all import *
85
from sage.graphs.all import *
86
from sage.groups.all import *
87
from sage.databases.all import *
88
from sage.structure.all import *
89
from sage.categories.all import *
90
from sage.sets.all import *
91
from sage.probability.all import *
92
from sage.interfaces.all import *
93
94
from sage.symbolic.all import *
95
96
from sage.functions.all import *
97
from sage.calculus.all import *
98
99
from sage.server.all import *
100
import sage.tests.all as tests
101
102
from sage.crypto.all import *
103
import sage.crypto.mq as mq
104
105
from sage.plot.all import *
106
from sage.plot.plot3d.all import *
107
108
from sage.coding.all import *
109
from sage.combinat.all import *
110
111
from sage.lfunctions.all import *
112
113
from sage.geometry.all import *
114
from sage.geometry.triangulation.all import *
115
116
from sage.homology.all import *
117
118
from sage.quadratic_forms.all import *
119
120
from sage.gsl.all import *
121
122
from sage.games.all import *
123
124
from sage.media.all import *
125
126
from sage.logic.all import *
127
128
from sage.numerical.all import *
129
130
from sage.stats.all import *
131
import sage.stats.all as stats
132
133
import sage.finance.all as finance
134
135
import sage.interacts.all as interacts
136
from sage.interacts.debugger import debug
137
138
from sage.parallel.all import *
139
140
from sage.ext.fast_callable import fast_callable
141
from sage.ext.fast_eval import fast_float
142
143
from sage.sandpiles.all import *
144
145
from sage.tensor.all import *
146
147
from copy import copy, deepcopy
148
149
# The code executed here uses a large amount of Sage components
150
from sage.rings.qqbar import _init_qqbar
151
_init_qqbar()
152
153
#Deprecate the is_* functions from the top level
154
#All of these functions should be removed from the top level
155
#after a few releases, and this code should be removed.
156
#--Mike Hansen 9/25/2008
157
message = "\nUsing %(name)s from the top level is deprecated since it was designed to be used by developers rather than end users.\nIt most likely does not do what you would expect it to do. If you really need to use it, import it from the module that it is defined in."
158
sage.misc.misc.deprecated_callable_import(None, globals(), locals(),
159
[name for name in globals().keys()
160
if name.startswith('is_') and name[3].isupper()], message)
161
162
del message, name
163
164
165
###########################################################
166
#### WARNING:
167
# DO *not* import numpy / matplotlib / networkx here!!
168
# Each takes a surprisingly long time to initialize,
169
# and that initialization should be done more on-the-fly
170
# when they are first needed.
171
###########################################################
172
173
###################################################################
174
175
# maximize memory resources
176
#try:
177
# import resource # unix only...
178
# resource.setrlimit(resource.RLIMIT_AS, (-1,-1))
179
#except:
180
# pass
181
182
# very useful 2-letter shortcuts
183
CC = ComplexField()
184
QQ = RationalField()
185
RR = RealField() # default real field
186
ZZ = IntegerRing()
187
# NOTE: QQ, RR, and ZZ are used by the pre-parser, and should not be
188
# overwritten by the user, unless they want to change the meaning of
189
# int and real in the interpreter (which is a potentially valid thing
190
# to do, and doesn't mess up anything else in the Sage library).
191
# E.g., typing "int = ZZ" in the Sage interpreter makes int literals
192
# acts as Python ints again.
193
194
195
196
# Some shorter shortcuts:
197
# Q = QQ
198
# Z = ZZ
199
# C = CC
200
#i = CC.gen(0)
201
true = True
202
false = False
203
204
oo = infinity
205
#x = PolynomialRing(QQ,'x').gen()
206
207
from sage.misc.copying import license
208
copying = license
209
copyright = license
210
211
_cpu_time_ = cputime()
212
_wall_time_ = walltime()
213
214
def quit_sage(verbose=True):
215
"""
216
If you use Sage in library mode, you should call this function
217
when your application quits.
218
219
It makes sure any child processes are also killed, etc.
220
"""
221
if verbose:
222
t1 = cputime(_cpu_time_)
223
t1m = int(t1/60); t1s=t1-t1m*60
224
t2 = walltime(_wall_time_)
225
t2m = int(t2/60); t2s=t2-t2m*60
226
print "Exiting Sage (CPU time %sm%.2fs, Wall time %sm%.2fs)."%(
227
t1m,t1s,t2m,t2s)
228
from sage.interfaces.quit import expect_quitall
229
expect_quitall(verbose=verbose)
230
231
import sage.matrix.matrix_mod2_dense
232
sage.matrix.matrix_mod2_dense.free_m4ri()
233
234
import sage.libs.flint.flint
235
sage.libs.flint.flint.free_flint_stack()
236
237
pari._unsafe_deallocate_pari_stack()
238
239
### The following is removed -- since it would cleanup
240
### the tmp directory that the sage cleaner depends upon.
241
# The following code close all open file descriptors,
242
# so that on shared file systems the delete_tmpfiles
243
# command below works.
244
# AUTHOR:
245
# * Kate Minola (2007-05-03)
246
#import resource # Resource usage information.
247
#maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
248
#if maxfd != resource.RLIM_INFINITY:
249
# Iterate through and close all file descriptors.
250
# for fd in range(0, maxfd):
251
# try:
252
# os.close(fd)
253
# except OSError: # ERROR, fd wasn't open to begin with (ignored)
254
# pass
255
# Now delete the temp files
256
#from sage.misc.misc import delete_tmpfiles
257
#delete_tmpfiles()
258
259
# stop the twisted reactor
260
try:
261
from twisted.internet import reactor
262
if reactor.running:
263
reactor.callFromThread(reactor.stop)
264
except ImportError:
265
pass
266
267
# Free globally allocated mpir integers.
268
import sage.rings.integer
269
sage.rings.integer.free_integer_pool()
270
sage.rings.integer.clear_mpz_globals()
271
import sage.algebras.quatalg.quaternion_algebra_element
272
sage.algebras.quatalg.quaternion_algebra_element._clear_globals()
273
274
from sage.libs.all import symmetrica
275
symmetrica.end()
276
277
def _quit_sage_(self):
278
import sage.misc.preparser_ipython
279
if sage.misc.preparser_ipython.interface != None:
280
sage.misc.preparser_ipython.switch_interface('sage')
281
self.exit_now = False
282
return
283
284
from IPython.genutils import ask_yes_no
285
if self.rc.confirm_exit:
286
if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
287
self.exit_now = True
288
else:
289
self.exit_now = True
290
if self.exit_now:
291
quit_sage()
292
self.exit_now = True
293
294
return self.exit_now
295
296
from IPython.iplib import InteractiveShell
297
InteractiveShell.exit = _quit_sage_
298
299
import sage.misc.displayhook
300
sage.misc.displayhook.install()
301
302
from sage.ext.interactive_constructors_c import inject_on, inject_off
303
304
sage.structure.sage_object.register_unpickle_override('sage.categories.category', 'Sets', Sets)
305
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'HeckeModules', HeckeModules)
306
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Objects', Objects)
307
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Rings', Rings)
308
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Fields', Fields)
309
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'VectorSpaces', VectorSpaces)
310
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'Schemes_over_base', sage.categories.schemes.Schemes_over_base)
311
sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', 'ModularAbelianVarieties', ModularAbelianVarieties)
312
#sage.structure.sage_object.register_unpickle_override('sage.categories.category_types', '', )
313
314
# Cache the contents of star imports.
315
import sage.misc.lazy_import
316
sage.misc.lazy_import.save_cache_file()
317
318
319
### Debugging for Singular, see trac #10903
320
# from sage.libs.singular.ring import poison_currRing
321
# sys.settrace(poison_currRing)
322
323
324
# Write a file indicating that Sage was started up successfully.
325
def _write_started_file():
326
"""
327
Write a ``sage-started.txt`` file if it does not exist. The
328
contents of this file do not matter, only its existence.
329
330
The non-existence of this file will be used as a trigger to run
331
``sage-starts`` during the Sage build.
332
333
TESTS:
334
335
Check that the file exists when Sage is running::
336
337
sage: started_file = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-started.txt')
338
sage: os.path.isfile(started_file)
339
True
340
"""
341
started_file = os.path.join(SAGE_ROOT, 'local', 'lib', 'sage-started.txt')
342
# Do nothing if the file already exists
343
if os.path.isfile(started_file):
344
return
345
346
# Current time with a resolution of 1 second
347
import datetime
348
t = datetime.datetime.now().replace(microsecond=0)
349
350
O = open(started_file, 'w')
351
O.write("Sage %s was started at %s\n"%(sage.version.version, t))
352
O.close()
353
354
_write_started_file()
355
356
357
# Set a new random number seed as the very last thing
358
# (so that printing initial_seed() and using that seed
359
# in set_random_seed() will result in the same sequence you got at
360
# Sage startup).
361
set_random_seed()
362
363