Ticket #3927: sage-trac3927a.patch

File sage-trac3927a.patch, 2.6 kB (added by cremona, 5 months ago)
  • a/sage/structure/factorization.py

    old new  
    828828            d2 = dict(other) 
    829829            s = {} 
    830830            for a in set(d1.keys()).union(set(d2.keys())): 
    831                 s[a] = 0 
    832                 if d1.has_key(a): 
    833                     s[a] += d1[a] 
    834                 if d2.has_key(a): 
    835                     s[a] += d2[a] 
     831                s[a] = d1.get(a,0) + d2.get(a,0) 
    836832            return Factorization(list(s.iteritems()), unit=self.unit()*other.unit()) 
    837833        else: 
    838834            return Factorization(list(self) + list(other), unit=self.unit()*other.unit()) 
     
    951947        """ 
    952948        return self.value() 
    953949         
     950    def gcd(self, other): 
     951        r""" 
     952        Return the gcd of two factorizations. 
     953 
     954        EXAMPLES: 
     955            sage: factor(-30).gcd(factor(-160)) 
     956            2 * 5 
     957            sage: factor(gcd(-30,160)) 
     958            2 * 5 
     959 
     960        """ 
     961        if not isinstance(other, Factorization): 
     962            return self * Factorization([(other, 1)]) 
     963        if self.is_commutative() and other.is_commutative(): 
     964            d1 = dict(self) 
     965            d2 = dict(other) 
     966            s = {} 
     967            for a in set(d1.keys()).intersection(set(d2.keys())): 
     968                s[a] = min(d1[a],d2[a]) 
     969            return Factorization(list(s.iteritems()), unit=self.base_ring()(1)) 
     970        else: 
     971            raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 
     972 
     973    def lcm(self, other): 
     974        r""" 
     975        Return the lcm of two factorizations. 
     976 
     977        EXAMPLES: 
     978            sage: factor(-10).lcm(factor(-16)) 
     979            2^4 * 5 
     980            sage: factor(lcm(-10,16)) 
     981            2^4 * 5 
     982 
     983        """ 
     984        if not isinstance(other, Factorization): 
     985            return self * Factorization([(other, 1)]) 
     986        if self.is_commutative() and other.is_commutative(): 
     987            d1 = dict(self) 
     988            d2 = dict(other) 
     989            s = {} 
     990            for a in set(d1.keys()).union(set(d2.keys())): 
     991                s[a] = max(d1.get(a,0),d2.get(a,0)) 
     992            return Factorization(list(s.iteritems()), unit=self.base_ring()(1)) 
     993        else: 
     994            raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 
     995 
    954996