sympy/functions/elementary/tests/test_interface.py
author Vinzent Steinberg <Vinzent.Steinberg@gmail.com>
Thu Nov 20 02:14:29 2008 +0100 (2 weeks ago)
changeset 1916 42fa25e25e6e
parent 816378e7736109d
permissions -rw-r--r--
add a comment why a "working" test is xfailed
     1 # This test file tests the SymPy function interface, that people use to create
     2 # their own new functions. It should be as easy as possible.
     3 
     4 from sympy import Function, sympify, sin, cos, limit, tanh
     5 from sympy.abc import x
     6 
     7 def test_function_series1():
     8     """Create our new "sin" function."""
     9 
    10     class my_function(Function):
    11         nargs = 1
    12 
    13         def fdiff(self, argindex = 1):
    14             return cos(self.args[0])
    15 
    16         @classmethod
    17         def canonize(cls, arg):
    18             arg = sympify(arg)
    19             if arg == 0:
    20                 return sympify(0)
    21 
    22     #Test that the taylor series is correct
    23     assert my_function(x).series(x, 0, 10) == sin(x).series(x, 0, 10)
    24     assert limit(my_function(x)/x, x, 0) == 1
    25 
    26 def test_function_series2():
    27     """Create our new "cos" function."""
    28 
    29     class my_function2(Function):
    30         nargs = 1
    31 
    32         def fdiff(self, argindex = 1):
    33             return -sin(self.args[0])
    34 
    35         @classmethod
    36         def canonize(cls, arg):
    37             arg = sympify(arg)
    38             if arg == 0:
    39                 return sympify(1)
    40 
    41     #Test that the taylor series is correct
    42     assert my_function2(x).series(x, 0, 10) == cos(x).series(x, 0, 10)
    43 
    44 def test_function_series3():
    45     """
    46     Test our easy "tanh" function.
    47 
    48     This test tests two things:
    49       * that the Function interface works as expected and it's easy to use
    50       * that the general algorithm for the series expansion works even when the
    51         derivative is defined recursively in terms of the original function,
    52         since tanh(x).diff(x) == 1-tanh(x)**2
    53     """
    54 
    55     class mytanh(Function):
    56         nargs = 1
    57 
    58         def fdiff(self, argindex = 1):
    59             return 1-mytanh(self.args[0])**2
    60 
    61         @classmethod
    62         def canonize(cls, arg):
    63             arg = sympify(arg)
    64             if arg == 0:
    65                 return sympify(0)
    66 
    67     e = tanh(x)
    68     f = mytanh(x)
    69     assert tanh(x).series(x, 0, 6) == mytanh(x).series(x, 0, 6)