Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/combinat/dict_addition.pyx
4079 views
1
"""
2
Provides function to add dictionaries pointwise with values in a common ring and to compute linear combinations
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2010 Christian Stump [email protected]
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#*****************************************************************************
10
11
def dict_addition( dict_iter ):
12
"""
13
returns the pointwise addition of dictionaries with coefficients
14
15
INPUT:
16
dict_iter -- iterator of dictionaries with values in a common ring R
17
18
OUTPUT:
19
dictionary containing all keys of dictionaries in dict_list (and non-zero values) being the the sum of the values in the different
20
dictionaries
21
22
EXAMPLES:
23
sage: from sage.combinat.dict_addition import dict_addition
24
sage: D = { 0:1, 1:1 }; D
25
{0: 1, 1: 1}
26
sage: dict_addition( D for _ in range(5) )
27
{0: 5, 1: 5}
28
"""
29
cdef dict D, D_tmp
30
31
D = {}
32
for D_tmp in dict_iter:
33
if D == {}:
34
D = D_tmp.copy()
35
else:
36
for key, value in D_tmp.iteritems():
37
if key in D:
38
D[ key ] += value
39
else:
40
D[ key ] = value
41
42
for_removal = [ key for key, value in D.iteritems() if value == 0 ]
43
for key in for_removal:
44
del D[key]
45
46
return D
47
48
def dict_linear_combination( dict_factor_iter, factor_on_left=True ):
49
"""
50
returns the pointwise addition of dictionaries with coefficients
51
52
INPUT:
53
dict_factor_iter -- iterator of pairs D, coeff, where
54
the D's are dictionaries with values in a common ring R
55
the coeff's are coefficients in R
56
factor_on_left(optional) -- if True, the coefficients are multiplied on the left, otherwise they are multiplied on the right
57
58
OUTPUT:
59
dictionary containing all keys of dictionaries in dict_list (and non-zero values) being the the sum of the values in the different
60
dictionaries each one first multiplied by the given factor
61
62
EXAMPLES:
63
sage: from sage.combinat.dict_addition import dict_linear_combination
64
sage: D = { 0:1, 1:1 }; D
65
{0: 1, 1: 1}
66
sage: dict_linear_combination( (D,i) for i in range(5) )
67
{0: 10, 1: 10}
68
sage: dict_linear_combination( [(D,1),(D,-1)] )
69
{}
70
"""
71
D = {}
72
for D_tmp, fac_tmp in dict_factor_iter:
73
if D == {} and fac_tmp == 1:
74
D = D_tmp.copy()
75
elif fac_tmp == 1:
76
for key, value in D_tmp.iteritems():
77
if key in D:
78
D[ key ] += value
79
else:
80
D[ key ] = value
81
elif fac_tmp == -1:
82
for key, value in D_tmp.iteritems():
83
if key in D:
84
D[ key ] -= value
85
else:
86
D[ key ] = -value
87
else:
88
if factor_on_left:
89
for key, value in D_tmp.iteritems():
90
if key in D:
91
D[ key ] += fac_tmp * value
92
else:
93
D[ key ] = fac_tmp * value
94
else:
95
for key, value in D_tmp.iteritems():
96
if key in D:
97
D[ key ] += value * fac_tmp
98
else:
99
D[ key ] = value * fac_tmp
100
101
for_removal = [ key for key, value in D.iteritems() if value == 0 ]
102
for key in for_removal:
103
del D[key]
104
105
return D
106
107