Ticket #13061: sage-trac_13061.patch

File sage-trac_13061.patch, 3.1 KB (added by mjo, 10 years ago)

Add an Expression.rectform() method.

  • sage/symbolic/expression.pyx

    # HG changeset patch
    # User Michael Orlitzky <michael@orlitzky.com>
    # Date 1338307639 14400
    # Node ID ce3cf5742774551d5d65a57704e339a15a836a9e
    # Parent  9ab4ab6e12d0ce97566db069205815303514de4d
    Trac #13061: Add an interface to Maxima's rectform.
    
    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b  
    68566856        from sage.symbolic.maxima_wrapper import MaximaWrapper
    68576857        return MaximaWrapper(self)
    68586858
     6859    def rectform(self):
     6860        r"""       
     6861        Convert this symbolic expression to rectangular form; that
     6862        is, the form `a + bi` where `a` and `b` are real numbers and
     6863        `i` is the imaginary unit.
     6864
     6865        .. note::
     6866
     6867           The name \"rectangular\" comes from the fact that, in the
     6868           complex plane, `a` and `bi` are perpendicular.
     6869
     6870        INPUT:
     6871
     6872        - ``self`` -- the expression to convert.
     6873
     6874        OUTPUT:
     6875
     6876        A new expression, equivalent to the original, but expressed in
     6877        the form `a + bi`.
     6878
     6879        ALGORITHM:
     6880
     6881        We call Maxima's ``rectform()`` and return the result unmodified.
     6882
     6883        EXAMPLES:
     6884
     6885        The exponential form of `\sin(x)`::
     6886
     6887            sage: f = (e^(I*x) - e^(-I*x)) / (2*I)
     6888            sage: f.rectform()
     6889            sin(x)
     6890
     6891        And `\cos(x)`::
     6892
     6893            sage: f = (e^(I*x) + e^(-I*x)) / 2
     6894            sage: f.rectform()
     6895            cos(x)
     6896
     6897        In some cases, this will simplify the given expression. For
     6898        example, here, `e^{ik\pi}`, `\sin(k\pi)=0` should cancel
     6899        leaving only `\cos(k\pi)` which can then be simplified::
     6900
     6901            sage: k = var('k')
     6902            sage: assume(k, 'integer')
     6903            sage: f = e^(I*pi*k)
     6904            sage: f.rectform()
     6905            (-1)^k
     6906
     6907        However, in general, the resulting expression may be more
     6908        complicated than the original::
     6909
     6910            sage: f = e^(I*x)
     6911            sage: f.rectform()
     6912            I*sin(x) + cos(x)
     6913
     6914        TESTS:
     6915
     6916        If the expression is already in rectangular form, it should be
     6917        left alone::
     6918
     6919            sage: a,b = var('a,b')
     6920            sage: assume((a, 'real'), (b, 'real'))
     6921            sage: f = a + b*I
     6922            sage: f.rectform()
     6923            a + I*b
     6924            sage: forget()
     6925
     6926        We can check with specific real numbers::
     6927
     6928            sage: a = RR.random_element()   
     6929            sage: b = RR.random_element()
     6930            sage: f = a + b*I
     6931            sage: bool(f.rectform() == a + b*I)
     6932            True
     6933
     6934        If we decompose a complex number into its real and imaginary
     6935        parts, they should correspond to the real and imaginary terms
     6936        of the rectangular form::
     6937
     6938            sage: z = CC.random_element()
     6939            sage: a = z.real_part()
     6940            sage: b = z.imag_part()
     6941            sage: bool(SR(z).rectform() == a + b*I)
     6942            True
     6943
     6944        """
     6945        return self.maxima_methods().rectform()
     6946
    68596947    def simplify(self):
    68606948        """
    68616949        Returns a simplified version of this symbolic expression.