6443 | | """ |
6444 | | return self.parent()(self._maxima_().num()) |
6445 | | |
6446 | | def denominator(self): |
6447 | | """ |
6448 | | Returns the denominator of this symbolic expression. If the |
6449 | | expression is not a quotient, then this will just return 1. |
6450 | | |
6451 | | EXAMPLES:: |
6452 | | |
| 6459 | TESTS:: |
| 6460 | |
| 6461 | sage: ((x+y)^2/(x-y)^3*x^3).numerator(normalize=False) |
| 6462 | (x + y)^2*x^3 |
| 6463 | sage: ((x+y)^2*x^3).numerator(normalize=False) |
| 6464 | (x + y)^2*x^3 |
| 6465 | sage: (y/x^3).numerator(normalize=False) |
| 6466 | y |
| 6467 | sage: t = y/x^3/(x+y)^(1/2); t |
| 6468 | y/(sqrt(x + y)*x^3) |
| 6469 | sage: t.numerator(normalize=False) |
| 6470 | y |
| 6471 | sage: (1/x^3).numerator(normalize=False) |
| 6472 | 1 |
| 6473 | sage: (x^3).numerator(normalize=False) |
| 6474 | x^3 |
| 6475 | sage: (y*x^sin(x)).numerator(normalize=False) |
| 6476 | Traceback (most recent call last): |
| 6477 | ... |
| 6478 | TypeError: self is not a rational expression |
| 6479 | """ |
| 6480 | cdef GExVector vec |
| 6481 | cdef GEx oper, power |
| 6482 | if normalize: |
| 6483 | return new_Expression_from_GEx(self._parent, self._gobj.numer()) |
| 6484 | elif is_a_mul(self._gobj): |
| 6485 | for i from 0 <= i < self._gobj.nops(): |
| 6486 | oper = self._gobj.op(i) |
| 6487 | if not is_a_power(oper): |
| 6488 | vec.push_back(oper) |
| 6489 | else: |
| 6490 | power = oper.op(1) |
| 6491 | if not is_a_numeric(power): |
| 6492 | raise TypeError, "self is not a rational expression" |
| 6493 | elif ex_to_numeric(power).is_positive(): |
| 6494 | vec.push_back(oper) |
| 6495 | return new_Expression_from_GEx(self._parent, |
| 6496 | g_mul_construct(vec, True)) |
| 6497 | elif is_a_power(self._gobj): |
| 6498 | power = self._gobj.op(1) |
| 6499 | if is_a_numeric(power) and ex_to_numeric(power).is_negative(): |
| 6500 | return self._parent.one() |
| 6501 | return self |
| 6502 | |
| 6503 | def denominator(self, bint normalize=True): |
| 6504 | """ |
| 6505 | Returns the denominator of this symbolic expression |
| 6506 | |
| 6507 | INPUT: |
| 6508 | |
| 6509 | - ``normalize`` -- (default: ``True``) a boolean. |
| 6510 | |
| 6511 | If ``normalize`` is ``True``, the expression is first normalized to |
| 6512 | have it as a fraction before getting the denominator. |
| 6513 | |
| 6514 | If ``normalize`` is ``False``, the expression is kept and if it is not |
| 6515 | a quotient, then this will just return 1. |
| 6516 | |
| 6517 | EXAMPLES:: |
| 6518 | |
6465 | | """ |
6466 | | return self.parent()(self._maxima_().denom()) |
| 6538 | |
| 6539 | TESTS:: |
| 6540 | |
| 6541 | sage: ((x+y)^2/(x-y)^3*x^3).denominator(normalize=False) |
| 6542 | (x - y)^3 |
| 6543 | sage: ((x+y)^2*x^3).denominator(normalize=False) |
| 6544 | 1 |
| 6545 | sage: (y/x^3).denominator(normalize=False) |
| 6546 | x^3 |
| 6547 | sage: t = y/x^3/(x+y)^(1/2); t |
| 6548 | y/(sqrt(x + y)*x^3) |
| 6549 | sage: t.denominator(normalize=False) |
| 6550 | sqrt(x + y)*x^3 |
| 6551 | sage: (1/x^3).denominator(normalize=False) |
| 6552 | x^3 |
| 6553 | sage: (x^3).denominator(normalize=False) |
| 6554 | 1 |
| 6555 | sage: (y*x^sin(x)).denominator(normalize=False) |
| 6556 | Traceback (most recent call last): |
| 6557 | ... |
| 6558 | TypeError: self is not a rational expression |
| 6559 | """ |
| 6560 | cdef GExVector vec |
| 6561 | cdef GEx oper, ex, power |
| 6562 | if normalize: |
| 6563 | return new_Expression_from_GEx(self._parent, self._gobj.denom()) |
| 6564 | elif is_a_mul(self._gobj): |
| 6565 | for i from 0 <= i < self._gobj.nops(): |
| 6566 | oper = self._gobj.op(i) |
| 6567 | if is_a_power(oper): |
| 6568 | ex = oper.op(0) |
| 6569 | power = oper.op(1) |
| 6570 | if not is_a_numeric(power): |
| 6571 | raise TypeError, "self is not a rational expression" |
| 6572 | elif ex_to_numeric(power).is_negative(): |
| 6573 | vec.push_back(g_pow(ex, g_abs(power))) |
| 6574 | return new_Expression_from_GEx(self._parent, |
| 6575 | g_mul_construct(vec, False)) |
| 6576 | elif is_a_power(self._gobj): |
| 6577 | power = self._gobj.op(1) |
| 6578 | if is_a_numeric(power) and ex_to_numeric(power).is_negative(): |
| 6579 | return new_Expression_from_GEx(self._parent, |
| 6580 | g_pow(self._gobj.op(0), g_abs(power))) |
| 6581 | |
| 6582 | return self._parent.one() |
| 6583 | |
| 6584 | def numerator_denominator(self, bint normalize=True): |
| 6585 | """ |
| 6586 | Returns the numerator and the denominator of this symbolic expression |
| 6587 | |
| 6588 | INPUT: |
| 6589 | |
| 6590 | - ``normalize`` -- (default: ``True``) a boolean. |
| 6591 | |
| 6592 | If ``normalize`` is ``True``, the expression is first normalized to |
| 6593 | have it as a fraction before getting the numerator and denominator. |
| 6594 | |
| 6595 | If ``normalize`` is ``False``, the expression is kept and if it is not |
| 6596 | a quotient, then this will return the expression itself together with |
| 6597 | 1. |
| 6598 | |
| 6599 | EXAMPLE:: |
| 6600 | |
| 6601 | sage: x, y, a = var("x y a") |
| 6602 | sage: ((x+y)^2/(x-y)^3*x^3).numerator_denominator() |
| 6603 | ((x + y)^2*x^3, (x - y)^3) |
| 6604 | |
| 6605 | sage: ((x+y)^2/(x-y)^3*x^3).numerator_denominator(False) |
| 6606 | ((x + y)^2*x^3, (x - y)^3) |
| 6607 | |
| 6608 | sage: g = x + y/(x + 2) |
| 6609 | sage: g.numerator_denominator() |
| 6610 | (x^2 + 2*x + y, x + 2) |
| 6611 | sage: g.numerator_denominator(normalize=False) |
| 6612 | (x + y/(x + 2), 1) |
| 6613 | |
| 6614 | sage: g = x^2*(x + 2) |
| 6615 | sage: g.numerator_denominator() |
| 6616 | ((x + 2)*x^2, 1) |
| 6617 | sage: g.numerator_denominator(normalize=False) |
| 6618 | ((x + 2)*x^2, 1) |
| 6619 | |
| 6620 | TESTS:: |
| 6621 | |
| 6622 | sage: ((x+y)^2/(x-y)^3*x^3).numerator_denominator(normalize=False) |
| 6623 | ((x + y)^2*x^3, (x - y)^3) |
| 6624 | sage: ((x+y)^2*x^3).numerator_denominator(normalize=False) |
| 6625 | ((x + y)^2*x^3, 1) |
| 6626 | sage: (y/x^3).numerator_denominator(normalize=False) |
| 6627 | (y, x^3) |
| 6628 | sage: t = y/x^3/(x+y)^(1/2); t |
| 6629 | y/(sqrt(x + y)*x^3) |
| 6630 | sage: t.numerator_denominator(normalize=False) |
| 6631 | (y, sqrt(x + y)*x^3) |
| 6632 | sage: (1/x^3).numerator_denominator(normalize=False) |
| 6633 | (1, x^3) |
| 6634 | sage: (x^3).numerator_denominator(normalize=False) |
| 6635 | (x^3, 1) |
| 6636 | sage: (y*x^sin(x)).numerator_denominator(normalize=False) |
| 6637 | Traceback (most recent call last): |
| 6638 | ... |
| 6639 | TypeError: self is not a rational expression |
| 6640 | """ |
| 6641 | cdef GExVector vecnumer, vecdenom |
| 6642 | cdef GEx oper, ex, power |
| 6643 | cdef int py_pow |
| 6644 | cdef GNumeric power_num |
| 6645 | if normalize: |
| 6646 | ex = self._gobj.numer_denom() |
| 6647 | return (new_Expression_from_GEx(self._parent, ex.op(0)), |
| 6648 | new_Expression_from_GEx(self._parent, ex.op(1))) |
| 6649 | elif is_a_mul(self._gobj): |
| 6650 | for i from 0 <= i < self._gobj.nops(): |
| 6651 | oper = self._gobj.op(i) |
| 6652 | if is_a_power(oper): # oper = ex^power |
| 6653 | ex = oper.op(0) |
| 6654 | power = oper.op(1) |
| 6655 | if not is_a_numeric(power): |
| 6656 | raise TypeError, "self is not a rational expression" |
| 6657 | elif is_a_numeric(power): |
| 6658 | power_num = ex_to_numeric(power) |
| 6659 | if power_num.is_positive(): |
| 6660 | vecnumer.push_back(oper) |
| 6661 | else: |
| 6662 | vecdenom.push_back(g_pow(ex, g_abs(power))) |
| 6663 | else: |
| 6664 | vecnumer.push_back(oper) |
| 6665 | return (new_Expression_from_GEx(self._parent, |
| 6666 | g_mul_construct(vecnumer, False)), |
| 6667 | new_Expression_from_GEx(self._parent, |
| 6668 | g_mul_construct(vecdenom, False))) |
| 6669 | elif is_a_power(self._gobj): |
| 6670 | power = self._gobj.op(1) |
| 6671 | if is_a_numeric(power) and ex_to_numeric(power).is_positive(): |
| 6672 | return (self, self._parent.one()) |
| 6673 | else: |
| 6674 | return (self._parent.one(), |
| 6675 | new_Expression_from_GEx(self._parent, |
| 6676 | g_pow(self._gobj.op(0), g_abs(power)))) |
| 6677 | else: |
| 6678 | return (self, self._parent.one()) |