[Numpy-discussion] "aligned" matrix / ctypes

Robert Kern robert.kern at gmail.com
Thu Apr 24 14:45:58 EDT 2008


On Thu, Apr 24, 2008 at 11:29 AM, Zachary Pincus
<zachary.pincus at yale.edu> wrote:
>  Here's my attempt at aligned_empty(). It only accepts alignments that are
> an integer multiple (or divisor) of the dtype's itemsize, because of
> limitations in ndarray.view().

aligned_empty() works for me (after removing the if: suite that guards
this case) because it correctly .view()s before it slices.
aligned_empty_arbitrary() doesn't because it slices before it
.view()s.

In [14]: aligned_allocator.aligned_empty((5,5), float, 3)
Out[14]:
array([[ -2.00773895e+002,   2.12196342e-314,   3.41914975e-307,
          5.47724529e-306,   9.16023223e-310],
       [  7.82990492e-317,   1.36379628e-316,   0.00000000e+000,
          0.00000000e+000,   1.27361195e-313],
       [  4.94065646e-324,   7.00258612e-313,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000],
       [  0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
          0.00000000e+000,   0.00000000e+000]])

In [15]: aligned_allocator.aligned_empty_arbitrary((5,5), float, 3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/rkern/today/<ipython console> in <module>()

/Users/rkern/today/aligned_allocator.py in
aligned_empty_arbitrary(shape, dtype, byte_alignment, order)
     53   byte_shape = tuple(other_shape) + (aligned_byte_width,)
     54   aligned_view = aligned_buffer.reshape(byte_shape, order='C')
---> 55   requested_view = aligned_view[..., :byte_width].view(dtype)
     56   if order == 'F':
     57     return requested_view.T

ValueError: new type not compatible with array.


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the NumPy-Discussion mailing list