What I am doing
I have a lot of tiny matrices (3x3,5x5,3x4 and so on), with sizes that are known at compile time. Until now I used numpy to create these
A = np.zeros((3,5))
And use the numpy array as if it was a memory view. Now, I would like to get rid of these numpy calls and instead use C arrays (or something similarily fast that is not dynamically allocated). I did the following:
cdef double[3][5] A_c
cdef double[:,:] A = A_c
A[:] = 0.0
Of course, the last line depends on the importance of settings the elements to zero. For dynamically sized arrays I am doing this:
double[:,:] A = view.array(shape=(4, N), itemsize=sizeof(double), format="d")
and I am quite happy with that.
What I would like to do
I would like to use a more concise way to do this. I know I could implement a class like described here for conciseness:
Cython: Create memoryview without NumPy array?
But this is not a c array with a size known at compile time. Maybe there is a way to use the DEF macros with arguments? Like so:
** NOT WORKING, DO NOT COPY AND PASTE **
DEF small_matrix(name,size):
cdef double[size[0],size[1]] name_c
cdef double[:,:] name = name_c
...
small_matrix(A,(3,5))
small_matrix(B,(5,1))
...
small_matrix(C,(3,1))
for i in range(3):
C[i,0] = 0.0
for j in range(5):
C[i,0] += A[i,j]*B[j,0]
Maybe I am also missing a simple way to create a cdef class of vectors/matrices with not-dynamically allocated data.