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 186 186 denom = self.denominator() 187 187 whole, numer = self.numerator().quo_rem(denom) 188 188 factors = denom.factor() 189 if factors.unit _part!= 1:190 numer *= ~factors.unit _part()189 if factors.unit() != 1: 190 numer *= ~factors.unit() 191 191 if not self.parent().is_exact(): 192 192 # factors not grouped in this case 193 193 # TODO: think about changing the factor code itself -
a/sage/rings/polynomial/multi_polynomial_element.py
old new 1330 1330 sage: K.<a> = NumberField(x^2 + 1) 1331 1331 sage: R.<y, z> = PolynomialRing(K) 1332 1332 sage: f = 2*y^2 + 2*z^2 1333 sage: F = f.factor(); F.unit _part()1333 sage: F = f.factor(); F.unit() 1334 1334 2 1335 1335 """ 1336 1336 # I do not think this applied anymore. Or at least it's -
a/sage/rings/polynomial/multi_polynomial_libsingular.pyx
old new 3264 3264 (-2) * (c - d) * (b - d) * (b - c) * (-a + b) * (a - d) * (a - c) 3265 3265 sage: F[0][0] 3266 3266 c - d 3267 sage: F.unit _part()3267 sage: F.unit() 3268 3268 -2 3269 3269 3270 3270 Factorization of multivariate polynomials over non-prime -
a/sage/structure/factorization.py
old new 1 1 r""" 2 2 Factorizations 3 3 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$: 4 The \code{Factorization} class provides a structure for holding quite 5 general lists of objects with integer multiplicities. These may hold 6 the results of an arithmetic or algebraic factorization, where the 7 objects may be primes or irreducible polynomials and the 8 multiplicities are the (non-zero) exponents in the factorization. For 9 other types of example, see below. 10 11 \code{Factorization} class objects contain a \code{list}, so can be 12 printed nicely and be manipulated like a list of prime-exponent pairs, 13 or easily turned into a plain list. For example, we factor the 14 integer $-45$: 7 15 8 16 sage: F = factor(-45) 9 17 … … 19 27 unit part} (!). 20 28 sage: list(F) 21 29 [(3, 2), (5, 1)] 30 31 A \code{Factorization} is not actually a list: 22 32 sage: isinstance(F, list) 23 33 False 24 34 25 We can access the \code{Factorization} F itself as if it were a list:35 However, we can access the \code{Factorization} F itself as if it were a list: 26 36 sage: F[0] 27 37 (3, 2) 28 38 sage: F[1] 29 39 (5, 1) 30 40 31 To get at the unit part, use the \code{unit _part} function:32 sage: F.unit _part()41 To get at the unit part, use the \code{unit} function: 42 sage: F.unit() 33 43 -1 34 44 35 45 All factorizations are immutable. Thus if you write a function that … … 59 69 sage: expand(F) 60 70 -5*x^2 + 25*x - 30 61 71 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.72 The 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 74 is discarded by the list. 65 75 66 76 sage: list(F) 67 77 [(x - 3, 1), (x - 2, 1)] … … 79 89 -5*x^2 + 25*x - 30 80 90 sage: F = f.factor(); F 81 91 (-1) * 5 * (x - 3) * (x - 2) 92 sage: F.universe() 93 Univariate Polynomial Ring in x over Integer Ring 82 94 sage: F.unit() 83 95 -1 84 96 sage: list(F) … … 107 119 sage: type(F) 108 120 <class 'sage.structure.factorization.Factorization'> 109 121 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 110 141 TESTS: 111 142 sage: F = factor(-20); F 112 143 -1 * 2^2 * 5 … … 122 153 -- William Stein (2008-01-17): wrote much of the documentation and fixed 123 154 a couple of bugs. 124 155 -- 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 125 158 """ 126 159 127 160 #***************************************************************************** … … 136 169 137 170 import sage.misc.latex as latex 138 171 from sage.structure.sage_object import SageObject 172 from sage.structure.sequence import Sequence 139 173 140 174 class Factorization(SageObject): 141 175 """ 142 176 A formal factorization of an object. 143 177 178 NOTES: 179 144 180 EXAMPLES: 145 181 sage: N = 2006 146 182 sage: F = N.factor(); F … … 238 274 except TypeError: 239 275 raise TypeError, "powers of factors must be integers" 240 276 277 try: 278 self.__universe = Sequence(t[0] for t in x).universe() 279 except TypeError: 280 self.__universe = None 281 241 282 self.__x = [ (t[0],int(t[1])) for t in x] 242 283 if unit is None: 243 284 if len(x) > 0: 244 285 try: 245 unit = self. base_ring()(1)246 except AttributeError:286 unit = self.__universe(1) 287 except (AttributeError, TypeError): 247 288 unit = Integer(1) 248 289 else: 249 290 unit = Integer(1) … … 417 458 import copy 418 459 return Factorization(copy.deepcopy(list(self), memo), cr=self.__cr, sort=False) 419 460 420 def base_ring(self):421 """461 def universe(self): 462 r""" 422 463 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. 423 467 424 468 EXAMPLES: 425 469 sage: F = factor(2006) 426 sage: F. base_ring()470 sage: F.universe() 427 471 Integer Ring 428 472 429 473 sage: R.<x,y,z> = FreeAlgebra(QQ, 3) 430 474 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() 433 480 """ 434 if len(self) > 0:435 return self [0][0].parent()436 e lse:437 return self.unit().parent()481 try: 482 return self.__universe 483 except AttributeError: 484 return None 438 485 439 486 def is_commutative(self): 440 487 """ … … 453 500 sage: F.is_commutative() 454 501 False 455 502 sage: (F*F^-1).is_commutative() 456 True503 False 457 504 """ 458 505 try: 459 return self. base_ring().is_commutative()506 return self.universe().is_commutative() 460 507 except: 461 508 # This is not the mathematically correct default, but agrees with 462 509 # history -- we've always assumed factored things commute … … 579 626 self.__x.sort(_cmp) 580 627 581 628 def unit(self): 582 """629 r""" 583 630 Return the unit part of this factorization. 584 585 EXAMPLES:586 sage: F = factor(-2006); F587 -1 * 2 * 17 * 59588 sage: F.unit()589 -1590 """591 return self.__unit592 593 def unit_part(self):594 r"""595 Same as \code{self.unit()}.596 631 597 632 EXAMPLES: 598 633 We create a polynomial over the real double field and factor it: … … 601 636 (-2.0) * (1.0*x^2 + 0.5) 602 637 603 638 Note that the unit part of the factorization is $-2.0$. 604 sage: F.unit _part()639 sage: F.unit() 605 640 -2.0 641 642 sage: F = factor(-2006); F 643 -1 * 2 * 17 * 59 644 sage: F.unit() 645 -1 646 606 647 """ 607 648 return self.__unit 608 649 … … 672 713 return repr(self.__unit) 673 714 try: 674 715 atomic = ((isinstance(self.__x[0][0], (int, long)) or \ 675 self. base_ring().is_atomic_repr()))716 self.universe().is_atomic_repr())) 676 717 except AttributeError: 677 718 atomic = False 678 719 s = '' … … 713 754 return latex.latex(self.__unit) 714 755 try: 715 756 atomic = ((isinstance(self.__x[0][0], (int, long)) or \ 716 self. base_ring().is_atomic_repr()))757 self.universe().is_atomic_repr())) 717 758 except AttributeError: 718 759 atomic = False 719 760 s = '' … … 819 860 sage: F*F 820 861 x^3 * y^2 * x^4 * y^2 * x 821 862 sage: -1 * F 822 -1 * x^4 * y^2863 (-1) * x^3 * y^2 * x 823 864 """ 824 865 if not isinstance(other, Factorization): 825 866 return self * Factorization([(other, 1)]) … … 966 1007 s = {} 967 1008 for a in set(d1.keys()).intersection(set(d2.keys())): 968 1009 s[a] = min(d1[a],d2[a]) 969 return Factorization(list(s.iteritems()) , unit=self.base_ring()(1))1010 return Factorization(list(s.iteritems())) 970 1011 else: 971 1012 raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 972 1013 … … 989 1030 s = {} 990 1031 for a in set(d1.keys()).union(set(d2.keys())): 991 1032 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())) 993 1034 else: 994 1035 raise NotImplementedError, "gcd is not implemented for non-commutative factorizations" 995 1036 996 1037 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