% Copyright The Numerical Algorithms Group Limited 1991.
% Certain derivative-work portions Copyright (C) 1988 by Leslie Lamport.
% All rights reserved.

\begin{page}{FunctionPage}{Functions in \Language{}}
%
In \Language{}, a function is an expression in one or more variables.
(Think of it as a function of those variables).
You can also define a function by rules or use a built-in function
\Language{} lets you convert expressions to compiled functions.
\beginscroll
\beginmenu
\menulink{Rational Functions}{RationatFunctionPage} \tab{22}
Quotients of polynomials.

\menulink{Algebraic Functions}{AlgebraicFunctionPage} \tab{22}
Those defined by polynomial equations.

\menulink{Elementary Functions}{ElementaryFunctionPage} \tab{22}
The elementary functions of calculus.

\menulink{Simplification}{FunctionSimplificationPage} \tab{22}
How to simplify expressions.

\menulink{Pattern Matching}{ugUserRulesPage} \tab{22}
How to use the pattern matcher.
\endmenu
\endscroll
\newline
Additional Topics:
\beginmenu

\menulink{Operator Algebra}{OperatorXmpPage}\tab{22}
The operator algebra facility.

\endmenu
\autobuttons \end{page}

\begin{page}{RationatFunctionPage}{Rational Functions}
\beginscroll
To create a rational function, just compute the
quotient of two polynomials:
\spadpaste{f := (x - y) / (x + y)\bound{f}}
Use the functions \spadfun{numer} and \spadfun{denom}:
to recover the numerator and denominator of a fraction:
%
\spadpaste{numer f\free{f}}
\spadpaste{denom f\free{f}}
%
Since these are polynomials, you can apply all the
\downlink{polynomial operations}{PolynomialPage}
to them.
You can substitute values or
other rational functions for the variables using
the function \spadfun{eval}. The syntax for \spadfun{eval} is
similar to the one for polynomials:
\spadpaste{eval(f, x = 1/x)\free{f}}
\spadpaste{eval(f, [x = y, y = x])\free{f}}
\endscroll
\autobuttons
\end{page}

\begin{page}{AlgebraicFunctionPage}{Algebraic Functions}
\beginscroll
Algebraic functions are functions defined by algebraic equations. There
are two ways of constructing them: using rational powers, or implicitly.
For rational powers, use \spadopFrom{**}{RadicalCategory}
(or the system functions \spadfun{sqrt} and
\spadfun{nthRoot} for square and nth roots):
\spadpaste{f := sqrt(1 + x ** (1/3))\bound{f}}
To define an algebraic function implicitly
use \spadfun{rootOf}. The following
line defines a function \spad{y} of \spad{x} satisfying the equation
\spad{y**3 = x*y - y**2 - x**3 + 1}:
\spadpaste{y := rootOf(y**3 + y**2 - x*y + x**3 - 1, y)\bound{y}}
You can manipulate, differentiate or integrate an implicitly defined
algebraic function like any other \Language{} function:
\spadpaste{differentiate(y, x)\free{y}}
Higher powers of algebraic functions are automatically reduced during
calculations:
\spadpaste{(y + 1) ** 3\free{y}}
But denominators, are not automatically rationalized:
\spadpaste{g := inv f\bound{g}\free{y}}
Use \spadfun{ratDenom} to remove the algebraic quantities from the denominator:
\spadpaste{ratDenom g\free{g}}
\endscroll
\autobuttons \end{page}

\begin{page}{ElementaryFunctionPage}{Elementary Functions}
\beginscroll
\Language{} has most of the usual functions from calculus built-in:
\spadpaste{f := x * log y * sin(1/(x+y))\bound{f}}
You can substitute values or another elementary functions for the variables
with the function \spadfun{eval}:
\spadpaste{eval(f, [x = y, y = x])\free{f}}
As you can see, the substitutions are made 'in parallel' as in the case
of polynomials. It's also possible to substitute expressions for kernels
instead of variables:
\spadpaste{eval(f, log y = acosh(x + sqrt y))\free{f}}
\endscroll
\autobuttons \end{page}

\begin{page}{FunctionSimplificationPage}{Simplification}
\beginscroll
Simplifying an expression often means different things at
different times, so \Language{} offers a large number of
`simplification' functions.
The most common one, which performs the usual trigonometric
simplifications is \spadfun{simplify}:
\spadpaste{f := cos(x)/sec(x) * log(sin(x)**2/(cos(x)**2+sin(x)**2)) \bound{f}}
\spadpaste{g := simplify f\bound{g}\free{f}}
If the result of \spadfun{simplify} is not satisfactory, specific
transformations are available.
For example, to rewrite \spad{g} in terms of secants and
cosecants instead of sines and cosines, issue:
%
\spadpaste{h := sin2csc cos2sec g\bound{h}\free{g}}
%
To apply the logarithm simplification rules to \spad{h}, issue:
\spadpaste{expandLog h\free{h}}
Since the square root of \spad{x**2} is the absolute value of
\spad{x} and not \spad{x} itself, algebraic radicals are not
automatically simplified, but you can specifically request it by
calling \spadfun{rootSimp}:
%
\spadpaste{f1 := sqrt((x+1)**3)\bound{f1}}
\spadpaste{rootSimp f1\free{f1}}
%
There are other transformations which are sometimes useful.
Use the functions \spadfun{complexElementary} and \spadfun{trigs}
to go back and forth between the complex exponential and
trigonometric forms of an elementary function:
%
\spadpaste{g1 := sin(x + cos x)\bound{g1}}
\spadpaste{g2 := complexElementary g1\bound{g2}\free{g1}}
\spadpaste{trigs g2\free{g2}}
%
Similarly, the functions \spadfun{realElementary} and
\spadfun{htrigs} convert hyperbolic functions in and out of their
exponential form:
%
\spadpaste{h1 := sinh(x + cosh x)\bound{h1}}
\spadpaste{h2 := realElementary h1\bound{h2}\free{h1}}
\spadpaste{htrigs h2\free{h2}}
%
\Language{} has other transformations, most of which
are in the packages
\spadtype{ElementaryFunctionStructurePackage},
\spadtype{TrigonometricManipulations},
\spadtype{AlgebraicManipulations},
and \spadtype{TranscendentalManipulations}.
If you need to apply a simplification rule not built into the
system, you can use \Language{}'s \downlink{pattern
matcher}{ugUserRulesPage}.
\endscroll
\autobuttons
\end{page}