173
173
universe functions
174
174
"""
175
175
176
- #* ****************************************************************************
176
+ # ****************************************************************************
177
177
# Copyright (C) 2005 William Stein <[email protected] >
178
178
#
179
179
# This program is free software: you can redistribute it and/or modify
180
180
# it under the terms of the GNU General Public License as published by
181
181
# the Free Software Foundation, either version 2 of the License, or
182
182
# (at your option) any later version.
183
- # http ://www.gnu.org/licenses/
184
- #* ****************************************************************************
183
+ # https ://www.gnu.org/licenses/
184
+ # ****************************************************************************
185
185
186
186
from sage .structure .sage_object import SageObject
187
187
from sage .structure .element import Element
@@ -465,7 +465,7 @@ def __copy__(self):
465
465
# No need to sort, since the factorization is already sorted
466
466
# in whatever order is desired.
467
467
return Factorization (self .__x , unit = self .__unit , cr = self .__cr ,
468
- sort = False , simplify = False )
468
+ sort = False , simplify = False )
469
469
470
470
def __deepcopy__ (self , memo ):
471
471
r"""
@@ -567,9 +567,9 @@ def base_change(self, U):
567
567
try :
568
568
return Factorization ([(U (f [0 ]), f [1 ]) for f in list (self )], unit = U (self .unit ()))
569
569
except TypeError :
570
- raise TypeError ("Impossible to coerce the factors of %s into %s" % (self , U ))
570
+ raise TypeError ("Impossible to coerce the factors of %s into %s" % (self , U ))
571
571
572
- def is_commutative (self ):
572
+ def is_commutative (self ) -> bool :
573
573
"""
574
574
Return True if my factors commute.
575
575
@@ -837,17 +837,17 @@ def _repr_(self):
837
837
n = self .__x [i ][1 ]
838
838
if not atomic and (n != 1 or len (self ) > 1 or self .__unit != one ):
839
839
if '+' in t or '-' in t or ' ' in t :
840
- t = '(%s)' % t
840
+ t = '(%s)' % t
841
841
if n != 1 :
842
- t += '^%s' % n
842
+ t += '^%s' % n
843
843
s += t
844
- if i < len (self )- 1 :
844
+ if i < len (self ) - 1 :
845
845
s += mul
846
846
if self .__unit != one :
847
847
if atomic :
848
848
u = repr (self .__unit )
849
849
else :
850
- u = '(%s)' % self .__unit
850
+ u = '(%s)' % self .__unit
851
851
s = u + mul + s
852
852
return s
853
853
@@ -877,18 +877,18 @@ def _latex_(self):
877
877
for i in range (len (self )):
878
878
t = self .__x [i ][0 ]._latex_ ()
879
879
if not atomic and ('+' in t or '-' in t or ' ' in t ):
880
- t = '(%s)' % t
880
+ t = '(%s)' % t
881
881
n = self .__x [i ][1 ]
882
882
if n != 1 :
883
- t += '^{%s}' % n
883
+ t += '^{%s}' % n
884
884
s += t
885
- if i < len (self )- 1 :
885
+ if i < len (self ) - 1 :
886
886
s += ' \\ cdot '
887
887
if self .__unit != 1 :
888
888
if atomic :
889
889
u = self .__unit ._latex_ ()
890
890
else :
891
- u = '\\ left(%s\\ right)' % self .__unit ._latex_ ()
891
+ u = '\\ left(%s\\ right)' % self .__unit ._latex_ ()
892
892
s = u + ' \\ cdot ' + s
893
893
return s
894
894
@@ -1073,17 +1073,17 @@ def __mul__(self, other):
1073
1073
self = self .base_change (U )
1074
1074
other = other .base_change (U )
1075
1075
except TypeError :
1076
- raise TypeError ("Cannot multiply %s and %s because they cannot be coerced into a common universe" % (self ,other ))
1076
+ raise TypeError ("Cannot multiply %s and %s because they cannot be coerced into a common universe" % (self , other ))
1077
1077
1078
1078
if self .is_commutative () and other .is_commutative ():
1079
1079
d1 = dict (self )
1080
1080
d2 = dict (other )
1081
1081
s = {}
1082
1082
for a in set (d1 ).union (set (d2 )):
1083
- s [a ] = d1 .get (a ,0 ) + d2 .get (a ,0 )
1084
- return Factorization (list (s .items ()), unit = self .unit ()* other .unit ())
1083
+ s [a ] = d1 .get (a , 0 ) + d2 .get (a , 0 )
1084
+ return Factorization (list (s .items ()), unit = self .unit () * other .unit ())
1085
1085
else :
1086
- return Factorization (list (self ) + list (other ), unit = self .unit ()* other .unit ())
1086
+ return Factorization (list (self ) + list (other ), unit = self .unit () * other .unit ())
1087
1087
1088
1088
def __pow__ (self , n ):
1089
1089
"""
@@ -1123,7 +1123,8 @@ def __pow__(self, n):
1123
1123
if n == 0 :
1124
1124
return Factorization ([])
1125
1125
if self .is_commutative ():
1126
- return Factorization ([(p , n * e ) for p , e in self ], unit = self .unit ()** n , cr = self .__cr , sort = False , simplify = False )
1126
+ return Factorization ([(p , n * e ) for p , e in self ], unit = self .unit ()** n ,
1127
+ cr = self .__cr , sort = False , simplify = False )
1127
1128
if n < 0 :
1128
1129
self = ~ self
1129
1130
n = - n
@@ -1147,8 +1148,8 @@ def __invert__(self):
1147
1148
sage: F^-1 # optional - sage.combinat sage.modules
1148
1149
(1/2) * x^-1 * y^-2 * x^-3
1149
1150
"""
1150
- return Factorization ([(p ,- e ) for p ,e in reversed (self )],
1151
- cr = self ._cr (), unit = self .unit ()** (- 1 ))
1151
+ return Factorization ([(p , - e ) for p , e in reversed (self )],
1152
+ cr = self ._cr (), unit = self .unit ()** (- 1 ))
1152
1153
1153
1154
def __truediv__ (self , other ):
1154
1155
r"""
@@ -1174,6 +1175,41 @@ def __truediv__(self, other):
1174
1175
return self / Factorization ([(other , 1 )])
1175
1176
return self * other ** - 1
1176
1177
1178
+ def __call__ (self , * args , ** kwds ):
1179
+ """
1180
+ Implement the substitution.
1181
+
1182
+ There is another mechanism for substitution
1183
+ in symbolic products.
1184
+
1185
+ EXAMPLES::
1186
+
1187
+ sage: R.<x,y> = FreeAlgebra(QQ, 2) # optional - sage.combinat sage.modules
1188
+ sage: F = Factorization([(x,3), (y, 2), (x,1)]) # optional - sage.combinat sage.modules
1189
+ sage: F(x=4) # optional - sage.combinat sage.modules
1190
+ (1) * 4^3 * y^2 * 4
1191
+ sage: F.subs({y:2}) # optional - sage.combinat sage.modules
1192
+ x^3 * 2^2 * x
1193
+
1194
+ sage: R.<x,y> = PolynomialRing(QQ, 2)
1195
+ sage: F = Factorization([(x,3), (y, 2), (x,1)])
1196
+ sage: F(x=4)
1197
+ 4 * 4^3 * y^2
1198
+ sage: F.subs({y:x})
1199
+ x * x^2 * x^3
1200
+ sage: F(x=y+x)
1201
+ (x + y) * y^2 * (x + y)^3
1202
+ """
1203
+ unit = self .__unit .subs (* args , ** kwds )
1204
+ if unit == 0 :
1205
+ return self .universe ().zero ()
1206
+ data = [(p .subs (* args , ** kwds ), e ) for p , e in self .__x ]
1207
+ if any (p == 0 for p , _ in data ):
1208
+ return self .universe ().zero ()
1209
+ return Factorization (data , unit = unit , simplify = False )
1210
+
1211
+ subs = __call__
1212
+
1177
1213
def value (self ):
1178
1214
"""
1179
1215
Return the product of the factors in the factorization, multiplied out.
@@ -1196,7 +1232,7 @@ def value(self):
1196
1232
1197
1233
# Two aliases for ``value(self)``.
1198
1234
expand = value
1199
- prod = value
1235
+ prod = value
1200
1236
1201
1237
def gcd (self , other ):
1202
1238
r"""
@@ -1228,14 +1264,14 @@ def gcd(self, other):
1228
1264
self = self .base_change (U )
1229
1265
other = other .base_change (U )
1230
1266
except TypeError :
1231
- raise TypeError ("Cannot take the gcd of %s and %s because they cannot be coerced into a common universe" % (self ,other ))
1267
+ raise TypeError ("Cannot take the gcd of %s and %s because they cannot be coerced into a common universe" % (self , other ))
1232
1268
1233
1269
if self .is_commutative () and other .is_commutative ():
1234
1270
d1 = dict (self )
1235
1271
d2 = dict (other )
1236
1272
s = {}
1237
1273
for a in set (d1 ).intersection (set (d2 )):
1238
- s [a ] = min (d1 [a ],d2 [a ])
1274
+ s [a ] = min (d1 [a ], d2 [a ])
1239
1275
return Factorization (list (s .items ()))
1240
1276
else :
1241
1277
raise NotImplementedError ("gcd is not implemented for non-commutative factorizations" )
@@ -1270,19 +1306,19 @@ def lcm(self, other):
1270
1306
self = self .base_change (U )
1271
1307
other = other .base_change (U )
1272
1308
except TypeError :
1273
- raise TypeError ("Cannot take the lcm of %s and %s because they cannot be coerced into a common universe" % (self ,other ))
1309
+ raise TypeError ("Cannot take the lcm of %s and %s because they cannot be coerced into a common universe" % (self , other ))
1274
1310
1275
1311
if self .is_commutative () and other .is_commutative ():
1276
1312
d1 = dict (self )
1277
1313
d2 = dict (other )
1278
1314
s = {}
1279
1315
for a in set (d1 ).union (set (d2 )):
1280
- s [a ] = max (d1 .get (a ,0 ),d2 .get (a ,0 ))
1316
+ s [a ] = max (d1 .get (a , 0 ), d2 .get (a , 0 ))
1281
1317
return Factorization (list (s .items ()))
1282
1318
else :
1283
1319
raise NotImplementedError ("lcm is not implemented for non-commutative factorizations" )
1284
1320
1285
- def is_integral (self ):
1321
+ def is_integral (self ) -> bool :
1286
1322
r"""
1287
1323
Return True iff all exponents of this Factorization are non-negative.
1288
1324
@@ -1318,11 +1354,12 @@ def radical(self):
1318
1354
sage: factor(1/2).radical()
1319
1355
Traceback (most recent call last):
1320
1356
...
1321
- ValueError: All exponents in the factorization must be positive.
1357
+ ValueError: all exponents in the factorization must be positive
1322
1358
"""
1323
- if not all (e > 0 for p , e in self .__x ):
1324
- raise ValueError ("All exponents in the factorization must be positive." )
1325
- return Factorization ([(p ,1 ) for p ,e in self .__x ], unit = self .unit ().parent ()(1 ), cr = self .__cr , sort = False , simplify = False )
1359
+ if not all (e > 0 for _ , e in self .__x ):
1360
+ raise ValueError ("all exponents in the factorization must be positive" )
1361
+ return Factorization ([(p , 1 ) for p , e in self .__x ], unit = self .unit ().parent ()(1 ),
1362
+ cr = self .__cr , sort = False , simplify = False )
1326
1363
1327
1364
def radical_value (self ):
1328
1365
"""
@@ -1343,9 +1380,9 @@ def radical_value(self):
1343
1380
sage: factor(1/2).radical_value()
1344
1381
Traceback (most recent call last):
1345
1382
...
1346
- ValueError: All exponents in the factorization must be positive.
1383
+ ValueError: all exponents in the factorization must be positive
1347
1384
"""
1348
- if not all (e > 0 for p , e in self .__x ):
1349
- raise ValueError ("All exponents in the factorization must be positive. " )
1385
+ if not all (e > 0 for _ , e in self .__x ):
1386
+ raise ValueError ("all exponents in the factorization must be positive" )
1350
1387
from sage .misc .misc_c import prod
1351
- return prod ([p for p , e in self .__x ])
1388
+ return prod ([p for p , _ in self .__x ])
0 commit comments