def signature(self): r""" Return the signature of the rational field, which is `(1,0)`, since there are 1 real and no complex embeddings. EXAMPLES:: sage: QQ.signature() (1, 0) """ return (integer.Integer(1), integer.Integer(0))
def __iter__(self): r""" Creates an iterator that generates the rational numbers without repetition, in order of the height. See also :meth:`range_by_height()`. EXAMPLES: The first 17 rational numbers, ordered by height:: sage: import itertools sage: lst = [a for a in itertools.islice(Rationals(),17)] sage: lst [0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3, -3, 2/3, -2/3, 3/2, -3/2, 1/4, -1/4] sage: [a.height() for a in lst] [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4] """ yield self(0) yield self(1) yield self(-1) height = integer.Integer(1) while True: height = height + 1 for other in range(1, height): if height.gcd(other) == 1: yield self(other / height) yield self(-other / height) yield self(height / other) yield self(-height / other)
def __iter__(self): r""" Creates an iterator that generates the rational numbers without repetition, in order of the height. See also :meth:`range_by_height()`. EXAMPLES: The first 17 rational numbers, ordered by height:: sage: import itertools sage: lst = [a for a in itertools.islice(Rationals(),17)] sage: lst [0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3, -3, 2/3, -2/3, 3/2, -3/2, 1/4, -1/4] sage: [a.height() for a in lst] [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4] """ #The previous version of this function, implemented by Nils #Bruin, used the sequence defined by $a_0=0$ and #$a_{n+1}=\frac{1}{2\lfloor a_n\rfloor+1-a_n}$ and generated the #sequence $$a_0,a_1,-a_1,a_2,-a_2,\ldots$$. This is [A002487] #in Sloane's encyclopedia, attributed to [Stern]. It is not #monotone in height, but has other interesting properties #described in [CalkinWilf]. #REFERENCES: # [A002487] Sloane's OLEIS, # http://oeis.org/classic/A002487 # [CalkinWilf] N. Calkin and H.S. Wilf, Recounting the # rationals, American Mathematical Monthly 107 (2000), # 360--363 # [Stern] M.A. Stern, Ueber eine zahlentheoretische Funktion, # Journal fuer die reine und angewandte Mathematik 55 # (1858), 193--220 # # [beginning of Nils' code] #from sage.rings.arith import integer_floor as floor # #n=self(0) #yield n #while True: # n=1/(2*floor(n)+1-n) # yield n # yield -n # [end of Nils' code] yield self(0) yield self(1) yield self(-1) height = integer.Integer(1) while True: height = height + 1 for other in range(1, height): if height.gcd(other) == 1: yield self(other / height) yield self(-other / height) yield self(height / other) yield self(-height / other)
def absolute_degree(self): """ EXAMPLES:: sage: QQ.absolute_degree() 1 """ return integer.Integer(1)
def ngens(self): """ EXAMPLES:: sage: QQ.ngens() 1 """ return integer.Integer(1)
def degree(self): """ EXAMPLES:: sage: QQ.degree() 1 """ return integer.Integer(1)
def characteristic(self): """ Return the characteristic of the complex (interval) field, which is 0. EXAMPLES:: sage: CIF.characteristic() 0 """ return integer.Integer(0)
def characteristic(self): r""" Return the characteristic of `\CC`, which is 0. EXAMPLES:: sage: ComplexField().characteristic() 0 """ return integer.Integer(0)
def ngens(self): r""" Return the number of generators of `\QQ` which is 1. EXAMPLES:: sage: QQ.ngens() 1 """ return integer.Integer(1)
def discriminant(self): """ Return the discriminant of the field of rational numbers, which is 1. EXAMPLES:: sage: QQ.discriminant() 1 """ return integer.Integer(1)
def absolute_degree(self): r""" Return the absolute degree of `\QQ` which is 1. EXAMPLES:: sage: QQ.absolute_degree() 1 """ return integer.Integer(1)
def degree(self): r""" Return the degree of `\QQ` which is 1. EXAMPLES:: sage: QQ.degree() 1 """ return integer.Integer(1)
def class_number(self): """ Return the class number of the field of rational numbers, which is 1. EXAMPLES:: sage: QQ.class_number() 1 """ return integer.Integer(1)
def characteristic(self): r""" Return 0 since the rational field has characteristic 0. EXAMPLES:: sage: c = QQ.characteristic(); c 0 sage: parent(c) Integer Ring """ return integer.Integer(0)
def _element_constructor_(self, f, prec=infinity, check=True): """ Coerce object to this power series ring. Returns a new instance unless the parent of f is self, in which case f is returned (since f is immutable). INPUT: - ``f`` - object, e.g., a power series ring element - ``prec`` - (default: infinity); truncation precision for coercion - ``check`` - bool (default: True), whether to verify that the coefficients, etc., coerce in correctly. EXAMPLES:: sage: R.<t> = PowerSeriesRing(ZZ) sage: R(t+O(t^5)) # indirect doctest t + O(t^5) sage: R(13) 13 sage: R(2/3) Traceback (most recent call last): ... TypeError: no conversion of this rational to integer sage: R([1,2,3]) 1 + 2*t + 3*t^2 sage: S.<w> = PowerSeriesRing(QQ) sage: R(w + 3*w^2 + O(w^3)) t + 3*t^2 + O(t^3) sage: x = polygen(QQ,'x') sage: R(x + x^2 + x^3 + x^5, 3) t + t^2 + O(t^3) sage: R(1/(1-x), prec=5) 1 + t + t^2 + t^3 + t^4 + O(t^5) sage: R(1/x, 5) Traceback (most recent call last): ... TypeError: no canonical coercion from Laurent Series Ring in t over Integer Ring to Power Series Ring in t over Integer Ring sage: PowerSeriesRing(PowerSeriesRing(QQ,'x'),'y')(x) x sage: PowerSeriesRing(PowerSeriesRing(QQ,'y'),'x')(x) x sage: PowerSeriesRing(PowerSeriesRing(QQ,'t'),'y')(x) y sage: PowerSeriesRing(PowerSeriesRing(QQ,'t'),'y')(1/(1+x), 5) 1 - y + y^2 - y^3 + y^4 + O(y^5) sage: PowerSeriesRing(PowerSeriesRing(QQ,'x',5),'y')(1/(1+x)) 1 - x + x^2 - x^3 + x^4 + O(x^5) sage: PowerSeriesRing(PowerSeriesRing(QQ,'y'),'x')(1/(1+x), 5) 1 - x + x^2 - x^3 + x^4 + O(x^5) sage: PowerSeriesRing(PowerSeriesRing(QQ,'x'),'x')(x).coefficients() [x] Laurent series with non-negative valuation are accepted (see :trac:`6431`):: sage: L.<q> = LaurentSeriesRing(QQ) sage: P = L.power_series_ring() sage: P(q) q sage: P(1/q) Traceback (most recent call last): ... TypeError: self is not a power series It is checked that the precision is non-negative (see :trac:`19409`):: sage: PowerSeriesRing(ZZ, 'x')(1, prec=-5) Traceback (most recent call last): ... ValueError: prec (= -5) must be non-negative """ if prec is not infinity: prec = integer.Integer(prec) if prec < 0: raise ValueError("prec (= %s) must be non-negative" % prec) if isinstance( f, power_series_ring_element.PowerSeries) and f.parent() is self: if prec >= f.prec(): return f f = f.truncate(prec) elif isinstance(f, laurent_series_ring_element.LaurentSeries ) and f.parent().power_series_ring() is self: return self(f.power_series(), prec, check=check) elif isinstance(f, MagmaElement) and str(f.Type()) == 'RngSerPowElt': v = sage_eval(f.Eltseq()) return self(v) * (self.gen(0)**f.Valuation()) elif isinstance(f, FractionFieldElement): if self.base_ring().has_coerce_map_from(f.parent()): return self.element_class(self, [f], prec, check=check) else: num = self.element_class(self, f.numerator(), prec, check=check) den = self.element_class(self, f.denominator(), prec, check=check) return self.coerce(num / den) return self.element_class(self, f, prec, check=check)
def characteristic(self): return integer.Integer(0)
else: return x return NearZeroKiller()(fft(wrap_intermediate(x), sign=sign, wrap_intermediate=wrap_intermediate)) def csr_matrix_multiply(S, x): """Multiplies a scipy.sparse.csr_matrix S by an object-array vector x. """ h, w = S.shape import numpy result = numpy.empty_like(x) for i in xrange(h): result[i] = sum(S.data[idx] * x[S.indices[idx]] for idx in range(S.indptr[i], S.indptr[i + 1])) return result if __name__ == "__main__": import integer q = integer.Integer(14) r = integer.Integer(22) gcd, a, b = extended_euclidean(q, r) print gcd, "=", a, "*", q, "+", b, "*", r print a * q + b * r