Inspired by this ask.sagemath.org question <https://ask.sagemath.org/question/65949/problem-with-solving-equation-and-finding-the-maximum-and-minimum/>. Let sage: f = x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991))
Then, one would may like to accelerate f evaluation via cythonization. but : sage: cf=cython_lambda('double x', f) --------------------------------------------------------------------------- CompileError Traceback (most recent call last) File /usr/local/sage-9/src/sage/misc/cython.py:358, in cython(filename, verbose, compile_message, use_cache, create_local_c_file, annotate, sage_namespace, create_local_so_file) 357 with cython_namespace_package_support(): --> 358 ext, = cythonize([ext], 359 aliases=aliases, 360 include_path=includes, 361 compiler_directives=directives, 362 quiet=(verbose <= 0), 363 errors_to_stderr=False, 364 use_listing_file=True) 365 finally: 366 # Read the "listing file" which is the file containing 367 # warning and error messages generated by Cython. File /usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/Cython/Build/Dependencies.py:1127, in cythonize(module_list, exclude, nthreads, aliases, quiet, force, language, exclude_failures, **options) 1126 for args in to_compile: -> 1127 cythonize_one(*args) 1129 if exclude_failures: File /usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/Cython/Build/Dependencies.py:1250, in cythonize_one(pyx_file, c_file, fingerprint, quiet, options, raise_on_failure, embedded_metadata, full_module_name, progress) 1249 if raise_on_failure: -> 1250 raise CompileError(None, pyx_file) 1251 elif os.path.exists(c_file): CompileError: /tmp/tmpx6ekg9s7/spyx/_tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx/_tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx During handling of the above exception, another exception occurred: RuntimeError Traceback (most recent call last) Cell In [40], line 1 ----> 1 cf=cython_lambda('double x', f) File /usr/local/sage-9/src/sage/misc/lazy_import.pyx:402, in sage.misc.lazy_import.LazyImport.__call__() 400 True 401 """ --> 402 return self.get_object()(*args, **kwds) 403 404 def __repr__(self): File /usr/local/sage-9/src/sage/misc/cython.py:510, in cython_lambda(vars, expr, verbose, **kwds) 507 f.write(s) 509 d = {} --> 510 cython_import_all(tmpfile, d, verbose=verbose, **kwds) 511 return d['f'] File /usr/local/sage-9/src/sage/misc/cython.py:563, in cython_import_all(filename, globals, **kwds) 548 def cython_import_all(filename, globals, **kwds): 549 """ 550 Imports all non-private (i.e., not beginning with an underscore) 551 attributes of the specified Cython module into the given context. (...) 561 code 562 """ --> 563 m = cython_import(filename, **kwds) 564 for k, x in m.__dict__.items(): 565 if k[0] != '_': File /usr/local/sage-9/src/sage/misc/cython.py:534, in cython_import(filename, **kwds) 517 def cython_import(filename, **kwds): 518 """ 519 Compile a file containing Cython code, then import and return the 520 module. Raises an ``ImportError`` if anything goes wrong. (...) 532 - the module that contains the compiled Cython code. 533 """ --> 534 name, build_dir = cython(filename, **kwds) 536 oldpath = sys.path 537 try: File /usr/local/sage-9/src/sage/misc/cython.py:374, in cython(filename, verbose, compile_message, use_cache, create_local_c_file, annotate, sage_namespace, create_local_so_file) 372 cython_messages = "Error compiling Cython file" 373 except CompileError: --> 374 raise RuntimeError(cython_messages.strip()) 376 if verbose >= 0: 377 sys.stderr.write(cython_messages) RuntimeError: Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:15: undeclared name not builtin: sin Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:24: undeclared name not builtin: cos Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:31: undeclared name not builtin: e Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:40: undeclared name not builtin: log Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:45: Invalid operand types for '^' (double; long) Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x^2*sin(x) - cos(x)*e^(-x) + log(x^2 + 1) - log(sqrt(1991)) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_q3q1r4on_pyx_0.pyx:18:59: undeclared name not builtin: sqrt Similarly : sage: cf=cython_lambda('double x', preparse(str(f))) --------------------------------------------------------------------------- CompileError Traceback (most recent call last) File /usr/local/sage-9/src/sage/misc/cython.py:358, in cython(filename, verbose, compile_message, use_cache, create_local_c_file, annotate, sage_namespace, create_local_so_file) 357 with cython_namespace_package_support(): --> 358 ext, = cythonize([ext], 359 aliases=aliases, 360 include_path=includes, 361 compiler_directives=directives, 362 quiet=(verbose <= 0), 363 errors_to_stderr=False, 364 use_listing_file=True) 365 finally: 366 # Read the "listing file" which is the file containing 367 # warning and error messages generated by Cython. File /usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/Cython/Build/Dependencies.py:1127, in cythonize(module_list, exclude, nthreads, aliases, quiet, force, language, exclude_failures, **options) 1126 for args in to_compile: -> 1127 cythonize_one(*args) 1129 if exclude_failures: File /usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/Cython/Build/Dependencies.py:1250, in cythonize_one(pyx_file, c_file, fingerprint, quiet, options, raise_on_failure, embedded_metadata, full_module_name, progress) 1249 if raise_on_failure: -> 1250 raise CompileError(None, pyx_file) 1251 elif os.path.exists(c_file): CompileError: /tmp/tmpx6ekg9s7/spyx/_tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx/_tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx During handling of the above exception, another exception occurred: RuntimeError Traceback (most recent call last) Cell In [42], line 1 ----> 1 cf=cython_lambda('double x', preparse(str(f))) File /usr/local/sage-9/src/sage/misc/lazy_import.pyx:402, in sage.misc.lazy_import.LazyImport.__call__() 400 True 401 """ --> 402 return self.get_object()(*args, **kwds) 403 404 def __repr__(self): File /usr/local/sage-9/src/sage/misc/cython.py:510, in cython_lambda(vars, expr, verbose, **kwds) 507 f.write(s) 509 d = {} --> 510 cython_import_all(tmpfile, d, verbose=verbose, **kwds) 511 return d['f'] File /usr/local/sage-9/src/sage/misc/cython.py:563, in cython_import_all(filename, globals, **kwds) 548 def cython_import_all(filename, globals, **kwds): 549 """ 550 Imports all non-private (i.e., not beginning with an underscore) 551 attributes of the specified Cython module into the given context. (...) 561 code 562 """ --> 563 m = cython_import(filename, **kwds) 564 for k, x in m.__dict__.items(): 565 if k[0] != '_': File /usr/local/sage-9/src/sage/misc/cython.py:534, in cython_import(filename, **kwds) 517 def cython_import(filename, **kwds): 518 """ 519 Compile a file containing Cython code, then import and return the 520 module. Raises an ``ImportError`` if anything goes wrong. (...) 532 - the module that contains the compiled Cython code. 533 """ --> 534 name, build_dir = cython(filename, **kwds) 536 oldpath = sys.path 537 try: File /usr/local/sage-9/src/sage/misc/cython.py:374, in cython(filename, verbose, compile_message, use_cache, create_local_c_file, annotate, sage_namespace, create_local_so_file) 372 cython_messages = "Error compiling Cython file" 373 except CompileError: --> 374 raise RuntimeError(cython_messages.strip()) 376 if verbose >= 0: 377 sys.stderr.write(cython_messages) RuntimeError: Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:14: undeclared name not builtin: Integer Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:25: undeclared name not builtin: sin Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:34: undeclared name not builtin: cos Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:41: undeclared name not builtin: e Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:51: undeclared name not builtin: log Error compiling Cython file: ------------------------------------------------------------ ... raise NameError("global {!r} is not defined".format(name)) sage = _s() def f(double x): return x**Integer(2)*sin(x) - cos(x)*e**(-x) + log(x**Integer(2) + Integer(1)) - log(sqrt(Integer(1991))) ^ ------------------------------------------------------------ _tmp_tmpgo2z5pbk_tmp_9kfdyg6w_pyx_0.pyx:18:89: undeclared name not builtin: sqrt s What works : %%cython from sage.functions.trig import sin, cos from sage.misc.functional import log, sqrt from sage.functions.log import exp cpdef double cf(double x): return x**2*sin(x) - cos(x)*exp(-x) + log(x**2 + 1) - log(sqrt(1991)) Is cython_lambda a work in progress ? Trac doesn’t seem to have any *active* cython_lambda ticket. Advice ? -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/17b12f2e-e3c2-46a6-a73b-7afe452df63an%40googlegroups.com.