本文整理汇总了Python中numpy.bitwise_xor函数的典型用法代码示例。如果您正苦于以下问题:Python bitwise_xor函数的具体用法?Python bitwise_xor怎么用?Python bitwise_xor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了bitwise_xor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_getTraining
def test_getTraining(self):
test = np.array(Image.open(self.filename), 'uint8')
train = np.array(Image.open(self.filename), 'uint8')
np.bitwise_xor(test,train,test)
image = np.ones(test.shape,test.dtype)
le.getTraining(image,self.filename, self.filename)
assert((test == image).all(), True)
示例2: count_sensitive_neighborhood_hash
def count_sensitive_neighborhood_hash(g):
""" Compute the count sensitive neighborhood hashed
version of a graph.
"""
gnh = g.copy()
g = array_labels_to_str(g)
#iterate over every node in the graph
for node in iter(g.nodes()):
neighbors_labels = [g.node[n]["label"] for n in g.neighbors_iter(node)]
#if node has no neighboors, nh is its own label
if len(neighbors_labels) > 0:
#count number of unique labels
c = Counter(neighbors_labels)
count_weighted_neighbors_labels = []
for label, c in c.iteritems():
label = str_to_array(label)
c_bin = np.array( list(np.binary_repr( c, len(label) ) ), dtype=np.int64 )
label = np.bitwise_xor( label, c_bin)
label = np.roll( label, c )
count_weighted_neighbors_labels.append( label )
x = count_weighted_neighbors_labels[0]
for l in count_weighted_neighbors_labels[1:]:
x = np.bitwise_xor( x, l)
node_label = str_to_array(g.node[node]["label"])
csnh = np.bitwise_xor( np.roll( node_label, 1 ), x )
else:
csnh = str_to_array(g.node[node]["label"])
gnh.node[node]["label"] = csnh
return gnh
示例3: unmask
def unmask(buf, f):
s2a = lambda s: [ord(c) for c in s]
s2b = lambda s: s
pstart = f['hlen'] + 4
pend = pstart + f['length']
if numpy:
b = c = s2b('')
if f['length'] >= 4:
mask = numpy.frombuffer(buf, dtype=numpy.dtype('<u4'),
offset=f['hlen'], count=1)
data = numpy.frombuffer(buf, dtype=numpy.dtype('<u4'),
offset=pstart, count=int(f['length'] / 4))
b = numpy.bitwise_xor(data, mask).tostring()
if f['length'] % 4:
mask = numpy.frombuffer(buf, dtype=numpy.dtype('B'),
offset=f['hlen'], count=(f['length'] % 4))
data = numpy.frombuffer(buf, dtype=numpy.dtype('B'),
offset=pend - (f['length'] % 4),
count=(f['length'] % 4))
c = numpy.bitwise_xor(data, mask).tostring()
return b + c
else:
data = array.array('B')
mask = s2a(f['mask'])
data.fromstring(buf[pstart:pend])
for i in range(len(data)):
data[i] ^= mask[i % 4]
return data.tostring()
示例4: iota
def iota(ain, rnd):
# Initialize empty arrays
aout = np.zeros((5,5,64), dtype = int)
bit = np.zeros(dtype = int, shape = (5,5,64))
rc = np.zeros(dtype = int, shape = 168)
# Linear Feedback Shift Register
w = np.array([1,0,0,0,0,0,0,0], dtype = int)
rc[0] = w[0]
for i in range(1, 7*24):
a = np.bitwise_xor(w[0], w[4])
b = np.bitwise_xor(w[5], w[6])
tail = np.bitwise_xor(a, b)
w = [w[1],w[2],w[3],w[4],w[5],w[6],w[7], tail]
rc[i] = w[0]
# Calculate bits
for l in range(7):
q = pow(2, l) - 1
t = l + 7*rnd
bit[0][0][q] = rc[l + 7*rnd]
# Calculate aout
for i in range(5):
for j in range(5):
for k in range(64):
aout[i][j][k] = np.bitwise_xor(ain[i][j][k], bit[i][j][k])
return aout
示例5: reconciliate
def reconciliate(matlabEng, arrDelta, arrData_bin_2, n, k, m, strCoder):
"""
Given the delta, try to deduce data1 via reconciliation
"""
arrMsg_bin_2 = None
arrCodeword_bin_2 = None
if (strCoder == CODER_GOLAY):
n = 23
k = 12
arrMsg_bin_2 = golay.decode(matlabEng,
np.bitwise_xor(arrData_bin_2, arrDelta),
n)
arrCodeword_bin_2 = golay.encode(matlabEng, arrMsg_bin_2, k)
elif (strCoder == CODER_RS):
arrMsg_bin_2 = rs.decode(matlabEng,
np.bitwise_xor(arrData_bin_2, arrDelta),
n, k, m)
arrCodeword_bin_2 = rs.encode(matlabEng, arrMsg_bin_2, n, k, m)
elif (strCoder == CODER_HAMMING):
arrMsg_bin_2 = fec.decode(matlabEng,
np.bitwise_xor(arrData_bin_2, arrDelta),
n, k, strCoder)
arrCodeword_bin_2 = fec.encode(matlabEng, arrMsg_bin_2, n, k)
else:
raise ValueError("Unkown coder")
# deduce data 1 from data 2 + delta
arrDeducedData_bin_1 = np.bitwise_xor(arrCodeword_bin_2, arrDelta)
return arrDeducedData_bin_1
示例6: spa_model
def spa_model(plaintext, key):
hd_arr = np.zeros(16)
hw_arr = np.zeros(16)
zo_arr = np.zeros(16)
byte_list = [0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 15, 11, 7]
for byte in byte_list:
print 'byte: ' + str(byte)
plaintext_nth_byte = plaintext[byte]
# XOR the plaintext byte with key byte and put the result through the S-BOX
xored_plaintxt_key_bytes = np.bitwise_xor(plaintext_nth_byte, key[byte])
# Use the Hamming Weight model to calculate the hypothetical power consumption of
# the SBOX operation.
hw_arr[byte] = num_ones(sbox_hex[xored_plaintxt_key_bytes])
# Use the Hamming Distance model
hd_arr[byte] = num_ones(np.bitwise_xor(xored_plaintxt_key_bytes, sbox_hex[xored_plaintxt_key_bytes]))
# Other power model! 0 -> 1 transition
zo_arr[byte] = num_ones(~xored_plaintxt_key_bytes & sbox_hex[xored_plaintxt_key_bytes])
print '%x, %x, %d, %d, %d' %(xored_plaintxt_key_bytes, sbox_hex[xored_plaintxt_key_bytes], hw_arr[byte], hd_arr[byte], zo_arr[byte])
plt.plot(hd_arr, 'r')
plt.plot(hw_arr, 'b')
plt.plot(zo_arr, 'g')
plt.show()
示例7: _unmask
def _unmask(self, buf, mask):
# Unmask a frame
if numpy:
plen = len(buf)
pstart = 0
pend = plen
b = c = ''.encode('ascii')
if plen >= 4:
dtype=numpy.dtype('<u4')
if sys.byteorder == 'big':
dtype = dtype.newbyteorder('>')
mask = numpy.frombuffer(mask, dtype, count=1)
data = numpy.frombuffer(buf, dtype, count=int(plen / 4))
#b = numpy.bitwise_xor(data, mask).data
b = numpy.bitwise_xor(data, mask).tostring()
if plen % 4:
dtype=numpy.dtype('B')
if sys.byteorder == 'big':
dtype = dtype.newbyteorder('>')
mask = numpy.frombuffer(mask, dtype, count=(plen % 4))
data = numpy.frombuffer(buf, dtype,
offset=plen - (plen % 4), count=(plen % 4))
c = numpy.bitwise_xor(data, mask).tostring()
return b + c
else:
# Slower fallback
if sys.hexversion < 0x3000000:
mask = [ ord(c) for c in mask ]
data = array.array('B')
data.fromstring(buf)
for i in range(len(data)):
data[i] ^= mask[i % 4]
return data.tostring()
示例8: main
def main():
mp.figure(1)
mp.clf()
ax = mp.subplot(211)
#ax = mp.subplot(211)
#x, y = testData()
x, y, flag = jupiter1()
cin = np.arange(len(x))
#import pdb; pdb.set_trace()
idx = noise.singlePointDifferenceSigmaClip(y, 4, initialClip=flag)
mp.plot(cin[~flag],y[~flag], 'k.')
mp.plot(cin[idx], y[idx], 'ro', ms=10)
mp.plot(cin[flag], y[flag], 'gs')
mp.axvline(499, color='b')
mp.subplot(212, sharex=ax)
mp.plot(cin, y - np.roll(y, -1), 'ko-')
mp.axvline(499, color='b')
outliers = np.where(np.bitwise_xor(idx, flag))[0]
outliers = np.bitwise_xor(idx, flag).astype(int)
mp.figure(2)
mp.clf()
mp.plot(np.convolve(outliers, outliers, mode='same'))
示例9: sign_change
def sign_change(img):
img = img.reshape(28, 28)
top = numpy.bitwise_xor(img, shift_up(img))
top = [top[:, i].sum() / 255 for i in xrange(img.shape[1])]
side = numpy.bitwise_xor(img, shift_left(img))
side = [side[i, :].sum() / 255 for i in xrange(img.shape[1])]
return numpy.array(top), numpy.array(side)
示例10: detect_corruption
def detect_corruption(self, fname):
"""
single disk corruption detection
:param fname: data name in RAID6 system
:return: corrupted disk index; for p, self.N-2; for q, self.N-1
"""
# all disks, including P, Q
byte_ndarray = self._read_n(fname, self.N)
data_ndarray = byte_ndarray[:-2]
self._logger.info("byte_ndarray=\n{}".format(byte_ndarray))
P = byte_ndarray[-2:-1]
self._logger.info("p={}".format(P))
Q = byte_ndarray[-1]
self._logger.info("Q={}".format(Q))
P_prime = utils.gen_p(data_ndarray, ndim=2)
self._logger.info("P_prime={}".format(P_prime))
Q_prime = utils.gen_q(data_ndarray, ndim=2)
self._logger.info("Q_prime={}".format(Q_prime))
P_star = np.bitwise_xor(P, P_prime)
Q_star = np.bitwise_xor(Q, Q_prime)
P_nonzero = np.count_nonzero(P_star)
Q_nonzero = np.count_nonzero(Q_star)
if P_nonzero == 0 and Q_nonzero == 0:
print("no corruption")
return None
elif P_nonzero == 0 and Q_nonzero != 0:
print("Q corruption")
return self.N - 1
elif P_nonzero != 0 and Q_nonzero == 0:
print("P corruption")
return self.N - 2
else:
index = self._get_corrupted_data_disk(P_star, Q_star)
print("data disk {} corruption".format(index))
return index
示例11: chi
def chi(ain):
aout = np.zeros((5,5,64), dtype = int) # Initialize empty 5x5x64 array
for i in range(5):
for j in range(5):
for k in range(64):
xor = np.bitwise_xor(ain[(i+1)%5][j][k], 1)
mul = xor * (ain[(i+2)%5][j][k])
aout[i][j][k] = np.bitwise_xor(ain[i][j][k], mul)
return aout
示例12: train_and_test
def train_and_test(X_train,Y_train,X_test,Y_test,dtype,alpha,beta_percentile,cv=True):
k = X_train.shape[0]
n = X_test.shape[0]
A = np.zeros(shape=(k,k))
lambd = 1
part1 = np.column_stack((X_train['guarantee_type'], X_train['gender']))
part2 = np.column_stack((X_train['age'], X_train['account_value'], X_train['withdrawal_rate'],X_train['maturity']))
for i in range(k):
tmp = np.array([X_train[i],]*k, dtype=dtype)
tmp1 = np.column_stack((tmp['guarantee_type'], tmp['gender'])) #copy this row k times
tmp2 = np.column_stack((tmp['age'], tmp['account_value'], tmp['withdrawal_rate'],tmp['maturity']))
result1 = lambd*np.sum(np.bitwise_xor(part1, tmp1), axis=1)
result2 = np.sum((part2-tmp2)**2,axis=1)
A[i] = np.sqrt(result1+result2)
# tmp = np.triu(A)
# tmp = tmp[tmp!=0]
max_dist = A.max()
B = np.zeros(shape=(k+1,k+1))
B[k] = B[:,k] = 1
B[k,k] = 0
part1 = np.column_stack((X_test['guarantee_type'], X_test['gender']))
part2 = np.column_stack((X_test['age'], X_test['account_value'], X_test['withdrawal_rate'],X_test['maturity']))
weight_mat = np.zeros((k+1,n))
weight_mat[k] = 1
beta = max_dist*beta_percentile
for j in range(k):
B[j][:k] = alpha+np.exp(-3*A[j]/beta)
tmp = np.array([X_train[j],]*n, dtype=dtype)
tmp1 = np.column_stack((tmp['guarantee_type'], tmp['gender'])) #copy this row k times
tmp2 = np.column_stack((tmp['age'], tmp['account_value'], tmp['withdrawal_rate'],tmp['maturity']))
result1 = lambd*np.sum(np.bitwise_xor(part1, tmp1), axis=1)
result2 = np.sum((part2-tmp2)**2,axis=1)
weight_mat[j] = alpha+np.exp(np.sqrt(result1+result2)*(-3/beta))
Y_train = np.append(Y_train, 0)
const = np.dot(np.linalg.inv(B),Y_train)
Y_hat = np.dot(const, weight_mat)
if cv == True:
RMSE = np.sqrt(sum((Y_test-Y_hat)**2)/n)
return RMSE
else:
RMSE = np.sqrt(sum((Y_test-Y_hat)**2)/(n+k))
MAD = sum(abs(Y_test-Y_hat))/(n+k)
total = sum(Y_hat)+sum(Y_train)
benchmark = sum(Y_test)+sum(Y_train)
APD = abs(total-benchmark)
RPD = APD/benchmark
return APD,RPD,MAD,RMSE
示例13: xor_arrays
def xor_arrays(cls, source, target):
"""
Moved to its own function for profiling purposes.
May want to consider moving out to inline.
The source array will be xored into place into
the target array
Arguments:
source -- Numpy array source array
target -- Numpy array target array
"""
numpy.bitwise_xor(source, target, target)
示例14: key_expansion
def key_expansion(self, cipher_key):
if self.is_verbose:
print "Computing key schedule..."
key_schedule = np.copy(cipher_key)
n_k = len(cipher_key)
n_r = n_k + 6
if self.is_verbose:
print
print " After After Rcon XOR"
print " i Previous RotWord SubWord Value w/ Rcon w[i-Nk] Final"
print "=== ======== ======== ======== ======== ======== ======== ========"
for i in range(n_k, N_B * (n_r + 1)):
prev = key_schedule[i - 1]
first = key_schedule[i - n_k]
temp = prev
after_rot_word = None
after_sub_word = None
rcon_val = None
after_xor_rcon = None
if i % n_k == 0:
after_rot_word = rot_word(prev)
after_sub_word = sub_word(after_rot_word)
rcon_val = rcon(i / n_k)
after_xor_rcon = np.bitwise_xor(after_sub_word, rcon_val)
temp = after_xor_rcon
elif n_k > 6 and i % n_k == 4:
after_sub_word = sub_word(prev)
temp = after_sub_word
final = np.bitwise_xor(first, temp)
key_schedule = np.append(key_schedule, final.reshape(1, 4), axis=0)
if self.is_verbose:
print "{:02}: {} {} {} {} {} {} {}".format(
i,
w2s(prev),
w2s(after_rot_word),
w2s(after_sub_word),
w2s(rcon_val),
w2s(after_xor_rcon),
w2s(first),
w2s(final),
)
if self.is_verbose:
print
return key_schedule, n_k, n_r
示例15: expand_key
def expand_key(self, key):
self.round_keys = []
self.round_keys.append(key)
for i in range(1, NUM_ROUNDS+1):
# Always transpose, words are columns and not rows
previous_key = self.round_keys[i-1].transpose()
round_key = np.zeros(key.shape, dtype=np.uint8)
round_key[0] = np.bitwise_xor(self.g(previous_key[3], i),
previous_key[0])
round_key[1] = np.bitwise_xor(round_key[0], previous_key[1])
round_key[2] = np.bitwise_xor(round_key[1], previous_key[2])
round_key[3] = np.bitwise_xor(round_key[2], previous_key[3])
self.round_keys.append(round_key.transpose())