def __call__(self, x=None, order=unk): cls = self._element_class BR = self.base_ring() if x is None: res = cls(self, stream=None, order=unk, aorder=unk, aorder_changed=True, is_initialized=False) res.compute_aorder = uninitialized return res #Must be changed because inheritance #Useless for the moment # if isinstance(x, LazyPowerSeries): # x_parent = x.parent() # if x_parent.__class__ != self.__class__: # raise ValueError # if x_parent.base_ring() == self.base_ring(): # return x # else: # if self.base_ring().has_coerce_map_from(x_parent.base_ring()): # return x._new(partial(x._change_ring_gen, self.base_ring()), lambda ao: ao, x, parent=self) if hasattr(x, "parent") and BR.has_coerce_map_from(x.parent()): x = BR(x) return self.term(x, [0]*self._ngens) if hasattr(x, "__iter__") and not isinstance(x, Stream_class): x = iter(x) if is_iterator(x): x = Stream(x) if isinstance(x, Stream_class): aorder = order if order != unk else 0 return cls(self, stream=x, order=order, aorder=aorder, aorder_changed=False, is_initialized=True) elif not hasattr(x, "parent"): x = BR(x) return self.term(x, [0]*self._ngens) raise TypeError, "do not know how to coerce %s into self"%x
def __call__(self, x=None, order=unk): """ EXAMPLES:: sage: from sage.combinat.species.stream import Stream sage: L = LazyPowerSeriesRing(QQ) sage: L() Uninitialized lazy power series sage: L(1) 1 sage: L(ZZ).coefficients(10) [0, 1, -1, 2, -2, 3, -3, 4, -4, 5] sage: L(iter(ZZ)).coefficients(10) [0, 1, -1, 2, -2, 3, -3, 4, -4, 5] sage: L(Stream(ZZ)).coefficients(10) [0, 1, -1, 2, -2, 3, -3, 4, -4, 5] :: sage: a = L([1,2,3]) sage: a.coefficients(3) [1, 2, 3] sage: L(a) is a True sage: L_RR = LazyPowerSeriesRing(RR) sage: b = L_RR(a) sage: b.coefficients(3) [1.00000000000000, 2.00000000000000, 3.00000000000000] sage: L(b) Traceback (most recent call last): ... TypeError: do not know how to coerce ... into self TESTS:: sage: L(pi) Traceback (most recent call last): ... TypeError: do not know how to coerce pi into self """ cls = self._element_class BR = self.base_ring() if x is None: res = cls(self, stream=None, order=unk, aorder=unk, aorder_changed=True, is_initialized=False) res.compute_aorder = uninitialized return res if isinstance(x, LazyPowerSeries): x_parent = x.parent() if x_parent.__class__ != self.__class__: raise ValueError if x_parent.base_ring() == self.base_ring(): return x else: if self.base_ring().has_coerce_map_from(x_parent.base_ring()): return x._new(partial(x._change_ring_gen, self.base_ring()), lambda ao: ao, x, parent=self) if hasattr(x, "parent") and BR.has_coerce_map_from(x.parent()): x = BR(x) return self.term(x, 0) if hasattr(x, "__iter__") and not isinstance(x, Stream_class): x = iter(x) if is_iterator(x): x = Stream(x) if isinstance(x, Stream_class): aorder = order if order != unk else 0 return cls(self, stream=x, order=order, aorder=aorder, aorder_changed=False, is_initialized=True) elif not hasattr(x, "parent"): x = BR(x) return self.term(x, 0) raise TypeError, "do not know how to coerce %s into self"%x
def Set(X=frozenset()): r""" Create the underlying set of ``X``. If ``X`` is a list, tuple, Python set, or ``X.is_finite()`` is ``True``, this returns a wrapper around Python's enumerated immutable ``frozenset`` type with extra functionality. Otherwise it returns a more formal wrapper. If you need the functionality of mutable sets, use Python's builtin set type. EXAMPLES:: sage: X = Set(GF(9,'a')) sage: X {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} sage: type(X) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: Y = X.union(Set(QQ)) sage: Y Set-theoretic union of {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and Set of elements of Rational Field sage: type(Y) <class 'sage.sets.set.Set_object_union_with_category'> Usually sets can be used as dictionary keys. :: sage: d={Set([2*I,1+I]):10} sage: d # key is randomly ordered {{I + 1, 2*I}: 10} sage: d[Set([1+I,2*I])] 10 sage: d[Set((1+I,2*I))] 10 The original object is often forgotten. :: sage: v = [1,2,3] sage: X = Set(v) sage: X {1, 2, 3} sage: v.append(5) sage: X {1, 2, 3} sage: 5 in X False Set also accepts iterators, but be careful to only give *finite* sets. :: sage: list(Set(iter([1, 2, 3, 4, 5]))) [1, 2, 3, 4, 5] We can also create sets from different types:: sage: Set([Sequence([3,1], immutable=True), 5, QQ, Partition([3,1,1])]) {Rational Field, [3, 1, 1], [3, 1], 5} However each of the objects must be hashable:: sage: Set([QQ, [3, 1], 5]) Traceback (most recent call last): ... TypeError: unhashable type: 'list' TESTS:: sage: Set(Primes()) Set of all prime numbers: 2, 3, 5, 7, ... sage: Set(Subsets([1,2,3])).cardinality() 8 sage: Set(iter([1,2,3])) {1, 2, 3} sage: type(_) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: S = Set([]) sage: TestSuite(S).run() Check that :trac:`16090` is fixed:: sage: Set() {} """ if is_Set(X): return X if isinstance(X, Element): raise TypeError("Element has no defined underlying set") elif isinstance(X, (list, tuple, set, frozenset)): return Set_object_enumerated(frozenset(X)) try: if X.is_finite(): return Set_object_enumerated(X) except AttributeError: pass if is_iterator(X): # Note we are risking an infinite loop here, # but this is the way Python behaves too: try # sage: set(an iterator which does not terminate) return Set_object_enumerated(list(X)) return Set_object(X)
def Set(X): r""" Create the underlying set of $X$. If $X$ is a list, tuple, Python set, or ``X.is_finite()`` is true, this returns a wrapper around Python's enumerated immutable frozenset type with extra functionality. Otherwise it returns a more formal wrapper. If you need the functionality of mutable sets, use Python's builtin set type. EXAMPLES:: sage: X = Set(GF(9,'a')) sage: X {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} sage: type(X) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: Y = X.union(Set(QQ)) sage: Y Set-theoretic union of {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and Set of elements of Rational Field sage: type(Y) <class 'sage.sets.set.Set_object_union_with_category'> Usually sets can be used as dictionary keys. :: sage: d={Set([2*I,1+I]):10} sage: d # key is randomly ordered {{I + 1, 2*I}: 10} sage: d[Set([1+I,2*I])] 10 sage: d[Set((1+I,2*I))] 10 The original object is often forgotten. :: sage: v = [1,2,3] sage: X = Set(v) sage: X {1, 2, 3} sage: v.append(5) sage: X {1, 2, 3} sage: 5 in X False Set also accepts iterators, but be careful to only give *finite* sets. :: sage: list(Set(iter([1, 2, 3, 4, 5]))) [1, 2, 3, 4, 5] TESTS:: sage: Set(Primes()) Set of all prime numbers: 2, 3, 5, 7, ... sage: Set(Subsets([1,2,3])).cardinality() 8 sage: Set(iter([1,2,3])) {1, 2, 3} sage: type(_) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: S = Set([]) sage: TestSuite(S).run() """ if is_Set(X): return X if isinstance(X, Element): raise TypeError, "Element has no defined underlying set" elif isinstance(X, (list, tuple, set, frozenset)): return Set_object_enumerated(frozenset(X)) try: if X.is_finite(): return Set_object_enumerated(X) except AttributeError: pass if is_iterator(X): # Note we are risking an infinite loop here, # but this is the way Python behaves too: try # sage: set(an iterator which does not terminate) return Set_object_enumerated(list(X)) return Set_object(X)
def Set(X=frozenset()): r""" Create the underlying set of ``X``. If ``X`` is a list, tuple, Python set, or ``X.is_finite()`` is ``True``, this returns a wrapper around Python's enumerated immutable ``frozenset`` type with extra functionality. Otherwise it returns a more formal wrapper. If you need the functionality of mutable sets, use Python's builtin set type. EXAMPLES:: sage: X = Set(GF(9,'a')) sage: X {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} sage: type(X) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: Y = X.union(Set(QQ)) sage: Y Set-theoretic union of {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and Set of elements of Rational Field sage: type(Y) <class 'sage.sets.set.Set_object_union_with_category'> Usually sets can be used as dictionary keys. :: sage: d={Set([2*I,1+I]):10} sage: d # key is randomly ordered {{I + 1, 2*I}: 10} sage: d[Set([1+I,2*I])] 10 sage: d[Set((1+I,2*I))] 10 The original object is often forgotten. :: sage: v = [1,2,3] sage: X = Set(v) sage: X {1, 2, 3} sage: v.append(5) sage: X {1, 2, 3} sage: 5 in X False Set also accepts iterators, but be careful to only give *finite* sets. :: sage: list(Set(iter([1, 2, 3, 4, 5]))) [1, 2, 3, 4, 5] We can also create sets from different types:: sage: sorted(Set([Sequence([3,1], immutable=True), 5, QQ, Partition([3,1,1])]), key=str) [5, Rational Field, [3, 1, 1], [3, 1]] However each of the objects must be hashable:: sage: Set([QQ, [3, 1], 5]) Traceback (most recent call last): ... TypeError: unhashable type: 'list' TESTS:: sage: Set(Primes()) Set of all prime numbers: 2, 3, 5, 7, ... sage: Set(Subsets([1,2,3])).cardinality() 8 sage: S = Set(iter([1,2,3])); S {1, 2, 3} sage: type(S) <class 'sage.sets.set.Set_object_enumerated_with_category'> sage: S = Set([]) sage: TestSuite(S).run() Check that :trac:`16090` is fixed:: sage: Set() {} """ if is_Set(X): return X if isinstance(X, Element): raise TypeError("Element has no defined underlying set") elif isinstance(X, (list, tuple, set, frozenset)): return Set_object_enumerated(frozenset(X)) try: if X.is_finite(): return Set_object_enumerated(X) except AttributeError: pass if is_iterator(X): # Note we are risking an infinite loop here, # but this is the way Python behaves too: try # sage: set(an iterator which does not terminate) return Set_object_enumerated(list(X)) return Set_object(X)