{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "person 1: my code is too slow\n", "\n", "person 2: make better algorithm\n", "\n", "person 1: I did and it is still slow\n", "\n", "person 2: let us try Cython\n", "\n", "References: Cython website (+ cysignals)\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Cython extension is already loaded. To reload it, use:\n", " %reload_ext Cython\n" ] } ], "source": [ "%load_ext Cython\n", "# to activate the option -a of Cython\n", "# be careful: it might mess everything up!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/usr/lib/python2.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/vincent/.cache/ipython/cython/_cython_magic_8fa21c1b43de7954c881a3cf375d946e.pyx\n", " tree = Parsing.p_module(s, pxd, full_module_name)\n" ] } ], "source": [ "%%cython\n", "print(\"hello\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.84 s, sys: 91 ms, total: 1.93 s\n", "Wall time: 1.78 s\n" ] } ], "source": [ "%%time\n", "s = 0\n", "for i in range(100):\n", " for j in range(100):\n", " for k in range(100):\n", " s += i**2 + i*j*k + 2*k + i - j" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "124669875000" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[, , , ]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map(type, range(int(4)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/lib/python2.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/vincent/.cache/ipython/cython/_cython_magic_0f6407bef3789247c6025a198ff7b605.pyx\n", " tree = Parsing.p_module(s, pxd, full_module_name)\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_0f6407bef3789247c6025a198ff7b605.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.12

\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", "

\n", "
+01: # distutils: language = c++
\n", "
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "
 02: 
\n", "
 03: # importing types that are defined in other Cython files
\n", "
 04: from sage.rings.integer cimport Integer
\n", "
 05: 
\n", "
 06: # importing C++
\n", "
 07: from libcpp.vector cimport vector
\n", "
 08: 
\n", "
+09: def f1(long N1, long N2, long N3):
\n", "
/* Python wrapper */\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_1f1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
       "static PyMethodDef __pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_1f1 = {\"f1\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_1f1, METH_VARARGS|METH_KEYWORDS, 0};\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_1f1(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
       "  long __pyx_v_N1;\n",
       "  long __pyx_v_N2;\n",
       "  long __pyx_v_N3;\n",
       "  PyObject *__pyx_r = 0;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f1 (wrapper)\", 0);\n",
       "  {\n",
       "    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_N1,&__pyx_n_s_N2,&__pyx_n_s_N3,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",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
       "        CYTHON_FALLTHROUGH;\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] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N1)) != 0)) kw_args--;\n",
       "        else goto __pyx_L5_argtuple_error;\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1:\n",
       "        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N2)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f1\", 1, 3, 3, 1); __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "        }\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2:\n",
       "        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N3)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f1\", 1, 3, 3, 2); __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "        }\n",
       "      }\n",
       "      if (unlikely(kw_args > 0)) {\n",
       "        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"f1\") < 0)) __PYX_ERR(0, 9, __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_N1 = __Pyx_PyInt_As_long(values[0]); if (unlikely((__pyx_v_N1 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "    __pyx_v_N2 = __Pyx_PyInt_As_long(values[1]); if (unlikely((__pyx_v_N2 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "    __pyx_v_N3 = __Pyx_PyInt_As_long(values[2]); if (unlikely((__pyx_v_N3 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "  }\n",
       "  goto __pyx_L4_argument_unpacking_done;\n",
       "  __pyx_L5_argtuple_error:;\n",
       "  __Pyx_RaiseArgtupleInvalid(\"f1\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 9, __pyx_L3_error)\n",
       "  __pyx_L3_error:;\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f1\", __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_0f6407bef3789247c6025a198ff7b605_f1(__pyx_self, __pyx_v_N1, __pyx_v_N2, __pyx_v_N3);\n",
       "\n",
       "  /* function exit code */\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "\n",
       "static PyObject *__pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_f1(CYTHON_UNUSED PyObject *__pyx_self, long __pyx_v_N1, long __pyx_v_N2, long __pyx_v_N3) {\n",
       "  long __pyx_v_s2;\n",
       "  long __pyx_v_i;\n",
       "  long __pyx_v_j;\n",
       "  long __pyx_v_k;\n",
       "  PyObject *__pyx_r = NULL;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f1\", 0);\n",
       "/* … */\n",
       "  /* function exit code */\n",
       "  __pyx_L1_error:;\n",
       "  __Pyx_XDECREF(__pyx_t_10);\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f1\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
       "  __pyx_r = NULL;\n",
       "  __pyx_L0:;\n",
       "  __Pyx_XGIVEREF(__pyx_r);\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "/* … */\n",
       "  __pyx_tuple__2 = PyTuple_Pack(7, __pyx_n_s_N1, __pyx_n_s_N2, __pyx_n_s_N3, __pyx_n_s_s2, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_k); if (unlikely(!__pyx_tuple__2)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_tuple__2);\n",
       "  __Pyx_GIVEREF(__pyx_tuple__2);\n",
       "/* … */\n",
       "  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_1f1, NULL, __pyx_n_s_cython_magic_0f6407bef3789247c6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f1, __pyx_t_1) < 0) __PYX_ERR(0, 9, __pyx_L1_error)\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "  __pyx_codeobj__3 = (PyObject*)__Pyx_PyCode_New(3, 0, 7, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__2, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_cache_ipython_cython__cython_ma, __pyx_n_s_f1, 9, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__3)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
       "
+10:     cdef long s2 = 0   # this declares s as a C int
\n", "
  __pyx_v_s2 = 0;\n",
       "
 11:     cdef long i,j,k
\n", "
+12:     for i in range(N1):
\n", "
  __pyx_t_1 = __pyx_v_N1;\n",
       "  __pyx_t_2 = __pyx_t_1;\n",
       "  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {\n",
       "    __pyx_v_i = __pyx_t_3;\n",
       "
+13:         for j in range(N2):
\n", "
    __pyx_t_4 = __pyx_v_N2;\n",
       "    __pyx_t_5 = __pyx_t_4;\n",
       "    for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6+=1) {\n",
       "      __pyx_v_j = __pyx_t_6;\n",
       "
+14:             for k in range(N3):
\n", "
      __pyx_t_7 = __pyx_v_N3;\n",
       "      __pyx_t_8 = __pyx_t_7;\n",
       "      for (__pyx_t_9 = 0; __pyx_t_9 < __pyx_t_8; __pyx_t_9+=1) {\n",
       "        __pyx_v_k = __pyx_t_9;\n",
       "
+15:                 s2 += i**2 + i*j*k + 2*k + i - j
\n", "
        __pyx_v_s2 = (__pyx_v_s2 + ((((__Pyx_pow_long(__pyx_v_i, 2) + ((__pyx_v_i * __pyx_v_j) * __pyx_v_k)) + (2 * __pyx_v_k)) + __pyx_v_i) - __pyx_v_j));\n",
       "      }\n",
       "    }\n",
       "  }\n",
       "
+16:     return s2
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
       "  __pyx_t_10 = __Pyx_PyInt_From_long(__pyx_v_s2); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_10);\n",
       "  __pyx_r = __pyx_t_10;\n",
       "  __pyx_t_10 = 0;\n",
       "  goto __pyx_L0;\n",
       "
 17: 
\n", "
+18: def f2(N1, N2, N3):
\n", "
/* Python wrapper */\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_3f2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
       "static PyMethodDef __pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_3f2 = {\"f2\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_3f2, METH_VARARGS|METH_KEYWORDS, 0};\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_3f2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
       "  PyObject *__pyx_v_N1 = 0;\n",
       "  PyObject *__pyx_v_N2 = 0;\n",
       "  PyObject *__pyx_v_N3 = 0;\n",
       "  PyObject *__pyx_r = 0;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f2 (wrapper)\", 0);\n",
       "  {\n",
       "    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_N1,&__pyx_n_s_N2,&__pyx_n_s_N3,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",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
       "        CYTHON_FALLTHROUGH;\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] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N1)) != 0)) kw_args--;\n",
       "        else goto __pyx_L5_argtuple_error;\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1:\n",
       "        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N2)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f2\", 1, 3, 3, 1); __PYX_ERR(0, 18, __pyx_L3_error)\n",
       "        }\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2:\n",
       "        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N3)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f2\", 1, 3, 3, 2); __PYX_ERR(0, 18, __pyx_L3_error)\n",
       "        }\n",
       "      }\n",
       "      if (unlikely(kw_args > 0)) {\n",
       "        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"f2\") < 0)) __PYX_ERR(0, 18, __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_N1 = values[0];\n",
       "    __pyx_v_N2 = values[1];\n",
       "    __pyx_v_N3 = values[2];\n",
       "  }\n",
       "  goto __pyx_L4_argument_unpacking_done;\n",
       "  __pyx_L5_argtuple_error:;\n",
       "  __Pyx_RaiseArgtupleInvalid(\"f2\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 18, __pyx_L3_error)\n",
       "  __pyx_L3_error:;\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f2\", __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_0f6407bef3789247c6025a198ff7b605_2f2(__pyx_self, __pyx_v_N1, __pyx_v_N2, __pyx_v_N3);\n",
       "\n",
       "  /* function exit code */\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "\n",
       "static PyObject *__pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_2f2(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_N1, PyObject *__pyx_v_N2, PyObject *__pyx_v_N3) {\n",
       "  PyObject *__pyx_v_s2 = NULL;\n",
       "  PyObject *__pyx_v_i = NULL;\n",
       "  PyObject *__pyx_v_j = NULL;\n",
       "  PyObject *__pyx_v_k = NULL;\n",
       "  PyObject *__pyx_r = NULL;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f2\", 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_5);\n",
       "  __Pyx_XDECREF(__pyx_t_8);\n",
       "  __Pyx_XDECREF(__pyx_t_11);\n",
       "  __Pyx_XDECREF(__pyx_t_12);\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f2\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
       "  __pyx_r = NULL;\n",
       "  __pyx_L0:;\n",
       "  __Pyx_XDECREF(__pyx_v_s2);\n",
       "  __Pyx_XDECREF(__pyx_v_i);\n",
       "  __Pyx_XDECREF(__pyx_v_j);\n",
       "  __Pyx_XDECREF(__pyx_v_k);\n",
       "  __Pyx_XGIVEREF(__pyx_r);\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "/* … */\n",
       "  __pyx_tuple__4 = PyTuple_Pack(7, __pyx_n_s_N1, __pyx_n_s_N2, __pyx_n_s_N3, __pyx_n_s_s2, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_k); if (unlikely(!__pyx_tuple__4)) __PYX_ERR(0, 18, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_tuple__4);\n",
       "  __Pyx_GIVEREF(__pyx_tuple__4);\n",
       "/* … */\n",
       "  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_3f2, NULL, __pyx_n_s_cython_magic_0f6407bef3789247c6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 18, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f2, __pyx_t_1) < 0) __PYX_ERR(0, 18, __pyx_L1_error)\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "  __pyx_codeobj__5 = (PyObject*)__Pyx_PyCode_New(3, 0, 7, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__4, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_cache_ipython_cython__cython_ma, __pyx_n_s_f2, 18, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__5)) __PYX_ERR(0, 18, __pyx_L1_error)\n",
       "
+19:     s2 = 0   # this declares s as a C int
\n", "
  __Pyx_INCREF(__pyx_int_0);\n",
       "  __pyx_v_s2 = __pyx_int_0;\n",
       "
+20:     for i in range(N1):
\n", "
  __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_v_N1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (likely(PyList_CheckExact(__pyx_t_1)) || PyTuple_CheckExact(__pyx_t_1)) {\n",
       "    __pyx_t_2 = __pyx_t_1; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0;\n",
       "    __pyx_t_4 = NULL;\n",
       "  } else {\n",
       "    __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "    __Pyx_GOTREF(__pyx_t_2);\n",
       "    __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "  }\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "  for (;;) {\n",
       "    if (likely(!__pyx_t_4)) {\n",
       "      if (likely(PyList_CheckExact(__pyx_t_2))) {\n",
       "        if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break;\n",
       "        #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "        __pyx_t_1 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "        #else\n",
       "        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "        #endif\n",
       "      } else {\n",
       "        if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break;\n",
       "        #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "        __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_1); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "        #else\n",
       "        __pyx_t_1 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "        #endif\n",
       "      }\n",
       "    } else {\n",
       "      __pyx_t_1 = __pyx_t_4(__pyx_t_2);\n",
       "      if (unlikely(!__pyx_t_1)) {\n",
       "        PyObject* exc_type = PyErr_Occurred();\n",
       "        if (exc_type) {\n",
       "          if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n",
       "          else __PYX_ERR(0, 20, __pyx_L1_error)\n",
       "        }\n",
       "        break;\n",
       "      }\n",
       "      __Pyx_GOTREF(__pyx_t_1);\n",
       "    }\n",
       "    __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_1);\n",
       "    __pyx_t_1 = 0;\n",
       "/* … */\n",
       "  }\n",
       "  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
       "
+21:         for j in range(N2):
\n", "
    __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_v_N2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "    __Pyx_GOTREF(__pyx_t_1);\n",
       "    if (likely(PyList_CheckExact(__pyx_t_1)) || PyTuple_CheckExact(__pyx_t_1)) {\n",
       "      __pyx_t_5 = __pyx_t_1; __Pyx_INCREF(__pyx_t_5); __pyx_t_6 = 0;\n",
       "      __pyx_t_7 = NULL;\n",
       "    } else {\n",
       "      __pyx_t_6 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "      __Pyx_GOTREF(__pyx_t_5);\n",
       "      __pyx_t_7 = Py_TYPE(__pyx_t_5)->tp_iternext; if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "    }\n",
       "    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "    for (;;) {\n",
       "      if (likely(!__pyx_t_7)) {\n",
       "        if (likely(PyList_CheckExact(__pyx_t_5))) {\n",
       "          if (__pyx_t_6 >= PyList_GET_SIZE(__pyx_t_5)) break;\n",
       "          #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "          __pyx_t_1 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_6); __Pyx_INCREF(__pyx_t_1); __pyx_t_6++; if (unlikely(0 < 0)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "          #else\n",
       "          __pyx_t_1 = PySequence_ITEM(__pyx_t_5, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "          __Pyx_GOTREF(__pyx_t_1);\n",
       "          #endif\n",
       "        } else {\n",
       "          if (__pyx_t_6 >= PyTuple_GET_SIZE(__pyx_t_5)) break;\n",
       "          #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "          __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_6); __Pyx_INCREF(__pyx_t_1); __pyx_t_6++; if (unlikely(0 < 0)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "          #else\n",
       "          __pyx_t_1 = PySequence_ITEM(__pyx_t_5, __pyx_t_6); __pyx_t_6++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "          __Pyx_GOTREF(__pyx_t_1);\n",
       "          #endif\n",
       "        }\n",
       "      } else {\n",
       "        __pyx_t_1 = __pyx_t_7(__pyx_t_5);\n",
       "        if (unlikely(!__pyx_t_1)) {\n",
       "          PyObject* exc_type = PyErr_Occurred();\n",
       "          if (exc_type) {\n",
       "            if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n",
       "            else __PYX_ERR(0, 21, __pyx_L1_error)\n",
       "          }\n",
       "          break;\n",
       "        }\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "      }\n",
       "      __Pyx_XDECREF_SET(__pyx_v_j, __pyx_t_1);\n",
       "      __pyx_t_1 = 0;\n",
       "/* … */\n",
       "    }\n",
       "    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
       "
+22:             for k in range(N3):
\n", "
      __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_v_N3); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "      __Pyx_GOTREF(__pyx_t_1);\n",
       "      if (likely(PyList_CheckExact(__pyx_t_1)) || PyTuple_CheckExact(__pyx_t_1)) {\n",
       "        __pyx_t_8 = __pyx_t_1; __Pyx_INCREF(__pyx_t_8); __pyx_t_9 = 0;\n",
       "        __pyx_t_10 = NULL;\n",
       "      } else {\n",
       "        __pyx_t_9 = -1; __pyx_t_8 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_8);\n",
       "        __pyx_t_10 = Py_TYPE(__pyx_t_8)->tp_iternext; if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "      }\n",
       "      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "      for (;;) {\n",
       "        if (likely(!__pyx_t_10)) {\n",
       "          if (likely(PyList_CheckExact(__pyx_t_8))) {\n",
       "            if (__pyx_t_9 >= PyList_GET_SIZE(__pyx_t_8)) break;\n",
       "            #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "            __pyx_t_1 = PyList_GET_ITEM(__pyx_t_8, __pyx_t_9); __Pyx_INCREF(__pyx_t_1); __pyx_t_9++; if (unlikely(0 < 0)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "            #else\n",
       "            __pyx_t_1 = PySequence_ITEM(__pyx_t_8, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "            __Pyx_GOTREF(__pyx_t_1);\n",
       "            #endif\n",
       "          } else {\n",
       "            if (__pyx_t_9 >= PyTuple_GET_SIZE(__pyx_t_8)) break;\n",
       "            #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
       "            __pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_8, __pyx_t_9); __Pyx_INCREF(__pyx_t_1); __pyx_t_9++; if (unlikely(0 < 0)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "            #else\n",
       "            __pyx_t_1 = PySequence_ITEM(__pyx_t_8, __pyx_t_9); __pyx_t_9++; if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "            __Pyx_GOTREF(__pyx_t_1);\n",
       "            #endif\n",
       "          }\n",
       "        } else {\n",
       "          __pyx_t_1 = __pyx_t_10(__pyx_t_8);\n",
       "          if (unlikely(!__pyx_t_1)) {\n",
       "            PyObject* exc_type = PyErr_Occurred();\n",
       "            if (exc_type) {\n",
       "              if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n",
       "              else __PYX_ERR(0, 22, __pyx_L1_error)\n",
       "            }\n",
       "            break;\n",
       "          }\n",
       "          __Pyx_GOTREF(__pyx_t_1);\n",
       "        }\n",
       "        __Pyx_XDECREF_SET(__pyx_v_k, __pyx_t_1);\n",
       "        __pyx_t_1 = 0;\n",
       "/* … */\n",
       "      }\n",
       "      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
       "
+23:                 s2 += i**2 + i*j*k + 2*k + i - j
\n", "
        __pyx_t_1 = PyNumber_Power(__pyx_v_i, __pyx_int_2, Py_None); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "        __pyx_t_11 = PyNumber_Multiply(__pyx_v_i, __pyx_v_j); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_11);\n",
       "        __pyx_t_12 = PyNumber_Multiply(__pyx_t_11, __pyx_v_k); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_12);\n",
       "        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;\n",
       "        __pyx_t_11 = PyNumber_Add(__pyx_t_1, __pyx_t_12); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_11);\n",
       "        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "        __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n",
       "        __pyx_t_12 = PyNumber_Multiply(__pyx_int_2, __pyx_v_k); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_12);\n",
       "        __pyx_t_1 = PyNumber_Add(__pyx_t_11, __pyx_t_12); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\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_Add(__pyx_t_1, __pyx_v_i); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_12);\n",
       "        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "        __pyx_t_1 = PyNumber_Subtract(__pyx_t_12, __pyx_v_j); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "        __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n",
       "        __pyx_t_12 = PyNumber_InPlaceAdd(__pyx_v_s2, __pyx_t_1); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 23, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_12);\n",
       "        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "        __Pyx_DECREF_SET(__pyx_v_s2, __pyx_t_12);\n",
       "        __pyx_t_12 = 0;\n",
       "
+24:     return s2
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
       "  __Pyx_INCREF(__pyx_v_s2);\n",
       "  __pyx_r = __pyx_v_s2;\n",
       "  goto __pyx_L0;\n",
       "
 25: 
\n", "
+26: def f3(Integer N1, Integer N2, Integer N3):
\n", "
/* Python wrapper */\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_5f3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
       "static PyMethodDef __pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_5f3 = {\"f3\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_5f3, METH_VARARGS|METH_KEYWORDS, 0};\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_5f3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
       "  struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N1 = 0;\n",
       "  struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N2 = 0;\n",
       "  struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N3 = 0;\n",
       "  PyObject *__pyx_r = 0;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f3 (wrapper)\", 0);\n",
       "  {\n",
       "    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_N1,&__pyx_n_s_N2,&__pyx_n_s_N3,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",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
       "        CYTHON_FALLTHROUGH;\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] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N1)) != 0)) kw_args--;\n",
       "        else goto __pyx_L5_argtuple_error;\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  1:\n",
       "        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N2)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f3\", 1, 3, 3, 1); __PYX_ERR(0, 26, __pyx_L3_error)\n",
       "        }\n",
       "        CYTHON_FALLTHROUGH;\n",
       "        case  2:\n",
       "        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_N3)) != 0)) kw_args--;\n",
       "        else {\n",
       "          __Pyx_RaiseArgtupleInvalid(\"f3\", 1, 3, 3, 2); __PYX_ERR(0, 26, __pyx_L3_error)\n",
       "        }\n",
       "      }\n",
       "      if (unlikely(kw_args > 0)) {\n",
       "        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"f3\") < 0)) __PYX_ERR(0, 26, __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_N1 = ((struct __pyx_obj_4sage_5rings_7integer_Integer *)values[0]);\n",
       "    __pyx_v_N2 = ((struct __pyx_obj_4sage_5rings_7integer_Integer *)values[1]);\n",
       "    __pyx_v_N3 = ((struct __pyx_obj_4sage_5rings_7integer_Integer *)values[2]);\n",
       "  }\n",
       "  goto __pyx_L4_argument_unpacking_done;\n",
       "  __pyx_L5_argtuple_error:;\n",
       "  __Pyx_RaiseArgtupleInvalid(\"f3\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 26, __pyx_L3_error)\n",
       "  __pyx_L3_error:;\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return NULL;\n",
       "  __pyx_L4_argument_unpacking_done:;\n",
       "  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_N1), __pyx_ptype_4sage_5rings_7integer_Integer, 1, \"N1\", 0))) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_N2), __pyx_ptype_4sage_5rings_7integer_Integer, 1, \"N2\", 0))) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_N3), __pyx_ptype_4sage_5rings_7integer_Integer, 1, \"N3\", 0))) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  __pyx_r = __pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_4f3(__pyx_self, __pyx_v_N1, __pyx_v_N2, __pyx_v_N3);\n",
       "\n",
       "  /* function exit code */\n",
       "  goto __pyx_L0;\n",
       "  __pyx_L1_error:;\n",
       "  __pyx_r = NULL;\n",
       "  __pyx_L0:;\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "\n",
       "static PyObject *__pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_4f3(CYTHON_UNUSED PyObject *__pyx_self, struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N1, struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N2, struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_N3) {\n",
       "  struct __pyx_obj_4sage_5rings_7integer_Integer *__pyx_v_s2 = 0;\n",
       "  long __pyx_v_i;\n",
       "  long __pyx_v_j;\n",
       "  long __pyx_v_k;\n",
       "  PyObject *__pyx_r = NULL;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"f3\", 0);\n",
       "/* … */\n",
       "  /* function exit code */\n",
       "  __pyx_L1_error:;\n",
       "  __Pyx_XDECREF(__pyx_t_1);\n",
       "  __Pyx_XDECREF(__pyx_t_11);\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.f3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
       "  __pyx_r = NULL;\n",
       "  __pyx_L0:;\n",
       "  __Pyx_XDECREF((PyObject *)__pyx_v_s2);\n",
       "  __Pyx_XGIVEREF(__pyx_r);\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "/* … */\n",
       "  __pyx_tuple__6 = PyTuple_Pack(7, __pyx_n_s_N1, __pyx_n_s_N2, __pyx_n_s_N3, __pyx_n_s_s2, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_k); if (unlikely(!__pyx_tuple__6)) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_tuple__6);\n",
       "  __Pyx_GIVEREF(__pyx_tuple__6);\n",
       "/* … */\n",
       "  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_5f3, NULL, __pyx_n_s_cython_magic_0f6407bef3789247c6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f3, __pyx_t_1) < 0) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "  __pyx_codeobj__7 = (PyObject*)__Pyx_PyCode_New(3, 0, 7, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__6, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_cache_ipython_cython__cython_ma, __pyx_n_s_f3, 26, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__7)) __PYX_ERR(0, 26, __pyx_L1_error)\n",
       "
+27:     cdef Integer s2 = Integer(0)
\n", "
  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)__pyx_ptype_4sage_5rings_7integer_Integer), __pyx_tuple_, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 27, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  __pyx_v_s2 = ((struct __pyx_obj_4sage_5rings_7integer_Integer *)__pyx_t_1);\n",
       "  __pyx_t_1 = 0;\n",
       "/* … */\n",
       "  __pyx_tuple_ = PyTuple_Pack(1, __pyx_int_0); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 27, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_tuple_);\n",
       "  __Pyx_GIVEREF(__pyx_tuple_);\n",
       "
 28:     cdef long i,j,k
\n", "
+29:     for i in range(N1):
\n", "
  __pyx_t_2 = __Pyx_PyInt_As_long(((PyObject *)__pyx_v_N1)); if (unlikely((__pyx_t_2 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 29, __pyx_L1_error)\n",
       "  __pyx_t_3 = __pyx_t_2;\n",
       "  for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {\n",
       "    __pyx_v_i = __pyx_t_4;\n",
       "
+30:         for j in range(N2):
\n", "
    __pyx_t_5 = __Pyx_PyInt_As_long(((PyObject *)__pyx_v_N2)); if (unlikely((__pyx_t_5 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 30, __pyx_L1_error)\n",
       "    __pyx_t_6 = __pyx_t_5;\n",
       "    for (__pyx_t_7 = 0; __pyx_t_7 < __pyx_t_6; __pyx_t_7+=1) {\n",
       "      __pyx_v_j = __pyx_t_7;\n",
       "
+31:             for k in range(N3):
\n", "
      __pyx_t_8 = __Pyx_PyInt_As_long(((PyObject *)__pyx_v_N3)); if (unlikely((__pyx_t_8 == (long)-1) && PyErr_Occurred())) __PYX_ERR(0, 31, __pyx_L1_error)\n",
       "      __pyx_t_9 = __pyx_t_8;\n",
       "      for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {\n",
       "        __pyx_v_k = __pyx_t_10;\n",
       "
+32:                 s2 += i**2 + i*j*k + 2*k + i - j
\n", "
        __pyx_t_1 = __Pyx_PyInt_From_long(((((__Pyx_pow_long(__pyx_v_i, 2) + ((__pyx_v_i * __pyx_v_j) * __pyx_v_k)) + (2 * __pyx_v_k)) + __pyx_v_i) - __pyx_v_j)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 32, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_1);\n",
       "        __pyx_t_11 = PyNumber_InPlaceAdd(((PyObject *)__pyx_v_s2), __pyx_t_1); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 32, __pyx_L1_error)\n",
       "        __Pyx_GOTREF(__pyx_t_11);\n",
       "        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "        if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_4sage_5rings_7integer_Integer))))) __PYX_ERR(0, 32, __pyx_L1_error)\n",
       "        __Pyx_DECREF_SET(__pyx_v_s2, ((struct __pyx_obj_4sage_5rings_7integer_Integer *)__pyx_t_11));\n",
       "        __pyx_t_11 = 0;\n",
       "      }\n",
       "    }\n",
       "  }\n",
       "
+33:     return s2
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
       "  __Pyx_INCREF(((PyObject *)__pyx_v_s2));\n",
       "  __pyx_r = ((PyObject *)__pyx_v_s2);\n",
       "  goto __pyx_L0;\n",
       "
 34: 
\n", "
+35: def g():
\n", "
/* Python wrapper */\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_7g(PyObject *__pyx_self, CYTHON_UNUSED PyObject *unused); /*proto*/\n",
       "static PyMethodDef __pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_7g = {\"g\", (PyCFunction)__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_7g, METH_NOARGS, 0};\n",
       "static PyObject *__pyx_pw_46_cython_magic_0f6407bef3789247c6025a198ff7b605_7g(PyObject *__pyx_self, CYTHON_UNUSED PyObject *unused) {\n",
       "  PyObject *__pyx_r = 0;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"g (wrapper)\", 0);\n",
       "  __pyx_r = __pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_6g(__pyx_self);\n",
       "\n",
       "  /* function exit code */\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "\n",
       "static PyObject *__pyx_pf_46_cython_magic_0f6407bef3789247c6025a198ff7b605_6g(CYTHON_UNUSED PyObject *__pyx_self) {\n",
       "  std::vector<int>  __pyx_v_a;\n",
       "  PyObject *__pyx_r = NULL;\n",
       "  __Pyx_RefNannyDeclarations\n",
       "  __Pyx_RefNannySetupContext(\"g\", 0);\n",
       "/* … */\n",
       "  /* function exit code */\n",
       "  __pyx_L1_error:;\n",
       "  __Pyx_XDECREF(__pyx_t_1);\n",
       "  __Pyx_XDECREF(__pyx_t_2);\n",
       "  __Pyx_AddTraceback(\"_cython_magic_0f6407bef3789247c6025a198ff7b605.g\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
       "  __pyx_r = NULL;\n",
       "  __pyx_L0:;\n",
       "  __Pyx_XGIVEREF(__pyx_r);\n",
       "  __Pyx_RefNannyFinishContext();\n",
       "  return __pyx_r;\n",
       "}\n",
       "/* … */\n",
       "  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_n_s_a); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 35, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_tuple__8);\n",
       "  __Pyx_GIVEREF(__pyx_tuple__8);\n",
       "/* … */\n",
       "  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_0f6407bef3789247c6025a198ff7b605_7g, NULL, __pyx_n_s_cython_magic_0f6407bef3789247c6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 35, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  if (PyDict_SetItem(__pyx_d, __pyx_n_s_g, __pyx_t_1) < 0) __PYX_ERR(0, 35, __pyx_L1_error)\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "
 36:     cdef vector[int] a
\n", "
+37:     a.push_back(3)
\n", "
  try {\n",
       "    __pyx_v_a.push_back(3);\n",
       "  } catch(...) {\n",
       "    __Pyx_CppExn2PyErr();\n",
       "    __PYX_ERR(0, 37, __pyx_L1_error)\n",
       "  }\n",
       "
+38:     a.push_back(2)
\n", "
  try {\n",
       "    __pyx_v_a.push_back(2);\n",
       "  } catch(...) {\n",
       "    __Pyx_CppExn2PyErr();\n",
       "    __PYX_ERR(0, 38, __pyx_L1_error)\n",
       "  }\n",
       "
+39:     return list(a)
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
       "  __pyx_t_1 = __pyx_convert_vector_to_py_int(__pyx_v_a); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 39, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_1);\n",
       "  __pyx_t_2 = PySequence_List(__pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 39, __pyx_L1_error)\n",
       "  __Pyx_GOTREF(__pyx_t_2);\n",
       "  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
       "  __pyx_r = __pyx_t_2;\n",
       "  __pyx_t_2 = 0;\n",
       "  goto __pyx_L0;\n",
       "
" ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython -a\n", "# distutils: language = c++\n", "\n", "# importing types that are defined in other Cython files\n", "from sage.rings.integer cimport Integer\n", "\n", "# importing C++\n", "from libcpp.vector cimport vector\n", "\n", "def f1(long N1, long N2, long N3):\n", " cdef long s2 = 0 # this declares s as a C int\n", " cdef long i,j,k\n", " for i in range(N1):\n", " for j in range(N2):\n", " for k in range(N3):\n", " s2 += i**2 + i*j*k + 2*k + i - j\n", " return s2\n", "\n", "def f2(N1, N2, N3):\n", " s2 = 0 # this declares s as a C int\n", " for i in range(N1):\n", " for j in range(N2):\n", " for k in range(N3):\n", " s2 += i**2 + i*j*k + 2*k + i - j\n", " return s2\n", "\n", "def f3(Integer N1, Integer N2, Integer N3):\n", " cdef Integer s2 = Integer(0)\n", " cdef long i,j,k\n", " for i in range(N1):\n", " for j in range(N2):\n", " for k in range(N3):\n", " s2 += i**2 + i*j*k + 2*k + i - j\n", " return s2\n", "\n", "def g():\n", " cdef vector[int] a\n", " a.push_back(3)\n", " a.push_back(2)\n", " return list(a) # wow!" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 626 µs, sys: 0 ns, total: 626 µs\n", "Wall time: 638 µs\n" ] }, { "data": { "text/plain": [ "124669875000" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "f1(100,100,100)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 108 ms, sys: 0 ns, total: 108 ms\n", "Wall time: 108 ms\n" ] }, { "data": { "text/plain": [ "124669875000" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "f2(100,100,100)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 219 ms, sys: 0 ns, total: 219 ms\n", "Wall time: 219 ms\n" ] }, { "data": { "text/plain": [ "124669875000" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "f3(100,100,100)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "124554051584" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s - 115823416" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.8", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.16" } }, "nbformat": 4, "nbformat_minor": 2 }