如何取消包含LaTeX符号的函数?

时间:2016-11-19 21:28:24

标签: python sympy numpy-ufunc

此代码

from sympy import symbols
from sympy.utilities.autowrap import ufuncify
a = symbols(r'\alpha')
phi = a**2
phi_f = ufuncify(a, phi)

抛出下面的错误,这些错误显然来自Python / C代码中的\alpha。遗憾的是,将\alpha替换为a作为Sympy符号是非常重要的,因为在我的实际代码库中,此代码是较大程序的一部分,该程序也会显示/写出很多LaTeX输出。

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
/home/ma/apps/anaconda3/lib/python3.5/site-packages/sympy/utilities/autowrap.py in _process_files(self, routine)
    162         try:
--> 163             retoutput = check_output(command, stderr=STDOUT)
    164         except CalledProcessError as e:

/home/ma/apps/anaconda3/lib/python3.5/subprocess.py in check_output(timeout, *popenargs, **kwargs)
    625     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 626                **kwargs).stdout
    627 

/home/ma/apps/anaconda3/lib/python3.5/subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
    707             raise CalledProcessError(retcode, process.args,
--> 708                                      output=stdout, stderr=stderr)
    709     return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['/home/ma/apps/anaconda3/bin/python', 'setup.py', 'build_ext', '--inplace']' returned non-zero exit status 1

During handling of the above exception, another exception occurred:

CodeWrapError                             Traceback (most recent call last)
<ipython-input-66-17455e4d65ae> in <module>()
      4 a = symbols(r'\alpha')
      5 phi = a**2
----> 6 phi_f = ufuncify(a, phi)

/home/ma/apps/anaconda3/lib/python3.5/site-packages/sympy/utilities/autowrap.py in ufuncify(args, expr, language, backend, tempdir, flags, verbose, helpers)
    886         code_wrapper = UfuncifyCodeWrapper(CCodeGen("ufuncify"), tempdir,
    887                                            flags, verbose)
--> 888         return code_wrapper.wrap_code(routine, helpers=helps)
    889     else:
    890         # Dummies are used for all added expressions to prevent name clashes

/home/ma/apps/anaconda3/lib/python3.5/site-packages/sympy/utilities/autowrap.py in wrap_code(self, routine, helpers)
    142             self._generate_code(routine, helpers)
    143             self._prepare_files(routine)
--> 144             self._process_files(routine)
    145             mod = __import__(self.module_name)
    146         finally:

/home/ma/apps/anaconda3/lib/python3.5/site-packages/sympy/utilities/autowrap.py in _process_files(self, routine)
    165             raise CodeWrapError(
    166                 "Error while executing command: %s. Command output is:\n%s" % (
--> 167                     " ".join(command), e.output.decode()))
    168         if not self.quiet:
    169             print(retoutput)

CodeWrapError: Error while executing command: /home/ma/apps/anaconda3/bin/python setup.py build_ext --inplace. Command output is:
running build_ext
running build_src
build_src
building extension "wrapper_module_3" sources
build_src: building npy-pkg config files
customize UnixCCompiler
customize UnixCCompiler using build_ext
building 'wrapper_module_3' extension
compiling C sources
C compiler: gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC

creating build
creating build/temp.linux-x86_64-3.5
compile options: '-I/home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include -I/home/ma/apps/anaconda3/include/python3.5m -c'
gcc: wrapper_module_3.c
In file included from /home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1777:0,
                 from wrapper_module_3.c:3:
/home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
In file included from wrapper_module_3.c:6:0:
wrapped_code_3.h:3:1: error: stray ‘\’ in program
 double autofunc(double \alpha);
 ^
In file included from /home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1777:0,
                 from wrapper_module_3.c:3:
/home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
In file included from wrapper_module_3.c:6:0:
wrapped_code_3.h:3:1: error: stray â\â in program
 double autofunc(double \alpha);
 ^
error: Command "gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/ma/apps/anaconda3/lib/python3.5/site-packages/numpy/core/include -I/home/ma/apps/anaconda3/include/python3.5m -c wrapper_module_3.c -o build/temp.linux-x86_64-3.5/wrapper_module_3.o" failed with exit status 1

1 个答案:

答案 0 :(得分:2)

您可能需要跳过\,因为sympy.latex会自动添加from sympy import symbols from sympy.utilities.autowrap import ufuncify import sympy a = symbols('alpha') phi = a**2 phi_f = ufuncify(a, phi) print(sympy.latex(phi)) # \alpha^{2}

{{1}}