[Numpy-discussion] dtype "reduction"

Nicolas Rougier Nicolas.Rougier at inria.fr
Thu Dec 27 03:11:05 EST 2012


Yep, I'm trying to construct dtype2 programmaticaly and was hoping for some function giving me a "canonical" expression of the dtype. I've started playing with fields but it's just a bit harder than I though (lot of different cases and recursion).

Thanks for the answer.


Nicolas

On Dec 27, 2012, at 1:32 , Nathaniel Smith wrote:

> On Wed, Dec 26, 2012 at 8:09 PM, Nicolas Rougier
> <Nicolas.Rougier at inria.fr> wrote:
>> 
>> 
>> Hi all,
>> 
>> 
>> I'm looking for a way to "reduce" dtype1 into dtype2 (when it is possible of course).
>> Is there some easy way to do that by any chance ?
>> 
>> 
>> dtype1 = np.dtype( [ ('vertex',  [('x', 'f4'),
>>                                  ('y', 'f4'),
>>                                  ('z', 'f4')]),
>>                    ('normal',  [('x', 'f4'),
>>                                 ('y', 'f4'),
>>                                 ('z', 'f4')]),
>>                    ('color',   [('r', 'f4'),
>>                                 ('g', 'f4'),
>>                                 ('b', 'f4'),
>>                                 ('a', 'f4')]) ] )
>> 
>> dtype2 = np.dtype( [ ('vertex',  'f4', 3),
>>                     ('normal',  'f4', 3),
>>                     ('color',   'f4', 4)] )
>> 
> 
> If you have an array whose dtype is dtype1, and you want to convert it
> into an array with dtype2, then you just do
>  my_dtype2_array = my_dtype1_array.view(dtype2)
> 
> If you have dtype1 and you want to programmaticaly construct dtype2,
> then that's a little more fiddly and depends on what exactly you're
> trying to do, but start by poking around with dtype1.names and
> dtype1.fields, which contain information on how dtype1 is put together
> in the form of regular python structures.
> 
> -n
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list