Weijie Sun
Weijie Sun

Reputation: 204

order types in numpy.zeros_like

I am looking at Numba Cuda library.

import numbapro.cudalib.cublas as cublas
blas = cublas.Blas()

n =100
A = np.random.random((n, n)).astype(np.float32)
B = np.random.random((n, n)).astype(np.float32)
C = np.zeros_like(A, order='F')

blas.gemm('T', 'T', n, n, n, 1.0, A, B, 1.0, C)

assert(np.allclose(np.dot(A, B), C))

After checking numpy.zeros_like, I am curious about the optional parameter order which has 4 different types: 'C', 'F', 'A', and 'K'.

order : {‘C’, ‘F’, ‘A’, or ‘K’}, optional Overrides the memory layout of the result. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible.

There are descriptions in the documentation. But I am still confused. What is the difference between different order types?

Upvotes: 1

Views: 203

Answers (1)

hpaulj
hpaulj

Reputation: 231375

The clearest example I can imagine of these orders is with a simple 2d array:

The default ordering, 'C':

In [5]: x = np.arange(12).reshape(3,4)                                                                                                   
In [6]: x                                                                                                                                
Out[6]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

'F' - not how values count down the columns:

In [7]: x = np.arange(12).reshape(3,4, order='F')                                                                                        
In [8]: x                                                                                                                                
Out[8]: 
array([[ 0,  3,  6,  9],
       [ 1,  4,  7, 10],
       [ 2,  5,  8, 11]])

now take that last 'F' order, and ravel the values

In [9]: x.ravel(order='C')                                                                                                               
Out[9]: array([ 0,  3,  6,  9,  1,  4,  7, 10,  2,  5,  8, 11])
In [10]: x.ravel(order='F')                                                                                                              
Out[10]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
In [11]: x.ravel(order='K')                                                                                                              
Out[11]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

And so on; we can play with other combinations.

Upvotes: 2

Related Questions