Skip to content Skip to sidebar Skip to footer

Is The UPDATEIFCOPY Flag Ever True?

I'm trying to deeper understand numpy arrays; in particular memory layout / ownership / sharing related aspects. In that endeavour I stumbled across the UPDATEIFCOPY flag which sou

Solution 1:

You can set if when you use np.nditer (example taken from NumPy source code):

>>> import numpy as np
>>> a = np.zeros((6*4+1,), dtype='i1')[1:]
>>> a.dtype = 'f4'
>>> a[:] = np.arange(6, dtype='f4')
>>> i = np.nditer(a, [], [['readwrite', 'updateifcopy', 'aligned']])
>>> print(i.operands[0].flags)
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : True     # <--- :-)

However I don't know under what circumstances this is really set because if I remove the first two lines then it doesn't work anymore:

>>> import numpy as np

>>> a = np.arange(6, dtype='f4')
>>> i = np.nditer(a, [], [['readwrite', 'updateifcopy', 'aligned']])
>>> print(i.operands[0].flags)
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False     # <--- :-(

Solution 2:

The UPDATEIFCOPY flag can never be set to True.

UPDATE

If an array does not own its own memory, then the base attribute returns the object whose memory this array is referencing.

The returned object may not be the original allocator of the memory, but may be borrowing it from still another object. If this array does own its own memory, then None is returned unless the UPDATEIFCOPY flag is True in which case self.base is the array that will be updated when self is deleted.

UPDATEIFCOPY gets set automatically for an array that is created as a behaved copy of a general array. The intent is for the misaligned array to get any changes that occur to the copy.


Post a Comment for "Is The UPDATEIFCOPY Flag Ever True?"