Ticket #3927: sage-trac3927b.patch

File sage-trac3927b.patch, 11.3 kB (added by cremona, 5 months ago)
  • a/sage/rings/fraction_field_element.py

    old new  
    186186        denom = self.denominator() 
    187187        whole, numer = self.numerator().quo_rem(denom) 
    188188        factors = denom.factor() 
    189         if factors.unit_part != 1: 
    190             numer *= ~factors.unit_part() 
     189        if factors.unit() != 1: 
     190            numer *= ~factors.unit() 
    191191        if not self.parent().is_exact(): 
    192192            # factors not grouped in this case 
    193193            # TODO: think about changing the factor code itself 
  • a/sage/rings/polynomial/multi_polynomial_element.py

    old new  
    13301330            sage: K.<a> = NumberField(x^2 + 1) 
    13311331            sage: R.<y, z> = PolynomialRing(K) 
    13321332            sage: f = 2*y^2 + 2*z^2 
    1333             sage: F = f.factor(); F.unit_part() 
     1333            sage: F = f.factor(); F.unit() 
    13341334            2 
    13351335        """ 
    13361336        # I do not think this applied anymore.  Or at least it's 
  • a/sage/rings/polynomial/multi_polynomial_libsingular.pyx

    old new  
    32643264            (-2) * (c - d) * (b - d) * (b - c) * (-a + b) * (a - d) * (a - c) 
    32653265            sage: F[0][0] 
    32663266            c - d 
    3267             sage: F.unit_part() 
     3267            sage: F.unit() 
    32683268            -2 
    32693269 
    32703270        Factorization of multivariate polynomials over non-prime 
  • a/sage/structure/factorization.py

    old new  
    11r""" 
    22Factorizations 
    33 
    4 The \code{Factorization} class derives from \code{list}, so it can 
    5 print nicely and be manipulated like a list of prime-exponent pairs or 
    6 easily turned into a list.  For example, we factor the integer $-45$: 
     4The \code{Factorization} class provides a structure for holding quite 
     5general lists of objects with integer multiplicities.  These may hold 
     6the results of an arithmetic or algebraic factorization, where the 
     7objects may be primes or irreducible polynomials and the 
     8multiplicities are the (non-zero) exponents in the factorization.  For 
     9other types of example, see below. 
     10 
     11\code{Factorization} class objects contain a \code{list}, so can be 
     12printed nicely and be manipulated like a list of prime-exponent pairs, 
     13or easily turned into a plain list.  For example, we factor the 
     14integer $-45$: 
    715 
    816    sage: F = factor(-45) 
    917 
     
    1927unit part} (!). 
    2028    sage: list(F) 
    2129    [(3, 2), (5, 1)] 
     30 
     31A \code{Factorization} is not actually a list: 
    2232    sage: isinstance(F, list) 
    2333    False 
    2434 
    25 We can access the \code{Factorization} F itself as if it were a list: 
     35However, we can access the \code{Factorization} F itself as if it were a list: 
    2636    sage: F[0] 
    2737    (3, 2) 
    2838    sage: F[1] 
    2939    (5, 1) 
    3040 
    31 To get at the unit part, use the \code{unit_part} function: 
    32     sage: F.unit_part() 
     41To get at the unit part, use the \code{unit} function: 
     42    sage: F.unit() 
    3343    -1 
    3444 
    3545All factorizations are immutable.  Thus if you write a function that 
     
    5969    sage: expand(F)    
    6070    -5*x^2 + 25*x - 30 
    6171 
    62 The underlying list is the list of pairs $(p_i, e_i)$, where $p_i$ 
    63 is prime and $e_i$ is an integer. The unit part is discarded by 
    64 the list. 
     72The underlying list is the list of pairs $(p_i, e_i)$, where each 
     73$p_i$ is a 'prime' and each $e_i$ is an integer. The unit part 
     74is discarded by the list. 
    6575 
    6676    sage: list(F) 
    6777    [(x - 3, 1), (x - 2, 1)] 
     
    7989    -5*x^2 + 25*x - 30 
    8090    sage: F = f.factor(); F 
    8191    (-1) * 5 * (x - 3) * (x - 2) 
     92    sage: F.universe() 
     93    Univariate Polynomial Ring in x over Integer Ring 
    8294    sage: F.unit() 
    8395    -1 
    8496    sage: list(F) 
     
    107119    sage: type(F) 
    108120    <class 'sage.structure.factorization.Factorization'> 
    109121 
     122 
     123    sage: K.<a> = NumberField(x^2 + 3); K 
     124    Number Field in a with defining polynomial x^2 + 3 
     125    sage: f = K.factor(15); f                                   
     126    (Fractional ideal (1/2*a - 3/2))^2 * (Fractional ideal (5)) 
     127    sage: f.universe() 
     128    Monoid of ideals of Number Field in a with defining polynomial x^2 + 3 
     129    sage: f.unit() 
     130    Fractional ideal (1) 
     131    sage: g=K.factor(9); g 
     132    (Fractional ideal (1/2*a - 3/2))^4 
     133    sage: f.lcm(g) 
     134    (Fractional ideal (1/2*a - 3/2))^4 * (Fractional ideal (5)) 
     135    sage: f.gcd(g) 
     136    (Fractional ideal (1/2*a - 3/2))^2 
     137    sage: f.is_integral() 
     138    True 
     139 
     140 
    110141TESTS: 
    111142    sage: F = factor(-20); F 
    112143    -1 * 2^2 * 5 
     
    122153    -- William Stein (2008-01-17): wrote much of the documentation and fixed 
    123154                                   a couple of bugs. 
    124155    -- Nick Alexander (2008-01-19): added support for non-commuting factors. 
     156    -- John Cremona (2008-08-22): added division, lcm, gcd, 
     157                                  is_integral and universe functions 
    125158""" 
    126159 
    127160#***************************************************************************** 
     
    136169 
    137170import sage.misc.latex as latex 
    138171from sage.structure.sage_object import SageObject 
     172from sage.structure.sequence import Sequence 
    139173 
    140174class Factorization(SageObject): 
    141175    """ 
    142176    A formal factorization of an object.  
    143177     
     178    NOTES:   
     179 
    144180    EXAMPLES: 
    145181        sage: N = 2006 
    146182        sage: F = N.factor(); F 
     
    238274                except TypeError: 
    239275                    raise TypeError, "powers of factors must be integers" 
    240276 
     277        try: 
     278            self.__universe = Sequence(t[0] for t in x).universe() 
     279        except TypeError: 
     280            self.__universe = None 
     281 
    241282        self.__x = [ (t[0],int(t[1])) for t in x] 
    242283        if unit is None: 
    243284            if len(x) > 0: 
    244285                try: 
    245                     unit = self.base_ring()(1) 
    246                 except AttributeError
     286                    unit = self.__universe(1) 
     287                except (AttributeError, TypeError)
    247288                    unit = Integer(1) 
    248289            else: 
    249290                unit = Integer(1) 
     
    417458        import copy 
    418459        return Factorization(copy.deepcopy(list(self), memo), cr=self.__cr, sort=False) 
    419460 
    420     def base_ring(self): 
    421         """ 
     461    def universe(self): 
     462        r""" 
    422463        Return the parent structure of my factors. 
     464 
     465        NOTE: This used to be called \code{base_ring}, but the 
     466        universe of a factorization need not be a ring. 
    423467 
    424468        EXAMPLES: 
    425469            sage: F = factor(2006) 
    426             sage: F.base_ring() 
     470            sage: F.universe() 
    427471            Integer Ring 
    428472 
    429473            sage: R.<x,y,z> = FreeAlgebra(QQ, 3) 
    430474            sage: F = Factorization([(z, 2)], 3) 
    431             sage: (F*F^-1).base_ring() 
    432             Rational Field 
     475            sage: (F*F^-1).universe() 
     476            Free Algebra on 3 generators (x, y, z) over Rational Field 
     477 
     478            sage: F = ModularSymbols(11,4).factorization() 
     479            sage: F.universe() 
    433480        """ 
    434         if len(self) > 0
    435             return self[0][0].parent() 
    436         else
    437             return self.unit().parent() 
     481        try
     482            return self.__universe 
     483        except AttributeError
     484            return None 
    438485 
    439486    def is_commutative(self): 
    440487        """ 
     
    453500            sage: F.is_commutative() 
    454501            False 
    455502            sage: (F*F^-1).is_commutative() 
    456             Tru
     503            Fals
    457504        """ 
    458505        try: 
    459             return self.base_ring().is_commutative() 
     506            return self.universe().is_commutative() 
    460507        except: 
    461508            # This is not the mathematically correct default, but agrees with 
    462509            # history -- we've always assumed factored things commute 
     
    579626        self.__x.sort(_cmp) 
    580627 
    581628    def unit(self): 
    582         """ 
     629        r""" 
    583630        Return the unit part of this factorization. 
    584  
    585         EXAMPLES: 
    586             sage: F = factor(-2006); F 
    587             -1 * 2 * 17 * 59 
    588             sage: F.unit() 
    589             -1 
    590         """ 
    591         return self.__unit 
    592  
    593     def unit_part(self): 
    594         r""" 
    595         Same as \code{self.unit()}. 
    596631 
    597632        EXAMPLES: 
    598633        We create a polynomial over the real double field and factor it: 
     
    601636            (-2.0) * (1.0*x^2 + 0.5) 
    602637 
    603638        Note that the unit part of the factorization is $-2.0$.  
    604             sage: F.unit_part() 
     639            sage: F.unit() 
    605640            -2.0         
     641 
     642            sage: F = factor(-2006); F 
     643            -1 * 2 * 17 * 59 
     644            sage: F.unit() 
     645            -1 
     646 
    606647       """ 
    607648        return self.__unit 
    608649 
     
    672713            return repr(self.__unit) 
    673714        try: 
    674715            atomic = ((isinstance(self.__x[0][0], (int, long)) or \ 
    675                        self.base_ring().is_atomic_repr())) 
     716                       self.universe().is_atomic_repr())) 
    676717        except AttributeError: 
    677718            atomic = False 
    678719        s = '' 
     
    713754            return latex.latex(self.__unit) 
    714755        try: 
    715756            atomic = ((isinstance(self.__x[0][0], (int, long)) or \ 
    716                        self.base_ring().is_atomic_repr())) 
     757                       self.universe().is_atomic_repr())) 
    717758        except AttributeError: 
    718759            atomic = False 
    719760        s = '' 
     
    819860            sage: F*F 
    820861            x^3 * y^2 * x^4 * y^2 * x 
    821862            sage: -1 * F 
    822             -1 * x^4 * y^2 
     863            (-1) * x^3 * y^2 * x 
    823864        """ 
    824865        if not isinstance(other, Factorization): 
    825866            return self * Factorization([(other, 1)]) 
     
    9661007            s = {} 
    9671008            for a in set(d1.keys()).intersection(set(d2.keys())): 
    9681009                s[a] = min(d1[a],d2[a]) 
    969             return Factorization(list(s.iteritems()), unit=self.base_ring()(1)
     1010            return Factorization(list(s.iteritems())
    9701011        else: 
    9711012            raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 
    9721013 
     
    9891030            s = {} 
    9901031            for a in set(d1.keys()).union(set(d2.keys())): 
    9911032                s[a] = max(d1.get(a,0),d2.get(a,0)) 
    992             return Factorization(list(s.iteritems()), unit=self.base_ring()(1)
     1033            return Factorization(list(s.iteritems())
    9931034        else: 
    9941035            raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 
    9951036 
    9961037         
     1038    def is_integral(self): 
     1039        r""" 
     1040        Return True iff all exponents of this Factorization are non-negative 
     1041 
     1042        EXAMPLES: 
     1043            sage: F = factor(-10); F 
     1044            -1 * 2 * 5 
     1045            sage: F.is_integral() 
     1046            True 
     1047 
     1048            sage: F = factor(-10) / factor(16); F 
     1049            -1 * 2^-3 * 5 
     1050            sage: F.is_integral() 
     1051            False 
     1052 
     1053        """ 
     1054        return all([t[1] >=0 for t in self.__x]) 
     1055 
     1056