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