www.delorie.com/gnu/docs/calc/calc_256.html   search  
Buy the book!

GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

11.5.3 Customizing the Integrator

Calc has two built-in rewrite rules called IntegRules and IntegAfterRules which you can edit to define new integration methods. See section 11.11 Rewrite Rules. At each step of the integration process, Calc wraps the current integrand in a call to the fictitious function `integtry(expr,var)', where expr is the integrand and var is the integration variable. If your rules rewrite this to be a plain formula (not a call to integtry), then Calc will use this formula as the integral of expr. For example, the rule `integtry(mysin(x),x) := -mycos(x)' would define a rule to integrate a function mysin that acts like the sine function. Then, putting `4 mysin(2y+1)' on the stack and typing a i y will produce the integral `-2 mycos(2y+1)'. Note that Calc has automatically made various transformations on the integral to allow it to use your rule; integral tables generally give rules for `mysin(a x + b)', but you don't need to use this much generality in your IntegRules.

As a more serious example, the expression `exp(x)/x' cannot be integrated in terms of the standard functions, so the "exponential integral" function Ei(x) was invented to describe it. We can get Calc to do this integral in terms of a made-up Ei function by adding the rule `[integtry(exp(x)/x, x) := Ei(x)]' to IntegRules. Now entering `exp(2x)/x' on the stack and typing a i x yields `Ei(2 x)'. This new rule will work with Calc's various built-in integration methods (such as integration by substitution) to solve a variety of other problems involving Ei: For example, now Calc will also be able to integrate `exp(exp(x))' and `ln(ln(x))' (to get `Ei(exp(x))' and `x ln(ln(x)) - Ei(ln(x))', respectively).

Your rule may do further integration by calling integ. For example, `integtry(twice(u),x) := twice(integ(u))' allows Calc to integrate `twice(sin(x))' to get `twice(-cos(x))'. Note that integ was called with only one argument. This notation is allowed only within IntegRules; it means "integrate this with respect to the same integration variable." If Calc is unable to integrate u, the integration that invoked IntegRules also fails. Thus integrating `twice(f(x))' fails, returning the unevaluated integral `integ(twice(f(x)), x)'. It is still legal to call integ with two or more arguments, however; in this case, if u is not integrable, twice itself will still be integrated: If the above rule is changed to `... := twice(integ(u,x))', then integrating `twice(f(x))' will yield `twice(integ(f(x),x))'.

If a rule instead produces the formula `integsubst(sexpr, svar)', either replacing the top-level integtry call or nested anywhere inside the expression, then Calc will apply the substitution `u = sexpr(svar)' to try to integrate the original expr. For example, the rule `sqrt(a) := integsubst(sqrt(x),x)' says that if Calc ever finds a square root in the integrand, it should attempt the substitution `u = sqrt(x)'. (This particular rule is unnecessary because Calc always tries "obvious" substitutions where sexpr actually appears in the integrand.) The variable svar may be the same as the var that appeared in the call to integtry, but it need not be.

When integrating according to an integsubst, Calc uses the equation solver to find the inverse of sexpr (if the integrand refers to var anywhere except in subexpressions that exactly match sexpr). It uses the differentiator to find the derivative of sexpr and/or its inverse (it has two methods that use one derivative or the other). You can also specify these items by adding extra arguments to the integsubst your rules construct; the general form is `integsubst(sexpr, svar, sinv, sprime)', where sinv is the inverse of sexpr (still written as a function of svar), and sprime is the derivative of sexpr with respect to svar. If you don't specify these things, and Calc is not able to work them out on its own with the information it knows, then your substitution rule will work only in very specific, simple cases.

Calc applies IntegRules as if by C-u 1 a r IntegRules; in other words, Calc stops rewriting as soon as any rule in your rule set succeeds. (If it weren't for this, the `integsubst(sqrt(x),x)' example above would keep on adding layers of integsubst calls forever!)

Another set of rules, stored in IntegSimpRules, are applied every time the integrator uses a s to simplify an intermediate result. For example, putting the rule `twice(x) := 2 x' into IntegSimpRules would tell Calc to convert the twice function into a form it knows whenever integration is attempted.

One more way to influence the integrator is to define a function with the Z F command (see section 18.4 Programming with Formulas). Calc's integrator automatically expands such functions according to their defining formulas, even if you originally asked for the function to be left unevaluated for symbolic arguments. (Certain other Calc systems, such as the differentiator and the equation solver, also do this.)

Sometimes Calc is able to find a solution to your integral, but it expresses the result in a way that is unnecessarily complicated. If this happens, you can either use integsubst as described above to try to hint at a more direct path to the desired result, or you can use IntegAfterRules. This is an extra rule set that runs after the main integrator returns its result; basically, Calc does an a r IntegAfterRules on the result before showing it to you. (It also does an a s, without IntegSimpRules, after that to further simplify the result.) For example, Calc's integrator sometimes produces expressions of the form `ln(1+x) - ln(1-x)'; the default IntegAfterRules rewrite this into the more readable form `2 arctanh(x)'. Note that, unlike IntegRules, IntegSimpRules and IntegAfterRules are applied any number of times until no further changes are possible. Rewriting by IntegAfterRules occurs only after the main integrator has finished, not at every step as for IntegRules and IntegSimpRules.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003