% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\ExpressionXmpTitle}{Expression}
\newcommand{\ExpressionXmpNumber}{9.21}
%
% =====================================================================
\begin{page}{ExpressionXmpPage}{9.21 Expression}
% =====================================================================
\beginscroll

%
%
%
%
%


%

\spadtype{Expression} is a constructor that creates domains whose
objects can have very general symbolic forms.
Here are some examples:
\xtc{
This is an object of type \spadtype{Expression Integer}.
}{
\spadpaste{sin(x) + 3*cos(x)^2}
}
\xtc{
This is an object of type \spadtype{Expression Float}.
}{
\spadpaste{tan(x) - 3.45*x}
}
\xtc{
This object contains symbolic function applications, sums,
products, square roots, and a quotient.
}{
\spadpaste{(tan sqrt 7 - sin sqrt 11)^2 / (4 - cos(x - y))}
}
As you can see, \spadtype{Expression} actually takes an argument
domain.
The {\it coefficients} of the terms within the expression belong
to the argument domain.
\spadtype{Integer} and \spadtype{Float}, along with
\spadtype{Complex Integer} and \spadtype{Complex Float}
are the most common coefficient domains.
\xtc{
The choice of whether
to use a \spadtype{Complex} coefficient domain or not is
important since \Language{} can perform some simplifications
on real-valued objects
}{
\spadpaste{log(exp  x)@Expression(Integer)}
}
\xtc{
... which are not valid on complex ones.
}{
\spadpaste{log(exp  x)@Expression(Complex Integer)}
}
\xtc{
Many potential coefficient domains, such as
\spadtype{AlgebraicNumber}, are not usually used because
\spadtype{Expression} can subsume them.
}{
\spadpaste{sqrt 3 + sqrt(2 + sqrt(-5)) \bound{algnum1}}
}
\xtc{
}{
\spadpaste{\% :: Expression Integer \free{algnum1}}
}
Note that we sometimes talk about ``an object of type
\spadtype{Expression}.'' This is not really correct because we
should say, for example, ``an object of type \spadtype{Expression
Integer}'' or ``an object of type \spadtype{Expression Float}.''
By a similar abuse of language, when we refer to an ``expression''
in this section we will mean an object of type
\spadtype{Expression R} for some domain {\bf R}.

The \Language{} documentation contains many examples of the use
of \spadtype{Expression}.
For the rest of this section, we'll give you some pointers to those
examples plus give you some idea of how to manipulate expressions.

It is important for you to know that \spadtype{Expression}
creates domains that have category \spadtype{Field}.
Thus you can invert any non-zero expression and you shouldn't
expect an operation like \spadfun{factor} to give you much
information.
You can imagine expressions as being represented as quotients of
``multivariate'' polynomials where the ``variables'' are kernels
(see \downlink{`Kernel'}{KernelXmpPage}\ignore{Kernel}).
A kernel can either be a symbol such as \spad{x} or a symbolic
function application like \spad{sin(x + 4)}.
The second example is actually a nested kernel since the argument
to \spadfun{sin} contains the kernel \spad{x}.
\xtc{
}{
\spadpaste{height mainKernel sin(x + 4)}
}
Actually, the argument to \spadfun{sin} is an expression, and so
the structure of \spadtype{Expression} is recursive.
\downlink{`Kernel'}{KernelXmpPage}\ignore{Kernel} demonstrates how to extract the kernels in an
expression.

Use the \HyperName{} Browse facility to see what operations are
applicable to expression.
At the time of this writing, there were 262 operations with 147
distinct name in \spadtype{Expression Integer}.
For example, \spadfunFrom{numer}{Expression} and
\spadfunFrom{denom}{Expression} extract the numerator and
denominator of an expression.
\xtc{
}{
\spadpaste{e := (sin(x) - 4)^2 / ( 1 - 2*y*sqrt(- y) ) \bound{e}}
}
\xtc{
}{
\spadpaste{numer e \free{e}}
}
\xtc{
}{
\spadpaste{denom e \free{e}}
}
\xtc{
Use \spadfunFrom{D}{Expression} to compute partial derivatives.
}{
\spadpaste{D(e, x) \free{e}}
}
\xtc{
See \downlink{``\ugIntroCalcDerivTitle''}{ugIntroCalcDerivPage} in Section \ugIntroCalcDerivNumber\ignore{ugIntroCalcDeriv}
for more examples of expressions and derivatives.
}{
\spadpaste{D(e, [x, y], [1, 2]) \free{e}}
}
See \downlink{``\ugIntroCalcLimitsTitle''}{ugIntroCalcLimitsPage} in Section \ugIntroCalcLimitsNumber\ignore{ugIntroCalcLimits} and
\downlink{``\ugIntroSeriesTitle''}{ugIntroSeriesPage} in Section \ugIntroSeriesNumber\ignore{ugIntroSeries}
for more examples of expressions and calculus.
Differential equations involving expressions are discussed in
\downlink{``\ugProblemDEQTitle''}{ugProblemDEQPage} in Section \ugProblemDEQNumber\ignore{ugProblemDEQ}.
Chapter 8 has many advanced examples: see
\downlink{``\ugProblemIntegrationTitle''}{ugProblemIntegrationPage} in Section \ugProblemIntegrationNumber\ignore{ugProblemIntegration}
for a discussion of \Language{}'s integration facilities.

When an expression involves no ``symbol kernels'' (for example,
\spad{x}), it may be possible to numerically evaluate the
expression.
\xtc{
If you suspect the evaluation will create a complex number,
use \spadfun{complexNumeric}.
}{
\spadpaste{complexNumeric(cos(2 - 3*\%i))}
}
\xtc{
If you know it will be real, use \spadfun{numeric}.
}{
\spadpaste{numeric(tan 3.8)}
}
The \spadfun{numeric} operation will display an error message if
the evaluation yields a calue with an non-zero imaginary part.
Both of these operations have an optional second argument
\spad{n} which specifies that the accuracy of the approximation
be up to \spad{n} decimal places.

When an expression involves no ``symbolic application'' kernels,
it may be possible to convert it a polynomial or rational
function in the variables that are present.
\xtc{
}{
\spadpaste{e2 := cos(x^2 - y + 3) \bound{e2}}
}
\xtc{
}{
\spadpaste{e3 := asin(e2) - \%pi/2 \free{e2}\bound{e3}}
}
\xtc{
}{
\spadpaste{e4 := normalize(e3) \free{e3}\bound{e4}}
}
\xtc{
}{
\spadpaste{e4 :: Polynomial Integer \free{e4}}
}
\xtc{
This also works for the polynomial types where specific variables
and their ordering are given.
}{
\spadpaste{e4 :: DMP([x, y], Integer) \free{e4}}
}

Finally, a certain amount of simplication takes place as
expressions are constructed.
\xtc{
}{
\spadpaste{sin \%pi}
}
\xtc{
}{
\spadpaste{cos(\%pi / 4)}
}
\xtc{
For simplications that involve multiple terms of the expression,
use \spadfun{simplify}.
}{
\spadpaste{tan(x)^6 + 3*tan(x)^4 + 3*tan(x)^2 + 1 \bound{tan6}}
}
\xtc{
}{
\spadpaste{simplify \% \free{tan6}}
}
See \downlink{``\ugUserRulesTitle''}{ugUserRulesPage} in Section \ugUserRulesNumber\ignore{ugUserRules}
for examples of how to write your own rewrite rules for
expressions.
\endscroll
\autobuttons
\end{page}
%
