Dictionary convolution
This commit is contained in:
parent
2ffce67867
commit
c5300991d5
@ -144,7 +144,6 @@ def remove_in_dict(d, value = 0):
|
|||||||
return new_dict
|
return new_dict
|
||||||
|
|
||||||
def convolution_dict(D1, D2, op = lambda x,y:x*y,\
|
def convolution_dict(D1, D2, op = lambda x,y:x*y,\
|
||||||
op_key = lambda x,y: x + y, \
|
|
||||||
commutative = True, op_twice = lambda x,y: x + y):
|
commutative = True, op_twice = lambda x,y: x + y):
|
||||||
"""Convolution of two dictionaries
|
"""Convolution of two dictionaries
|
||||||
|
|
||||||
@ -171,17 +170,14 @@ def convolution_dict(D1, D2, op = lambda x,y:x*y,\
|
|||||||
|
|
||||||
for k1 in sorted(D1.keys()):
|
for k1 in sorted(D1.keys()):
|
||||||
for k2 in sorted(D2.keys()):
|
for k2 in sorted(D2.keys()):
|
||||||
if op_key(k1,k2) in new_dict.keys():
|
if k1+k2 in new_dict.keys():
|
||||||
key = op_key(k1,k2)
|
new_dict[k1+k2] = op_twice(new_dict[k1+k2], op(D1[k1],D2[k2]))
|
||||||
new_dict[key] = op_twice(new_dict[key], op(D1[k1],D2[k2]))
|
|
||||||
|
|
||||||
elif op_key(k2,k1) in new_dict.keys() and commutative:
|
elif k2+k1 in new_dict.keys() and commutative:
|
||||||
key = op_key(k1,k2)
|
new_dict[k2+k1] = op_twice(new_dict[k2+k1], op(D1[k1],D2[k2]))
|
||||||
new_dict[key] = op_twice(new_dict[key], op(D1[k1],D2[k2]))
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
key = op_key(k1,k2)
|
new_dict[k1+k2] = op(D1[k1],D2[k2])
|
||||||
new_dict[key] = op(D1[k1],D2[k2])
|
|
||||||
|
|
||||||
return new_dict
|
return new_dict
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user