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