3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Evaluation |
3294
|
6 @chapter Evaluation |
|
7 |
|
8 Normally, you evaluate expressions simply by typing them at the Octave |
|
9 prompt, or by asking Octave to interpret commands that you have saved in |
|
10 a file. |
|
11 |
|
12 Sometimes, you may find it necessary to evaluate an expression that has |
6641
|
13 been computed and stored in a string, which is exactly what the |
|
14 @code{eval} lets you do. |
3294
|
15 |
3371
|
16 @DOCSTRING(eval) |
3294
|
17 |
6641
|
18 @menu |
|
19 * Calling a Function by its Name:: |
|
20 * Evaluation in a Different Context:: |
|
21 @end menu |
|
22 |
|
23 @node Calling a Function by its Name |
|
24 @section Calling a Function by its Name |
3294
|
25 |
6641
|
26 The @code{feval} function allow you to call a function from a string |
|
27 containing its name. This is useful when writing a function that need to |
|
28 call user-supplied functions. The @code{feval} function takes the name |
|
29 of the function to call as its first argument, and the remaining |
|
30 arguments are given to the function. |
|
31 |
|
32 The following example is a simple-minded function using @code{feval} |
|
33 that finds the root of a user-supplied function of one variable using |
|
34 Newton's method. |
3294
|
35 |
|
36 @example |
|
37 @group |
|
38 @cindex Fordyce, A. P. |
|
39 @findex newtroot |
|
40 function result = newtroot (fname, x) |
|
41 |
|
42 # usage: newtroot (fname, x) |
|
43 # |
|
44 # fname : a string naming a function f(x). |
|
45 # x : initial guess |
|
46 |
|
47 delta = tol = sqrt (eps); |
|
48 maxit = 200; |
|
49 fx = feval (fname, x); |
|
50 for i = 1:maxit |
|
51 if (abs (fx) < tol) |
|
52 result = x; |
|
53 return; |
|
54 else |
|
55 fx_new = feval (fname, x + delta); |
|
56 deriv = (fx_new - fx) / delta; |
|
57 x = x - fx / deriv; |
|
58 fx = fx_new; |
|
59 endif |
|
60 endfor |
|
61 |
|
62 result = x; |
|
63 |
|
64 endfunction |
|
65 @end group |
|
66 @end example |
|
67 |
|
68 Note that this is only meant to be an example of calling user-supplied |
|
69 functions and should not be taken too seriously. In addition to using a |
|
70 more robust algorithm, any serious code would check the number and type |
|
71 of all the arguments, ensure that the supplied function really was a |
6641
|
72 function, etc. @xref{Predicates for Numeric Objects}, for example, |
|
73 for a list of predicates for numeric objects, and see @ref{Status of |
3294
|
74 Variables}, for a description of the @code{exist} function. |
6549
|
75 |
6641
|
76 @DOCSTRING(feval) |
|
77 |
|
78 @node Evaluation in a Different Context |
|
79 @section Evaluation in a Different Context |
|
80 |
|
81 Before you evaluate an expression you need to substitute |
|
82 the values of the variables used in the expression. These |
|
83 are stored in the symbol table. Whenever the interpreter |
|
84 starts a new function it saves the current symbol table |
|
85 and creates a new one, initializing it with the list of |
|
86 function parameters and a couple of predefined variables |
|
87 such as @code{nargin}. Expressions inside the function use the |
|
88 new symbol table. |
|
89 |
|
90 Sometimes you want to write a function so that when you |
|
91 call it, it modifies variables in your own context. This |
|
92 allows you to use a pass-by-name style of function, |
|
93 which is similar to using a pointer in programming languages such |
|
94 as C. |
|
95 |
|
96 Consider how you might write @code{save} and @code{load} as |
|
97 m-files. For example, |
|
98 |
|
99 @example |
|
100 function create_data |
|
101 x = linspace (0, 10, 10); |
|
102 y = sin (x); |
|
103 save mydata x y |
|
104 endfunction |
|
105 @end example |
|
106 |
|
107 With @code{evalin}, you could write save as follows: |
|
108 |
|
109 @example |
|
110 function save (file, name1, name2) |
|
111 f = open_save_file (file); |
|
112 save_var(f, name1, evalin ("caller", name1)); |
|
113 save_var(f, name2, evalin ("caller", name2)); |
|
114 endfunction |
|
115 @end example |
|
116 |
|
117 @noindent |
|
118 Here, @samp{caller} is the @code{create_data} function and @code{name1} |
|
119 is the string @code{"x"}, which evaluates simply as the value of @code{x}. |
|
120 |
|
121 You later want to load the values back from @code{mydata} |
|
122 in a different context: |
|
123 |
|
124 @example |
|
125 function process_data |
|
126 load mydata |
|
127 @dots{} do work @dots{} |
|
128 endfunction |
|
129 @end example |
|
130 |
|
131 @noindent |
|
132 With @code{assignin}, you could write @code{load} as follows: |
|
133 |
|
134 @example |
|
135 function load (file) |
|
136 f = open_load_file (file); |
|
137 [name, val] = load_var (f); |
|
138 assignin ("caller", name, val); |
|
139 [name, val] = load_var (f); |
|
140 assignin ("caller", name, val); |
|
141 endfunction |
|
142 @end example |
|
143 |
|
144 @noindent |
|
145 Here, @samp{caller} is the @code{process_data} function. |
|
146 |
|
147 You can set and use variables at the command prompt |
|
148 using the context @samp{base} rather than @samp{caller}. |
|
149 |
|
150 These functions are rarely used in practice. One |
|
151 example is the @code{fail (@samp{code}, @samp{pattern})} function |
|
152 which evaluates @samp{code} in the caller's context and |
|
153 checks that the error message it produces matches |
|
154 the given pattern. Other examples such as @code{save} and @code{load} |
|
155 are written in C++ where all octave variables |
|
156 are in the @samp{caller} context and @code{evalin} is not needed. |
|
157 |
6549
|
158 @DOCSTRING(evalin) |
|
159 |
|
160 @DOCSTRING(assignin) |