Numpy.itwise_and.Reduce si comporta in modo imprevisto?
-
22-12-2019 - |
Domanda
the ufunc.reduce
per Numpy.ByWise_and.Reduce non sembra comportarsi correttamente ... Io mi sto male?
>>> import numpy as np
>>> x = [0x211f,0x1013,0x1111]
>>> np.bitwise_or.accumulate(x)
array([ 8479, 12575, 12575])
>>> np.bitwise_and.accumulate(x)
array([8479, 19, 17])
>>> '%04x' % np.bitwise_or.reduce(x)
'311f'
>>> '%04x' % np.bitwise_and.reduce(x)
'0001'
.
Il risultato del reduce()
dovrebbe essere l'ultimo valore di accumulate()
e non lo è.Cosa mi manca qui?
Per il momento, posso aggirare usando l'identità dell'identità di Demorgan (scambiatura o e e, e rovesciare input e output):
>>> ~np.bitwise_or.reduce(np.invert(x))
17
. Soluzione
Secondo la documentazione fornita, ufunc.reduce
utilizza op.identity
come valore iniziale.
numpy.bitwise_and.identity
è 1
, non 0xffffffff....
né -1
.
>>> np.bitwise_and.identity
1
.
Così numpy.bitwise_and.reduce([0x211f,0x1013,0x1111])
è equivalente a:
>>> np.bitwise_and(np.bitwise_and(np.bitwise_and(1, 0x211f), 0x1013), 0x1111)
1
>>> 1 & 0x211f & 0x1013 & 0x1111
1
.
.
>>> -1 & 0x211f & 0x1013 & 0x1111
17
.
Non sembra esserci modo di specificare il valore iniziale in base alla documentazione.(A differenza di Python Buibin Function reduce
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow