6418 | | |
6419 | | def numerator(self): |
6420 | | """ |
6421 | | Returns the numerator of this symbolic expression. If the |
6422 | | expression is not a quotient, then this will return the |
6423 | | expression itself. |
6424 | | |
6425 | | EXAMPLES:: |
6426 | | |
| 6418 | |
| 6419 | def numerator(self, bint normalize = True): |
| 6420 | """ |
| 6421 | Returns the numerator of this symbolic expression |
| 6422 | |
| 6423 | INPUT: |
| 6424 | |
| 6425 | - ``normalize`` -- (default: ``True``) a boolean. |
| 6426 | |
| 6427 | If ``normalize`` is ``True``, the expression is first normalized to |
| 6428 | have it as a fraction before getting the numerator. |
| 6429 | |
| 6430 | If ``normalize`` is ``False``, the expression is kept and if it is not |
| 6431 | a quotient, then this will return the expression itself. |
| 6432 | |
| 6433 | EXAMPLES:: |
| 6434 | |
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 | """ |
| 6466 | cdef GExVector vec |
| 6467 | cdef GEx oper, power |
| 6468 | cdef int py_pow |
| 6469 | if normalize: |
| 6470 | return new_Expression_from_GEx(self._parent, self._gobj.numer()) |
| 6471 | elif is_a_mul(self._gobj): |
| 6472 | for i from 0 <= i < self._gobj.nops(): |
| 6473 | oper = self._gobj.op(i) |
| 6474 | if not is_a_power(oper): |
| 6475 | vec.push_back(oper) |
| 6476 | else: |
| 6477 | power = oper.op(1) |
| 6478 | if not is_a_numeric(power): |
| 6479 | raise TypeError, "self is not a rational expression" |
| 6480 | elif py_object_from_numeric(power) >= 0: |
| 6481 | vec.push_back(oper) |
| 6482 | return new_Expression_from_GEx(self._parent, |
| 6483 | g_mul_construct(vec, True)) |
| 6484 | else: |
| 6485 | return self |
| 6486 | |
| 6487 | def denominator(self, bint normalize=True): |
| 6488 | """ |
| 6489 | Returns the denominator of this symbolic expression |
| 6490 | |
| 6491 | INPUT: |
| 6492 | |
| 6493 | - ``normalize`` -- (default: ``True``) a boolean. |
| 6494 | |
| 6495 | If ``normalize`` is ``True``, the expression is first normalized to |
| 6496 | have it as a fraction before getting the denominator. |
| 6497 | |
| 6498 | If ``normalize`` is ``False``, the expression is kept and if it is not |
| 6499 | a quotient, then this will just return 1. |
| 6500 | |
| 6501 | EXAMPLES:: |
| 6502 | |
6465 | | """ |
6466 | | return self.parent()(self._maxima_().denom()) |
| 6522 | |
| 6523 | TESTS:: |
| 6524 | |
| 6525 | sage: ((x+y)^2/(x-y)^3*x^3).denominator(normalize=False) |
| 6526 | (x - y)^3 |
| 6527 | sage: ((x+y)^2*x^3).denominator(normalize=False) |
| 6528 | 1 |
| 6529 | |
| 6530 | """ |
| 6531 | cdef GExVector vec |
| 6532 | cdef GEx oper, ex, power |
| 6533 | cdef int py_pow |
| 6534 | if normalize: |
| 6535 | return new_Expression_from_GEx(self._parent, self._gobj.denom()) |
| 6536 | elif is_a_mul(self._gobj): |
| 6537 | for i from 0 <= i < self._gobj.nops(): |
| 6538 | oper = self._gobj.op(i) |
| 6539 | if is_a_power(oper): |
| 6540 | ex = oper.op(0) |
| 6541 | power = oper.op(1) |
| 6542 | if not is_a_numeric(power): |
| 6543 | raise TypeError, "self is not a rational expression" |
| 6544 | else: |
| 6545 | py_pow = py_object_from_numeric(power) |
| 6546 | if py_pow == -1: |
| 6547 | vec.push_back(ex) |
| 6548 | elif py_pow < 0: |
| 6549 | vec.push_back(g_pow(ex, g_abs(power))) |
| 6550 | return new_Expression_from_GEx(self._parent, |
| 6551 | g_mul_construct(vec, False)) |
| 6552 | else: |
| 6553 | return self._parent.one() |
| 6554 | |
| 6555 | def numerator_denominator(self, bint normalize=True): |
| 6556 | """ |
| 6557 | Returns the numerator and the denominator of this symbolic expression |
| 6558 | |
| 6559 | INPUT: |
| 6560 | |
| 6561 | - ``normalize`` -- (default: ``True``) a boolean. |
| 6562 | |
| 6563 | If ``normalize`` is ``True``, the expression is first normalized to |
| 6564 | have it as a fraction before getting the numerator and denominator. |
| 6565 | |
| 6566 | If ``normalize`` is ``False``, the expression is kept and if it is not |
| 6567 | a quotient, then this will return the expression itself together with |
| 6568 | 1. |
| 6569 | |
| 6570 | EXAMPLE:: |
| 6571 | |
| 6572 | sage: x, y, a = var("x y a") |
| 6573 | sage: ((x+y)^2/(x-y)^3*x^3).numerator_denominator() |
| 6574 | ((x + y)^2*x^3, (x - y)^3) |
| 6575 | |
| 6576 | sage: ((x+y)^2/(x-y)^3*x^3).numerator_denominator(False) |
| 6577 | ((x + y)^2*x^3, (x - y)^3) |
| 6578 | |
| 6579 | sage: g = x + y/(x + 2) |
| 6580 | sage: g.numerator_denominator() |
| 6581 | (x^2 + 2*x + y, x + 2) |
| 6582 | sage: g.numerator_denominator(normalize=False) |
| 6583 | (x + y/(x + 2), 1) |
| 6584 | |
| 6585 | sage: g = x^2*(x + 2) |
| 6586 | sage: g.numerator_denominator() |
| 6587 | ((x + 2)*x^2, 1) |
| 6588 | sage: g.numerator_denominator(normalize=False) |
| 6589 | ((x + 2)*x^2, 1) |
| 6590 | """ |
| 6591 | cdef GExVector vecnum, vecdenom |
| 6592 | cdef GEx oper, ex, power |
| 6593 | cdef int py_pow |
| 6594 | if normalize: |
| 6595 | ex = self._gobj.numer_denom() |
| 6596 | return (new_Expression_from_GEx(self._parent, ex.op(0)), |
| 6597 | new_Expression_from_GEx(self._parent, ex.op(1))) |
| 6598 | elif is_a_mul(self._gobj): |
| 6599 | for i from 0 <= i < self._gobj.nops(): |
| 6600 | oper = self._gobj.op(i) |
| 6601 | if is_a_power(oper): # oper = ex^power |
| 6602 | ex = oper.op(0) |
| 6603 | power = oper.op(1) |
| 6604 | if not is_a_numeric(power): |
| 6605 | raise TypeError, "self is not a rational expression" |
| 6606 | else: |
| 6607 | py_pow = py_object_from_numeric(power) |
| 6608 | if py_pow >= 0: |
| 6609 | vecnum.push_back(oper) |
| 6610 | elif py_pow == -1: |
| 6611 | vecdenom.push_back(ex) |
| 6612 | else: |
| 6613 | vecdenom.push_back(g_pow(ex, g_abs(power))) |
| 6614 | else: |
| 6615 | vecnum.push_back(oper) |
| 6616 | return (new_Expression_from_GEx(self._parent, |
| 6617 | g_mul_construct(vecnum, False)), |
| 6618 | new_Expression_from_GEx(self._parent, |
| 6619 | g_mul_construct(vecdenom, False))) |
| 6620 | else: |
| 6621 | return (self, self._parent.one()) |