Mathematical Functions

All functions support the methods documented below, inherited from diofant.core.function.Function.

class diofant.core.function.Function(*args)[source]

Base class for applied mathematical functions.

It also serves as a constructor for undefined function classes.

Examples

First example shows how to use Function as a constructor for undefined function classes:

>>> g = g(x)
>>> f
f
>>> f(x)
f(x)
>>> g
g(x)
>>> f(x).diff(x)
Derivative(f(x), x)
>>> g.diff(x)
Derivative(g(x), x)

In the following example Function is used as a base class for MyFunc that represents a mathematical function MyFunc. Suppose that it is well known, that MyFunc(0) is 1 and MyFunc at infinity goes to 0, so we want those two simplifications to occur automatically. Suppose also that MyFunc(x) is real exactly when x is real. Here is an implementation that honours those requirements:

>>> class MyFunc(Function):
...
...     @classmethod
...     def eval(cls, x):
...         if x.is_Number:
...             if x == 0:
...                 return Integer(1)
...             elif x is oo:
...                 return Integer(0)
...
...     def _eval_is_real(self):
...         return self.args[0].is_real
...
>>> MyFunc(0) + sin(0)
1
>>> MyFunc(oo)
0
>>> MyFunc(3.54).evalf()  # Not yet implemented for MyFunc.
MyFunc(3.54)
>>> MyFunc(I).is_real
False

In order for MyFunc to become useful, several other methods would need to be implemented. See source code of some of the already implemented functions for more complete examples.

Also, if the function can take more than one argument, then nargs must be defined, e.g. if MyFunc can take one or two arguments then,

>>> class MyFunc(Function):
...     nargs = (1, 2)
...
>>>
classmethod class_key()[source]

Nice order of classes.

fdiff(argindex=1)[source]

Returns the first derivative of the function.