python - How do I get the strides from a dtype in numpy? -


i think can do: np.zeros((), dtype=dt).strides, doesn't seem efficient when dtype large array type like: ('<f8', (200, 100)). there way of going directly dtype strides in numpy?

you can strides of sub-array within structured array without creating "full" array.

sub-arrays within structured array required contiguous , in c-order according documentation. note sentence above first example:

sub-arrays have c-contiguous memory layout.

therefore, structured array no fields such 1 in example, can (as unreadable one-liner):

import numpy np  x = np.dtype(('<f8', (200, 100)))  strides = x.base.itemsize * np.r_[1, np.cumprod(x.shape[::-1][:-1])][::-1] 

avoiding code golf:

shape = list(x.shape)  # first, let's make strides array itemsize of 1 in c-order tmp_strides = shape[::-1] tmp_strides[1:] = list(np.cumprod(tmp_strides[:-1])) tmp_strides[0] = 1  # adjust real itemsize: tmp_strides = x.base.itemsize * np.array(tmp_strides)  # , convert tuple, reversing proper c-order strides = tuple(tmp_strides[::-1]) 

this gets more complex when there multiple fields, however. you'd need put in approriate checks in general. example: dtype have shape attribute? have fields? fields have shape attributes?


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -