I am trying to optimize a python class by using cython
extension type. My operations are using numpy
without indexing at all i.e. piecewise addition , matrix multiplication, so my primary goal is to reduce python overhead. I am mostly interested in the type declaration: should I use numpy
or cython
structures?
Here's a simple example:
cdef class Analyzer:
cdef float coeff
cdef float[:] v1, v2 #should I use memoryview? can I use a ndarray?
def __init__(self):
# some stuff ...
cpdef void func(self, matrix, vector):
# some stuff ...
cdef Py_ssize_t i
for i in range(big_number):
self.update_vectors(i)
self.v1 = matrix @ vector + self.coeff * self.v2 # all my operations are like this i.e. no indeces
# should I convert views to nd.arrays?
Note, that when I try self.coeff * self.v2
, I get errors since they are not considered numpy arrays:
Invalid operand types for '*' (float; float[:] *)
So, since I am using numpy
and there's is no point to create loops just for standard linear algebra stuff, how do I declare (class) attribute types to reduce overhead?