Ticket #10716: trac_10716_adding_weighted_degree_v2.patch

File trac_10716_adding_weighted_degree_v2.patch, 3.4 KB (added by mhampton, 2 years ago)

Cumulative patch with minor docstring fixes.

  • sage/rings/polynomial/multi_polynomial_libsingular.pyx

    # HG changeset patch
    # User Johan Sebastian Rosenkilde Nielsen <j.s.r.nielsen@mat.dtu.dk>
    # Date 1296392046 -3600
    # Node ID aab83c25fd6e3eae9eb08c78124aabab431531d0
    # Parent  f2aae6a54581c208649a23d86e25fa3626aa6c22
    Trac 10716: Adding a weighted_degree function to MPolynomial over libsingular
    
    diff -r f2aae6a54581 -r aab83c25fd6e sage/rings/polynomial/multi_polynomial_libsingular.pyx
    a b  
    199199from sage.rings.arith import gcd 
    200200from sage.structure.element import coerce_binop 
    201201 
     202from sage.modules.free_module_element import vector 
     203 
    202204from sage.structure.parent cimport Parent 
    203205from sage.structure.parent_base cimport ParentWithBase 
    204206from sage.structure.parent_gens cimport ParentWithGens 
     
    519521        Construct a new element in this polynomial ring, i.e. try to 
    520522        coerce in ``element`` if at all possible. 
    521523 
    522         INPUT:: 
     524        INPUT: 
    523525 
    524526        - ``element`` - several types are supported, see below 
    525527         
     
    22582260        cdef ring *r = (<MPolynomialRing_libsingular>self._parent)._ring 
    22592261        return singular_polynomial_deg(p,NULL,r) 
    22602262 
     2263    def weighted_degree(self, *weights): 
     2264        """ 
     2265        Return the weighted degree of ``self``, which is the maximum weighted 
     2266        degree of all monomials in ``self``; the weighted degree of a monomial 
     2267        is the sum of all powers of the variables in the monomial, each power 
     2268        multiplied with its respective weight in ``weights''. 
     2269 
     2270        TODO: This implementation is slow. It should be reimplemented to use 
     2271        the Singular interface. 
     2272 
     2273        INPUT: 
     2274 
     2275        - ``weights`` - Either individual numbers, a tuple or a dictionary, 
     2276          specifying the weights of each variable. If it is a dictionary, it 
     2277          maps each variable of ``self'' to its weight. If it is individual 
     2278          numbers or a tuple, it specified the weights in the order of the 
     2279          generators as given by ``self.parent().gens()'': 
     2280 
     2281        EXAMPLES:: 
     2282 
     2283            sage: R.<x,y,z> = GF(7)[] 
     2284            sage: p = x^3 + y + x*z^2 
     2285            sage: p.weighted_degree({z:0, x:1, y:2}) 
     2286            3 
     2287            sage: p.weighted_degree(1, 2, 0) 
     2288            3 
     2289            sage: p.weighted_degree((1, 4, 2)) 
     2290            5 
     2291            sage: p.weighted_degree((1, 4, 1)) 
     2292            4 
     2293            sage: q = R.random_element(100, 20) #random 
     2294            sage: q.weighted_degree(1,1,1) == q.total_degree()  
     2295            True 
     2296        """ 
     2297        # Make the weights into a vector so we can use dot_product 
     2298        if len(weights) ==  1: 
     2299            # First unwrap it if it is given as one element argument 
     2300            weights = weights[0] 
     2301 
     2302        if isinstance(weights, tuple):  
     2303            weights = vector(weights) 
     2304        elif isinstance(weights, dict): 
     2305            weights_list = [ weights[g] for g in self.parent().gens() ] 
     2306            weights = vector(weights_list) 
     2307        else: 
     2308            raise Exception('weights must be a tuple or a dictionary') 
     2309 
     2310        # Go through each monomial, calculating the weight by dot product 
     2311        deg = max([ weights.dot_product(vector(m.degrees())) 
     2312                        for m in self.monomials() ]) 
     2313        return deg 
     2314 
     2315 
    22612316    def degrees(self): 
    22622317        """  
    22632318        Returns a tuple with the maximal degree of each variable in