1

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.

Community
  • 1
  • 1
Maximilian
  • 83
  • 5
  • If cython supported non-type template parameters, I think there'd be a nice-ish c++ solution using `std::array` and `constexpr`. Unfortunately it doesn't, some open PRs to that end - https://github.com/cython/cython/pull/426 – chrisb Mar 01 '18 at 20:39
  • 1
    I must confess, I don't understand, why you would do it: either use the C-array directly without any overhead, or create a memory-view on the fly if you need to pass it to a function. – ead Mar 01 '18 at 22:25
  • I hope you understand, that what you are trying to achieve is quite murky: you memory view would have a pointer to a temporary object and if you would return this memory view from the function strange things could happen... – ead Mar 01 '18 at 22:27
  • @ead To clarify, the "enhanced" DEF should be a macro that at compile time puts the code (changed by the parameters) into the c code and would look just like in my example above. Thus, the memory view would point to the static c array. The point of doing so is the following: I have functions that turn these matrices into memory views so the slight overhead of creating the memoryview for the matrix exists anyway, and I would like to use the indexing that memory views allow. Setting all to zero, indexing like in numpy, ... – Maximilian Mar 02 '18 at 06:51
  • It would be best to just create dynamically allocated C arrays and pass them as-is to the memory view using functions. No change in code is needed as memory views can work on known size C arrays. The size does not need to be known at compile time either - the example you linked is also dynamically allocated. – danny Mar 07 '18 at 17:45

0 Answers0