Functions �
The Function class is a subclass of Expr, which makes it easier to define mathematical functions called with arguments. This includes named functions like cos(x) and log(x) as well as undefined functions like f(x). Subclasses of Function should define a class method eval, which returns a canonical form of the function application (usually an instance of some other class, i.e. a Number) or None, if for given arguments that function should not be automatically evaluated.
Many SymPy functions perform various evaluations down the expression tree. Classes define their behavior in such functions by defining a relevant _eval_* method. For instance, an object can indicate to the diff function how to take the derivative of itself by defining the _eval_derivative(self, x) method, which may in turn call diff on its args. (Subclasses of Function should implement fdiff method instead, it returns the derivative of the function without considering the chain rule.) The most common _eval_* methods relate to the assumptions: _eval_is_assumption is used to deduce assumption on the object.
As an example of the notions presented in this section, Listing presents a minimal version of the gamma function Γ(x) from SymPy, which evaluates itself on positive integer arguments, has the positive and real assumptions defined, can be rewritten in terms of factorial with gamma(x).rewrite(factorial), and can be differentiated. self.func is used throughout instead of referencing gamma explicitly so that potential subclasses of gamma can reuse the methods.
>>> from sympy import Function, Integer, floor, factorial, polygamma
>>> class gamma(Function)
@classmethod
def eval(cls, arg):
if isinstance(arg, Integer) and arg.is_positive:
return factorial(arg - 1)
def _eval_is_positive(self):
x = self.args[0]
if x.is_positive:
return True
elif x.is_noninteger:
return floor(x).is_even
All functions support the methods documented below, inherited from
sympy.core.function.Function
.
-
class
sympy.core.function.
Function
[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:
>>> from sympy import Function, Symbol >>> x = Symbol('x') >>> f = Function('f') >>> g = Function('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
my_func
that represents a mathematical function my_func. Suppose that it is well known, that my_func(0) is 1 and my_func at infinity goes to 0, so we want those two simplifications to occur automatically. Suppose also that my_func(x) is real exactly when x is real. Here is an implementation that honours those requirements:>>> from sympy import Function, S, oo, I, sin >>> class my_func(Function): ... ... @classmethod ... def eval(cls, x): ... if x.is_Number: ... if x is S.Zero: ... return S.One ... elif x is S.Infinity: ... return S.Zero ... ... def _eval_is_real(self): ... return self.args[0].is_real ... >>> x = S('x') >>> my_func(0) + sin(0) 1 >>> my_func(oo) 0 >>> my_func(3.54).n() # Not yet implemented for my_func. my_func(3.54) >>> my_func(I).is_real False
In order for
my_func
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. ifmy_func
can take one or two arguments then,>>> class my_func(Function): ... nargs = (1, 2) ... >>>
-
as_base_exp
()[source] Returns the method as the 2-tuple (base, exponent).
-
fdiff
(argindex=1)[source] Returns the first derivative of the function.
-
is_commutative
Returns whether the functon is commutative.
-
Contents�
- Elementary
- sympy.functions.elementary.complexes
- sympy.functions.elementary.trigonometric
- Trionometric Functions
- Trigonometric Inverses
- sympy.functions.elementary.hyperbolic
- Hyperbolic Functions
- Hyperbolic Inverses
- sympy.functions.elementary.integers
- sympy.functions.elementary.exponential
- sympy.functions.elementary.piecewise
- sympy.functions.elementary.miscellaneous
- Combinatorial
- Enumeration
- Special
- DiracDelta
- Heaviside
- Singularity Function
- Gamma, Beta and related Functions
- Error Functions and Fresnel Integrals
- Exponential, Logarithmic and Trigonometric Integrals
- Bessel Type Functions
- Airy Functions
- B-Splines
- Riemann Zeta and Related Functions
- Hypergeometric Functions
- Elliptic integrals
- Mathieu Functions
- Orthogonal Polynomials
- Spherical Harmonics
- Tensor Functions