Ticket #3662: trac_3662-2.patch
| File trac_3662-2.patch, 9.7 kB (added by mhansen, 4 months ago) |
|---|
-
a/sage/combinat/combinatorial_algebra.py
old new 62 62 from sage.algebras.algebra_element import AlgebraElement 63 63 from sage.matrix.all import MatrixSpace 64 64 from sage.combinat.free_module import CombinatorialFreeModuleElement, CombinatorialFreeModule, CombinatorialFreeModuleInterface 65 from sage.misc.misc import repr_lincomb 65 66 66 67 class CombinatorialAlgebraElement(AlgebraElement, CombinatorialFreeModuleElement): 67 68 def __init__(self, A, x): … … 225 226 """ 226 227 return self._matrix_() 227 228 229 230 def __repr__(self): 231 """ 232 EXAMPLES: 233 sage: QS3 = SymmetricGroupAlgebra(QQ,3) 234 sage: a = 2 + QS3([2,1,3]) 235 sage: print a.__repr__() 236 2*[1, 2, 3] + [2, 1, 3] 237 """ 238 v = self._monomial_coefficients.items() 239 v.sort() 240 prefix = self.parent().prefix() 241 mons = [ prefix + repr(m) for (m, _) in v ] 242 cffs = [ x for (_, x) in v ] 243 x = repr_lincomb(mons, cffs).replace("*1 "," ") 244 if x[len(x)-2:] == "*1": 245 return x[:len(x)-2] 246 else: 247 return x 248 228 249 229 250 class CombinatorialAlgebra(CombinatorialFreeModuleInterface, Algebra): 230 251 def __init__(self, R, element_class = None): … … 256 277 self._element_class = element_class 257 278 258 279 CombinatorialFreeModuleInterface.__init__(self, R, self._element_class) 280 281 def __call__(self, x): 282 try: 283 return CombinatorialFreeModuleInterface.__call__(self, x) 284 except TypeError: 285 pass 286 287 R = self.base_ring() 288 eclass = self._element_class 289 #Coerce elements of the base ring 290 if not hasattr(x, 'parent'): 291 x = R(x) 292 if x.parent() == R: 293 if x == R(0): 294 return eclass(self, {}) 295 else: 296 return eclass(self, {self._one:x}) 297 #Coerce things that coerce into the base ring 298 elif R.has_coerce_map_from(x.parent()): 299 rx = R(x) 300 if rx == R(0): 301 return eclass(self, {}) 302 else: 303 return eclass(self, {self._one:R(x)}) 304 305 raise TypeError, "do not know how to make x (= %s) an element of self (=%s)"%(x,self) 306 307 def _an_element_impl(self): 308 """ 309 Returns an element of self, namely the unit element. 310 311 EXAMPLES: 312 sage: s = SFASchur(QQ) 313 sage: s._an_element_impl() 314 s[] 315 sage: _.parent() is s 316 True 317 """ 318 return self._element_class(self, {self._one:self.base_ring()(1)}) 319 259 320 260 321 def multiply(self,left,right): 261 322 """ -
a/sage/combinat/finite_class.py
old new 48 48 """ 49 49 self.l = l 50 50 51 def object_class(self, x): 52 """ 53 EXAMPLES: 54 sage: F = FiniteCombinatorialClass([1,2,3]) 55 sage: F.object_class(1) 56 1 57 """ 58 return x 59 51 60 def __repr__(self): 52 61 """ 53 62 TESTS: -
a/sage/combinat/free_module.py
old new 19 19 from sage.rings.all import Ring, Integer 20 20 import sage.structure.parent_base 21 21 from sage.combinat.family import Family 22 from sage.combinat.finite_class import FiniteCombinatorialClass 23 from sage.combinat.combinat import CombinatorialClass 22 24 23 25 # TODO: 24 26 # Rewrite all tests in a more self contained way … … 82 84 def __repr__(self): 83 85 """ 84 86 EXAMPLES: 85 sage: QS3 = SymmetricGroupAlgebra(QQ,3) 86 sage: a = 2 + QS3([2,1,3]) 87 sage: print a.__repr__() 88 2*[1, 2, 3] + [2, 1, 3] 87 sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c'], prefix='F') 88 sage: e = F.basis() 89 sage: e['a'] + 2*e['b'] 90 F('a') + 2*F('b') 91 89 92 """ 90 93 v = self._monomial_coefficients.items() 91 94 v.sort() 92 95 prefix = self.parent().prefix() 93 mons = [ prefix + repr(m)for (m, _) in v ]96 mons = [ prefix + "(" + repr(m) + ")" for (m, _) in v ] 94 97 cffs = [ x for (_, x) in v ] 95 98 x = repr_lincomb(mons, cffs).replace("*1 "," ") 96 99 if x[len(x)-2:] == "*1": … … 409 412 sage: a.map_coefficients(lambda x: x*2) 410 413 2*s[2, 1] + 4*s[3, 2] 411 414 """ 412 res = self.parent()(0)413 415 z_elt = {} 414 416 for m,c in self.monomial_coefficients().iteritems(): 415 417 z_elt[m] = f(c) 416 re s._monomial_coefficients = z_elt417 return res 418 return self.parent()._from_dict(z_elt) 419 418 420 419 421 def map_basis(self, f): 420 422 """ … … 437 439 res._monomial_coefficients = z_elt 438 440 return res 439 441 440 def map _mc(self, f):442 def map(self, f): 441 443 """ 442 444 Returns a new element of self.parent() obtained 443 445 by applying the function f to a monomial coefficient … … 457 459 new_m, new_c = f(m,c) 458 460 z_elt[new_m] = new_c 459 461 return self.parent()._from_dict(z_elt) 462 463 map_mc = map 464 465 def _l_action(self, x): 466 x = self.base_ring()(x) 467 return self.map_coefficients(lambda c: x*c) 468 469 def _r_action(self, x): 470 x = self.base_ring()(x) 471 return self.map_coefficients(lambda c: c*x) 472 460 473 461 474 # NT: There is nothing combinatorial here 462 475 # NT: It's really too bad that we can't have the ring as second optional argument … … 537 550 else: 538 551 return eclass(self, dict([ (e1,R(e2)) for e1,e2 in x._monomial_coefficients.items()])) 539 552 #x is an element of the basis combinatorial class 540 elif isinstance( x, self._combinatorial_class.object_class):553 elif isinstance(self._combinatorial_class.object_class, type) and isinstance(x, self._combinatorial_class.object_class): 541 554 return eclass(self, {x:R(1)}) 542 555 elif x in self._combinatorial_class: 543 556 return eclass(self, {self._combinatorial_class.object_class(x):R(1)}) 544 #Coerce elements of the base ring545 elif hasattr(x, 'parent') and x.parent() is R:546 if x == R(0):547 return eclass(self, {})548 else:549 return eclass(self, {self._one:x})550 #Coerce things that coerce into the base ring551 elif R.has_coerce_map_from(x.parent()):552 rx = R(x)553 if rx == R(0):554 return eclass(self, {})555 else:556 return eclass(self, {self._one:R(x)})557 557 else: 558 558 if hasattr(self, '_coerce_end'): 559 559 try: 560 return self._coerce_ start(x)560 return self._coerce_end(x) 561 561 except TypeError: 562 562 pass 563 563 raise TypeError, "do not know how to make x (= %s) an element of self (=%s)"%(x,self) … … 567 567 Returns an element of self, namely the unit element. 568 568 569 569 EXAMPLES: 570 sage: s = SFASchur(QQ)571 sage: s._an_element_impl()572 s[]573 sage: _.parent() is s570 sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 571 sage: F._an_element_impl() 572 0 573 sage: _.parent() is F 574 574 True 575 575 """ 576 return self._element_class(self, { self._one:self.base_ring()(1)})576 return self._element_class(self, {}) 577 577 578 578 def __repr__(self): 579 579 """ … … 581 581 sage: QS3 = SymmetricGroupAlgebra(QQ,3) 582 582 sage: print QS3.__repr__() 583 583 Symmetric group algebra of order 3 over Rational Field 584 585 584 """ 586 585 return self._name + " over %s"%self.base_ring() 587 586 … … 744 743 return self._from_dict(z_elt) 745 744 746 745 def term(self, i): 747 return self._from_dict({i:self.base_ring()._one_element}) 746 """ 747 EXAMPLES: 748 sage: F = CombinatorialFreeModule(QQ, ['a', 'b', 'c']) 749 sage: F.term('a') 750 B('a') 751 """ 752 return self._from_dict({i:self.base_ring().one_element()}) 748 753 749 754 def _from_dict(self, d, coerce=False): 750 755 """ … … 773 778 774 779 return self._element_class(self, d) 775 780 781 776 782 class CombinatorialFreeModule(CombinatorialFreeModuleInterface, Module): 777 783 r""" 778 784 EXAMPLES: … … 797 803 sage: 2*e['a'] 798 804 2*B('a') 799 805 sage: e['a'] + 3*e['b'] 800 2*B('a') + 3*e['b']806 B('a') + 3*B('b') 801 807 """ 802 808 803 809 804 def __init__(self, R, combinatorial_class): 805 self._combinatorial_class = combinatorial_class 806 self._name = "Free module generated by %s"%combinatorial_class 810 def __init__(self, R, cc, prefix="B"): 811 if isinstance(cc, list): 812 cc = FiniteCombinatorialClass(cc) 813 if not isinstance(cc, CombinatorialClass): 814 raise TypeError, "cc = (%s) must be an instance of CombinatorialClass"%cc 815 self._combinatorial_class = cc 816 self._prefix = prefix 817 self._name = "Free module generated by %s"%cc 807 818 CombinatorialFreeModuleInterface.__init__(self, R, CombinatorialFreeModuleElement) 808 819 pass