Ticket #13727: trac_13727_dict_addition_doc-cs.patch

File trac_13727_dict_addition_doc-cs.patch, 6.7 KB (added by stumpc5, 8 years ago)
  • doc/en/reference/combinat/index.rst

    # HG changeset patch
    # User Christian Stump <christian.stump at gmail.com>
    # Date 1352826820 -3600
    # Node ID 6649e8cbd0859a38b436f52dc7f7fad8ef591a34
    # Parent  8b31ca975b7726025a2ff646e261d8744bd81857
    13727 improvement of documentation, and some minor other changes
    
    diff --git a/doc/en/reference/combinat/index.rst b/doc/en/reference/combinat/index.rst
    a b Combinatorics 
    6767   words
    6868   iet
    6969
     70   ../sage/combinat/dict_addition
    7071   ../sage/combinat/misc
    7172
  • sage/combinat/dict_addition.pyx

    diff --git a/sage/combinat/dict_addition.pyx b/sage/combinat/dict_addition.pyx
    a b  
    1 """
     1r"""
     2Pointwise addition of dictionaries
     3
    24Provides function to add dictionaries pointwise with values in a common ring and to compute linear combinations
     5
     6EXAMPLES::
     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}
    312"""
    413#*****************************************************************************
    514#       Copyright (C) 2010 Christian Stump christian.stump@univie.ac.at
    Provides function to add dictionaries po 
    716#  Distributed under the terms of the GNU General Public License (GPL)
    817#                  http://www.gnu.org/licenses/
    918#*****************************************************************************
     19from cpython cimport PyDict_Copy
    1020
    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        
     21cpdef 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
    1827    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:
     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
    2333        sage: from sage.combinat.dict_addition import dict_addition
    2434        sage: D = { 0:1, 1:1 }; D
    2535        {0: 1, 1: 1}
    2636        sage: dict_addition( D for _ in range(5) )
    2737        {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}
    2842    """
    2943    cdef dict D, D_tmp
    3044
    3145    D = {}
    3246    for D_tmp in dict_iter:
    3347        if D == {}:
    34             D = D_tmp.copy()
    35         else:   
    36             for key, value in D_tmp.iteritems():
     48            D = PyDict_Copy( D_tmp )
     49        else:
     50            for key in D_tmp:
     51                value = D_tmp[key]
    3752                if key in D:
    38                     D[ key ] += value
     53                    D[key] += value
    3954                else:
    40                     D[ key ]  = value
    41 
    42     for_removal = [ key for key, value in D.iteritems() if value == 0 ]
     55                    D[key]  = value
     56    for_removal = [key for key in D if not D[key]]
    4357    for key in for_removal:
    44         del D[key]   
    45 
     58        del D[key]
    4659    return D
    4760
    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        
     61cpdef 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
    5873    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:
     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
    6379        sage: from sage.combinat.dict_addition import dict_linear_combination
    6480        sage: D = { 0:1, 1:1 }; D
    6581        {0: 1, 1: 1}
    def dict_linear_combination( dict_factor 
    7187    D = {}
    7288    for D_tmp, fac_tmp in dict_factor_iter:
    7389        if D == {} and fac_tmp == 1:
    74             D = D_tmp.copy()
     90            D = PyDict_Copy(D_tmp)
    7591        elif fac_tmp == 1:
    76             for key, value in D_tmp.iteritems():
     92            for key in D_tmp:
     93                value = D_tmp[key]
    7794                if key in D:
    7895                    D[ key ] += value
    7996                else:
    8097                    D[ key ]  = value
    8198        elif fac_tmp == -1:
    82             for key, value in D_tmp.iteritems():
     99            for key in D_tmp:
     100                value = D_tmp[key]
    83101                if key in D:
    84                     D[ key ]  -= value
     102                    D[ key ] -= value
    85103                else:
    86                     D[ key ]   = -value
     104                    D[ key ]  = -value
    87105        else:
    88106            if factor_on_left:
    89                 for key, value in D_tmp.iteritems():
     107                for key in D_tmp:
     108                    value = D_tmp[key]
    90109                    if key in D:
    91110                        D[ key ] += fac_tmp * value
    92111                    else:
    93112                        D[ key ]  = fac_tmp * value
    94113            else:
    95                 for key, value in D_tmp.iteritems():
     114                for key in D_tmp:
     115                    value = D_tmp[key]
    96116                    if key in D:
    97117                        D[ key ] += value * fac_tmp
    98118                    else:
    99119                        D[ key ]  = value * fac_tmp
    100120
    101     for_removal = [ key for key, value in D.iteritems() if value == 0 ]
     121    for_removal = [key for key in D if not D[key]]
    102122    for key in for_removal:
    103         del D[key]   
     123        del D[key]
    104124
    105125    return D