1
2% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
3% All rights reserved.
4%
5% Redistribution and use in source and binary forms, with or without
6% modification, are permitted provided that the following conditions are
7% met:
8%
9%     - Redistributions of source code must retain the above copyright
10%       notice, this list of conditions and the following disclaimer.
11%
12%     - Redistributions in binary form must reproduce the above copyright
13%       notice, this list of conditions and the following disclaimer in
14%       the documentation and/or other materials provided with the
15%       distribution.
16%
17%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
18%       names of its contributors may be used to endorse or promote products
19%       derived from this software without specific prior written permission.
20%
21% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
28% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
34%
35% EXPR.htex
36
37\spadtype{Expression} is a constructor that creates domains whose
38objects can have very general symbolic forms.
39Here are some examples:
40\xtc{
41This is an object of type \spadtype{Expression Integer}.
42}{
43\spadcommand{sin(x) + 3*cos(x)^2}
44}
45\xtc{
46This is an object of type \spadtype{Expression Float}.
47}{
48\spadcommand{tan(x) - 3.45*x}
49}
50\xtc{
51This object contains symbolic function applications, sums,
52products, square roots, and a quotient.
53}{
54\spadcommand{(tan sqrt 7 - sin sqrt 11)^2 / (4 - cos(x - y))}
55}
56As you can see, \spadtype{Expression} actually takes an argument
57domain.
58The {\it coefficients} of the terms within the expression belong
59to the argument domain.
60\spadtype{Integer} and \spadtype{Float}, along with
61\spadtype{Complex Integer} and \spadtype{Complex Float}
62are the most common coefficient domains.
63\xtc{
64The choice of whether
65to use a \spadtype{Complex} coefficient domain or not is
66important since \Language{} can perform some simplifications
67on real-valued objects
68}{
69\spadcommand{log(exp  x)@Expression(Integer)}
70}
71\xtc{
72... which are not valid on complex ones.
73}{
74\spadcommand{log(exp  x)@Expression(Complex Integer)}
75}
76\xtc{
77Many potential coefficient domains, such as
78\spadtype{AlgebraicNumber}, are not usually used because
79\spadtype{Expression} can subsume them.
80}{
81\spadcommand{sqrt 3 + sqrt(2 + sqrt(-5)) \bound{algnum1}}
82}
83\xtc{
84}{
85\spadcommand{\% :: Expression Integer \free{algnum1}}
86}
87Note that we sometimes talk about ``an object of type
88\spadtype{Expression}.'' This is not really correct because we
89should say, for example, ``an object of type
90\spadtype{Expression Integer}''
91or ``an object of type \spadtype{Expression Float}.''
92By a similar abuse of language, when we refer to an ``expression''
93in this section we will mean an object of type
94\spadtype{Expression R} for some domain {\bf R}.
95
96The \Language{} documentation contains many examples of the use
97of \spadtype{Expression}.
98For the rest of this section, we'll give you some pointers to those
99examples plus give you some idea of how to manipulate expressions.
100
101It is important for you to know that \spadtype{Expression}
102creates domains that have category \spadtype{Field}.
103Thus you can invert any non-zero expression and you shouldn't
104expect an operation like \spadfun{factor} to give you much
105information.
106You can imagine expressions as being represented as quotients of
107``multivariate'' polynomials where the ``variables'' are kernels
108(see \xmpref{Kernel}).
109A kernel can either be a symbol such as \spad{x} or a symbolic
110function application like \spad{sin(x + 4)}.
111The second example is actually a nested kernel since the argument
112to \spadfun{sin} contains the kernel \spad{x}.
113\xtc{
114}{
115\spadcommand{height mainKernel sin(x + 4)}
116}
117Actually, the argument to \spadfun{sin} is an expression, and so
118the structure of \spadtype{Expression} is recursive.
119\xmpref{Kernel} demonstrates how to extract the kernels in an
120expression.
121
122Use the \HyperName{} Browse facility to see what operations are
123applicable to expression.
124At the time of this writing, there were 262 operations with 147
125distinct name in \spadtype{Expression Integer}.
126For example, \spadfunFrom{numer}{Expression} and
127\spadfunFrom{denom}{Expression} extract the numerator and
128denominator of an expression.
129\xtc{
130}{
131\spadcommand{e := (sin(x) - 4)^2 / ( 1 - 2*y*sqrt(- y) ) \bound{e}}
132}
133\xtc{
134}{
135\spadcommand{numer e \free{e}}
136}
137\xtc{
138}{
139\spadcommand{denom e \free{e}}
140}
141\xtc{
142Use \spadfunFrom{D}{Expression} to compute partial derivatives.
143}{
144\spadcommand{D(e, x) \free{e}}
145}
146\xtc{
147See \spadref{ugIntroCalcDeriv}
148for more examples of expressions and derivatives.
149}{
150\spadcommand{D(e, [x, y], [1, 2]) \free{e}}
151}
152See \spadref{ugIntroCalcLimits} and
153\spadref{ugIntroSeries}
154for more examples of expressions and calculus.
155Differential equations involving expressions are discussed in
156\spadref{ugProblemDEQ}.
157Chapter 8 has many advanced examples: see
158\spadref{ugProblemIntegration}
159for a discussion of \Language{}'s integration facilities.
160
161When an expression involves no ``symbol kernels'' (for example,
162\spad{x}), it may be possible to numerically evaluate the
163expression.
164\xtc{
165If you suspect the evaluation will create a complex number,
166use \spadfun{complexNumeric}.
167}{
168\spadcommand{complexNumeric(cos(2 - 3*\%i))}
169}
170\xtc{
171If you know it will be real, use \spadfun{numeric}.
172}{
173\spadcommand{numeric(tan 3.8)}
174}
175The \spadfun{numeric} operation will display an error message if
176the evaluation yields a value with an non-zero imaginary part.
177Both of these operations have an optional second argument
178\spad{n} which specifies that the accuracy of the approximation
179be up to \spad{n} decimal places.
180
181When an expression involves no ``symbolic application'' kernels,
182it may be possible to convert it a polynomial or rational
183function in the variables that are present.
184\xtc{
185}{
186\spadcommand{e2 := cos(x^2 - y + 3) \bound{e2}}
187}
188\xtc{
189}{
190\spadcommand{e3 := asin(e2) - \%pi/2 \free{e2}\bound{e3}}
191}
192\xtc{
193}{
194\spadcommand{e4 := normalize(e3) \free{e3}\bound{e4}}
195}
196\xtc{
197}{
198\spadcommand{e4 :: Polynomial Integer \free{e4}}
199}
200\xtc{
201This also works for the polynomial types where specific variables
202and their ordering are given.
203}{
204\spadcommand{e4 :: DMP([x, y], Integer) \free{e4}}
205}
206
207Finally, a certain amount of simplication takes place as
208expressions are constructed.
209\xtc{
210}{
211\spadcommand{sin \%pi}
212}
213\xtc{
214}{
215\spadcommand{cos(\%pi / 4)}
216}
217\xtc{
218For simplications that involve multiple terms of the expression,
219use \spadfun{simplify}.
220}{
221\spadcommand{tan(x)^6 + 3*tan(x)^4 + 3*tan(x)^2 + 1 \bound{tan6}}
222}
223\xtc{
224}{
225\spadcommand{simplify \% \free{tan6}}
226}
227See \spadref{ugUserRules}
228for examples of how to write your own rewrite rules for
229expressions.
230