A Sage extension which adds sage-specific features:
magics
%crun
%runfile
%attach
%display
%mode
(like %maxima
, etc.)
%%cython
%%fortran
preparsing of input
loading Sage library
running init.sage
changing prompt to Sage prompt
Display hook
Bases: object
Initialize the Sage plugin.
Return a Python module containing all globals which should be made available to the user.
EXAMPLES:
Sagesage: from sage.repl.ipython_extension import SageCustomizations sage: SageCustomizations.all_globals() <module 'sage.all_cmdline' ...>Python
>>> from sage.all import * >>> from sage.repl.ipython_extension import SageCustomizations >>> SageCustomizations.all_globals() <module 'sage.all_cmdline' ...>
Set up Sage command-line environment
Set up transforms (like the preparser).
Register magics for each of the Sage interfaces
Run Sage’s initial startup file.
Bases: SageCustomizations
Return a Python module containing all globals which should be made available to the user when running the Jupyter notebook.
EXAMPLES:
Sagesage: from sage.repl.ipython_extension import SageJupyterCustomizations sage: SageJupyterCustomizations.all_globals() <module 'sage.repl.ipython_kernel.all_jupyter' ...>Python
>>> from sage.all import * >>> from sage.repl.ipython_extension import SageJupyterCustomizations >>> SageJupyterCustomizations.all_globals() <module 'sage.repl.ipython_kernel.all_jupyter' ...>
Bases: Magics
Attach the code contained in the file s
.
This is designed to be used from the command line as %attach /path/to/file
.
s
– string. The file to be attached
EXAMPLES:
Sagesage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: from tempfile import NamedTemporaryFile as NTF sage: with NTF(mode='w+t', suffix='.py', delete=False) as f: ....: _ = f.write('a = 2\n') sage: shell.run_cell('%attach ' + f.name) sage: shell.run_cell('a') 2 sage: sleep(1) # filesystem timestamp granularity sage: with open(f.name, 'w') as f: _ = f.write('a = 3\n')Python
>>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> from tempfile import NamedTemporaryFile as NTF >>> with NTF(mode='w+t', suffix='.py', delete=False) as f: ... _ = f.write('a = 2\n') >>> shell.run_cell('%attach ' + f.name) >>> shell.run_cell('a') 2 >>> sleep(Integer(1)) # filesystem timestamp granularity >>> with open(f.name, 'w') as f: _ = f.write('a = 3\n')
Note that the doctests are never really at the command prompt, so we call the input hook manually:
Sagesage: shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified') sage: shell.run_cell('reload_attached_files_if_modified()') ### reloading attached file ... modified at ... ### sage: shell.run_cell('a') 3 sage: shell.run_cell('detach(%r)' % f.name) sage: shell.run_cell('attached_files()') [] sage: os.remove(f.name) sage: shell.quit()Python
>>> from sage.all import * >>> shell.run_cell('from sage.repl.attach import reload_attached_files_if_modified') >>> shell.run_cell('reload_attached_files_if_modified()') ### reloading attached file ... modified at ... ### >>> shell.run_cell('a') 3 >>> shell.run_cell('detach(%r)' % f.name) >>> shell.run_cell('attached_files()') [] >>> os.remove(f.name) >>> shell.quit()
Profile C function calls.
INPUT:
s
– string; Sage command to profile
EXAMPLES:
Sagesage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell('%crun sum(1/(1+n^2) for n in range(100))') # optional - gperftools PROFILE: interrupts/evictions/bytes = ... Using local file ... Using local file ... sage: shell.quit()Python
>>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> shell.run_cell('%crun sum(1/(1+n^2) for n in range(100))') # optional - gperftools PROFILE: interrupts/evictions/bytes = ... Using local file ... Using local file ... >>> shell.quit()
Cython cell magic.
This is syntactic sugar on the cython_compile()
function.
Note that there is also the %%cython
cell magic provided by Cython, which can be loaded with %load_ext cython
, see Cython documentation for more details. The semantic is slightly different from the version provided by Sage.
INPUT:
line
– parsed as keyword arguments. The allowed arguments are:
--verbose N
/ -v N
--compile-message
--use-cache
--create-local-c-file
--annotate
--view-annotate
--sage-namespace
--create-local-so-file
--no-compile-message
, --no-use-cache
, etc.
See cython()
for details.
If --view-annotate
is given, the annotation is either displayed inline in the Sage notebook or opened in a new web browser, depending on whether the Sage notebook is used.
You can override the selection by specifying --view-annotate=webbrowser
or --view-annotate=displayhtml
.
cell
– string; the Cython source code to process
OUTPUT: none; the Cython code is compiled and loaded
EXAMPLES:
Sagesage: # needs sage.misc.cython sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell( ....: ''' ....: %%cython -v1 --annotate --no-sage-namespace ....: def f(): ....: print('test') ....: ''') Compiling ....pyx because it changed. [1/1] Cythonizing ....pyx sage: f() testPython
>>> from sage.all import * >>> # needs sage.misc.cython >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> shell.run_cell( ... ''' ... %%cython -v1 --annotate --no-sage-namespace ... def f(): ... print('test') ... ''') Compiling ....pyx because it changed. [1/1] Cythonizing ....pyx >>> f() test
A magic command to switch between simple display and ASCII art display.
args
– string. See sage.repl.rich_output.preferences
for allowed values. If the mode is ascii_art
, it can optionally be followed by a width.
How to use: if you want to activate the ASCII art mode:
Sagesage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell('%display ascii_art')Python
>>> from sage.all import * >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> shell.run_cell('%display ascii_art')
That means you do not have to use ascii_art()
to get an ASCII art output:
sage: shell.run_cell("i = var('i')") # needs sage.symbolic sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 10 9 8 7 6 5 4 3 2 100*x + 81*x + 64*x + 49*x + 36*x + 25*x + 16*x + 9*x + 4*x + xPython
>>> from sage.all import * >>> shell.run_cell("i = var('i')") # needs sage.symbolic >>> shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 10 9 8 7 6 5 4 3 2 100*x + 81*x + 64*x + 49*x + 36*x + 25*x + 16*x + 9*x + 4*x + x
Then when you want to return to ‘textual mode’:
Sagesage: shell.run_cell('%display text plain') sage: shell.run_cell('%display plain') # shortcut for "text plain" sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 100*x^10 + 81*x^9 + 64*x^8 + 49*x^7 + 36*x^6 + 25*x^5 + 16*x^4 + 9*x^3 + 4*x^2 + xPython
>>> from sage.all import * >>> shell.run_cell('%display text plain') >>> shell.run_cell('%display plain') # shortcut for "text plain" >>> shell.run_cell('sum(i^2*x^i, i, 0, 10)') # needs sage.symbolic 100*x^10 + 81*x^9 + 64*x^8 + 49*x^7 + 36*x^6 + 25*x^5 + 16*x^4 + 9*x^3 + 4*x^2 + x
Sometime you could have to use a special output width and you could specify it:
Sagesage: shell.run_cell('%display ascii_art') sage: shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ 1 4 1 3 [ 1 3 4 1 2 4 1 2 3 1 3 1 2 2 2 [ 1 2 3 4, 2 , 3 , 4 , 2 4, 3 4, 3 , 4 , 1 ] 1 2 2 ] 3 3 ] 4 , 4 ] sage: shell.run_cell('%display ascii_art 50') sage: shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ [ 1 3 4 1 2 4 1 2 3 [ 1 2 3 4, 2 , 3 , 4 , 1 ] 1 4 1 3 1 2 2 ] 1 3 1 2 2 2 3 3 ] 2 4, 3 4, 3 , 4 , 4 , 4 ]Python
>>> from sage.all import * >>> shell.run_cell('%display ascii_art') >>> shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ 1 4 1 3 [ 1 3 4 1 2 4 1 2 3 1 3 1 2 2 2 [ 1 2 3 4, 2 , 3 , 4 , 2 4, 3 4, 3 , 4 , <BLANKLINE> 1 ] 1 2 2 ] 3 3 ] 4 , 4 ] >>> shell.run_cell('%display ascii_art 50') >>> shell.run_cell('StandardTableaux(4).list()') # needs sage.combinat [ [ [ 1 3 4 1 2 4 1 2 3 [ 1 2 3 4, 2 , 3 , 4 , <BLANKLINE> 1 ] 1 4 1 3 1 2 2 ] 1 3 1 2 2 2 3 3 ] 2 4, 3 4, 3 , 4 , 4 , 4 ]
As yet another option, typeset mode. This is used in the emacs interface:
Sagesage: shell.run_cell('%display text latex') sage: shell.run_cell('1/2') 1/2Python
>>> from sage.all import * >>> shell.run_cell('%display text latex') >>> shell.run_cell('1/2') 1/2
Switch back:
Sagesage: shell.run_cell('%display default')Python
>>> from sage.all import * >>> shell.run_cell('%display default')
Switch graphics to default to vector or raster graphics file formats:
Sagesage: shell.run_cell('%display graphics vector')Python
>>> from sage.all import * >>> shell.run_cell('%display graphics vector')
Fortran cell magic.
This is syntactic sugar on the fortran()
function.
INPUT:
line
– ignored
cell
– string; the Cython source code to process
OUTPUT: none; the Fortran code is compiled and loaded
EXAMPLES:
Sagesage: # needs numpy sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell(''' ....: %%fortran ....: C FILE: FIB1.F ....: SUBROUTINE FIB(A,N) ....: C ....: C CALCULATE FIRST N FIBONACCI NUMBERS ....: C ....: INTEGER N ....: REAL*8 A(N) ....: DO I=1,N ....: IF (I.EQ.1) THEN ....: A(I) = 0.0D0 ....: ELSEIF (I.EQ.2) THEN ....: A(I) = 1.0D0 ....: ELSE ....: A(I) = A(I-1) + A(I-2) ....: ENDIF ....: ENDDO ....: END ....: C END FILE FIB1.F ....: ''') sage: fib <fortran ...> sage: from numpy import array sage: a = array(range(10), dtype=float) sage: fib(a, 10) sage: a array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.])Python
>>> from sage.all import * >>> # needs numpy >>> from sage.repl.interpreter import get_test_shell >>> shell = get_test_shell() >>> shell.run_cell(''' ... %%fortran ... C FILE: FIB1.F ... SUBROUTINE FIB(A,N) ... C ... C CALCULATE FIRST N FIBONACCI NUMBERS ... C ... INTEGER N ... REAL*8 A(N) ... DO I=1,N ... IF (I.EQ.1) THEN ... A(I) = 0.0D0 ... ELSEIF (I.EQ.2) THEN ... A(I) = 1.0D0 ... ELSE ... A(I) = A(I-1) + A(I-2) ... ENDIF ... ENDDO ... END ... C END FILE FIB1.F ... ''') >>> fib <fortran ...> >>> from numpy import array >>> a = array(range(Integer(10)), dtype=float) >>> fib(a, Integer(10)) >>> a array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.])
A magic command to interactively load a file as in MAGMA.
args
– string. The file to be interactively loaded
Note
Currently, this cannot be completely doctested as it relies on raw_input()
.
EXAMPLES:
Sagesage: ip = get_ipython() # not tested: works only in interactive shell sage: ip.magic_iload('/dev/null') # not tested: works only in interactive shell Interactively loading "/dev/null" # not tested: works only in interactive shellPython
>>> from sage.all import * >>> ip = get_ipython() # not tested: works only in interactive shell >>> ip.magic_iload('/dev/null') # not tested: works only in interactive shell Interactively loading "/dev/null" # not tested: works only in interactive shell
Execute the code contained in the file s
.
This is designed to be used from the command line as %runfile /path/to/file
.
s
– string; the file to be loaded
See also
This is the same as load()
.
EXAMPLES:
Sagesage: import os sage: from sage.repl.interpreter import get_test_shell sage: from sage.misc.temporary_file import tmp_dir sage: shell = get_test_shell() sage: tmp = os.path.join(tmp_dir(), 'run_cell.py') sage: with open(tmp, 'w') as f: ....: _ = f.write('a = 2\n') sage: shell.run_cell('%runfile '+tmp) sage: shell.run_cell('a') 2 sage: shell.quit()Python
>>> from sage.all import * >>> import os >>> from sage.repl.interpreter import get_test_shell >>> from sage.misc.temporary_file import tmp_dir >>> shell = get_test_shell() >>> tmp = os.path.join(tmp_dir(), 'run_cell.py') >>> with open(tmp, 'w') as f: ... _ = f.write('a = 2\n') >>> shell.run_cell('%runfile '+tmp) >>> shell.run_cell('a') 2 >>> shell.quit()
Load the extension in IPython.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4