1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\MakeFunctionXmpTitle}{MakeFunction}
\newcommand{\MakeFunctionXmpNumber}{9.50}
%
% =====================================================================
\begin{page}{MakeFunctionXmpPage}{9.50 MakeFunction}
% =====================================================================
\beginscroll
It is sometimes useful to be able to define a function given by
the result of a calculation.
%
\xtc{
Suppose that you have obtained the following expression
after several computations
and that you now want to tabulate the numerical values of \spad{f}
for \spad{x} between \spad{-1} and \spad{+1} with increment
\spad{0.1}.
}{
\spadpaste{expr := (x - exp x + 1)**2 * (sin(x**2) * x + 1)**3 \bound{expr}}
}
%
You could, of course, use the function
\spadfunFrom{eval}{Expression} within a loop and evaluate
\spad{expr} twenty-one times, but this would be quite slow.
A better way is to create a numerical function \spad{f} such that
\spad{f(x)} is defined by the expression \spad{expr} above,
but without retyping \spad{expr}!
The package \spadtype{MakeFunction} provides the operation
\spadfunFrom{function}{MakeFunction} which does exactly this.
%
\xtc{
Issue this to create the function \spad{f(x)} given by \spad{expr}.
}{
\spadpaste{function(expr, f, x) \bound{f}\free{expr}}
}
\xtc{
To tabulate \spad{expr}, we can now quickly evaluate \spad{f} 21 times.
}{
\spadpaste{tbl := [f(0.1 * i - 1) for i in 0..20]; \free{f}\bound{tbl}}
}
%
%
\xtc{
Use the list \spad{[x1,...,xn]} as the
third argument to \spadfunFrom{function}{MakeFunction}
to create a multivariate function \spad{f(x1,...,xn)}.
}{
\spadpaste{e := (x - y + 1)**2 * (x**2 * y + 1)**2 \bound{e}}
}
\xtc{
}{
\spadpaste{function(e, g, [x, y]) \free{e}}
}
%
%
\xtc{
In the case of just two
variables, they can be given as arguments without making them into a list.
}{
\spadpaste{function(e, h, x, y) \free{e}\bound{h}}
}
%
%
\xtc{
Note that the functions created by \spadfunFrom{function}{MakeFunction}
are not limited to floating point numbers, but can be applied to any type
for which they are defined.
}{
\spadpaste{m1 := squareMatrix [[1, 2], [3, 4]] \bound{m1}}
}
\xtc{
}{
\spadpaste{m2 := squareMatrix [[1, 0], [-1, 1]] \bound{m2}}
}
\xtc{
}{
\spadpaste{h(m1, m2) \free{h m1 m2}}
}
%
For more information, see \downlink{``\ugUserMakeTitle''}{ugUserMakePage} in Section \ugUserMakeNumber\ignore{ugUserMake}.
\showBlurb{MakeFunction}
\endscroll
\autobuttons
\end{page}
%
|