6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
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 |
6863
|
78 A similar function @code{run} exists for calling user script files, that |
|
79 are not necessarily on the user path |
|
80 |
|
81 @DOCSTRING(run) |
|
82 |
6641
|
83 @node Evaluation in a Different Context |
|
84 @section Evaluation in a Different Context |
|
85 |
|
86 Before you evaluate an expression you need to substitute |
|
87 the values of the variables used in the expression. These |
|
88 are stored in the symbol table. Whenever the interpreter |
|
89 starts a new function it saves the current symbol table |
|
90 and creates a new one, initializing it with the list of |
|
91 function parameters and a couple of predefined variables |
|
92 such as @code{nargin}. Expressions inside the function use the |
|
93 new symbol table. |
|
94 |
|
95 Sometimes you want to write a function so that when you |
|
96 call it, it modifies variables in your own context. This |
|
97 allows you to use a pass-by-name style of function, |
|
98 which is similar to using a pointer in programming languages such |
|
99 as C. |
|
100 |
|
101 Consider how you might write @code{save} and @code{load} as |
|
102 m-files. For example, |
|
103 |
|
104 @example |
|
105 function create_data |
|
106 x = linspace (0, 10, 10); |
|
107 y = sin (x); |
|
108 save mydata x y |
|
109 endfunction |
|
110 @end example |
|
111 |
|
112 With @code{evalin}, you could write save as follows: |
|
113 |
|
114 @example |
|
115 function save (file, name1, name2) |
|
116 f = open_save_file (file); |
|
117 save_var(f, name1, evalin ("caller", name1)); |
|
118 save_var(f, name2, evalin ("caller", name2)); |
|
119 endfunction |
|
120 @end example |
|
121 |
|
122 @noindent |
|
123 Here, @samp{caller} is the @code{create_data} function and @code{name1} |
|
124 is the string @code{"x"}, which evaluates simply as the value of @code{x}. |
|
125 |
|
126 You later want to load the values back from @code{mydata} |
|
127 in a different context: |
|
128 |
|
129 @example |
|
130 function process_data |
|
131 load mydata |
|
132 @dots{} do work @dots{} |
|
133 endfunction |
|
134 @end example |
|
135 |
|
136 @noindent |
|
137 With @code{assignin}, you could write @code{load} as follows: |
|
138 |
|
139 @example |
|
140 function load (file) |
|
141 f = open_load_file (file); |
|
142 [name, val] = load_var (f); |
|
143 assignin ("caller", name, val); |
|
144 [name, val] = load_var (f); |
|
145 assignin ("caller", name, val); |
|
146 endfunction |
|
147 @end example |
|
148 |
|
149 @noindent |
|
150 Here, @samp{caller} is the @code{process_data} function. |
|
151 |
|
152 You can set and use variables at the command prompt |
|
153 using the context @samp{base} rather than @samp{caller}. |
|
154 |
|
155 These functions are rarely used in practice. One |
|
156 example is the @code{fail (@samp{code}, @samp{pattern})} function |
|
157 which evaluates @samp{code} in the caller's context and |
|
158 checks that the error message it produces matches |
|
159 the given pattern. Other examples such as @code{save} and @code{load} |
|
160 are written in C++ where all octave variables |
|
161 are in the @samp{caller} context and @code{evalin} is not needed. |
|
162 |
6549
|
163 @DOCSTRING(evalin) |
|
164 |
|
165 @DOCSTRING(assignin) |