laurama
unread,Jul 21, 2011, 6:43:46 PM7/21/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cython-users
Hi all,
I am fairly new in Python and brand new in Cython, so I apologize in
advance because this is probably a dumb question... I read the
tutorials about working with numpy arrays and cython and I remember
reading somewhere that once defined, a cython array cannot be modified
(ie cannot be reshaped) but I cannot find this anymore. Is this
correct?
In my functions need to interact with lists and numpy arrays a lot. I
defined the numpy arrays as cython arrays but I didn't redefine the
lists as anything. So for example simple_function1 receives a list and
returns an array (and I have functions that do the opposite). I have
some questions:
1. When I run the cython version of the function simple_function, I
have an error indicating me that I have the wrong dimensions:
"Buffer has wrong number of dimensions (expected 1, got 3)"
I understand this, I told cython the dim was 1 but at the end I want
to reshape the array to have 3 dimensions. Is it possible to do this?
If not, what is the best way to "cythonize" this function?
#chromosome is just a list of numbers
def simple_function(chromosome):
chromoArray = np.array((chromosome),dtype = DTYPE2)
chromoArray = chromoArray.reshape(len(anotherlist),m,2)
... more stuff
return chromoArray
my cython code looks like:
def simple_function(chromosome):
cdef np.ndarray[DTYPE2_t, ndim=1] chromoArray =
np.array((chromosome),dtype = DTYPE2)
chromoArray =
chromoArray.reshape(len(self.totalVaccineList),self.numberOfCities,2)
...more stuff
2. Is there a way to "cythonize" lists?
3. Inside a Python class, so something of the form:
class MyClass:
blabla
is it best to define cpdef functions? I read the documentation online
and tried to do this but got an error at compilation.
Any ideas would be highly highly appreciated!
thanks a lot!