def __call__(self, x=None, im=None): """ Create a complex number. EXAMPLES:: sage: CC(2) # indirect doctest 2.00000000000000 sage: CC(CC.0) 1.00000000000000*I sage: CC('1+I') 1.00000000000000 + 1.00000000000000*I sage: CC(2,3) 2.00000000000000 + 3.00000000000000*I sage: CC(QQ[I].gen()) 1.00000000000000*I sage: CC.gen() + QQ[I].gen() Traceback (most recent call last): ... TypeError: unsupported operand parent(s) for '+': 'Complex Field with 53 bits of precision' and 'Number Field in I with defining polynomial x^2 + 1' In the absence of arguments we return zero:: sage: a = CC(); a 0.000000000000000 sage: a.parent() Complex Field with 53 bits of precision """ if x is None: return self.zero_element() # we leave this here to handle the imaginary parameter if im is not None: x = x, im return Parent.__call__(self, x)
def __call__(self, *args, **kwds): r""" Construct an element of ``self`` from the input. EXAMPLES:: sage: M = Manifold(2, 'M', structure='topological') sage: X.<x,y> = M.chart() sage: N = Manifold(3, 'N', structure='topological') sage: Y.<u,v,w> = N.chart() sage: H = Hom(M,N) sage: f = H.__call__({(X, Y): [x+y, x-y, x*y]}, name='f') ; f Continuous map f from the 2-dimensional topological manifold M to the 3-dimensional topological manifold N sage: f.display() f: M → N (x, y) ↦ (u, v, w) = (x + y, x - y, x*y) There is also the following shortcut for :meth:`one`:: sage: M = Manifold(2, 'M', structure='topological') sage: H = Hom(M, M) sage: H(1) Identity map Id_M of the 2-dimensional topological manifold M """ if len(args) == 1: if self.domain() == self.codomain() and args[0] == 1: return self.one() if isinstance(args[0], ContinuousMap): return Homset.__call__(self, args[0]) return Parent.__call__(self, *args, **kwds)
def __call__(self, *args, **keywords): r""" TESTS:: sage: from sage.structure.set_factories_example import XYPairs sage: S = XYPairs() sage: el = S((2,3)); el (2, 3) sage: S(el) is el True sage: XYPairs(x=3)((2,3)) Traceback (most recent call last): ... ValueError: Wrong first coordinate sage: XYPairs(x=3)(el) Traceback (most recent call last): ... ValueError: Wrong first coordinate """ # Ensure idempotence of element construction if (len(args) == 1 and isinstance(args[0], self.element_class) and args[0].parent() == self._parent_for): check = keywords.get("check", True) if check: self.check_element(args[0], check) return args[0] else: return Parent.__call__(self, *args, **keywords)
def __call__(self, x=None, im=None): """ Create a complex number. EXAMPLES:: sage: CC(2) # indirect doctest 2.00000000000000 sage: CC(CC.0) 1.00000000000000*I sage: CC('1+I') 1.00000000000000 + 1.00000000000000*I sage: CC(2,3) 2.00000000000000 + 3.00000000000000*I sage: CC(QQ[I].gen()) 1.00000000000000*I sage: CC.gen() + QQ[I].gen() Traceback (most recent call last): ... TypeError: unsupported operand parent(s) for '+': 'Complex Field with 53 bits of precision' and 'Number Field in I with defining polynomial x^2 + 1' In the absence of arguments we return zero:: sage: a = CC(); a 0.000000000000000 sage: a.parent() Complex Field with 53 bits of precision """ if x is None: return self.zero() # we leave this here to handle the imaginary parameter if im is not None: x = x, im return Parent.__call__(self, x)
def __call__(self, *args, **kwds): r""" Construct an element of ``self`` from the input. EXAMPLES:: sage: M = Manifold(2, 'M', structure='topological') sage: X.<x,y> = M.chart() sage: N = Manifold(3, 'N', structure='topological') sage: Y.<u,v,w> = N.chart() sage: H = Hom(M,N) sage: f = H.__call__({(X, Y): [x+y, x-y, x*y]}, name='f') ; f Continuous map f from the 2-dimensional topological manifold M to the 3-dimensional topological manifold N sage: f.display() f: M --> N (x, y) |--> (u, v, w) = (x + y, x - y, x*y) There is also the following shortcut for :meth:`one`:: sage: M = Manifold(2, 'M', structure='topological') sage: H = Hom(M, M) sage: H(1) Identity map Id_M of the 2-dimensional topological manifold M """ if len(args) == 1: if self.domain() == self.codomain() and args[0] == 1: return self.one() if isinstance(args[0], ContinuousMap): return Homset.__call__(self, args[0]) return Parent.__call__(self, *args, **kwds)
def __call__(self, el): r""" Workaround for returning non elements. See the extensive documentation in :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet.__call__`. TESTS:: sage: Subsets(['a','b','c'])(['a','b']) # indirect doctest {'a', 'b'} """ if not isinstance(el, Element): return self._element_constructor_(el) else: return Parent.__call__(self, el)
def __call__(self, el): r""" Workaround for returning non elements. See the extensive documentation in :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet.__call__`. TESTS:: sage: Subsets(['a','b','c'])(['a','b']) # indirect doctest {'a', 'b'} """ if not isinstance(el, Element): return self._element_constructor_(el) else: return Parent.__call__(self, el)
def __call__(self, x=None, im=None, **kwds): """ Construct an element. EXAMPLES:: sage: CIF(2) # indirect doctest 2 sage: CIF(CIF.0) 1*I sage: CIF('1+I') Traceback (most recent call last): ... TypeError: unable to convert '1+I' to real interval sage: CIF(2,3) 2 + 3*I sage: CIF(pi, e) 3.141592653589794? + 2.718281828459046?*I sage: ComplexIntervalField(100)(CIF(RIF(2,3))) 3.? sage: QQi.<i> = QuadraticField(-1) sage: CIF(i) 1*I sage: QQi.<i> = QuadraticField(-1, embedding=CC(0,-1)) sage: CIF(i) -1*I sage: QQi.<i> = QuadraticField(-1, embedding=None) sage: CIF(i) 1*I :: sage: R.<x> = CIF[] sage: a = R(CIF(0,1)); a I sage: CIF(a) 1*I """ # Note: we override Parent.__call__ because we want to support # CIF(a, b) and that is hard to do using coerce maps. if im is not None or kwds: return self.element_class(self, x, im, **kwds) return Parent.__call__(self, x)
def __call__(self, *args, **kwds): r""" To bypass Homset.__call__, enforcing Parent.__call__ instead. EXAMPLE:: sage: M = Manifold(2, 'M') sage: X.<x,y> = M.chart() sage: N = Manifold(3, 'N') sage: Y.<u,v,w> = N.chart() sage: H = Hom(M,N) sage: f = H.__call__({(X, Y): [x+y, x-y, x*y]}, name='f') ; f differentiable mapping 'f' from the 2-dimensional manifold 'M' to the 3-dimensional manifold 'N' sage: f.display() f: M --> N (x, y) |--> (u, v, w) = (x + y, x - y, x*y) """ return Parent.__call__(self, *args, **kwds)
def __call__(self, *args, **kwds): r""" To bypass Homset.__call__, enforcing Parent.__call__ instead. EXAMPLE:: sage: M = Manifold(2, 'M') sage: X.<x,y> = M.chart() sage: N = Manifold(3, 'N') sage: Y.<u,v,w> = N.chart() sage: H = Hom(M,N) sage: f = H.__call__({(X, Y): [x+y, x-y, x*y]}, name='f') ; f differentiable mapping 'f' from the 2-dimensional manifold 'M' to the 3-dimensional manifold 'N' sage: f.display() f: M --> N (x, y) |--> (u, v, w) = (x + y, x - y, x*y) """ return Parent.__call__(self, *args, **kwds)
def __call__(self, *args, **kwds): r""" To bypass Homset.__call__, enforcing Parent.__call__ instead. EXAMPLES:: sage: M = FiniteRankFreeModule(ZZ, 2, name='M') sage: N = FiniteRankFreeModule(ZZ, 3, name='N') sage: H = Hom(M,N) sage: e = M.basis('e') ; f = N.basis('f') sage: a = H.__call__(0) ; a Generic morphism: From: Rank-2 free module M over the Integer Ring To: Rank-3 free module N over the Integer Ring sage: a.matrix(e,f) [0 0] [0 0] [0 0] sage: a == H.zero() True sage: a == H(0) True sage: a = H.__call__([[1,2],[3,4],[5,6]], bases=(e,f), name='a') ; a Generic morphism: From: Rank-2 free module M over the Integer Ring To: Rank-3 free module N over the Integer Ring sage: a.matrix(e,f) [1 2] [3 4] [5 6] sage: a == H([[1,2],[3,4],[5,6]], bases=(e,f)) True """ from sage.structure.parent import Parent return Parent.__call__(self, *args, **kwds)
def __call__(self, *args, **kwds): r""" To bypass Homset.__call__, enforcing Parent.__call__ instead. EXAMPLES:: sage: M = FiniteRankFreeModule(ZZ, 2, name='M') sage: N = FiniteRankFreeModule(ZZ, 3, name='N') sage: H = Hom(M,N) sage: e = M.basis('e') ; f = N.basis('f') sage: a = H.__call__(0) ; a Generic morphism: From: Rank-2 free module M over the Integer Ring To: Rank-3 free module N over the Integer Ring sage: a.matrix(e,f) [0 0] [0 0] [0 0] sage: a == H.zero() True sage: a == H(0) True sage: a = H.__call__([[1,2],[3,4],[5,6]], bases=(e,f), name='a') ; a Generic morphism: From: Rank-2 free module M over the Integer Ring To: Rank-3 free module N over the Integer Ring sage: a.matrix(e,f) [1 2] [3 4] [5 6] sage: a == H([[1,2],[3,4],[5,6]], bases=(e,f)) True """ from sage.structure.parent import Parent return Parent.__call__(self, *args, **kwds)
def __call__(self, el): """ Coerce or convert ``el`` into an element of ``self``. INPUT: - ``el`` -- some object As :meth:`Parent.__call__`, this tries to convert or coerce ``el`` into an element of ``self`` depending on the parent of ``el``. If no such conversion or coercion is available, this calls :meth:`_element_constructor_`. :meth:`Parent.__call__` enforces that :meth:`_element_constructor_` return an :class:`Element` (more precisely, it calls :meth:`_element_constructor_` through a :class:`sage.structure.coerce_maps.DefaultConvertMap_unique`, and any :class:`sage.categories.map.Map` requires its results to be instances of :class:`Element`). Since :class:`FiniteEnumeratedSets` is often a facade over plain Python objects, :trac:`16280` introduced this method which works around this limitation by calling directly :meth:`_element_constructor_` whenever ``el`` is not an :class:`Element`. Otherwise :meth:`Parent.__call__` is called as usual. .. WARNING:: This workaround prevents conversions or coercions from facade parents over plain Python objects into ``self``. If the :meth:`Parent.__call__` fails, then we try :meth:`_element_constructor_` directly as the element returned may not be a subclass of :class:`Element`, which is currently not supported (see :trac:`19553`). EXAMPLES:: sage: F = FiniteEnumeratedSet([1, 2, 'a', 'b']) sage: F(1) 1 sage: F('a') 'a' We check that conversions are properly honored for usual parents; this is not the case for facade parents over plain Python objects:: sage: F = FiniteEnumeratedSet([1, 2, 3, 'a', 'aa']) sage: phi = Hom(ZZ, F, Sets())(lambda i: i+i) sage: phi(1) 2 sage: phi.register_as_conversion() sage: from sage.sets.pythonclass import Set_PythonType_class sage: psi = Hom(Set_PythonType_class(str), F, Sets())(lambda s: ZZ(len(s))) sage: psi.register_as_conversion() sage: psi('a') 1 sage: F(1) 2 sage: F('a') 'a' Check that :trac:`19554` is fixed:: sage: S = FiniteEnumeratedSet(range(5)) sage: S(1) 1 sage: type(S(1)) <type 'int'> """ if not isinstance(el, Element): return self._element_constructor_(el) try: return Parent.__call__(self, el) except TypeError: return self._element_constructor_(el)
def __call__(self, el): """ Coerce or convert ``el`` into an element of ``self``. INPUT: - ``el`` -- some object As :meth:`Parent.__call__`, this tries to convert or coerce ``el`` into an element of ``self`` depending on the parent of ``el``. If no such conversion or coercion is available, this calls :meth:`_element_constructor_`. :meth:`Parent.__call__` enforces that :meth:`_element_constructor_` return an :class:`Element` (more precisely, it calls :meth:`_element_constructor_` through a :class:`sage.structure.coerce_maps.DefaultConvertMap`, and any :class:`sage.categories.map.Map` requires its results to be instances of :class:`Element`). Since :class:`FiniteEnumeratedSets` is often a facade over plain Python objects, :trac:`16280` introduced this method which works around this limitation by calling directly :meth:`_element_constructor_` whenever ``el`` is not an :class:`Element`. Otherwise :meth:`Parent.__call__` is called as usual. .. WARNING:: This workaround prevents conversions or coercions from facade parents over plain Python objects into ``self``. EXAMPLES:: sage: F = FiniteEnumeratedSet([1, 2, 'a', 'b']) sage: F(1) 1 sage: F('a') 'a' We check that conversions are properly honored for usual parents; this is not the case for facade parents over plain Python objects:: sage: F = FiniteEnumeratedSet([1, 2, 3, 'a', 'aa']) sage: phi = Hom(ZZ, F, Sets())(lambda i: i+i) sage: phi(1) 2 sage: phi.register_as_conversion() sage: from sage.structure.parent import Set_PythonType_class sage: psi = Hom(Set_PythonType_class(str), F, Sets())(lambda s: ZZ(len(s))) sage: psi.register_as_conversion() sage: psi('a') 1 sage: F(1) 2 sage: F('a') 'a' """ if not isinstance(el, Element): return self._element_constructor_(el) else: return Parent.__call__(self, el)