Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think tuple unpacking works in Cython like it does in Python. You have to specify the size of <code>checked</code> using <code>(board.shape[0], board.shape[1])</code>. </p> <p>You also need to specify the datatype of <code>checked</code>. By default <code>np.zeros</code> returns a <code>float64</code>. You need to change that to an <code>int32</code> to be compatible with the declared type.</p> <p>Here's a modified version that should run.</p> <pre><code>def score(np.ndarray[int, ndim=2] board): cdef np.ndarray[np.int32_t, ndim = 2] checked checked = np.zeros((board.shape[0], board.shape[1]), dtype='i4') for i in xrange(len(board)): for j in xrange(len(board)): if checked[i,j] == 0 and board[i,j] !=0: ... do stuff </code></pre> <p>To answer the second part of your question - yes you can append to lists within Cython functions. However, as I understand it this involves Python function calls, so it won't be any faster than the same operations within Python.</p> <p><strong>Update 1:</strong></p> <p>I believe those errors are because the compiler can't find the NumPy header files. Update your setup.py to include the path to those files. The path should be output by the <code>np.get_include()</code> function.</p> <pre><code>import numpy as np from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass={'build_ext': build_ext}, ext_modules = [ Extension("new_gox", ["new_gox.pyx"], include_dirs=[np.get_include()]) ] ) </code></pre> <p>The dtype <code>i4</code> is a 32 bit integer. It's equivalent to <code>np.int32</code>.</p> <p>I'm not sure what's going on with the 'too many arguments' errors. If you're calling that function from within Python, it needs to be declared as either <code>def</code> or <code>cpdef</code>. Functions declared with <code>cdef</code> can only be called from Cython. This is described <a href="http://docs.cython.org/src/userguide/language_basics.html#python-functions-vs-c-functions" rel="noreferrer">here</a> in the Cython docs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload