1 | | #***************************************************************************** |
2 | | # |
3 | | # Copyright (C) 2008 Mike Hansen <mhansen@gmail.com> |
4 | | # William Stien <wstein@gmail.com> |
5 | | # David Roe <roed314@gmail.com> |
6 | | # |
7 | | # Distributed under the terms of the GNU General Public License (GPL) |
8 | | # |
9 | | # This code is distributed in the hope that it will be useful, |
10 | | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | # General Public License for more details. |
13 | | # |
14 | | # The full text of the GPL is available at: |
15 | | # |
16 | | # http://www.gnu.org/licenses/ |
17 | | #***************************************************************************** |
18 | | |
19 | | import sage.rings.rational_field |
20 | | import sage.rings.rational |
21 | | import sage.rings.integer |
22 | | import sage.rings.infinity |
23 | | import sage.structure.element |
24 | | import sage.rings.extended_integer_ring |
25 | | import field |
26 | | from sage.structure.parent_gens import ParentWithGens |
27 | | from sage.categories.morphism import Morphism |
28 | | |
29 | | |
30 | | Rational = sage.rings.rational.Rational |
31 | | RationalField = sage.rings.rational_field.RationalField |
32 | | QQ = sage.rings.rational_field.QQ |
33 | | Integer = sage.rings.integer.Integer |
34 | | InfinityElement = sage.structure.element.InfinityElement |
35 | | PlusInfinityElement = sage.structure.element.PlusInfinityElement |
36 | | MinusInfinityElement = sage.structure.element.MinusInfinityElement |
37 | | InfinityRing = sage.rings.infinity.InfinityRing |
38 | | ExtendedIntegerRing = sage.rings.extended_integer_ring.ExtendedIntegerRing |
39 | | IntegerPlusInfinity = sage.rings.extended_integer_ring.IntegerPlusInfinity |
40 | | IntegerMinusInfinity = sage.rings.extended_integer_ring.IntegerMinusInfinity |
41 | | SignError = sage.rings.infinity.SignError |
42 | | |
43 | | import sage.rings.number_field.number_field_base as number_field_base |
44 | | |
45 | | class Q_to_ExtendedQ(Morphism): |
46 | | def __init__(self, ExtQQ=None): |
47 | | """ |
48 | | EXAMPLES: |
49 | | sage: from sage.rings.extended_rational_field import Q_to_ExtendedQ |
50 | | sage: f = Q_to_ExtendedQ() |
51 | | sage: loads(dumps(f)) |
52 | | Natural morphism: |
53 | | From: Rational Field |
54 | | To: Extended Rational Field |
55 | | """ |
56 | | import sage.categories.homset |
57 | | if ExtQQ is None: |
58 | | ExtQQ = ExtendedRationalField |
59 | | Morphism.__init__(self, sage.categories.homset.Hom(QQ, ExtQQ)) |
60 | | self._repr_type_str = "Natural" |
61 | | |
62 | | def _call_(self, x): |
63 | | """ |
64 | | Returns the image of x under self. |
65 | | |
66 | | EXAMPLES: |
67 | | sage: from sage.rings.extended_rational_field import Q_to_ExtendedQ |
68 | | sage: f = Q_to_ExtendedQ() |
69 | | sage: f(QQ(2)) #indirect doctest |
70 | | 2 |
71 | | sage: type(_) |
72 | | <class 'sage.rings.extended_rational_field.ExtendedRational'> |
73 | | sage: f(2) #indirect doctest |
74 | | 2 |
75 | | """ |
76 | | return ExtendedRational(x) |
77 | | |
78 | | |
79 | | |
80 | | _obj = {} |
81 | | class _uniq0(object): |
82 | | def __new__(cls): |
83 | | if _obj.has_key(0): |
84 | | return _obj[0] |
85 | | O = number_field_base.NumberField.__new__(cls) |
86 | | _obj[0] = O |
87 | | return O |
88 | | |
89 | | class _uniq1(object): |
90 | | def __new__(cls): |
91 | | if _obj.has_key(1): |
92 | | return _obj[1] |
93 | | O = PlusInfinityElement.__new__(cls) |
94 | | _obj[1] = O |
95 | | return O |
96 | | |
97 | | class _uniq2(object): |
98 | | def __new__(cls): |
99 | | if _obj.has_key(2): |
100 | | return _obj[2] |
101 | | O = MinusInfinityElement.__new__(cls) |
102 | | _obj[2] = O |
103 | | return O |
104 | | |
105 | | class ExtendedRationalField_class(_uniq0, RationalField): |
106 | | def __init__(self): |
107 | | """ |
108 | | TESTS: |
109 | | sage: E = ExtendedRationalField |
110 | | sage: E == loads(dumps(E)) |
111 | | True |
112 | | """ |
113 | | ParentWithGens.__init__(self, self) |
114 | | self._assign_names(('x'),normalize=False) # ??? |
115 | | self._populate_coercion_lists_(coerce_list=[Q_to_ExtendedQ(self)]) |
116 | | |
117 | | def __reduce__(self): |
118 | | return ExtendedRationalField_class, tuple([]) |
119 | | |
120 | | def _repr_(self): |
121 | | """ |
122 | | EXAMPLES: |
123 | | sage: ExtendedRationalField._repr_() |
124 | | 'Extended Rational Field' |
125 | | """ |
126 | | return "Extended Rational Field" |
127 | | |
128 | | def _latex_(self): |
129 | | """ |
130 | | EXAMPLES: |
131 | | sage: latex(ExtendedRationalField) #indirect doctest |
132 | | \mathbf{Q}\cup\{\pm\infty\} |
133 | | """ |
134 | | return "\\mathbf{Q}\\cup\\{\\pm\\infty\\}" |
135 | | |
136 | | def _element_constructor_(self, x, base = 0): |
137 | | """ |
138 | | EXAMPLES: |
139 | | sage: E = ExtendedRationalField |
140 | | sage: E(oo) |
141 | | +Infinity |
142 | | sage: type(_) |
143 | | <class 'sage.rings.extended_rational_field.RationalPlusInfinity'> |
144 | | sage: E(-oo) |
145 | | -Infinity |
146 | | sage: E(0) |
147 | | 0 |
148 | | sage: E(2) |
149 | | 2 |
150 | | |
151 | | """ |
152 | | if isinstance(x, sage.rings.infinity.MinusInfinity): |
153 | | return self.gen(2) |
154 | | if isinstance(x, sage.structure.element.InfinityElement): |
155 | | return self.gen(1) |
156 | | if isinstance(x, sage.rings.infinity.FiniteNumber): |
157 | | if x == 0: |
158 | | return ExtendedRational(0) |
159 | | raise TypeError, "cannot coerce unknown finite number into the extended rationals" |
160 | | return ExtendedRational(x, base) |
161 | | |
162 | | def _coerce_map_from_(self, S): |
163 | | """ |
164 | | EXAMPLES: |
165 | | sage: E = ExtendedRationalField |
166 | | sage: E.coerce_map_from(ZZ) #indirect doctest |
167 | | Composite map: |
168 | | From: Integer Ring |
169 | | To: Extended Rational Field |
170 | | Defn: Natural morphism: |
171 | | From: Integer Ring |
172 | | To: Rational Field |
173 | | then |
174 | | Natural morphism: |
175 | | From: Rational Field |
176 | | To: Extended Rational Field |
177 | | sage: E.coerce_map_from(QQ) #indirect doctest |
178 | | Natural morphism: |
179 | | From: Rational Field |
180 | | To: Extended Rational Field |
181 | | sage: E.coerce_map_from(ExtendedIntegerRing) |
182 | | Conversion map: |
183 | | From: Extended Integer Ring |
184 | | To: Extended Rational Field |
185 | | |
186 | | sage: E.coerce(int(2)) |
187 | | 2 |
188 | | sage: E.coerce(1/2) |
189 | | 1/2 |
190 | | sage: E.coerce(oo) |
191 | | +Infinity |
192 | | sage: R.<x> = QQ[] |
193 | | sage: E.coerce(R(1)) |
194 | | Traceback (most recent call last): |
195 | | ... |
196 | | TypeError: no canonical coercion from Univariate Polynomial Ring in x over Rational Field to Extended Rational Field |
197 | | |
198 | | TESTS: |
199 | | sage: ExtendedRationalField(2)*ExtendedIntegerRing(2) |
200 | | 4 |
201 | | sage: type(_) |
202 | | <class 'sage.rings.extended_rational_field.ExtendedRational'> |
203 | | |
204 | | """ |
205 | | if S is QQ: |
206 | | return Q_to_ExtendedQ() |
207 | | elif S == ExtendedIntegerRing or S == InfinityRing: |
208 | | return self._generic_convert_map(S) |
209 | | |
210 | | def _is_valid_homomorphism(self, codomain, im_gens): |
211 | | """ |
212 | | EXAMPLES: |
213 | | sage: E = ExtendedRationalField |
214 | | sage: E._is_valid_homomorphism(E, (0,0,0)) |
215 | | Traceback (most recent call last): |
216 | | ... |
217 | | NotImplementedError |
218 | | """ |
219 | | raise NotImplementedError |
220 | | |
221 | | def __iter__(self): |
222 | | """ |
223 | | Returns a generator for the elements of the extended rational |
224 | | field. |
225 | | |
226 | | EXAMPLES: |
227 | | sage: it = iter(ExtendedRationalField) |
228 | | sage: [it.next() for _ in range(10)] |
229 | | [0, 1, -1, +Infinity, -Infinity, 2, 1/2, -2, -1/2, 3] |
230 | | """ |
231 | | yield self(0) |
232 | | yield self(1) |
233 | | yield self(-1) |
234 | | yield self.gen(1) |
235 | | yield self.gen(2) |
236 | | from integer_ring import IntegerRing |
237 | | for n in IntegerRing(): |
238 | | m = abs(n) |
239 | | for d in abs(n).coprime_integers(m): |
240 | | yield n/d |
241 | | yield d/n |
242 | | |
243 | | def complex_embedding(self, prec=53): |
244 | | """ |
245 | | EXAMPLES: |
246 | | sage: ExtendedRationalField.complex_embedding() |
247 | | Traceback (most recent call last): |
248 | | ... |
249 | | NotImplementedError |
250 | | """ |
251 | | raise NotImplementedError |
252 | | |
253 | | def gens(self): |
254 | | """ |
255 | | Returns the generators of the extended rational field. |
256 | | |
257 | | EXAMPLES: |
258 | | sage: ExtendedRationalField.gens() |
259 | | (1, +Infinity, -Infinity) |
260 | | """ |
261 | | return (self(1), self.gen(1), self.gen(2), ) |
262 | | |
263 | | def gen(self, n=0): |
264 | | r""" |
265 | | Returns the $n^{th} generator of the extended rational field. |
266 | | |
267 | | EXAMPLES: |
268 | | sage: E = ExtendedRationalField |
269 | | sage: E.gen() |
270 | | 1 |
271 | | sage: E.gen(0) |
272 | | 1 |
273 | | sage: E.gen(1) |
274 | | +Infinity |
275 | | sage: E.gen(2) |
276 | | -Infinity |
277 | | sage: E.gen(3) |
278 | | Traceback (most recent call last): |
279 | | ... |
280 | | IndexError: n must be 0, 1, or 2 |
281 | | """ |
282 | | if n == 0: |
283 | | return self(1) |
284 | | elif n == 1: |
285 | | try: |
286 | | return self.gen1 |
287 | | except AttributeError: |
288 | | self.gen1 = RationalPlusInfinity() |
289 | | return self.gen1 |
290 | | elif n == 2: |
291 | | try: |
292 | | return self.gen2 |
293 | | except AttributeError: |
294 | | self.gen2 = RationalMinusInfinity() |
295 | | return self.gen2 |
296 | | else: |
297 | | raise IndexError, "n must be 0, 1, or 2" |
298 | | |
299 | | def is_prime_field(self): |
300 | | """ |
301 | | Returns whether or not the extended rational field is a |
302 | | prime field (which it is not). |
303 | | |
304 | | EXAMPLES: |
305 | | sage: ExtendedRationalField.is_prime_field() |
306 | | False |
307 | | """ |
308 | | return False |
309 | | |
310 | | def ngens(self): |
311 | | """ |
312 | | Returns the number of generators of the extended rational field. |
313 | | |
314 | | EXAMPLES: |
315 | | sage: ExtendedRationalField.ngens() |
316 | | 3 |
317 | | """ |
318 | | return 3 |
319 | | |
320 | | |
321 | | ExtendedRationalField = ExtendedRationalField_class() |
322 | | |
323 | | class ExtendedRational(Rational): |
324 | | def __init__(self, x = None, base = 0): |
325 | | """ |
326 | | Constructor for elements of the extended rational field. |
327 | | |
328 | | EXAMPLES: |
329 | | sage: E = ExtendedRationalField |
330 | | sage: a = E(2) |
331 | | sage: a == loads(dumps(a)) |
332 | | True |
333 | | """ |
334 | | Rational.__init__(self, x, base) |
335 | | self._set_parent(ExtendedRationalField) |
336 | | |
337 | | def _rational_(self): |
338 | | """ |
339 | | Returns a rational version of self. |
340 | | |
341 | | EXAMPLES: |
342 | | sage: E = ExtendedRationalField |
343 | | sage: E(2)._rational_() |
344 | | 2 |
345 | | sage: type(_) |
346 | | <type 'sage.rings.rational.Rational'> |
347 | | sage: type(QQ(E(1/2))) |
348 | | <type 'sage.rings.rational.Rational'> |
349 | | """ |
350 | | return Rational.copy(self) |
351 | | |
352 | | def __cmp__(self, other): |
353 | | """ |
354 | | EXAMPLES: |
355 | | sage: E = ExtendedRationalField |
356 | | sage: cmp(E(2),E(3)) #indirect doctest |
357 | | -1 |
358 | | sage: cmp(E(2),E(-2)) #indirect doctest |
359 | | 1 |
360 | | sage: cmp(E(2),E(-oo)) #indirect doctest |
361 | | 1 |
362 | | sage: cmp(E(2), E(oo)) #indirect doctest |
363 | | -1 |
364 | | sage: cmp(E(2), E(2)) #indirect doctest |
365 | | 0 |
366 | | sage: cmp(E(oo), E(oo)) #indirect doctest |
367 | | 0 |
368 | | sage: cmp(E(oo), E(-oo)) #indirect doctest |
369 | | 1 |
370 | | """ |
371 | | if isinstance(other, InfinityElement): |
372 | | return -other.__cmp__(self) |
373 | | return cmp(Rational(self), Rational(other)) |
374 | | |
375 | | def copy(self): |
376 | | """ |
377 | | Returns a copy of self. |
378 | | |
379 | | EXAMPLES: |
380 | | sage: E = ExtendedRationalField |
381 | | sage: a = E(2) |
382 | | sage: b = a.copy() |
383 | | sage: a == b |
384 | | True |
385 | | sage: a is b |
386 | | False |
387 | | """ |
388 | | return self.parent()(Rational.copy(self)) |
389 | | |
390 | | def lcm(self, other): |
391 | | """ |
392 | | Returns the least common multiple of self and other. If other is plus or |
393 | | minus infinity, then the lcm is +Infinity. |
394 | | |
395 | | EXAMPLES: |
396 | | sage: E = ExtendedRationalField |
397 | | sage: E(2).lcm(3) |
398 | | 6 |
399 | | sage: E(2).lcm(oo) |
400 | | +Infinity |
401 | | sage: E(2).lcm(-oo) |
402 | | +Infinity |
403 | | """ |
404 | | if isinstance(self, InfinityElement) or isinstance(other, InfinityElement): |
405 | | return self.parent().gen(1) |
406 | | else: |
407 | | return self.parent()(Rational.lcm(self, QQ(other))) |
408 | | |
409 | | def sqrt(self): |
410 | | """ |
411 | | Returns the square root of self. |
412 | | |
413 | | EXAMPLES: |
414 | | sage: E = ExtendedRationalField |
415 | | sage: E(2).sqrt() |
416 | | sqrt(2) |
417 | | sage: E(-2).sqrt() |
418 | | sqrt(2)*I |
419 | | sage: E(4).sqrt() |
420 | | 2 |
421 | | sage: type(_) |
422 | | <class 'sage.rings.extended_rational_field.ExtendedRational'> |
423 | | sage: sqrt(E(2)) |
424 | | sqrt(2) |
425 | | |
426 | | """ |
427 | | value = Rational.sqrt(self) |
428 | | try: |
429 | | return self.parent()(value) |
430 | | except TypeError: |
431 | | return value |
432 | | |
433 | | def nth_root(self, n): |
434 | | """ |
435 | | Computes the nth root of self, or raises a \exception{ValueError} |
436 | | if self is not a perfect nth power. |
437 | | |
438 | | INPUT: |
439 | | n -- integer (must fit in C int type) |
440 | | |
441 | | EXAMPLES: |
442 | | sage: E = ExtendedRationalField |
443 | | sage: E(8).nth_root(3) |
444 | | 2 |
445 | | sage: E(7).nth_root(3) |
446 | | Traceback (most recent call last): |
447 | | ... |
448 | | ValueError: not a perfect nth power |
449 | | """ |
450 | | return self.parent()(Rational.nth_root(self, n)) |
451 | | |
452 | | def _add_(self, right): |
453 | | """ |
454 | | Returns the sum of self and right. |
455 | | |
456 | | EXAMPLES: |
457 | | sage: E = ExtendedRationalField |
458 | | sage: E(2) + E(4) #indirect doctest |
459 | | 6 |
460 | | sage: E(2) + 4 |
461 | | 6 |
462 | | sage: E(2) + E(oo) |
463 | | +Infinity |
464 | | sage: E(2) + E(-oo) |
465 | | -Infinity |
466 | | """ |
467 | | if isinstance(right, InfinityElement): |
468 | | return right |
469 | | return self.parent()(Rational(self) + Rational(right)) |
470 | | |
471 | | def _sub_(self, right): |
472 | | """ |
473 | | Returns the difference between self and right. |
474 | | |
475 | | EXAMPLES: |
476 | | sage: E = ExtendedRationalField |
477 | | sage: E(2) - E(4) #indirect doctest |
478 | | -2 |
479 | | sage: E(2) - E(oo) |
480 | | -Infinity |
481 | | sage: E(2) - E(-oo) |
482 | | +Infinity |
483 | | """ |
484 | | if isinstance(right, InfinityElement): |
485 | | return -right |
486 | | return self.parent()(Rational(self) - Rational(right)) |
487 | | |
488 | | def _neg_(self): |
489 | | """ |
490 | | Returns the negation of self. |
491 | | |
492 | | EXAMPLES: |
493 | | sage: E = ExtendedRationalField |
494 | | sage: -E(2) #indirect doctest |
495 | | -2 |
496 | | sage: -E(oo) |
497 | | -Infinity |
498 | | sage: -E(-oo) |
499 | | +Infinity |
500 | | """ |
501 | | return self.parent()(-Rational(self)) |
502 | | |
503 | | def _mul_(self, right): |
504 | | """ |
505 | | Returns the product of self and right. |
506 | | |
507 | | EXAMPLES: |
508 | | sage: E = ExtendedRationalField |
509 | | sage: E(2)*E(4) #indirect doctest |
510 | | 8 |
511 | | sage: E(2)*E(oo) |
512 | | +Infinity |
513 | | sage: E(2)*E(-oo) |
514 | | -Infinity |
515 | | sage: E(-2)*E(oo) |
516 | | -Infinity |
517 | | sage: E(-2)*E(-oo) |
518 | | +Infinity |
519 | | sage: E(4)*2 |
520 | | 8 |
521 | | sage: E(0)*E(oo) |
522 | | Traceback (most recent call last): |
523 | | ... |
524 | | SignError: cannot multiply infinity by zero |
525 | | """ |
526 | | if isinstance(right, InfinityElement): |
527 | | return right._mul_(self) |
528 | | return self.parent()(Rational(self) * Rational(right)) |
529 | | |
530 | | def _div_(self, right): |
531 | | """ |
532 | | Returns self / right. |
533 | | |
534 | | EXAMPLES: |
535 | | sage: E = ExtendedRationalField |
536 | | sage: E(2)/4 #indirect doctest |
537 | | 1/2 |
538 | | sage: E(2)/E(oo) |
539 | | 0 |
540 | | sage: E(2)/E(-oo) |
541 | | 0 |
542 | | sage: E(-2)/E(oo) |
543 | | 0 |
544 | | """ |
545 | | if isinstance(right, InfinityElement): |
546 | | return self.parent()(0) |
547 | | return self.parent()(Rational(self) / Rational(right)) |
548 | | |
549 | | def __invert__(self): |
550 | | """ |
551 | | Returns the inverse of self. |
552 | | |
553 | | EXAMPLES: |
554 | | sage: E = ExtendedRationalField |
555 | | sage: ~E(2) #indirect doctest |
556 | | 1/2 |
557 | | sage: ~E(0) |
558 | | Traceback (most recent call last): |
559 | | ... |
560 | | ZeroDivisionError: rational division by zero |
561 | | """ |
562 | | return self.parent()(~Rational(self)) |
563 | | |
564 | | def __pow__(self, n): |
565 | | """ |
566 | | Returns self^n. |
567 | | |
568 | | EXAMPLES: |
569 | | sage: E = ExtendedRationalField |
570 | | sage: E(2)^2 #indirect doctest |
571 | | 4 |
572 | | sage: E(2)^1 |
573 | | 2 |
574 | | sage: E(2)^(1/3) |
575 | | 2^(1/3) |
576 | | sage: E(1)^E(oo) |
577 | | 1 |
578 | | sage: E(2)^E(oo) |
579 | | +Infinity |
580 | | sage: E(0)^E(oo) |
581 | | Traceback (most recent call last): |
582 | | ... |
583 | | SignError: 0^infinity is not defined |
584 | | sage: E(-1/2)^E(oo) |
585 | | 0 |
586 | | sage: E(-2)^E(oo) |
587 | | Traceback (most recent call last): |
588 | | ... |
589 | | SignError: negative^infinity is not defined |
590 | | |
591 | | sage: E(2)^E(-oo) |
592 | | 0 |
593 | | sage: E(1)^E(-oo) |
594 | | 1 |
595 | | sage: E(1/2)^E(-oo) |
596 | | +Infinity |
597 | | sage: E(-1)^E(-oo) |
598 | | Traceback (most recent call last): |
599 | | ... |
600 | | SignError: x^(-infinity) not defined for -1 <= x <= 0 |
601 | | sage: E(-2)^E(-oo) |
602 | | 0 |
603 | | """ |
604 | | if isinstance(n, InfinityElement): |
605 | | if isinstance(n, PlusInfinityElement): |
606 | | if self > 1: |
607 | | return self.parent().gen(1) |
608 | | elif self == 1: |
609 | | return self |
610 | | elif self > 0: |
611 | | return self.parent()(0) |
612 | | elif self == 0: |
613 | | raise SignError, "0^infinity is not defined" |
614 | | elif self > -1: |
615 | | return self.parent()(0) |
616 | | else: |
617 | | raise SignError, "negative^infinity is not defined" |
618 | | elif isinstance(n, MinusInfinityElement): |
619 | | if self > 1: |
620 | | return self.parent()(0) |
621 | | elif self == 1: |
622 | | return self |
623 | | elif self > 0: |
624 | | return self.parent().gen(1) |
625 | | elif self >= -1: |
626 | | raise SignError, "x^(-infinity) not defined for -1 <= x <= 0" |
627 | | else: |
628 | | return self.parent()(0) |
629 | | else: |
630 | | raise TypeError, "cannot raise n to an unsigned infinite power." |
631 | | value = Rational(self)**n |
632 | | try: |
633 | | return self.parent()(value) |
634 | | except TypeError: |
635 | | return value |
636 | | |
637 | | def __abs__(self): |
638 | | """ |
639 | | Returns the absolute value of self. |
640 | | |
641 | | EXAMPLES: |
642 | | sage: E = ExtendedRationalField |
643 | | sage: abs(E(-2)) #indirect doctest |
644 | | 2 |
645 | | sage: abs(E(2)) #indirect doctest |
646 | | 2 |
647 | | """ |
648 | | return self.parent()(Rational(self).__abs__()) |
649 | | |
650 | | def numerator(self): |
651 | | """ |
652 | | Returns the numerator of self as an extended integer. If you want an actual integer, |
653 | | use numer instead. |
654 | | |
655 | | EXAMPLES: |
656 | | sage: E = ExtendedRationalField |
657 | | sage: E(2).numerator() |
658 | | 2 |
659 | | sage: E(3/4).numerator() |
660 | | 3 |
661 | | """ |
662 | | return ExtendedIntegerRing(Rational(self).numerator()) |
663 | | |
664 | | def denominator(self): |
665 | | """ |
666 | | Returns the denominator of self as an extended integer. If you want an actual integer, |
667 | | use denom instead. |
668 | | |
669 | | EXAMPLES: |
670 | | sage: E = ExtendedRationalField |
671 | | sage: E(2).denominator() |
672 | | 1 |
673 | | sage: E(3/4).denominator() |
674 | | 4 |
675 | | """ |
676 | | return ExtendedIntegerRing(Rational(self).denominator()) |
677 | | |
678 | | def floor(self): |
679 | | """ |
680 | | Returns the floor of self. |
681 | | |
682 | | EXAMPLES: |
683 | | sage: E = ExtendedRationalField |
684 | | sage: E(4/3).floor() |
685 | | 1 |
686 | | sage: floor(E(4/3)) |
687 | | 1 |
688 | | sage: E(-4/3).floor() |
689 | | -2 |
690 | | """ |
691 | | return ExtendedIntegerRing(Rational(self).floor()) |
692 | | |
693 | | def ceil(self): |
694 | | """ |
695 | | Returns the ceiling of self. |
696 | | |
697 | | EXAMPLES: |
698 | | sage: E = ExtendedRationalField |
699 | | sage: E(4/3).ceil() |
700 | | 2 |
701 | | sage: ceil(E(4/3)) |
702 | | 2 |
703 | | sage: E(-4/3).ceil() |
704 | | -1 |
705 | | """ |
706 | | return ExtendedIntegerRing(Rational(self).ceil()) |
707 | | |
708 | | def __lshift__(self, n): |
709 | | r""" |
710 | | Returns $self*2^n$. |
711 | | |
712 | | EXAMPLES: |
713 | | sage: E = ExtendedRationalField |
714 | | sage: E(1/2) << 3 #indirect doctest |
715 | | 4 |
716 | | sage: E(-2) << 2 |
717 | | -8 |
718 | | """ |
719 | | return self.parent()(Rational(self).__lshift__(n)) |
720 | | |
721 | | def __rshift__(self, n): |
722 | | r""" |
723 | | Returns $self*2^{-n}$. |
724 | | |
725 | | EXAMPLES: |
726 | | sage: E = ExtendedRationalField |
727 | | sage: E(2) >> 1 #indirect doctest |
728 | | 1 |
729 | | sage: E(-8) >> 2 |
730 | | -2 |
731 | | """ |
732 | | return self.parent()(Rational(self).__rshift__(n)) |
733 | | |
734 | | |
735 | | class RationalPlusInfinity(_uniq1, PlusInfinityElement): |
736 | | def __init__(self): |
737 | | """ |
738 | | EXAMPLES: |
739 | | sage: E = ExtendedRationalField |
740 | | sage: a = E(oo) |
741 | | sage: a == loads(dumps(a)) |
742 | | True |
743 | | """ |
744 | | PlusInfinityElement.__init__(self, ExtendedRationalField) |
745 | | |
746 | | def __cmp__(self, other): |
747 | | """ |
748 | | EXAMPLES: |
749 | | sage: E = ExtendedRationalField |
750 | | sage: cmp(E(oo), 3) #indirect doctest |
751 | | 1 |
752 | | sage: cmp(E(oo), E(oo)) #indirect doctest |
753 | | 0 |
754 | | """ |
755 | | if isinstance(other, RationalPlusInfinity): |
756 | | return 0 |
757 | | return 1 |
758 | | |
759 | | def __repr__(self): |
760 | | """ |
761 | | EXAMPLES: |
762 | | sage: E = ExtendedRationalField |
763 | | sage: repr(E(oo)) #indirect doctest |
764 | | '+Infinity' |
765 | | """ |
766 | | return "+Infinity" |
767 | | |
768 | | def _latex_(self): |
769 | | """ |
770 | | EXAMPLES: |
771 | | sage: E = ExtendedRationalField |
772 | | sage: latex(E(oo)) #indirect doctest |
773 | | +\infty |
774 | | """ |
775 | | return "+\\infty" |
776 | | |
777 | | def _add_(self, other): |
778 | | """ |
779 | | Returns self + other. |
780 | | |
781 | | EXAMPLES: |
782 | | sage: E = ExtendedRationalField |
783 | | sage: E(oo) + 2 #indirect doctest |
784 | | +Infinity |
785 | | sage: E(oo) + E(-oo) |
786 | | Traceback (most recent call last): |
787 | | ... |
788 | | SignError: cannot add infinity to minus infinity |
789 | | """ |
790 | | |
791 | | if isinstance(other, RationalMinusInfinity): |
792 | | raise SignError, "cannot add infinity to minus infinity" |
793 | | return self |
794 | | |
795 | | def _mul_(self, other): |
796 | | """ |
797 | | Returns self*other. |
798 | | |
799 | | EXAMPLES: |
800 | | sage: E = ExtendedRationalField |
801 | | sage: E(oo)*2 #indirect doctest |
802 | | +Infinity |
803 | | sage: E(oo)*E(2) |
804 | | +Infinity |
805 | | sage: E(oo)*E(-2) |
806 | | -Infinity |
807 | | sage: E(oo)*E(oo) |
808 | | +Infinity |
809 | | sage: E(oo)*E(-oo) |
810 | | -Infinity |
811 | | sage: E(oo)*E(0) |
812 | | Traceback (most recent call last): |
813 | | ... |
814 | | SignError: cannot multiply infinity by zero |
815 | | """ |
816 | | |
817 | | if other < 0: |
818 | | return -self |
819 | | if other > 0: |
820 | | return self |
821 | | raise SignError, "cannot multiply infinity by zero" |
822 | | |
823 | | def _sub_(self, other): |
824 | | """ |
825 | | Returns self-other. |
826 | | |
827 | | EXAMPLES: |
828 | | sage: E = ExtendedRationalField |
829 | | sage: E(oo) - E(2) #indirect doctest |
830 | | +Infinity |
831 | | sage: E(oo) - E(-oo) |
832 | | +Infinity |
833 | | sage: E(oo) - E(oo) |
834 | | Traceback (most recent call last): |
835 | | ... |
836 | | SignError: cannot subtract infinity from infinity |
837 | | """ |
838 | | |
839 | | if isinstance(other, RationalPlusInfinity): |
840 | | raise SignError, "cannot subtract infinity from infinity" |
841 | | return self |
842 | | |
843 | | def _div_(self, other): |
844 | | """ |
845 | | Returns self/other. |
846 | | |
847 | | EXAMPLES: |
848 | | sage: E = ExtendedRationalField |
849 | | sage: E(oo)/E(2) #indirect doctest |
850 | | +Infinity |
851 | | """ |
852 | | |
853 | | return self * other.__invert__() |
854 | | |
855 | | def _neg_(self): |
856 | | """ |
857 | | Returns the negation of self. |
858 | | |
859 | | EXAMPLES: |
860 | | sage: E = ExtendedRationalField |
861 | | sage: -E(oo) #indirect doctest |
862 | | -Infinity |
863 | | """ |
864 | | return self.parent().gen(2) |
865 | | |
866 | | def __invert__(self): |
867 | | """ |
868 | | Returns 1 / self. |
869 | | EXAMPLES: |
870 | | sage: E = ExtendedRationalField |
871 | | sage: ~E(oo) #indirect doctest |
872 | | 0 |
873 | | """ |
874 | | |
875 | | return ExtendedRational(0) |
876 | | |
877 | | def __abs__(self): |
878 | | """ |
879 | | EXAMPLES: |
880 | | sage: E = ExtendedRationalField |
881 | | sage: abs(E(oo)) #indirect doctest |
882 | | +Infinity |
883 | | """ |
884 | | return self |
885 | | |
886 | | def __pow__(self, right): |
887 | | """ |
888 | | Returns self^right. |
889 | | |
890 | | EXAMPLES: |
891 | | sage: E = ExtendedRationalField |
892 | | sage: E(oo)^2 #indirect doctest |
893 | | +Infinity |
894 | | sage: E(oo)^(-2) |
895 | | 0 |
896 | | sage: E(oo)^0 |
897 | | Traceback (most recent call last): |
898 | | ... |
899 | | SignError: Cannot raise infinity to the zeroth power |
900 | | sage: E(oo)^E(oo) |
901 | | +Infinity |
902 | | sage: R.<x> = QQ[] |
903 | | sage: E(oo)^x |
904 | | Traceback (most recent call last): |
905 | | ... |
906 | | TypeError: cannot exponentiate |
907 | | """ |
908 | | |
909 | | if not isinstance(right, (int, long, Integer, Rational, PlusInfinityElement, MinusInfinityElement)): |
910 | | raise TypeError, "cannot exponentiate" |
911 | | if right < 0: |
912 | | return ExtendedRational(0) |
913 | | elif right > 0: |
914 | | return self |
915 | | else: |
916 | | raise SignError, "Cannot raise infinity to the zeroth power" |
917 | | |
918 | | def lcm(self, x): |
919 | | """ |
920 | | Return the least common multiple of oo and x, which |
921 | | is by definition oo unless x is 0. |
922 | | |
923 | | EXAMPLES: |
924 | | sage: E = ExtendedRationalField |
925 | | sage: E(oo).lcm(0) |
926 | | 0 |
927 | | sage: E(oo).lcm(oo) |
928 | | +Infinity |
929 | | sage: E(oo).lcm(10) |
930 | | +Infinity |
931 | | """ |
932 | | if x == 0: |
933 | | return x |
934 | | else: |
935 | | return self |
936 | | |
937 | | def sqrt(self): |
938 | | """ |
939 | | Returns the square root of self. |
940 | | |
941 | | EXAMPLES: |
942 | | sage: E = ExtendedRationalField |
943 | | sage: E(oo).sqrt() |
944 | | +Infinity |
945 | | """ |
946 | | return self |
947 | | |
948 | | def nth_root(self, n): |
949 | | r""" |
950 | | Returns the $n^{th}$ root of self. |
951 | | |
952 | | EXAMPLES: |
953 | | sage: E = ExtendedRationalField |
954 | | sage: E(oo).nth_root(4) |
955 | | +Infinity |
956 | | sage: E(oo).nth_root(2) |
957 | | +Infinity |
958 | | """ |
959 | | return self |
960 | | |
961 | | def floor(self): |
962 | | """ |
963 | | Returns the floor of self. |
964 | | |
965 | | EXAMPLES: |
966 | | sage: E = ExtendedRationalField |
967 | | sage: E(oo).floor() |
968 | | +Infinity |
969 | | """ |
970 | | return IntegerPlusInfinity() |
971 | | |
972 | | def ceil(self): |
973 | | """ |
974 | | Returns the ceiling of self. |
975 | | |
976 | | EXAMPLES: |
977 | | sage: E = ExtendedRationalField |
978 | | sage: E(oo).ceil() |
979 | | +Infinity |
980 | | """ |
981 | | return IntegerPlusInfinity() |
982 | | |
983 | | def numerator(self): |
984 | | """ |
985 | | Returns the numerator of self. |
986 | | |
987 | | EXAMPLES: |
988 | | sage: E = ExtendedRationalField |
989 | | sage: E(oo).numerator() |
990 | | +Infinity |
991 | | """ |
992 | | return IntegerPlusInfinity() |
993 | | |
994 | | def denominator(self): |
995 | | """ |
996 | | Returns the denominator of self. |
997 | | |
998 | | EXAMPLES: |
999 | | sage: E = ExtendedRationalField |
1000 | | sage: E(oo).denominator() |
1001 | | 1 |
1002 | | """ |
1003 | | return ExtendedIntegerRing(1) |
1004 | | |
1005 | | class RationalMinusInfinity(_uniq2, MinusInfinityElement): |
1006 | | def __init__(self): |
1007 | | """ |
1008 | | EXAMPLES: |
1009 | | sage: E = ExtendedRationalField |
1010 | | sage: a = E(-oo) |
1011 | | sage: a == loads(dumps(a)) |
1012 | | True |
1013 | | """ |
1014 | | InfinityElement.__init__(self, ExtendedRationalField) |
1015 | | |
1016 | | def __cmp__(self, other): |
1017 | | """ |
1018 | | EXAMPLES: |
1019 | | sage: E = ExtendedRationalField |
1020 | | sage: cmp(E(-oo), 2) #indirect doctest |
1021 | | -1 |
1022 | | sage: cmp(E(-oo), E(-oo)) #indirect doctest |
1023 | | 0 |
1024 | | """ |
1025 | | |
1026 | | if isinstance(other, RationalMinusInfinity): |
1027 | | return 0 |
1028 | | return -1 |
1029 | | |
1030 | | def __repr__(self): |
1031 | | """ |
1032 | | EXAMPLES: |
1033 | | sage: E = ExtendedRationalField |
1034 | | sage: E(-oo).__repr__() |
1035 | | '-Infinity' |
1036 | | """ |
1037 | | return "-Infinity" |
1038 | | |
1039 | | def _latex_(self): |
1040 | | """ |
1041 | | EXAMPLES: |
1042 | | sage: E = ExtendedRationalField |
1043 | | sage: latex(E(-oo)) #indirect doctest |
1044 | | -\infty |
1045 | | """ |
1046 | | |
1047 | | return "-\\infty" |
1048 | | |
1049 | | def _add_(self, other): |
1050 | | """ |
1051 | | Returns self + other. |
1052 | | |
1053 | | EXAMPLES: |
1054 | | sage: E = ExtendedRationalField |
1055 | | sage: E(-oo) + 2 #indirect doctest |
1056 | | -Infinity |
1057 | | sage: E(-oo) + E(-oo) |
1058 | | -Infinity |
1059 | | sage: E(-oo) + E(oo) |
1060 | | Traceback (most recent call last): |
1061 | | ... |
1062 | | SignError: cannot add infinity to minus infinity |
1063 | | """ |
1064 | | |
1065 | | if isinstance(other, RationalPlusInfinity): |
1066 | | raise SignError, "cannot add infinity to minus infinity" |
1067 | | return self |
1068 | | |
1069 | | def _mul_(self, other): |
1070 | | """ |
1071 | | Returns self*other. |
1072 | | |
1073 | | EXAMPLES: |
1074 | | sage: E = ExtendedRationalField |
1075 | | sage: E(-oo)*2 #indirect doctest |
1076 | | -Infinity |
1077 | | sage: E(-oo)*-2 |
1078 | | +Infinity |
1079 | | sage: E(-oo)*E(-oo) |
1080 | | +Infinity |
1081 | | sage: E(-oo)*E(oo) |
1082 | | -Infinity |
1083 | | sage: E(-oo)*0 |
1084 | | Traceback (most recent call last): |
1085 | | ... |
1086 | | SignError: cannot multiply minus infinity by zero |
1087 | | """ |
1088 | | |
1089 | | if other < 0: |
1090 | | return -self |
1091 | | if other > 0: |
1092 | | return self |
1093 | | raise SignError, "cannot multiply minus infinity by zero" |
1094 | | |
1095 | | def _sub_(self, other): |
1096 | | """ |
1097 | | Returns self - other. |
1098 | | |
1099 | | EXAMPLES: |
1100 | | sage: E = ExtendedRationalField |
1101 | | sage: E(-oo) - 2 #indirect doctest |
1102 | | -Infinity |
1103 | | sage: E(-oo) - E(oo) |
1104 | | -Infinity |
1105 | | sage: E(-oo) - E(-oo) |
1106 | | Traceback (most recent call last): |
1107 | | ... |
1108 | | SignError: cannot subtract minus infinity from minus infinity |
1109 | | """ |
1110 | | if isinstance(other, RationalMinusInfinity): |
1111 | | raise SignError, "cannot subtract minus infinity from minus infinity" |
1112 | | return self |
1113 | | |
1114 | | def _div_(self, other): |
1115 | | """ |
1116 | | Returns self / other. |
1117 | | |
1118 | | EXAMPLES: |
1119 | | sage: E = ExtendedRationalField |
1120 | | sage: E(-oo)/2 #indirect doctest |
1121 | | -Infinity |
1122 | | """ |
1123 | | return self * other.__invert__() |
1124 | | |
1125 | | def _neg_(self): |
1126 | | """ |
1127 | | Returns the negation of self. |
1128 | | |
1129 | | EXAMPLES: |
1130 | | sage: E = ExtendedRationalField |
1131 | | sage: -E(-oo) #indirect doctest |
1132 | | +Infinity |
1133 | | """ |
1134 | | return self.parent().gen(1) |
1135 | | |
1136 | | def __invert__(self): |
1137 | | """ |
1138 | | Returns 1 / self. |
1139 | | |
1140 | | EXAMPLES: |
1141 | | sage: E = ExtendedRationalField |
1142 | | sage: ~E(-oo) #indirect doctest |
1143 | | 0 |
1144 | | """ |
1145 | | return ExtendedRational(0) |
1146 | | |
1147 | | def __abs__(self): |
1148 | | """ |
1149 | | Returns the absolute value of self. |
1150 | | |
1151 | | EXAMPLES: |
1152 | | sage: E = ExtendedRationalField |
1153 | | sage: abs(E(-oo)) #indirect doctest |
1154 | | +Infinity |
1155 | | """ |
1156 | | return -self |
1157 | | |
1158 | | def __pow__(self, right): |
1159 | | """ |
1160 | | Returns self^right. |
1161 | | |
1162 | | EXAMPLES: |
1163 | | sage: E = ExtendedRationalField |
1164 | | sage: E(-oo)^2 #indirect doctest |
1165 | | +Infinity |
1166 | | sage: E(-oo)^(1/3) |
1167 | | -Infinity |
1168 | | sage: E(-oo)^(2/3) |
1169 | | +Infinity |
1170 | | sage: E(-oo)^(1/4) |
1171 | | Traceback (most recent call last): |
1172 | | ... |
1173 | | SignError: Cannot take an even root of minus infinity |
1174 | | sage: E(-oo)^(-2) |
1175 | | 0 |
1176 | | sage: E(-oo)^4 |
1177 | | +Infinity |
1178 | | sage: E(-oo)^0 |
1179 | | Traceback (most recent call last): |
1180 | | ... |
1181 | | SignError: Cannot raise minus infinity to the zeroth power |
1182 | | sage: R.<x> = QQ[] |
1183 | | sage: E(-oo)^x |
1184 | | Traceback (most recent call last): |
1185 | | ... |
1186 | | TypeError: cannot exponentiate |
1187 | | """ |
1188 | | if isinstance(right, Rational): |
1189 | | if right.denominator() % 2 == 0: |
1190 | | raise SignError, "Cannot take an even root of minus infinity" |
1191 | | elif not isinstance(right, (int, long, Integer, PlusInfinityElement, MinusInfinityElement)): |
1192 | | raise TypeError, "cannot exponentiate" |
1193 | | if right < 0: |
1194 | | return ExtendedRational(0) |
1195 | | elif right > 0: |
1196 | | right = QQ(right) |
1197 | | if right.numerator() % 2 == 0: |
1198 | | return -self |
1199 | | else: |
1200 | | return self |
1201 | | else: |
1202 | | raise SignError, "Cannot raise minus infinity to the zeroth power" |
1203 | | |
1204 | | def lcm(self, x): |
1205 | | """ |
1206 | | Return the least common multiple of -oo and x, which |
1207 | | is by definition oo unless x is 0. |
1208 | | |
1209 | | EXAMPLES: |
1210 | | sage: E = ExtendedRationalField |
1211 | | sage: moo = E(-oo) |
1212 | | sage: moo.lcm(0) |
1213 | | 0 |
1214 | | sage: moo.lcm(oo) |
1215 | | +Infinity |
1216 | | sage: moo.lcm(10) |
1217 | | +Infinity |
1218 | | """ |
1219 | | if x == 0: |
1220 | | return x |
1221 | | else: |
1222 | | return -self |
1223 | | |
1224 | | def sqrt(self): |
1225 | | """ |
1226 | | Returns the square root of self. |
1227 | | |
1228 | | EXAMPLES: |
1229 | | sage: E = ExtendedRationalField |
1230 | | sage: E(-oo).sqrt() |
1231 | | Traceback (most recent call last): |
1232 | | ... |
1233 | | SignError: cannot take the square root of minus infinity |
1234 | | """ |
1235 | | raise SignError, "cannot take the square root of minus infinity" |
1236 | | |
1237 | | def nth_root(self, n): |
1238 | | """ |
1239 | | Returns the nth root of self. |
1240 | | |
1241 | | EXAMPLES: |
1242 | | sage: E = ExtendedRationalField |
1243 | | sage: E(-oo).nth_root(3) |
1244 | | -Infinity |
1245 | | sage: E(-oo).nth_root(4) |
1246 | | Traceback (most recent call last): |
1247 | | ... |
1248 | | SignError: cannot take an even root of minus infinity |
1249 | | """ |
1250 | | if n % 2 == 0: |
1251 | | raise SignError, "cannot take an even root of minus infinity" |
1252 | | return self |
1253 | | |
1254 | | def floor(self): |
1255 | | """ |
1256 | | Returns the floor of self. |
1257 | | |
1258 | | EXAMPLES: |
1259 | | sage: E = ExtendedRationalField |
1260 | | sage: E(-oo).floor() |
1261 | | -Infinity |
1262 | | """ |
1263 | | return IntegerMinusInfinity() |
1264 | | |
1265 | | def ceil(self): |
1266 | | """ |
1267 | | Returns the ceiling of self. |
1268 | | |
1269 | | EXAMPLES: |
1270 | | sage: E = ExtendedRationalField |
1271 | | sage: E(-oo).ceil() |
1272 | | -Infinity |
1273 | | """ |
1274 | | |
1275 | | return IntegerMinusInfinity() |
1276 | | |
1277 | | def numerator(self): |
1278 | | """ |
1279 | | Returns the numerator of self. |
1280 | | |
1281 | | EXAMPLES: |
1282 | | sage: E = ExtendedRationalField |
1283 | | sage: E(-oo).numerator() |
1284 | | -Infinity |
1285 | | """ |
1286 | | return IntegerMinusInfinity() |
1287 | | |
1288 | | def denominator(self): |
1289 | | """ |
1290 | | Returns the denominator of self. |
1291 | | |
1292 | | EXAMPLES: |
1293 | | sage: E = ExtendedRationalField |
1294 | | sage: E(-oo).denominator() |
1295 | | 1 |
1296 | | """ |
1297 | | return ExtendedIntegerRing(1) |