{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fast python loops with cython\n", "\n", "Cython is essentially a Python to C translator. Cython allows you to use syntax similar to Python, while achieving speeds near that of C. \n", "\n", "This post describes how to use Cython to speed up a single Python function involving ‘tight loops’. I’ll leave more complicated applications - with many functions and classes - for a later post.\n", "\n", "# Should I use Cython?\n", "\n", "If you’re using Python and need performance there are a variety of options, see [quantecon](http://quant-econ.net/py/need_for_speed.html) for a detailed comparison. And of course you could always choose a different language like Julia, or be brave and learn C itself.\n", "\n", "While the static compilation approach of Cython may not be cutting edge, Cython is mature, well documented and capable of handling large complicated projects. Cython code lies behind many of the big Python scientific libraries including scikit-learn and pandas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The example\n", "\n", "Our example function evaluates a Radial Basis Function (RBF) approximation scheme. We assume each data point is a ‘center’ and use Gaussian type RBFs\n", "\n", "$$Y_{i}=\\sum_{j=0}^{N} \\beta_{j}~ e^{- ~\\theta ~||~X_{i}-X_{j} ~||^{2} }$$\n", "\n", "so our function takes an input data array X of shape (N, D), a parameter array $\\beta $ of length N and a ‘bandwidth’ parameter $\\theta $ and return an array of values Y of length N." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext cython" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.5.2 final 0\n", "Cython 0.23.4\n" ] } ], "source": [ "import sys\n", "import Cython\n", "print(\"Python %d.%d.%d %s %s\" % sys.version_info)\n", "print(\"Cython %s\" % Cython.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python loops\n", "\n", "Here’s the naive Python implementation" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import exp\n", "import numpy as np\n", "\n", "def rbf_network(X, beta, theta):\n", "\n", " N = X.shape[0]\n", " D = X.shape[1]\n", " Y = np.zeros(N)\n", "\n", " for i in range(N):\n", " for j in range(N):\n", " r = 0\n", " for d in range(D):\n", " r += (X[j, d] - X[i, d]) ** 2\n", " r = r**0.5\n", " Y[i] += beta[j] * exp(-(r * theta)**2)\n", "\n", " return Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s make up some data\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "D = 5\n", "N = 1000\n", "X = np.array([np.random.rand(N) for d in range(D)]).T\n", "beta = np.random.rand(N)\n", "theta = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Timing this in IPython we get\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loop, best of 3: 4.56 s per loop\n" ] } ], "source": [ "%timeit rbf_network(X, beta, theta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dam those Python loops are slow!\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# scipy.interpolate.Rbf\n", "\n", "So in this case we’re lucky and there’s an external numpy based implementation" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from scipy.interpolate import Rbf\n", "rbf = Rbf(X[:,0], X[:,1], X[:,2], X[:,3], X[:, 4], np.random.rand(N))\n", "Xtuple = tuple([X[:, i] for i in range(D)])" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loop, best of 3: 194 ms per loop\n" ] } ], "source": [ "%timeit rbf(Xtuple)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Much better. But what if we want to go faster or we don’t have a library we can use.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Cython (inline)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we just write %%cython at the beginning of the cell to load the cython commands." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With Cython there are a few ‘tricks’ involved in achieving good performance. Here’s the first one, if we add the option -a to the first line: %%cython -a " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we generate an inline output where lines highlighted in yellow are still using Python and are slowing our code down. Our goal is get rid of yellow lines, especially any inside of loops." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", " \n", "Generated by Cython 0.23.4
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n",
"
+01: from math import exp\n", "
__pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_INCREF(__pyx_n_s_exp);\n", " __Pyx_GIVEREF(__pyx_n_s_exp);\n", " PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_exp);\n", " __pyx_t_2 = __Pyx_Import(__pyx_n_s_math, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_exp); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_exp, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "
+02: import numpy as np\n", "
__pyx_t_2 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "
03:
\n",
"+04: def rbf_network_0(X,beta,theta):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_1rbf_network_0(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_1rbf_network_0 = {\"rbf_network_0\", (PyCFunction)__pyx_pw_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_1rbf_network_0, METH_VARARGS|METH_KEYWORDS, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_1rbf_network_0(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n", " PyObject *__pyx_v_X = 0;\n", " PyObject *__pyx_v_beta = 0;\n", " PyObject *__pyx_v_theta = 0;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_0 (wrapper)\", 0);\n", " {\n", " static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_X,&__pyx_n_s_beta,&__pyx_n_s_theta,0};\n", " PyObject* values[3] = {0,0,0};\n", " if (unlikely(__pyx_kwds)) {\n", " Py_ssize_t kw_args;\n", " const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n", " switch (pos_args) {\n", " case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " case 0: break;\n", " default: goto __pyx_L5_argtuple_error;\n", " }\n", " kw_args = PyDict_Size(__pyx_kwds);\n", " switch (pos_args) {\n", " case 0:\n", " if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_X)) != 0)) kw_args--;\n", " else goto __pyx_L5_argtuple_error;\n", " case 1:\n", " if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_0\", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " case 2:\n", " if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_theta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_0\", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " }\n", " if (unlikely(kw_args > 0)) {\n", " if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"rbf_network_0\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n", " goto __pyx_L5_argtuple_error;\n", " } else {\n", " values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " }\n", " __pyx_v_X = values[0];\n", " __pyx_v_beta = values[1];\n", " __pyx_v_theta = values[2];\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L5_argtuple_error:;\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_0\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_40e916e82ae86364c79b3425e383bd7d.rbf_network_0\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_rbf_network_0(__pyx_self, __pyx_v_X, __pyx_v_beta, __pyx_v_theta);\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_rbf_network_0(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_X, PyObject *__pyx_v_beta, PyObject *__pyx_v_theta) {\n", " PyObject *__pyx_v_N = NULL;\n", " PyObject *__pyx_v_D = NULL;\n", " PyObject *__pyx_v_Y = NULL;\n", " PyObject *__pyx_v_i = NULL;\n", " PyObject *__pyx_v_j = NULL;\n", " PyObject *__pyx_v_r = NULL;\n", " PyObject *__pyx_v_d = NULL;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_0\", 0);\n", "/* … */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_XDECREF(__pyx_t_2);\n", " __Pyx_XDECREF(__pyx_t_3);\n", " __Pyx_XDECREF(__pyx_t_4);\n", " __Pyx_XDECREF(__pyx_t_11);\n", " __Pyx_XDECREF(__pyx_t_12);\n", " __Pyx_XDECREF(__pyx_t_13);\n", " __Pyx_XDECREF(__pyx_t_14);\n", " __Pyx_XDECREF(__pyx_t_15);\n", " __Pyx_XDECREF(__pyx_t_16);\n", " __Pyx_AddTraceback(\"_cython_magic_40e916e82ae86364c79b3425e383bd7d.rbf_network_0\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __Pyx_XDECREF(__pyx_v_N);\n", " __Pyx_XDECREF(__pyx_v_D);\n", " __Pyx_XDECREF(__pyx_v_Y);\n", " __Pyx_XDECREF(__pyx_v_i);\n", " __Pyx_XDECREF(__pyx_v_j);\n", " __Pyx_XDECREF(__pyx_v_r);\n", " __Pyx_XDECREF(__pyx_v_d);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* … */\n", " __pyx_tuple_ = PyTuple_Pack(10, __pyx_n_s_X, __pyx_n_s_beta, __pyx_n_s_theta, __pyx_n_s_N, __pyx_n_s_D, __pyx_n_s_Y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_r, __pyx_n_s_d); if (unlikely(!__pyx_tuple_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_tuple_);\n", " __Pyx_GIVEREF(__pyx_tuple_);\n", "/* … */\n", " __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_40e916e82ae86364c79b3425e383bd7d_1rbf_network_0, NULL, __pyx_n_s_cython_magic_40e916e82ae86364c7); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_rbf_network_0, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "
05:
\n",
"+06: N = X.shape[0]\n", "
__pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_X, __pyx_n_s_shape); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_t_2 = __Pyx_GetItemInt(__pyx_t_1, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_v_N = __pyx_t_2;\n", " __pyx_t_2 = 0;\n", "
+07: D = X.shape[1]\n", "
__pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_X, __pyx_n_s_shape); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_2, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_v_D = __pyx_t_1;\n", " __pyx_t_1 = 0;\n", "
+08: Y = np.zeros(N)\n", "
__pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {\n", " __pyx_t_2 = PyMethod_GET_SELF(__pyx_t_3);\n", " if (likely(__pyx_t_2)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_t_2);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_3, function);\n", " }\n", " }\n", " if (!__pyx_t_2) {\n", " __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_v_N); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " } else {\n", " __pyx_t_4 = PyTuple_New(1+1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __Pyx_GIVEREF(__pyx_t_2); PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2); __pyx_t_2 = NULL;\n", " __Pyx_INCREF(__pyx_v_N);\n", " __Pyx_GIVEREF(__pyx_v_N);\n", " PyTuple_SET_ITEM(__pyx_t_4, 0+1, __pyx_v_N);\n", " __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_v_Y = __pyx_t_1;\n", " __pyx_t_1 = 0;\n", "
09:
\n",
"+10: for i in range(N):\n", "
__pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_INCREF(__pyx_v_N);\n", " __Pyx_GIVEREF(__pyx_v_N);\n", " PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_N);\n", " __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " if (likely(PyList_CheckExact(__pyx_t_3)) || PyTuple_CheckExact(__pyx_t_3)) {\n", " __pyx_t_1 = __pyx_t_3; __Pyx_INCREF(__pyx_t_1); __pyx_t_5 = 0;\n", " __pyx_t_6 = NULL;\n", " } else {\n", " __pyx_t_5 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_t_6 = Py_TYPE(__pyx_t_1)->tp_iternext; if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " for (;;) {\n", " if (likely(!__pyx_t_6)) {\n", " if (likely(PyList_CheckExact(__pyx_t_1))) {\n", " if (__pyx_t_5 >= PyList_GET_SIZE(__pyx_t_1)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_3 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_3); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " #endif\n", " } else {\n", " if (__pyx_t_5 >= PyTuple_GET_SIZE(__pyx_t_1)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_5); __Pyx_INCREF(__pyx_t_3); __pyx_t_5++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_5); __pyx_t_5++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " #endif\n", " }\n", " } else {\n", " __pyx_t_3 = __pyx_t_6(__pyx_t_1);\n", " if (unlikely(!__pyx_t_3)) {\n", " PyObject* exc_type = PyErr_Occurred();\n", " if (exc_type) {\n", " if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n", " else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " break;\n", " }\n", " __Pyx_GOTREF(__pyx_t_3);\n", " }\n", " __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_3);\n", " __pyx_t_3 = 0;\n", "/* … */\n", " }\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
+11: for j in range(N):\n", "
__pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_v_N);\n", " __Pyx_GIVEREF(__pyx_v_N);\n", " PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_N);\n", " __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " if (likely(PyList_CheckExact(__pyx_t_4)) || PyTuple_CheckExact(__pyx_t_4)) {\n", " __pyx_t_3 = __pyx_t_4; __Pyx_INCREF(__pyx_t_3); __pyx_t_7 = 0;\n", " __pyx_t_8 = NULL;\n", " } else {\n", " __pyx_t_7 = -1; __pyx_t_3 = PyObject_GetIter(__pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __pyx_t_8 = Py_TYPE(__pyx_t_3)->tp_iternext; if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n", " for (;;) {\n", " if (likely(!__pyx_t_8)) {\n", " if (likely(PyList_CheckExact(__pyx_t_3))) {\n", " if (__pyx_t_7 >= PyList_GET_SIZE(__pyx_t_3)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_4 = PyList_GET_ITEM(__pyx_t_3, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_4 = PySequence_ITEM(__pyx_t_3, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " #endif\n", " } else {\n", " if (__pyx_t_7 >= PyTuple_GET_SIZE(__pyx_t_3)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_3, __pyx_t_7); __Pyx_INCREF(__pyx_t_4); __pyx_t_7++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_4 = PySequence_ITEM(__pyx_t_3, __pyx_t_7); __pyx_t_7++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " #endif\n", " }\n", " } else {\n", " __pyx_t_4 = __pyx_t_8(__pyx_t_3);\n", " if (unlikely(!__pyx_t_4)) {\n", " PyObject* exc_type = PyErr_Occurred();\n", " if (exc_type) {\n", " if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n", " else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " break;\n", " }\n", " __Pyx_GOTREF(__pyx_t_4);\n", " }\n", " __Pyx_XDECREF_SET(__pyx_v_j, __pyx_t_4);\n", " __pyx_t_4 = 0;\n", "/* … */\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", "
+12: r = 0\n", "
__Pyx_INCREF(__pyx_int_0);\n", " __Pyx_XDECREF_SET(__pyx_v_r, __pyx_int_0);\n", "
+13: for d in range(D):\n", "
__pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __Pyx_INCREF(__pyx_v_D);\n", " __Pyx_GIVEREF(__pyx_v_D);\n", " PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_D);\n", " __pyx_t_2 = __Pyx_PyObject_Call(__pyx_builtin_range, __pyx_t_4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n", " if (likely(PyList_CheckExact(__pyx_t_2)) || PyTuple_CheckExact(__pyx_t_2)) {\n", " __pyx_t_4 = __pyx_t_2; __Pyx_INCREF(__pyx_t_4); __pyx_t_9 = 0;\n", " __pyx_t_10 = NULL;\n", " } else {\n", " __pyx_t_9 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __pyx_t_10 = Py_TYPE(__pyx_t_4)->tp_iternext; if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " for (;;) {\n", " if (likely(!__pyx_t_10)) {\n", " if (likely(PyList_CheckExact(__pyx_t_4))) {\n", " if (__pyx_t_9 >= PyList_GET_SIZE(__pyx_t_4)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_2 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_9); __Pyx_INCREF(__pyx_t_2); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_2 = PySequence_ITEM(__pyx_t_4, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " #endif\n", " } else {\n", " if (__pyx_t_9 >= PyTuple_GET_SIZE(__pyx_t_4)) break;\n", " #if CYTHON_COMPILING_IN_CPYTHON\n", " __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_9); __Pyx_INCREF(__pyx_t_2); __pyx_t_9++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " #else\n", " __pyx_t_2 = PySequence_ITEM(__pyx_t_4, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " #endif\n", " }\n", " } else {\n", " __pyx_t_2 = __pyx_t_10(__pyx_t_4);\n", " if (unlikely(!__pyx_t_2)) {\n", " PyObject* exc_type = PyErr_Occurred();\n", " if (exc_type) {\n", " if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n", " else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " break;\n", " }\n", " __Pyx_GOTREF(__pyx_t_2);\n", " }\n", " __Pyx_XDECREF_SET(__pyx_v_d, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", "/* … */\n", " }\n", " __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n", "
+14: r += (X[j, d] - X[i, d]) ** 2\n", "
__pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_INCREF(__pyx_v_j);\n", " __Pyx_GIVEREF(__pyx_v_j);\n", " PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_j);\n", " __Pyx_INCREF(__pyx_v_d);\n", " __Pyx_GIVEREF(__pyx_v_d);\n", " PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_d);\n", " __pyx_t_11 = PyObject_GetItem(__pyx_v_X, __pyx_t_2); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_11);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_INCREF(__pyx_v_i);\n", " __Pyx_GIVEREF(__pyx_v_i);\n", " PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i);\n", " __Pyx_INCREF(__pyx_v_d);\n", " __Pyx_GIVEREF(__pyx_v_d);\n", " PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_v_d);\n", " __pyx_t_12 = PyObject_GetItem(__pyx_v_X, __pyx_t_2); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_12);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = PyNumber_Subtract(__pyx_t_11, __pyx_t_12); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;\n", " __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n", " __pyx_t_12 = PyNumber_Power(__pyx_t_2, __pyx_int_2, Py_None); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_12);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_v_r, __pyx_t_12); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n", " __Pyx_DECREF_SET(__pyx_v_r, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", "
+15: r = r**0.5\n", "
__pyx_t_4 = PyNumber_Power(__pyx_v_r, __pyx_float_0_5, Py_None); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_4);\n", " __Pyx_DECREF_SET(__pyx_v_r, __pyx_t_4);\n", " __pyx_t_4 = 0;\n", "
+16: Y[i] += beta[j] * exp(-(r * theta)**2)\n", "
__Pyx_INCREF(__pyx_v_i);\n", " __pyx_t_4 = __pyx_v_i;\n", " __pyx_t_2 = PyObject_GetItem(__pyx_v_Y, __pyx_t_4); if (unlikely(__pyx_t_2 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_12 = PyObject_GetItem(__pyx_v_beta, __pyx_v_j); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n", " __Pyx_GOTREF(__pyx_t_12);\n", " __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s_exp); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_13);\n", " __pyx_t_14 = PyNumber_Multiply(__pyx_v_r, __pyx_v_theta); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_14);\n", " __pyx_t_15 = PyNumber_Power(__pyx_t_14, __pyx_int_2, Py_None); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_15);\n", " __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;\n", " __pyx_t_14 = PyNumber_Negative(__pyx_t_15); if (unlikely(!__pyx_t_14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_14);\n", " __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;\n", " __pyx_t_15 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_13))) {\n", " __pyx_t_15 = PyMethod_GET_SELF(__pyx_t_13);\n", " if (likely(__pyx_t_15)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_13);\n", " __Pyx_INCREF(__pyx_t_15);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_13, function);\n", " }\n", " }\n", " if (!__pyx_t_15) {\n", " __pyx_t_11 = __Pyx_PyObject_CallOneArg(__pyx_t_13, __pyx_t_14); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_14); __pyx_t_14 = 0;\n", " __Pyx_GOTREF(__pyx_t_11);\n", " } else {\n", " __pyx_t_16 = PyTuple_New(1+1); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_16);\n", " __Pyx_GIVEREF(__pyx_t_15); PyTuple_SET_ITEM(__pyx_t_16, 0, __pyx_t_15); __pyx_t_15 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_14);\n", " PyTuple_SET_ITEM(__pyx_t_16, 0+1, __pyx_t_14);\n", " __pyx_t_14 = 0;\n", " __pyx_t_11 = __Pyx_PyObject_Call(__pyx_t_13, __pyx_t_16, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_11);\n", " __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;\n", " __pyx_t_13 = PyNumber_Multiply(__pyx_t_12, __pyx_t_11); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_13);\n", " __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n", " __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;\n", " __pyx_t_11 = PyNumber_InPlaceAdd(__pyx_t_2, __pyx_t_13); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_11);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;\n", " if (unlikely(PyObject_SetItem(__pyx_v_Y, __pyx_t_4, __pyx_t_11) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;\n", " __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n", "
17:
\n",
"+18: return Y\n", "
__Pyx_XDECREF(__pyx_r);\n", " __Pyx_INCREF(__pyx_v_Y);\n", " __pyx_r = __pyx_v_Y;\n", " goto __pyx_L0;\n", "
Generated by Cython 0.23.4
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n",
"
+01: from math import exp\n", "
__pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_INCREF(__pyx_n_s_exp);\n", " __Pyx_GIVEREF(__pyx_n_s_exp);\n", " PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_exp);\n", " __pyx_t_2 = __Pyx_Import(__pyx_n_s_math, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_exp); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_exp, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "/* … */\n", " __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "
+02: import numpy as np\n", "
__pyx_t_2 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", "
03:
\n",
"+04: def rbf_network_1(double[:, :] X, double[:] beta, double theta):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_1rbf_network_1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_1rbf_network_1 = {\"rbf_network_1\", (PyCFunction)__pyx_pw_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_1rbf_network_1, METH_VARARGS|METH_KEYWORDS, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_1rbf_network_1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n", " __Pyx_memviewslice __pyx_v_X = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " __Pyx_memviewslice __pyx_v_beta = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " double __pyx_v_theta;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_1 (wrapper)\", 0);\n", " {\n", " static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_X,&__pyx_n_s_beta,&__pyx_n_s_theta,0};\n", " PyObject* values[3] = {0,0,0};\n", " if (unlikely(__pyx_kwds)) {\n", " Py_ssize_t kw_args;\n", " const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n", " switch (pos_args) {\n", " case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " case 0: break;\n", " default: goto __pyx_L5_argtuple_error;\n", " }\n", " kw_args = PyDict_Size(__pyx_kwds);\n", " switch (pos_args) {\n", " case 0:\n", " if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_X)) != 0)) kw_args--;\n", " else goto __pyx_L5_argtuple_error;\n", " case 1:\n", " if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_1\", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " case 2:\n", " if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_theta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_1\", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " }\n", " if (unlikely(kw_args > 0)) {\n", " if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"rbf_network_1\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n", " goto __pyx_L5_argtuple_error;\n", " } else {\n", " values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " }\n", " __pyx_v_X = __Pyx_PyObject_to_MemoryviewSlice_dsds_double(values[0]); if (unlikely(!__pyx_v_X.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_beta = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_beta.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_theta = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_theta == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L5_argtuple_error:;\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_1\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e.rbf_network_1\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_rbf_network_1(__pyx_self, __pyx_v_X, __pyx_v_beta, __pyx_v_theta);\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_rbf_network_1(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_X, __Pyx_memviewslice __pyx_v_beta, double __pyx_v_theta) {\n", " int __pyx_v_N;\n", " int __pyx_v_D;\n", " __Pyx_memviewslice __pyx_v_Y = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " int __pyx_v_i;\n", " int __pyx_v_j;\n", " int __pyx_v_d;\n", " double __pyx_v_r;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_1\", 0);\n", "/* … */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_XDECREF(__pyx_t_2);\n", " __Pyx_XDECREF(__pyx_t_3);\n", " __Pyx_XDECREF(__pyx_t_4);\n", " __Pyx_XDECREF(__pyx_t_5);\n", " __PYX_XDEC_MEMVIEW(&__pyx_t_6, 1);\n", " __Pyx_XDECREF(__pyx_t_19);\n", " __Pyx_AddTraceback(\"_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e.rbf_network_1\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_Y, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_X, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_beta, 1);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* … */\n", " __pyx_tuple__14 = PyTuple_Pack(10, __pyx_n_s_X, __pyx_n_s_beta, __pyx_n_s_theta, __pyx_n_s_N, __pyx_n_s_D, __pyx_n_s_Y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_d, __pyx_n_s_r); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_tuple__14);\n", " __Pyx_GIVEREF(__pyx_tuple__14);\n", "/* … */\n", " __pyx_t_2 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_a7c52605a97d01d30b3f60080cbb0e2e_1rbf_network_1, NULL, __pyx_n_s_cython_magic_a7c52605a97d01d30b); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_rbf_network_1, __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_dcorre_cache_ipython_cytho, __pyx_n_s_rbf_network_1, 4, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", "
05:
\n",
"+06: cdef int N = X.shape[0]\n", "
__pyx_v_N = (__pyx_v_X.shape[0]);\n", "
+07: cdef int D = X.shape[1]\n", "
__pyx_v_D = (__pyx_v_X.shape[1]);\n", "
+08: cdef double[:] Y = np.zeros(N)\n", "
__pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_N); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_4 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {\n", " __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_3);\n", " if (likely(__pyx_t_4)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_t_4);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_3, function);\n", " }\n", " }\n", " if (!__pyx_t_4) {\n", " __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_GOTREF(__pyx_t_1);\n", " } else {\n", " __pyx_t_5 = PyTuple_New(1+1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); __pyx_t_4 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_2);\n", " PyTuple_SET_ITEM(__pyx_t_5, 0+1, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", " __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_t_6 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(__pyx_t_1);\n", " if (unlikely(!__pyx_t_6.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_v_Y = __pyx_t_6;\n", " __pyx_t_6.memview = NULL;\n", " __pyx_t_6.data = NULL;\n", "
09: cdef int i, j, d\n", "
+10: cdef double r = 0\n", "
__pyx_v_r = 0.0;\n", "
11:
\n",
"+12: for i in range(N):\n", "
__pyx_t_7 = __pyx_v_N;\n", " for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_7; __pyx_t_8+=1) {\n", " __pyx_v_i = __pyx_t_8;\n", "
+13: for j in range(N):\n", "
__pyx_t_9 = __pyx_v_N;\n", " for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {\n", " __pyx_v_j = __pyx_t_10;\n", "
+14: r = 0\n", "
__pyx_v_r = 0.0;\n", "
+15: for d in range(D):\n", "
__pyx_t_11 = __pyx_v_D;\n", " for (__pyx_t_12 = 0; __pyx_t_12 < __pyx_t_11; __pyx_t_12+=1) {\n", " __pyx_v_d = __pyx_t_12;\n", "
+16: r += (X[j, d] - X[i, d]) ** 2\n", "
__pyx_t_13 = __pyx_v_j;\n", " __pyx_t_14 = __pyx_v_d;\n", " __pyx_t_15 = -1;\n", " if (__pyx_t_13 < 0) {\n", " __pyx_t_13 += __pyx_v_X.shape[0];\n", " if (unlikely(__pyx_t_13 < 0)) __pyx_t_15 = 0;\n", " } else if (unlikely(__pyx_t_13 >= __pyx_v_X.shape[0])) __pyx_t_15 = 0;\n", " if (__pyx_t_14 < 0) {\n", " __pyx_t_14 += __pyx_v_X.shape[1];\n", " if (unlikely(__pyx_t_14 < 0)) __pyx_t_15 = 1;\n", " } else if (unlikely(__pyx_t_14 >= __pyx_v_X.shape[1])) __pyx_t_15 = 1;\n", " if (unlikely(__pyx_t_15 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_15);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_t_16 = __pyx_v_i;\n", " __pyx_t_17 = __pyx_v_d;\n", " __pyx_t_15 = -1;\n", " if (__pyx_t_16 < 0) {\n", " __pyx_t_16 += __pyx_v_X.shape[0];\n", " if (unlikely(__pyx_t_16 < 0)) __pyx_t_15 = 0;\n", " } else if (unlikely(__pyx_t_16 >= __pyx_v_X.shape[0])) __pyx_t_15 = 0;\n", " if (__pyx_t_17 < 0) {\n", " __pyx_t_17 += __pyx_v_X.shape[1];\n", " if (unlikely(__pyx_t_17 < 0)) __pyx_t_15 = 1;\n", " } else if (unlikely(__pyx_t_17 >= __pyx_v_X.shape[1])) __pyx_t_15 = 1;\n", " if (unlikely(__pyx_t_15 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_15);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_v_r = (__pyx_v_r + pow(((*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_13 * __pyx_v_X.strides[0]) ) + __pyx_t_14 * __pyx_v_X.strides[1]) ))) - (*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_16 * __pyx_v_X.strides[0]) ) + __pyx_t_17 * __pyx_v_X.strides[1]) )))), 2.0));\n", " }\n", "
+17: r = r**0.5\n", "
__pyx_v_r = pow(__pyx_v_r, 0.5);\n", "
+18: Y[i] += beta[j] * exp(-(r * theta)**2)\n", "
__pyx_t_18 = __pyx_v_j;\n", " __pyx_t_11 = -1;\n", " if (__pyx_t_18 < 0) {\n", " __pyx_t_18 += __pyx_v_beta.shape[0];\n", " if (unlikely(__pyx_t_18 < 0)) __pyx_t_11 = 0;\n", " } else if (unlikely(__pyx_t_18 >= __pyx_v_beta.shape[0])) __pyx_t_11 = 0;\n", " if (unlikely(__pyx_t_11 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_11);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_t_1 = PyFloat_FromDouble((*((double *) ( /* dim=0 */ (__pyx_v_beta.data + __pyx_t_18 * __pyx_v_beta.strides[0]) )))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s_exp); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __pyx_t_2 = PyFloat_FromDouble((-pow((__pyx_v_r * __pyx_v_theta), 2.0))); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_4 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_5))) {\n", " __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_5);\n", " if (likely(__pyx_t_4)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_5);\n", " __Pyx_INCREF(__pyx_t_4);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_5, function);\n", " }\n", " }\n", " if (!__pyx_t_4) {\n", " __pyx_t_3 = __Pyx_PyObject_CallOneArg(__pyx_t_5, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_GOTREF(__pyx_t_3);\n", " } else {\n", " __pyx_t_19 = PyTuple_New(1+1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_19);\n", " __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_19, 0, __pyx_t_4); __pyx_t_4 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_2);\n", " PyTuple_SET_ITEM(__pyx_t_19, 0+1, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", " __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_t_19, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " __pyx_t_5 = PyNumber_Multiply(__pyx_t_1, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_t_20 = __pyx_PyFloat_AsDouble(__pyx_t_5); if (unlikely((__pyx_t_20 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " __pyx_t_21 = __pyx_v_i;\n", " __pyx_t_11 = -1;\n", " if (__pyx_t_21 < 0) {\n", " __pyx_t_21 += __pyx_v_Y.shape[0];\n", " if (unlikely(__pyx_t_21 < 0)) __pyx_t_11 = 0;\n", " } else if (unlikely(__pyx_t_21 >= __pyx_v_Y.shape[0])) __pyx_t_11 = 0;\n", " if (unlikely(__pyx_t_11 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_11);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " *((double *) ( /* dim=0 */ (__pyx_v_Y.data + __pyx_t_21 * __pyx_v_Y.strides[0]) )) += __pyx_t_20;\n", " }\n", " }\n", "
19:
\n",
"+20: return Y\n", "
__Pyx_XDECREF(__pyx_r);\n", " __pyx_t_5 = __pyx_memoryview_fromslice(__pyx_v_Y, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __pyx_r = __pyx_t_5;\n", " __pyx_t_5 = 0;\n", " goto __pyx_L0;\n", "
Generated by Cython 0.23.4
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n",
"
+01: #from math import exp\n", "
__pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
02: from libc.math cimport exp\n", "
03:
\n",
"+04: import numpy as np\n", "
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
05:
\n",
"+06: def rbf_network_2(double[:, :] X, double[:] beta, double theta):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_1rbf_network_2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_1rbf_network_2 = {\"rbf_network_2\", (PyCFunction)__pyx_pw_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_1rbf_network_2, METH_VARARGS|METH_KEYWORDS, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_1rbf_network_2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n", " __Pyx_memviewslice __pyx_v_X = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " __Pyx_memviewslice __pyx_v_beta = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " double __pyx_v_theta;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_2 (wrapper)\", 0);\n", " {\n", " static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_X,&__pyx_n_s_beta,&__pyx_n_s_theta,0};\n", " PyObject* values[3] = {0,0,0};\n", " if (unlikely(__pyx_kwds)) {\n", " Py_ssize_t kw_args;\n", " const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n", " switch (pos_args) {\n", " case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " case 0: break;\n", " default: goto __pyx_L5_argtuple_error;\n", " }\n", " kw_args = PyDict_Size(__pyx_kwds);\n", " switch (pos_args) {\n", " case 0:\n", " if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_X)) != 0)) kw_args--;\n", " else goto __pyx_L5_argtuple_error;\n", " case 1:\n", " if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_2\", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " case 2:\n", " if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_theta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_2\", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " }\n", " if (unlikely(kw_args > 0)) {\n", " if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"rbf_network_2\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n", " goto __pyx_L5_argtuple_error;\n", " } else {\n", " values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " }\n", " __pyx_v_X = __Pyx_PyObject_to_MemoryviewSlice_dsds_double(values[0]); if (unlikely(!__pyx_v_X.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_beta = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_beta.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_theta = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_theta == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L5_argtuple_error:;\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_2\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_8e84f5b2985cf43b081b58961850c2e5.rbf_network_2\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_rbf_network_2(__pyx_self, __pyx_v_X, __pyx_v_beta, __pyx_v_theta);\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_rbf_network_2(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_X, __Pyx_memviewslice __pyx_v_beta, double __pyx_v_theta) {\n", " int __pyx_v_N;\n", " int __pyx_v_D;\n", " __Pyx_memviewslice __pyx_v_Y = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " int __pyx_v_i;\n", " int __pyx_v_j;\n", " int __pyx_v_d;\n", " double __pyx_v_r;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_2\", 0);\n", "/* … */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_XDECREF(__pyx_t_2);\n", " __Pyx_XDECREF(__pyx_t_3);\n", " __Pyx_XDECREF(__pyx_t_4);\n", " __Pyx_XDECREF(__pyx_t_5);\n", " __PYX_XDEC_MEMVIEW(&__pyx_t_6, 1);\n", " __Pyx_AddTraceback(\"_cython_magic_8e84f5b2985cf43b081b58961850c2e5.rbf_network_2\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_Y, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_X, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_beta, 1);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* … */\n", " __pyx_tuple__14 = PyTuple_Pack(10, __pyx_n_s_X, __pyx_n_s_beta, __pyx_n_s_theta, __pyx_n_s_N, __pyx_n_s_D, __pyx_n_s_Y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_d, __pyx_n_s_r); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_tuple__14);\n", " __Pyx_GIVEREF(__pyx_tuple__14);\n", "/* … */\n", " __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_8e84f5b2985cf43b081b58961850c2e5_1rbf_network_2, NULL, __pyx_n_s_cython_magic_8e84f5b2985cf43b08); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_rbf_network_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_dcorre_cache_ipython_cytho, __pyx_n_s_rbf_network_2, 6, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", "
07:
\n",
"+08: cdef int N = X.shape[0]\n", "
__pyx_v_N = (__pyx_v_X.shape[0]);\n", "
+09: cdef int D = X.shape[1]\n", "
__pyx_v_D = (__pyx_v_X.shape[1]);\n", "
+10: cdef double[:] Y = np.zeros(N)\n", "
__pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_N); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_4 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {\n", " __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_3);\n", " if (likely(__pyx_t_4)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_t_4);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_3, function);\n", " }\n", " }\n", " if (!__pyx_t_4) {\n", " __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_GOTREF(__pyx_t_1);\n", " } else {\n", " __pyx_t_5 = PyTuple_New(1+1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); __pyx_t_4 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_2);\n", " PyTuple_SET_ITEM(__pyx_t_5, 0+1, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", " __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_t_6 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(__pyx_t_1);\n", " if (unlikely(!__pyx_t_6.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_v_Y = __pyx_t_6;\n", " __pyx_t_6.memview = NULL;\n", " __pyx_t_6.data = NULL;\n", "
11: cdef int i, j, d\n", "
+12: cdef double r = 0\n", "
__pyx_v_r = 0.0;\n", "
13:
\n",
"+14: for i in range(N):\n", "
__pyx_t_7 = __pyx_v_N;\n", " for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_7; __pyx_t_8+=1) {\n", " __pyx_v_i = __pyx_t_8;\n", "
+15: for j in range(N):\n", "
__pyx_t_9 = __pyx_v_N;\n", " for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {\n", " __pyx_v_j = __pyx_t_10;\n", "
+16: r = 0\n", "
__pyx_v_r = 0.0;\n", "
+17: for d in range(D):\n", "
__pyx_t_11 = __pyx_v_D;\n", " for (__pyx_t_12 = 0; __pyx_t_12 < __pyx_t_11; __pyx_t_12+=1) {\n", " __pyx_v_d = __pyx_t_12;\n", "
+18: r += (X[j, d] - X[i, d]) ** 2\n", "
__pyx_t_13 = __pyx_v_j;\n", " __pyx_t_14 = __pyx_v_d;\n", " __pyx_t_15 = -1;\n", " if (__pyx_t_13 < 0) {\n", " __pyx_t_13 += __pyx_v_X.shape[0];\n", " if (unlikely(__pyx_t_13 < 0)) __pyx_t_15 = 0;\n", " } else if (unlikely(__pyx_t_13 >= __pyx_v_X.shape[0])) __pyx_t_15 = 0;\n", " if (__pyx_t_14 < 0) {\n", " __pyx_t_14 += __pyx_v_X.shape[1];\n", " if (unlikely(__pyx_t_14 < 0)) __pyx_t_15 = 1;\n", " } else if (unlikely(__pyx_t_14 >= __pyx_v_X.shape[1])) __pyx_t_15 = 1;\n", " if (unlikely(__pyx_t_15 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_15);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_t_16 = __pyx_v_i;\n", " __pyx_t_17 = __pyx_v_d;\n", " __pyx_t_15 = -1;\n", " if (__pyx_t_16 < 0) {\n", " __pyx_t_16 += __pyx_v_X.shape[0];\n", " if (unlikely(__pyx_t_16 < 0)) __pyx_t_15 = 0;\n", " } else if (unlikely(__pyx_t_16 >= __pyx_v_X.shape[0])) __pyx_t_15 = 0;\n", " if (__pyx_t_17 < 0) {\n", " __pyx_t_17 += __pyx_v_X.shape[1];\n", " if (unlikely(__pyx_t_17 < 0)) __pyx_t_15 = 1;\n", " } else if (unlikely(__pyx_t_17 >= __pyx_v_X.shape[1])) __pyx_t_15 = 1;\n", " if (unlikely(__pyx_t_15 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_15);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_v_r = (__pyx_v_r + pow(((*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_13 * __pyx_v_X.strides[0]) ) + __pyx_t_14 * __pyx_v_X.strides[1]) ))) - (*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_16 * __pyx_v_X.strides[0]) ) + __pyx_t_17 * __pyx_v_X.strides[1]) )))), 2.0));\n", " }\n", "
+19: r = r**0.5\n", "
__pyx_v_r = pow(__pyx_v_r, 0.5);\n", "
+20: Y[i] += beta[j] * exp(-(r * theta)**2)\n", "
__pyx_t_18 = __pyx_v_j;\n", " __pyx_t_11 = -1;\n", " if (__pyx_t_18 < 0) {\n", " __pyx_t_18 += __pyx_v_beta.shape[0];\n", " if (unlikely(__pyx_t_18 < 0)) __pyx_t_11 = 0;\n", " } else if (unlikely(__pyx_t_18 >= __pyx_v_beta.shape[0])) __pyx_t_11 = 0;\n", " if (unlikely(__pyx_t_11 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_11);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " __pyx_t_19 = __pyx_v_i;\n", " __pyx_t_11 = -1;\n", " if (__pyx_t_19 < 0) {\n", " __pyx_t_19 += __pyx_v_Y.shape[0];\n", " if (unlikely(__pyx_t_19 < 0)) __pyx_t_11 = 0;\n", " } else if (unlikely(__pyx_t_19 >= __pyx_v_Y.shape[0])) __pyx_t_11 = 0;\n", " if (unlikely(__pyx_t_11 != -1)) {\n", " __Pyx_RaiseBufferIndexError(__pyx_t_11);\n", " {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " }\n", " *((double *) ( /* dim=0 */ (__pyx_v_Y.data + __pyx_t_19 * __pyx_v_Y.strides[0]) )) += ((*((double *) ( /* dim=0 */ (__pyx_v_beta.data + __pyx_t_18 * __pyx_v_beta.strides[0]) ))) * exp((-pow((__pyx_v_r * __pyx_v_theta), 2.0))));\n", " }\n", " }\n", "
21:
\n",
"+22: return Y\n", "
__Pyx_XDECREF(__pyx_r);\n", " __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_Y, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_r = __pyx_t_1;\n", " __pyx_t_1 = 0;\n", " goto __pyx_L0;\n", "
Generated by Cython 0.23.4
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n",
"
01:
\n",
"+02: #from math import exp\n", "
__pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
03: from libc.math cimport exp\n", "
+04: import numpy as np\n", "
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
05: import cython\n", "
06:
\n",
"07: @cython.boundscheck(False)\n", "
08: @cython.wraparound(False)\n", "
09: @cython.nonecheck(False)\n", "
+10: def rbf_network_3(double[:, :] X, double[:] beta, double theta):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_44cf836a4f843bea06613906e3e97f99_1rbf_network_3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_44cf836a4f843bea06613906e3e97f99_1rbf_network_3 = {\"rbf_network_3\", (PyCFunction)__pyx_pw_46_cython_magic_44cf836a4f843bea06613906e3e97f99_1rbf_network_3, METH_VARARGS|METH_KEYWORDS, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_44cf836a4f843bea06613906e3e97f99_1rbf_network_3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n", " __Pyx_memviewslice __pyx_v_X = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " __Pyx_memviewslice __pyx_v_beta = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " double __pyx_v_theta;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_3 (wrapper)\", 0);\n", " {\n", " static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_X,&__pyx_n_s_beta,&__pyx_n_s_theta,0};\n", " PyObject* values[3] = {0,0,0};\n", " if (unlikely(__pyx_kwds)) {\n", " Py_ssize_t kw_args;\n", " const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n", " switch (pos_args) {\n", " case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " case 0: break;\n", " default: goto __pyx_L5_argtuple_error;\n", " }\n", " kw_args = PyDict_Size(__pyx_kwds);\n", " switch (pos_args) {\n", " case 0:\n", " if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_X)) != 0)) kw_args--;\n", " else goto __pyx_L5_argtuple_error;\n", " case 1:\n", " if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_3\", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " case 2:\n", " if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_theta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_3\", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " }\n", " if (unlikely(kw_args > 0)) {\n", " if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"rbf_network_3\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n", " goto __pyx_L5_argtuple_error;\n", " } else {\n", " values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " }\n", " __pyx_v_X = __Pyx_PyObject_to_MemoryviewSlice_dsds_double(values[0]); if (unlikely(!__pyx_v_X.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_beta = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_beta.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_theta = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_theta == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L5_argtuple_error:;\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_3\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_44cf836a4f843bea06613906e3e97f99.rbf_network_3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_44cf836a4f843bea06613906e3e97f99_rbf_network_3(__pyx_self, __pyx_v_X, __pyx_v_beta, __pyx_v_theta);\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_44cf836a4f843bea06613906e3e97f99_rbf_network_3(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_X, __Pyx_memviewslice __pyx_v_beta, double __pyx_v_theta) {\n", " int __pyx_v_N;\n", " int __pyx_v_D;\n", " __Pyx_memviewslice __pyx_v_Y = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " int __pyx_v_i;\n", " int __pyx_v_j;\n", " int __pyx_v_d;\n", " double __pyx_v_r;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_3\", 0);\n", "/* … */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_XDECREF(__pyx_t_2);\n", " __Pyx_XDECREF(__pyx_t_3);\n", " __Pyx_XDECREF(__pyx_t_4);\n", " __Pyx_XDECREF(__pyx_t_5);\n", " __PYX_XDEC_MEMVIEW(&__pyx_t_6, 1);\n", " __Pyx_AddTraceback(\"_cython_magic_44cf836a4f843bea06613906e3e97f99.rbf_network_3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_Y, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_X, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_beta, 1);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* … */\n", " __pyx_tuple__14 = PyTuple_Pack(10, __pyx_n_s_X, __pyx_n_s_beta, __pyx_n_s_theta, __pyx_n_s_N, __pyx_n_s_D, __pyx_n_s_Y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_d, __pyx_n_s_r); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_tuple__14);\n", " __Pyx_GIVEREF(__pyx_tuple__14);\n", "/* … */\n", " __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_44cf836a4f843bea06613906e3e97f99_1rbf_network_3, NULL, __pyx_n_s_cython_magic_44cf836a4f843bea06); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_rbf_network_3, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_dcorre_cache_ipython_cytho, __pyx_n_s_rbf_network_3, 10, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", "
11:
\n",
"+12: cdef int N = X.shape[0]\n", "
__pyx_v_N = (__pyx_v_X.shape[0]);\n", "
+13: cdef int D = X.shape[1]\n", "
__pyx_v_D = (__pyx_v_X.shape[1]);\n", "
+14: cdef double[:] Y = np.zeros(N)\n", "
__pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_N); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_4 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {\n", " __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_3);\n", " if (likely(__pyx_t_4)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_t_4);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_3, function);\n", " }\n", " }\n", " if (!__pyx_t_4) {\n", " __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_GOTREF(__pyx_t_1);\n", " } else {\n", " __pyx_t_5 = PyTuple_New(1+1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); __pyx_t_4 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_2);\n", " PyTuple_SET_ITEM(__pyx_t_5, 0+1, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", " __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_t_6 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(__pyx_t_1);\n", " if (unlikely(!__pyx_t_6.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_v_Y = __pyx_t_6;\n", " __pyx_t_6.memview = NULL;\n", " __pyx_t_6.data = NULL;\n", "
15: cdef int i, j, d\n", "
+16: cdef double r = 0\n", "
__pyx_v_r = 0.0;\n", "
17:
\n",
"+18: for i in range(N):\n", "
__pyx_t_7 = __pyx_v_N;\n", " for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_7; __pyx_t_8+=1) {\n", " __pyx_v_i = __pyx_t_8;\n", "
+19: for j in range(N):\n", "
__pyx_t_9 = __pyx_v_N;\n", " for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {\n", " __pyx_v_j = __pyx_t_10;\n", "
+20: r = 0\n", "
__pyx_v_r = 0.0;\n", "
+21: for d in range(D):\n", "
__pyx_t_11 = __pyx_v_D;\n", " for (__pyx_t_12 = 0; __pyx_t_12 < __pyx_t_11; __pyx_t_12+=1) {\n", " __pyx_v_d = __pyx_t_12;\n", "
+22: r += (X[j, d] - X[i, d]) ** 2\n", "
__pyx_t_13 = __pyx_v_j;\n", " __pyx_t_14 = __pyx_v_d;\n", " __pyx_t_15 = __pyx_v_i;\n", " __pyx_t_16 = __pyx_v_d;\n", " __pyx_v_r = (__pyx_v_r + pow(((*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_13 * __pyx_v_X.strides[0]) ) + __pyx_t_14 * __pyx_v_X.strides[1]) ))) - (*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_15 * __pyx_v_X.strides[0]) ) + __pyx_t_16 * __pyx_v_X.strides[1]) )))), 2.0));\n", " }\n", "
+23: r = r**0.5\n", "
__pyx_v_r = pow(__pyx_v_r, 0.5);\n", "
+24: Y[i] += beta[j] * exp(-(r * theta)**2)\n", "
__pyx_t_17 = __pyx_v_j;\n", " __pyx_t_18 = __pyx_v_i;\n", " *((double *) ( /* dim=0 */ (__pyx_v_Y.data + __pyx_t_18 * __pyx_v_Y.strides[0]) )) += ((*((double *) ( /* dim=0 */ (__pyx_v_beta.data + __pyx_t_17 * __pyx_v_beta.strides[0]) ))) * exp((-pow((__pyx_v_r * __pyx_v_theta), 2.0))));\n", " }\n", " }\n", "
25:
\n",
"+26: return Y\n", "
__Pyx_XDECREF(__pyx_r);\n", " __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_Y, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_r = __pyx_t_1;\n", " __pyx_t_1 = 0;\n", " goto __pyx_L0;\n", "
Generated by Cython 0.23.4
\n", "\n",
" Yellow lines hint at Python interaction.
\n",
" Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n",
"
01:
\n",
"+02: import cython\n", "
__pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
03: from cython.parallel import prange, parallel\n", "
+04: import numpy as np\n", "
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
05: #from math import exp\n", "
06: from libc.math cimport exp\n", "
07:
\n",
"08: @cython.boundscheck(False)\n", "
09: @cython.wraparound(False)\n", "
10: @cython.nonecheck(False)\n", "
+11: def rbf_network_multithread(double[:, :] X, double[:] beta, double theta):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_f943c84460c21e5d152873f930413d2a_1rbf_network_multithread(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_f943c84460c21e5d152873f930413d2a_1rbf_network_multithread = {\"rbf_network_multithread\", (PyCFunction)__pyx_pw_46_cython_magic_f943c84460c21e5d152873f930413d2a_1rbf_network_multithread, METH_VARARGS|METH_KEYWORDS, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_f943c84460c21e5d152873f930413d2a_1rbf_network_multithread(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n", " __Pyx_memviewslice __pyx_v_X = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " __Pyx_memviewslice __pyx_v_beta = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " double __pyx_v_theta;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_multithread (wrapper)\", 0);\n", " {\n", " static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_X,&__pyx_n_s_beta,&__pyx_n_s_theta,0};\n", " PyObject* values[3] = {0,0,0};\n", " if (unlikely(__pyx_kwds)) {\n", " Py_ssize_t kw_args;\n", " const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n", " switch (pos_args) {\n", " case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " case 0: break;\n", " default: goto __pyx_L5_argtuple_error;\n", " }\n", " kw_args = PyDict_Size(__pyx_kwds);\n", " switch (pos_args) {\n", " case 0:\n", " if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_X)) != 0)) kw_args--;\n", " else goto __pyx_L5_argtuple_error;\n", " case 1:\n", " if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_beta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_multithread\", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " case 2:\n", " if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_theta)) != 0)) kw_args--;\n", " else {\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_multithread\", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " }\n", " if (unlikely(kw_args > 0)) {\n", " if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"rbf_network_multithread\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n", " goto __pyx_L5_argtuple_error;\n", " } else {\n", " values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n", " values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n", " values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n", " }\n", " __pyx_v_X = __Pyx_PyObject_to_MemoryviewSlice_dsds_double(values[0]); if (unlikely(!__pyx_v_X.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_beta = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_beta.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_v_theta = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_theta == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L5_argtuple_error:;\n", " __Pyx_RaiseArgtupleInvalid(\"rbf_network_multithread\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_f943c84460c21e5d152873f930413d2a.rbf_network_multithread\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_f943c84460c21e5d152873f930413d2a_rbf_network_multithread(__pyx_self, __pyx_v_X, __pyx_v_beta, __pyx_v_theta);\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_f943c84460c21e5d152873f930413d2a_rbf_network_multithread(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_X, __Pyx_memviewslice __pyx_v_beta, double __pyx_v_theta) {\n", " int __pyx_v_N;\n", " int __pyx_v_D;\n", " __Pyx_memviewslice __pyx_v_Y = { 0, 0, { 0 }, { 0 }, { 0 } };\n", " int __pyx_v_i;\n", " int __pyx_v_j;\n", " int __pyx_v_d;\n", " double __pyx_v_r;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"rbf_network_multithread\", 0);\n", "/* … */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_XDECREF(__pyx_t_2);\n", " __Pyx_XDECREF(__pyx_t_3);\n", " __Pyx_XDECREF(__pyx_t_4);\n", " __Pyx_XDECREF(__pyx_t_5);\n", " __PYX_XDEC_MEMVIEW(&__pyx_t_6, 1);\n", " __Pyx_AddTraceback(\"_cython_magic_f943c84460c21e5d152873f930413d2a.rbf_network_multithread\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_Y, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_X, 1);\n", " __PYX_XDEC_MEMVIEW(&__pyx_v_beta, 1);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* … */\n", " __pyx_tuple__14 = PyTuple_Pack(10, __pyx_n_s_X, __pyx_n_s_beta, __pyx_n_s_theta, __pyx_n_s_N, __pyx_n_s_D, __pyx_n_s_Y, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_d, __pyx_n_s_r); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_tuple__14);\n", " __Pyx_GIVEREF(__pyx_tuple__14);\n", "/* … */\n", " __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_f943c84460c21e5d152873f930413d2a_1rbf_network_multithread, NULL, __pyx_n_s_cython_magic_f943c84460c21e5d15); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_rbf_network_multithread, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_dcorre_cache_ipython_cytho, __pyx_n_s_rbf_network_multithread, 11, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", "
12:
\n",
"+13: cdef int N = X.shape[0]\n", "
__pyx_v_N = (__pyx_v_X.shape[0]);\n", "
+14: cdef int D = X.shape[1]\n", "
__pyx_v_D = (__pyx_v_X.shape[1]);\n", "
+15: cdef double[:] Y = np.zeros(N)\n", "
__pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_3);\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __pyx_t_2 = __Pyx_PyInt_From_int(__pyx_v_N); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_2);\n", " __pyx_t_4 = NULL;\n", " if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {\n", " __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_3);\n", " if (likely(__pyx_t_4)) {\n", " PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n", " __Pyx_INCREF(__pyx_t_4);\n", " __Pyx_INCREF(function);\n", " __Pyx_DECREF_SET(__pyx_t_3, function);\n", " }\n", " }\n", " if (!__pyx_t_4) {\n", " __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n", " __Pyx_GOTREF(__pyx_t_1);\n", " } else {\n", " __pyx_t_5 = PyTuple_New(1+1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_5);\n", " __Pyx_GIVEREF(__pyx_t_4); PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4); __pyx_t_4 = NULL;\n", " __Pyx_GIVEREF(__pyx_t_2);\n", " PyTuple_SET_ITEM(__pyx_t_5, 0+1, __pyx_t_2);\n", " __pyx_t_2 = 0;\n", " __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n", " }\n", " __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n", " __pyx_t_6 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(__pyx_t_1);\n", " if (unlikely(!__pyx_t_6.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", " __pyx_v_Y = __pyx_t_6;\n", " __pyx_t_6.memview = NULL;\n", " __pyx_t_6.data = NULL;\n", "
16: cdef int i, j, d\n", "
+17: cdef double r = 0\n", "
__pyx_v_r = 0.0;\n", "
18:
\n",
"+19: for i in prange(N, nogil=True,num_threads=4):\n", "
{\n", " #ifdef WITH_THREAD\n", " PyThreadState *_save;\n", " Py_UNBLOCK_THREADS\n", " #endif\n", " /*try:*/ {\n", " __pyx_t_7 = __pyx_v_N;\n", " if (1 == 0) abort();\n", " {\n", " #if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))\n", " #undef likely\n", " #undef unlikely\n", " #define likely(x) (x)\n", " #define unlikely(x) (x)\n", " #endif\n", " __pyx_t_9 = (__pyx_t_7 - 0) / 1;\n", " if (__pyx_t_9 > 0)\n", " {\n", " #ifdef _OPENMP\n", " #pragma omp parallel\n", " #endif /* _OPENMP */\n", " {\n", " #ifdef _OPENMP\n", " #pragma omp for lastprivate(__pyx_v_j) firstprivate(__pyx_v_i) lastprivate(__pyx_v_i) lastprivate(__pyx_v_d) lastprivate(__pyx_v_r) num_threads(4)\n", " #endif /* _OPENMP */\n", " for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_9; __pyx_t_8++){\n", " {\n", " __pyx_v_i = 0 + 1 * __pyx_t_8;\n", " /* Initialize private variables to invalid values */\n", " __pyx_v_j = ((int)0xbad0bad0);\n", " __pyx_v_d = ((int)0xbad0bad0);\n", " __pyx_v_r = ((double)__PYX_NAN());\n", "/* … */\n", " /*finally:*/ {\n", " /*normal exit:*/{\n", " #ifdef WITH_THREAD\n", " Py_BLOCK_THREADS\n", " #endif\n", " goto __pyx_L5;\n", " }\n", " __pyx_L5:;\n", " }\n", " }\n", "
+20: for j in range(N):\n", "
__pyx_t_10 = __pyx_v_N;\n", " for (__pyx_t_11 = 0; __pyx_t_11 < __pyx_t_10; __pyx_t_11+=1) {\n", " __pyx_v_j = __pyx_t_11;\n", "
+21: r = 0\n", "
__pyx_v_r = 0.0;\n", "
+22: for d in range(D):\n", "
__pyx_t_12 = __pyx_v_D;\n", " for (__pyx_t_13 = 0; __pyx_t_13 < __pyx_t_12; __pyx_t_13+=1) {\n", " __pyx_v_d = __pyx_t_13;\n", "
+23: r += (X[j, d] - X[i, d])**2\n", "
__pyx_t_14 = __pyx_v_j;\n", " __pyx_t_15 = __pyx_v_d;\n", " __pyx_t_16 = __pyx_v_i;\n", " __pyx_t_17 = __pyx_v_d;\n", " __pyx_v_r = (__pyx_v_r + pow(((*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_14 * __pyx_v_X.strides[0]) ) + __pyx_t_15 * __pyx_v_X.strides[1]) ))) - (*((double *) ( /* dim=1 */ (( /* dim=0 */ (__pyx_v_X.data + __pyx_t_16 * __pyx_v_X.strides[0]) ) + __pyx_t_17 * __pyx_v_X.strides[1]) )))), 2.0));\n", " }\n", "
+24: r = r**0.5\n", "
__pyx_v_r = pow(__pyx_v_r, 0.5);\n", "
+25: Y[i] += beta[j] * exp(-(r * theta)**2)\n", "
__pyx_t_18 = __pyx_v_j;\n", " __pyx_t_19 = __pyx_v_i;\n", " *((double *) ( /* dim=0 */ (__pyx_v_Y.data + __pyx_t_19 * __pyx_v_Y.strides[0]) )) += ((*((double *) ( /* dim=0 */ (__pyx_v_beta.data + __pyx_t_18 * __pyx_v_beta.strides[0]) ))) * exp((-pow((__pyx_v_r * __pyx_v_theta), 2.0))));\n", " }\n", " }\n", " }\n", " }\n", " }\n", " }\n", " #if ((defined(__APPLE__) || defined(__OSX__)) && (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)))))\n", " #undef likely\n", " #undef unlikely\n", " #define likely(x) __builtin_expect(!!(x), 1)\n", " #define unlikely(x) __builtin_expect(!!(x), 0)\n", " #endif\n", " }\n", "
26:
\n",
"+27: return Y\n", "
__Pyx_XDECREF(__pyx_r);\n", " __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_Y, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_r = __pyx_t_1;\n", " __pyx_t_1 = 0;\n", " goto __pyx_L0;\n", "