AUTHORS:
Returns the name of the ATLAS library to use. On Darwin or Cygwin, this is 'blas', and otherwise it is 'atlas'.
EXAMPLES:
sage: sage.misc.cython.atlas() # random -- depends on OS
'atlas'
Return the name of the cblas library on this system. If the environment variable $SAGE_CBLAS is set, just return its value. If not, return 'cblas' if /usr/lib/libcblas.so or /usr/lib/libcblas.dylib exists, return 'blas' if /usr/lib/libblas.dll.a exists, and return 'gslcblas' otherwise.
EXAMPLES:
sage: sage.misc.cython.cblas() # random -- depends on OS, etc.
'cblas'
INPUT:
OUTPUT: a module, which results from compiling the given code and importing it
EXAMPLES:
sage: module = sage.misc.cython.compile_and_load("def f(int n):\n return n*n")
sage: module.f(10)
100
Compile a Cython file. This converts a Cython file to a C (or C++ file), and then compiles that. The .c file and the .so file are created in a temporary directory.
INPUTS:
TESTS:
Before trac ticket #12975, it would have beeen needed to write #clang c++, but upper case C++ has resulted in an error:
sage: from sage.all import SAGE_ROOT
sage: code = [
... "#clang C++",
... "#cinclude %s/local/include/singular %s/local/include/factory"%(SAGE_ROOT,SAGE_ROOT),
... "#clib m readline singular givaro ntl gmpxx gmp",
... "from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomial_libsingular",
... "from sage.libs.singular.polynomial cimport singular_polynomial_pow",
... "def test(MPolynomial_libsingular p):",
... " singular_polynomial_pow(&p._poly, p._poly, 2, p._parent_ring)"]
sage: cython(os.linesep.join(code))
The function test now manipulates internal C data of polynomials, squaring them:
sage: P.<x,y>=QQ[]
sage: test(x)
sage: x
x^2
Compile filename and make it available as a loadable shared object file.
INPUT:
OUTPUT: None
EFFECT: A compiled, python “importable” loadable shared object file is created.
Note
Shared object files are not reloadable. The intent is for imports in other scripts. A possible development cycle might go thus:
EXAMPLES:
sage: curdir = os.path.abspath(os.curdir)
sage: dir = tmp_dir(); os.chdir(dir)
sage: f = file('hello.spyx', 'w')
sage: s = "def hello():\n print 'hello'\n"
sage: f.write(s)
sage: f.close()
sage: cython_create_local_so('hello.spyx')
sage: sys.path.append('.')
sage: import hello
sage: hello.hello()
hello
sage: os.chdir(curdir)
AUTHORS:
Create a compiled function which evaluates expr assuming machine values for vars.
INPUT:
Warning
Accessing globals() doesn’t actually work, see trac ticket #12446.
EXAMPLES:
We create a Lambda function in pure Python (using the r to make sure the 3.2 is viewed as a Python float):
sage: f = lambda x,y: x*x + y*y + x + y + 17r*x + 3.2r
We make the same Lambda function, but in a compiled form.
sage: g = cython_lambda('double x, double y', 'x*x + y*y + x + y + 17*x + 3.2')
sage: g(2,3)
55.2
sage: g(0,0)
3.2
The following should work but doesn’t, see trac ticket #12446:
sage: a = 25
sage: f = cython_lambda('double x', 'sage.math.sin(x) + sage.a')
sage: f(10) # known bug
24.455978889110629
sage: a = 50
sage: f(10) # known bug
49.455978889110632
Given a string s, find each substring of the form '$ABC'. If the environment variable $ABC is set, replace '$ABC' with its value and move on to the next such substring. If it is not set, stop parsing there.
EXAMPLES:
sage: from sage.misc.cython import environ_parse
sage: environ_parse('$SAGE_ROOT') == SAGE_ROOT
True
sage: environ_parse('$THIS_IS_NOT_DEFINED_ANYWHERE')
'$THIS_IS_NOT_DEFINED_ANYWHERE'
sage: os.environ['DEFINE_THIS'] = 'hello'
sage: environ_parse('$DEFINE_THIS/$THIS_IS_NOT_DEFINED_ANYWHERE/$DEFINE_THIS')
'hello/$THIS_IS_NOT_DEFINED_ANYWHERE/$DEFINE_THIS'
This is used by the testing infrastructure to test building Cython programs.
INPUT:
OUTPUT: a module, which results from compiling the given code and importing it
EXAMPLES:
sage: module = sage.misc.cython.import_test("trac11680b")
sage: module.f(2,3,4)
9
Given a keyword kwd and a string s, return a list of all arguments on the same line as that keyword in s, as well as a new copy of s in which each occurrence of kwd is in a comment. If a comment already occurs on the line containing kwd, no words after the # are added to the list.
EXAMPLES:
sage: sage.misc.cython.parse_keywords('clib', " clib foo bar baz\n #cinclude bar\n")
(['foo', 'bar', 'baz'], ' #clib foo bar baz\n #cinclude bar\n')
sage: sage.misc.cython.parse_keywords('clib', "# qux clib foo bar baz\n #cinclude bar\n")
(['foo', 'bar', 'baz'], '# qux clib foo bar baz\n #cinclude bar\n')
sage: sage.misc.cython.parse_keywords('clib', "# clib foo bar # baz\n #cinclude bar\n")
(['foo', 'bar'], '# clib foo bar # baz\n #cinclude bar\n')
Preparse a pyx file:
The pragmas:
OUTPUT: preamble, libs, includes, language, files, args
EXAMPLES:
sage: from sage.misc.cython import pyx_preparse
sage: pyx_preparse("")
('\ninclude "interrupt.pxi" # ctrl-c interrupt block support\ninclude "stdsage.pxi" # ctrl-c interrupt block support\n\ninclude "cdefs.pxi"\n',
['mpfr',
'gmp',
'gmpxx',
'stdc++',
'pari',
'm',
'ec',
'gsl',
'...blas',
...,
'ntl',
'csage'],
['.../include/csage',
'.../include',
'.../include/python2.7',
'.../lib/python/site-packages/numpy/core/include',
'.../devel/sage/sage/ext',
'.../devel/sage',
'.../devel/sage/sage/gsl'],
'c',
[], ['-w', '-O2'])
sage: s, libs, inc, lang, f, args = pyx_preparse("# clang c++\n #clib foo\n # cinclude bar\n")
sage: lang
'c++'
sage: libs
['foo', 'mpfr',
'gmp', 'gmpxx',
'stdc++',
'pari',
'm',
'ec',
'gsl', '...blas', ...,
'ntl',
'csage']
sage: libs[1:] == sage.misc.cython.standard_libs
True
sage: inc
['bar',
'.../include/csage',
'.../include',
'.../include/python2.7',
'.../lib/python/site-packages/numpy/core/include',
'.../devel/sage/sage/ext',
'.../devel/sage',
'.../devel/sage/sage/gsl']
sage: s, libs, inc, lang, f, args = pyx_preparse("# cargs -O3 -ggdb\n")
sage: args
['-w', '-O2', '-O3', '-ggdb']
TESTS:
sage: module = sage.misc.cython.import_test("trac11680") # long time (7s on sage.math, 2012)
sage: R.<x> = QQ[]
sage: module.evaluate_at_power_of_gen(x^3 + x - 7, 5) # long time
x^15 + x^5 - 7
Given a filename f, replace it by a filename that is a valid Python module name.
This means that the characters are all alphanumeric or _‘s and doesn’t begin with a numeral.
EXAMPLES:
sage: from sage.misc.cython import sanitize
sage: sanitize('abc')
'abc'
sage: sanitize('abc/def')
'abc_def'
sage: sanitize('123/def-hij/file.py')
'_123_def_hij_file_py'
Given a string s and an integer n, for any line of s which has the form 'text:NUM:text' subtract n from NUM and return 'text:(NUM-n):text'. Return other lines of s without change.
EXAMPLES:
sage: from sage.misc.cython import subtract_from_line_numbers
sage: subtract_from_line_numbers('hello:1234:hello', 3)
'hello:1231:hello\n'
sage: subtract_from_line_numbers('text:123\nhello:1234:', 3)
'text:123\nhello:1231:\n'