とらりもんHOME  Index  Search  Changes  Login

とらりもん - cython Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

2017/01/03

[[Cythonとは|http://omake.accense.com/static/doc-ja/cython/src/quickstart/overview.html]]。

まずは[[チュートリアル基礎編|http://omake.accense.com/static/doc-ja/cython/src/userguide/tutorial.html]]を読む。

次に[[言語仕様|http://omake.accense.com/static/doc-ja/cython/src/userguide/language_basics.html#language-basics]]を眺めて[[Numpyのページ|http://omake.accense.com/static/doc-ja/cython/src/tutorial/numpy.html]] を眺める。

より詳しく知りたい人は[[ドキュメント|http://omake.accense.com/static/doc-ja/cython/index.html]]を読む。

! 基本
!! 端末上で使う場合
* hoge.pyxでPythonプログラムを書く。
* setup.pyを作成
<<<
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("hoge", ["hoge.pyx"])]
)
>>>
* Cythonをビルド。
$ python setup.py build_ext --inplace

!! Jupyter notebookで使う場合
一行目に
%load_ext Cython

使用したいコードの一行目に
%%cython


*参考
**[[Jupyter NotebookでCythonを使う|http://qiita.com/kenmatsu4/items/7c08a85e41741e95b9ba]]

! 言語仕様(メモ)
[[公式の言語仕様|http://omake.accense.com/static/doc-ja/cython/src/userguide/language_basics.html#language-basics]]
!! 複数定義
cdef: でインデントする。
cdef:
    int i
    float f

!! 関数定義
def (Python)と cdef (Cの関数)がある。

Cythonモジュールの外側にエクスポートする場合はcdefは使えない。