An Example: Defining Your Own Integration Function
Now that we have introduced the basic features of patterns in Mathematica, we can use them to give a more or less complete example. We will show how you could define your own simple integration function in Mathematica.
From a mathematical point of view, the integration function is defined by a sequence of mathematical relations. By setting up transformation rules for patterns, you can implement these mathematical relations quite directly in Mathematica.
|mathematical form||Mathematica definition|
| ( independent of )||integrate[c_y_,x_]:=c integrate[y,x]/;FreeQ[c,x]|
Definitions for an integration function.
This implements the linearity relation for integrals:
The associativity of Plus
makes the linearity relation work with any number of terms in the sum.
pull out factors that are independent of the integration variable
tests each term in each product to see whether it satisfies the FreeQ
condition, and so can be pulled out.
This gives the integral
of a constant.
Now the constant term in the sum can be integrated.
This gives the standard formula for the integral of
. By using the pattern
, rather than
, we include the case of
Now this integral can be done completely.
Of course, the built-in integration function Integrate
(with a capital I
) could have done the integral anyway.
Here is the rule for integrating the reciprocal of a linear function. The pattern
stands for any linear function of
take on their default values.
Here is a more complicated case. The symbol
You can go on and add many more rules for integration. Here is a rule for integrating exponentials.