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 Functions and Scripts |
3294
|
6 @chapter Functions and Script Files |
|
7 @cindex defining functions |
|
8 @cindex user-defined functions |
|
9 @cindex functions, user-defined |
|
10 @cindex script files |
|
11 |
|
12 Complicated Octave programs can often be simplified by defining |
|
13 functions. Functions can be defined directly on the command line during |
|
14 interactive Octave sessions, or in external files, and can be called just |
|
15 like built-in functions. |
|
16 |
|
17 @menu |
|
18 * Defining Functions:: |
|
19 * Multiple Return Values:: |
|
20 * Variable-length Argument Lists:: |
|
21 * Variable-length Return Lists:: |
|
22 * Returning From a Function:: |
|
23 * Function Files:: |
|
24 * Script Files:: |
|
25 * Dynamically Linked Functions:: |
4933
|
26 * Function Handles and Inline:: |
3294
|
27 * Organization of Functions:: |
|
28 @end menu |
|
29 |
4167
|
30 @node Defining Functions |
3294
|
31 @section Defining Functions |
|
32 @cindex @code{function} statement |
|
33 @cindex @code{endfunction} statement |
|
34 |
|
35 In its simplest form, the definition of a function named @var{name} |
|
36 looks like this: |
|
37 |
|
38 @example |
|
39 @group |
|
40 function @var{name} |
|
41 @var{body} |
|
42 endfunction |
|
43 @end group |
|
44 @end example |
|
45 |
|
46 @noindent |
|
47 A valid function name is like a valid variable name: a sequence of |
|
48 letters, digits and underscores, not starting with a digit. Functions |
|
49 share the same pool of names as variables. |
|
50 |
|
51 The function @var{body} consists of Octave statements. It is the |
|
52 most important part of the definition, because it says what the function |
|
53 should actually @emph{do}. |
|
54 |
|
55 For example, here is a function that, when executed, will ring the bell |
|
56 on your terminal (assuming that it is possible to do so): |
|
57 |
|
58 @example |
|
59 @group |
|
60 function wakeup |
|
61 printf ("\a"); |
|
62 endfunction |
|
63 @end group |
|
64 @end example |
|
65 |
|
66 The @code{printf} statement (@pxref{Input and Output}) simply tells |
|
67 Octave to print the string @code{"\a"}. The special character @samp{\a} |
|
68 stands for the alert character (ASCII 7). @xref{Strings}. |
|
69 |
|
70 Once this function is defined, you can ask Octave to evaluate it by |
|
71 typing the name of the function. |
|
72 |
|
73 Normally, you will want to pass some information to the functions you |
|
74 define. The syntax for passing parameters to a function in Octave is |
|
75 |
|
76 @example |
|
77 @group |
|
78 function @var{name} (@var{arg-list}) |
|
79 @var{body} |
|
80 endfunction |
|
81 @end group |
|
82 @end example |
|
83 |
|
84 @noindent |
|
85 where @var{arg-list} is a comma-separated list of the function's |
|
86 arguments. When the function is called, the argument names are used to |
|
87 hold the argument values given in the call. The list of arguments may |
|
88 be empty, in which case this form is equivalent to the one shown above. |
|
89 |
|
90 To print a message along with ringing the bell, you might modify the |
|
91 @code{beep} to look like this: |
|
92 |
|
93 @example |
|
94 @group |
|
95 function wakeup (message) |
|
96 printf ("\a%s\n", message); |
|
97 endfunction |
|
98 @end group |
|
99 @end example |
|
100 |
|
101 Calling this function using a statement like this |
|
102 |
|
103 @example |
|
104 wakeup ("Rise and shine!"); |
|
105 @end example |
|
106 |
|
107 @noindent |
|
108 will cause Octave to ring your terminal's bell and print the message |
|
109 @samp{Rise and shine!}, followed by a newline character (the @samp{\n} |
|
110 in the first argument to the @code{printf} statement). |
|
111 |
|
112 In most cases, you will also want to get some information back from the |
|
113 functions you define. Here is the syntax for writing a function that |
|
114 returns a single value: |
|
115 |
|
116 @example |
|
117 @group |
|
118 function @var{ret-var} = @var{name} (@var{arg-list}) |
|
119 @var{body} |
|
120 endfunction |
|
121 @end group |
|
122 @end example |
|
123 |
|
124 @noindent |
|
125 The symbol @var{ret-var} is the name of the variable that will hold the |
|
126 value to be returned by the function. This variable must be defined |
|
127 before the end of the function body in order for the function to return |
|
128 a value. |
|
129 |
|
130 Variables used in the body of a function are local to the |
|
131 function. Variables named in @var{arg-list} and @var{ret-var} are also |
|
132 local to the function. @xref{Global Variables}, for information about |
|
133 how to access global variables inside a function. |
|
134 |
|
135 For example, here is a function that computes the average of the |
|
136 elements of a vector: |
|
137 |
|
138 @example |
|
139 @group |
|
140 function retval = avg (v) |
|
141 retval = sum (v) / length (v); |
|
142 endfunction |
|
143 @end group |
|
144 @end example |
|
145 |
|
146 If we had written @code{avg} like this instead, |
|
147 |
|
148 @example |
|
149 @group |
|
150 function retval = avg (v) |
4029
|
151 if (isvector (v)) |
3294
|
152 retval = sum (v) / length (v); |
|
153 endif |
|
154 endfunction |
|
155 @end group |
|
156 @end example |
|
157 |
|
158 @noindent |
|
159 and then called the function with a matrix instead of a vector as the |
|
160 argument, Octave would have printed an error message like this: |
|
161 |
|
162 @example |
|
163 @group |
|
164 error: `retval' undefined near line 1 column 10 |
|
165 error: evaluating index expression near line 7, column 1 |
|
166 @end group |
|
167 @end example |
|
168 |
|
169 @noindent |
|
170 because the body of the @code{if} statement was never executed, and |
|
171 @code{retval} was never defined. To prevent obscure errors like this, |
|
172 it is a good idea to always make sure that the return variables will |
|
173 always have values, and to produce meaningful error messages when |
|
174 problems are encountered. For example, @code{avg} could have been |
|
175 written like this: |
|
176 |
|
177 @example |
|
178 @group |
|
179 function retval = avg (v) |
|
180 retval = 0; |
4029
|
181 if (isvector (v)) |
3294
|
182 retval = sum (v) / length (v); |
|
183 else |
|
184 error ("avg: expecting vector argument"); |
|
185 endif |
|
186 endfunction |
|
187 @end group |
|
188 @end example |
|
189 |
|
190 There is still one additional problem with this function. What if it is |
|
191 called without an argument? Without additional error checking, Octave |
|
192 will probably print an error message that won't really help you track |
|
193 down the source of the error. To allow you to catch errors like this, |
|
194 Octave provides each function with an automatic variable called |
|
195 @code{nargin}. Each time a function is called, @code{nargin} is |
|
196 automatically initialized to the number of arguments that have actually |
|
197 been passed to the function. For example, we might rewrite the |
|
198 @code{avg} function like this: |
|
199 |
|
200 @example |
|
201 @group |
|
202 function retval = avg (v) |
|
203 retval = 0; |
|
204 if (nargin != 1) |
|
205 usage ("avg (vector)"); |
|
206 endif |
4029
|
207 if (isvector (v)) |
3294
|
208 retval = sum (v) / length (v); |
|
209 else |
|
210 error ("avg: expecting vector argument"); |
|
211 endif |
|
212 endfunction |
|
213 @end group |
|
214 @end example |
|
215 |
|
216 Although Octave does not automatically report an error if you call a |
|
217 function with more arguments than expected, doing so probably indicates |
|
218 that something is wrong. Octave also does not automatically report an |
|
219 error if a function is called with too few arguments, but any attempt to |
|
220 use a variable that has not been given a value will result in an error. |
|
221 To avoid such problems and to provide useful messages, we check for both |
|
222 possibilities and issue our own error message. |
|
223 |
4700
|
224 @DOCSTRING(nargin) |
3294
|
225 |
3371
|
226 @DOCSTRING(silent_functions) |
3294
|
227 |
3371
|
228 @DOCSTRING(warn_missing_semicolon) |
3294
|
229 |
4167
|
230 @node Multiple Return Values |
3294
|
231 @section Multiple Return Values |
|
232 |
|
233 Unlike many other computer languages, Octave allows you to define |
|
234 functions that return more than one value. The syntax for defining |
|
235 functions that return multiple values is |
|
236 |
|
237 @example |
|
238 function [@var{ret-list}] = @var{name} (@var{arg-list}) |
|
239 @var{body} |
|
240 endfunction |
|
241 @end example |
|
242 |
|
243 @noindent |
|
244 where @var{name}, @var{arg-list}, and @var{body} have the same meaning |
|
245 as before, and @var{ret-list} is a comma-separated list of variable |
|
246 names that will hold the values returned from the function. The list of |
|
247 return values must have at least one element. If @var{ret-list} has |
|
248 only one element, this form of the @code{function} statement is |
|
249 equivalent to the form described in the previous section. |
|
250 |
|
251 Here is an example of a function that returns two values, the maximum |
|
252 element of a vector and the index of its first occurrence in the vector. |
|
253 |
|
254 @example |
|
255 @group |
|
256 function [max, idx] = vmax (v) |
|
257 idx = 1; |
|
258 max = v (idx); |
|
259 for i = 2:length (v) |
|
260 if (v (i) > max) |
|
261 max = v (i); |
|
262 idx = i; |
|
263 endif |
|
264 endfor |
|
265 endfunction |
|
266 @end group |
|
267 @end example |
|
268 |
|
269 In this particular case, the two values could have been returned as |
|
270 elements of a single array, but that is not always possible or |
|
271 convenient. The values to be returned may not have compatible |
|
272 dimensions, and it is often desirable to give the individual return |
|
273 values distinct names. |
|
274 |
|
275 In addition to setting @code{nargin} each time a function is called, |
|
276 Octave also automatically initializes @code{nargout} to the number of |
|
277 values that are expected to be returned. This allows you to write |
|
278 functions that behave differently depending on the number of values that |
|
279 the user of the function has requested. The implicit assignment to the |
|
280 built-in variable @code{ans} does not figure in the count of output |
|
281 arguments, so the value of @code{nargout} may be zero. |
|
282 |
|
283 The @code{svd} and @code{lu} functions are examples of built-in |
|
284 functions that behave differently depending on the value of |
|
285 @code{nargout}. |
|
286 |
|
287 It is possible to write functions that only set some return values. For |
|
288 example, calling the function |
|
289 |
|
290 @example |
|
291 function [x, y, z] = f () |
|
292 x = 1; |
|
293 z = 2; |
|
294 endfunction |
|
295 @end example |
|
296 |
|
297 @noindent |
|
298 as |
|
299 |
|
300 @example |
|
301 [a, b, c] = f () |
|
302 @end example |
|
303 |
|
304 @noindent |
|
305 produces: |
|
306 |
|
307 @example |
|
308 a = 1 |
|
309 |
|
310 b = [](0x0) |
|
311 |
|
312 c = 2 |
|
313 @end example |
|
314 |
|
315 @noindent |
4466
|
316 along with a warning if the value of the built-in variable |
|
317 @code{warn_undefined_return_values} is nonzero. |
3294
|
318 |
4700
|
319 @DOCSTRING(nargout) |
3294
|
320 |
4466
|
321 @DOCSTRING(warn_undefined_return_values) |
3294
|
322 |
3371
|
323 @DOCSTRING(nargchk) |
3294
|
324 |
4167
|
325 @node Variable-length Argument Lists |
3294
|
326 @section Variable-length Argument Lists |
4933
|
327 @cindex variable-length argument lists |
3294
|
328 @cindex @code{...} |
|
329 |
4167
|
330 @node Variable-length Return Lists |
3294
|
331 @section Variable-length Return Lists |
4933
|
332 @cindex variable-length return lists |
3294
|
333 @cindex @code{...} |
|
334 |
4167
|
335 @node Returning From a Function |
3294
|
336 @section Returning From a Function |
|
337 |
|
338 The body of a user-defined function can contain a @code{return} statement. |
|
339 This statement returns control to the rest of the Octave program. It |
|
340 looks like this: |
|
341 |
|
342 @example |
|
343 return |
|
344 @end example |
|
345 |
|
346 Unlike the @code{return} statement in C, Octave's @code{return} |
|
347 statement cannot be used to return a value from a function. Instead, |
|
348 you must assign values to the list of return variables that are part of |
|
349 the @code{function} statement. The @code{return} statement simply makes |
|
350 it easier to exit a function from a deeply nested loop or conditional |
|
351 statement. |
|
352 |
|
353 Here is an example of a function that checks to see if any elements of a |
|
354 vector are nonzero. |
|
355 |
|
356 @example |
|
357 @group |
|
358 function retval = any_nonzero (v) |
|
359 retval = 0; |
|
360 for i = 1:length (v) |
|
361 if (v (i) != 0) |
|
362 retval = 1; |
|
363 return; |
|
364 endif |
|
365 endfor |
|
366 printf ("no nonzero elements found\n"); |
|
367 endfunction |
|
368 @end group |
|
369 @end example |
|
370 |
|
371 Note that this function could not have been written using the |
|
372 @code{break} statement to exit the loop once a nonzero value is found |
|
373 without adding extra logic to avoid printing the message if the vector |
|
374 does contain a nonzero element. |
|
375 |
|
376 @defvr {Keyword} return |
|
377 When Octave encounters the keyword @code{return} inside a function or |
|
378 script, it returns control to be caller immediately. At the top level, |
|
379 the return statement is ignored. A @code{return} statement is assumed |
|
380 at the end of every function definition. |
|
381 @end defvr |
|
382 |
3371
|
383 @DOCSTRING(return_last_computed_value) |
3294
|
384 |
4167
|
385 @node Function Files |
3294
|
386 @section Function Files |
|
387 @cindex function file |
|
388 |
|
389 Except for simple one-shot programs, it is not practical to have to |
|
390 define all the functions you need each time you need them. Instead, you |
|
391 will normally want to save them in a file so that you can easily edit |
|
392 them, and save them for use at a later time. |
|
393 |
|
394 Octave does not require you to load function definitions from files |
|
395 before using them. You simply need to put the function definitions in a |
|
396 place where Octave can find them. |
|
397 |
|
398 When Octave encounters an identifier that is undefined, it first looks |
|
399 for variables or functions that are already compiled and currently |
|
400 listed in its symbol table. If it fails to find a definition there, it |
|
401 searches the list of directories specified by the built-in variable |
|
402 @code{LOADPATH} for files ending in @file{.m} that have the same base |
|
403 name as the undefined identifier.@footnote{The @samp{.m} suffix was |
|
404 chosen for compatibility with @sc{Matlab}.} Once Octave finds a file |
|
405 with a name that matches, the contents of the file are read. If it |
|
406 defines a @emph{single} function, it is compiled and executed. |
|
407 @xref{Script Files}, for more information about how you can define more |
|
408 than one function in a single file. |
|
409 |
|
410 When Octave defines a function from a function file, it saves the full |
|
411 name of the file it read and the time stamp on the file. After |
|
412 that, it checks the time stamp on the file every time it needs the |
|
413 function. If the time stamp indicates that the file has changed since |
|
414 the last time it was read, Octave reads it again. |
|
415 |
|
416 Checking the time stamp allows you to edit the definition of a function |
|
417 while Octave is running, and automatically use the new function |
|
418 definition without having to restart your Octave session. Checking the |
|
419 time stamp every time a function is used is rather inefficient, but it |
|
420 has to be done to ensure that the correct function definition is used. |
|
421 |
|
422 To avoid degrading performance unnecessarily by checking the time stamps |
|
423 on functions that are not likely to change, Octave assumes that function |
|
424 files in the directory tree |
|
425 @file{@var{octave-home}/share/octave/@var{version}/m} |
|
426 will not change, so it doesn't have to check their time stamps every time the |
|
427 functions defined in those files are used. This is normally a very good |
|
428 assumption and provides a significant improvement in performance for the |
|
429 function files that are distributed with Octave. |
|
430 |
|
431 If you know that your own function files will not change while you are |
|
432 running Octave, you can improve performance by setting the variable |
|
433 @code{ignore_function_time_stamp} to @code{"all"}, so that Octave will |
|
434 ignore the time stamps for all function files. Setting it to |
|
435 @code{"system"} gives the default behavior. If you set it to anything |
|
436 else, Octave will check the time stamps on all function files. |
|
437 |
|
438 @c XXX FIXME XXX -- note about time stamps on files in NFS environments? |
|
439 |
3371
|
440 @DOCSTRING(DEFAULT_LOADPATH) |
3294
|
441 |
3371
|
442 @DOCSTRING(LOADPATH) |
3294
|
443 |
3428
|
444 @DOCSTRING(rehash) |
|
445 |
|
446 @DOCSTRING(file_in_loadpath) |
|
447 |
3371
|
448 @DOCSTRING(ignore_function_time_stamp) |
3294
|
449 |
3371
|
450 @DOCSTRING(warn_function_name_clash) |
3294
|
451 |
3428
|
452 @DOCSTRING(warn_future_time_stamp) |
|
453 |
4167
|
454 @node Script Files |
3294
|
455 @section Script Files |
|
456 |
|
457 A script file is a file containing (almost) any sequence of Octave |
|
458 commands. It is read and evaluated just as if you had typed each |
|
459 command at the Octave prompt, and provides a convenient way to perform a |
|
460 sequence of commands that do not logically belong inside a function. |
|
461 |
|
462 Unlike a function file, a script file must @emph{not} begin with the |
|
463 keyword @code{function}. If it does, Octave will assume that it is a |
|
464 function file, and that it defines a single function that should be |
|
465 evaluated as soon as it is defined. |
|
466 |
|
467 A script file also differs from a function file in that the variables |
|
468 named in a script file are not local variables, but are in the same |
|
469 scope as the other variables that are visible on the command line. |
|
470 |
|
471 Even though a script file may not begin with the @code{function} |
|
472 keyword, it is possible to define more than one function in a single |
|
473 script file and load (but not execute) all of them at once. To do |
|
474 this, the first token in the file (ignoring comments and other white |
|
475 space) must be something other than @code{function}. If you have no |
|
476 other statements to evaluate, you can use a statement that has no |
|
477 effect, like this: |
|
478 |
|
479 @example |
|
480 @group |
|
481 # Prevent Octave from thinking that this |
|
482 # is a function file: |
|
483 |
|
484 1; |
|
485 |
|
486 # Define function one: |
|
487 |
|
488 function one () |
|
489 ... |
|
490 @end group |
|
491 @end example |
|
492 |
|
493 To have Octave read and compile these functions into an internal form, |
|
494 you need to make sure that the file is in Octave's @code{LOADPATH}, then |
|
495 simply type the base name of the file that contains the commands. |
|
496 (Octave uses the same rules to search for script files as it does to |
|
497 search for function files.) |
|
498 |
|
499 If the first token in a file (ignoring comments) is @code{function}, |
|
500 Octave will compile the function and try to execute it, printing a |
|
501 message warning about any non-whitespace characters that appear after |
|
502 the function definition. |
|
503 |
|
504 Note that Octave does not try to look up the definition of any identifier |
|
505 until it needs to evaluate it. This means that Octave will compile the |
|
506 following statements if they appear in a script file, or are typed at |
|
507 the command line, |
|
508 |
|
509 @example |
|
510 @group |
|
511 # not a function file: |
|
512 1; |
|
513 function foo () |
|
514 do_something (); |
|
515 endfunction |
|
516 function do_something () |
|
517 do_something_else (); |
|
518 endfunction |
|
519 @end group |
|
520 @end example |
|
521 |
|
522 @noindent |
|
523 even though the function @code{do_something} is not defined before it is |
|
524 referenced in the function @code{foo}. This is not an error because |
|
525 Octave does not need to resolve all symbols that are referenced by a |
|
526 function until the function is actually evaluated. |
|
527 |
|
528 Since Octave doesn't look for definitions until they are needed, the |
|
529 following code will always print @samp{bar = 3} whether it is typed |
|
530 directly on the command line, read from a script file, or is part of a |
|
531 function body, even if there is a function or script file called |
|
532 @file{bar.m} in Octave's @code{LOADPATH}. |
|
533 |
|
534 @example |
|
535 @group |
|
536 eval ("bar = 3"); |
|
537 bar |
|
538 @end group |
|
539 @end example |
|
540 |
|
541 Code like this appearing within a function body could fool Octave if |
|
542 definitions were resolved as the function was being compiled. It would |
|
543 be virtually impossible to make Octave clever enough to evaluate this |
|
544 code in a consistent fashion. The parser would have to be able to |
|
545 perform the call to @code{eval} at compile time, and that would be |
|
546 impossible unless all the references in the string to be evaluated could |
|
547 also be resolved, and requiring that would be too restrictive (the |
|
548 string might come from user input, or depend on things that are not |
|
549 known until the function is evaluated). |
|
550 |
|
551 Although Octave normally executes commands from script files that have |
|
552 the name @file{@var{file}.m}, you can use the function @code{source} to |
|
553 execute commands from any file. |
|
554 |
3371
|
555 @DOCSTRING(source) |
3294
|
556 |
4167
|
557 @node Dynamically Linked Functions |
3294
|
558 @section Dynamically Linked Functions |
|
559 @cindex dynamic linking |
|
560 |
|
561 On some systems, Octave can dynamically load and execute functions |
|
562 written in C++. Octave can only directly call functions written in C++, |
|
563 but you can also load functions written in other languages |
|
564 by calling them from a simple wrapper function written in C++. |
|
565 |
|
566 Here is an example of how to write a C++ function that Octave can load, |
|
567 with commentary. The source for this function is included in the source |
|
568 distributions of Octave, in the file @file{examples/oregonator.cc}. It |
|
569 defines the same set of differential equations that are used in the |
|
570 example problem of @ref{Ordinary Differential Equations}. By running |
|
571 that example and this one, we can compare the execution times to see |
|
572 what sort of increase in speed you can expect by using dynamically |
|
573 linked functions. |
|
574 |
|
575 The function defined in @file{oregonator.cc} contains just 8 statements, |
|
576 and is not much different than the code defined in the corresponding |
|
577 M-file (also distributed with Octave in the file |
|
578 @file{examples/oregonator.m}). |
|
579 |
|
580 Here is the complete text of @file{oregonator.cc}: |
|
581 |
|
582 just |
|
583 |
|
584 @example |
|
585 @group |
|
586 #include <octave/oct.h> |
|
587 |
|
588 DEFUN_DLD (oregonator, args, , |
|
589 "The `oregonator'.") |
|
590 @{ |
|
591 ColumnVector dx (3); |
|
592 |
3760
|
593 ColumnVector x (args(0).vector_value ()); |
3294
|
594 |
|
595 dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0) |
|
596 - 8.375e-06*pow (x(0), 2)); |
|
597 |
|
598 dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27; |
|
599 |
|
600 dx(2) = 0.161*(x(0) - x(2)); |
|
601 |
|
602 return octave_value (dx); |
|
603 @} |
|
604 @end group |
|
605 @end example |
|
606 |
|
607 The first line of the file, |
|
608 |
|
609 @example |
|
610 #include <octave/oct.h> |
|
611 @end example |
|
612 |
|
613 @noindent |
|
614 includes declarations for all of Octave's internal functions that you |
|
615 will need. If you need other functions from the standard C++ or C |
|
616 libraries, you can include the necessary headers here. |
|
617 |
|
618 The next two lines |
|
619 @example |
|
620 @group |
|
621 DEFUN_DLD (oregonator, args, , |
|
622 "The `oregonator'.") |
|
623 @end group |
|
624 @end example |
|
625 |
|
626 @noindent |
|
627 declares the function. The macro @code{DEFUN_DLD} and the macros that |
|
628 it depends on are defined in the files @file{defun-dld.h}, |
|
629 @file{defun.h}, and @file{defun-int.h} (these files are included in the |
|
630 header file @file{octave/oct.h}). |
|
631 |
|
632 Note that the third parameter to @code{DEFUN_DLD} (@code{nargout}) is |
3686
|
633 not used, so it is omitted from the list of arguments in order to |
|
634 avoid the warning from gcc about an unused function parameter. |
|
635 |
|
636 The next line, |
|
637 |
|
638 @example |
|
639 ColumnVector dx (3); |
|
640 @end example |
3294
|
641 |
|
642 @noindent |
|
643 simply declares an object to store the right hand sides of the |
3686
|
644 differential equation, and the statement |
3294
|
645 |
|
646 @example |
3760
|
647 ColumnVector x (args(0).vector_value ()); |
3294
|
648 @end example |
|
649 |
|
650 @noindent |
3760
|
651 extracts a vector from the first input argument. The |
|
652 @code{vector_value} method is used so that the user of the function |
|
653 can pass either a row or column vector. The @code{ColumnVector} |
|
654 constructor is needed because the ODE class requires a column |
|
655 vector. The variable @code{args} is passed to functions defined with |
|
656 @code{DEFUN_DLD} as an @code{octave_value_list} object, which includes |
|
657 methods for getting the length of the list and extracting individual |
|
658 elements. |
3294
|
659 |
|
660 In this example, we don't check for errors, but that is not difficult. |
|
661 All of the Octave's built-in functions do some form of checking on their |
|
662 arguments, so you can check the source code for those functions for |
|
663 examples of various strategies for verifying that the correct number and |
|
664 types of arguments have been supplied. |
|
665 |
|
666 The next statements |
|
667 |
|
668 @example |
|
669 @group |
|
670 ColumnVector dx (3); |
|
671 |
|
672 dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0) |
|
673 - 8.375e-06*pow (x(0), 2)); |
|
674 |
|
675 dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27; |
|
676 |
|
677 dx(2) = 0.161*(x(0) - x(2)); |
|
678 @end group |
|
679 @end example |
|
680 |
|
681 @noindent |
|
682 define the right hand side of the differential equation. Finally, we |
|
683 can return @code{dx}: |
|
684 |
|
685 @example |
|
686 return octave_value (dx); |
|
687 @end example |
|
688 |
|
689 @noindent |
|
690 The actual return type is @code{octave_value_list}, but it is only |
|
691 necessary to convert the return type to an @code{octave_value} because |
|
692 there is a default constructor that can automatically create an object |
|
693 of that type from an @code{octave_value} object, so we can just use that |
|
694 instead. |
|
695 |
|
696 To use this file, your version of Octave must support dynamic linking. |
|
697 To find out if it does, type the command |
|
698 @kbd{octave_config_info ("dld")} at the Octave prompt. Support for |
|
699 dynamic linking is included if this command returns 1. |
|
700 |
|
701 To compile the example file, type the command @samp{mkoctfile |
|
702 oregonator.cc} at the shell prompt. The script @code{mkoctfile} should |
|
703 have been installed along with Octave. Running it will create a file |
|
704 called @file{oregonator.oct} that can be loaded by Octave. To test the |
|
705 @file{oregonator.oct} file, start Octave and type the command |
|
706 |
|
707 @example |
|
708 oregonator ([1, 2, 3], 0) |
|
709 @end example |
|
710 |
|
711 @noindent |
|
712 at the Octave prompt. Octave should respond by printing |
|
713 |
|
714 @example |
|
715 @group |
|
716 ans = |
|
717 |
|
718 77.269353 |
|
719 -0.012942 |
|
720 -0.322000 |
|
721 @end group |
|
722 @end example |
|
723 |
|
724 You can now use the @file{oregonator.oct} file just as you would the |
|
725 @code{oregonator.m} file to solve the set of differential equations. |
|
726 |
|
727 On a 133 MHz Pentium running Linux, Octave can solve the problem shown |
3402
|
728 in @ref{Ordinary Differential Equations}, in about 1.4 seconds using the |
3294
|
729 dynamically linked function, compared to about 19 seconds using the |
|
730 M-file. Similar decreases in execution time can be expected for other |
|
731 functions, particularly those that rely on functions like @code{lsode} |
|
732 that require user-supplied functions. |
|
733 |
3428
|
734 Just as for M-files, Octave will automatically reload a dynamically linked |
|
735 function when the file that defines it is more recent than the last |
|
736 time that the function was loaded. If more than one function is defined |
|
737 in a single @file{.oct} file, reloading the file may force other |
|
738 functions to be cleared and reloaded. If all the functions loaded from |
|
739 a given @file{.oct} file are cleared, Octave will automatically unload |
|
740 the @file{.oct} file. |
3294
|
741 |
3371
|
742 @DOCSTRING(warn_reload_forces_clear) |
3294
|
743 |
3428
|
744 @c XXX FIXME XXX -- is there a better place for this? |
|
745 |
|
746 @DOCSTRING(variables_can_hide_functions) |
|
747 |
3294
|
748 Additional examples for writing dynamically linked functions are |
|
749 available in the files in the @file{src} directory of the Octave |
|
750 distribution. Currently, this includes the files |
|
751 |
|
752 @example |
|
753 @group |
|
754 balance.cc fft2.cc inv.cc qzval.cc |
|
755 chol.cc filter.cc log.cc schur.cc |
|
756 colloc.cc find.cc lsode.cc sort.cc |
|
757 dassl.cc fsolve.cc lu.cc svd.cc |
|
758 det.cc givens.cc minmax.cc syl.cc |
|
759 eig.cc hess.cc pinv.cc |
|
760 expm.cc ifft.cc qr.cc |
|
761 fft.cc ifft2.cc quad.cc |
|
762 @end group |
|
763 @end example |
|
764 |
|
765 @noindent |
|
766 These files use the macro @code{DEFUN_DLD_BUILTIN} instead of |
|
767 @code{DEFUN_DLD}. The difference between these two macros is just that |
|
768 @code{DEFUN_DLD_BUILTIN} can define a built-in function that is not |
|
769 dynamically loaded if the operating system does not support dynamic |
|
770 linking. To define your own dynamically linked functions you should use |
|
771 @code{DEFUN_DLD}. |
|
772 |
|
773 There is currently no detailed description of all the functions that you |
|
774 can call in a built-in function. For the time being, you will have to |
|
775 read the source code for Octave. |
|
776 |
4933
|
777 @node Function Handles and Inline |
|
778 @section Function Handles and Inline |
|
779 @cindex handle, function handles |
|
780 @cindex inline, inline functions |
|
781 |
|
782 This is a place holder for the description of function handles and |
|
783 inline functions. |
|
784 |
|
785 @menu |
|
786 * Function Handles:: |
|
787 * Inline Functions:: |
|
788 @end menu |
|
789 |
|
790 @node Function Handles |
|
791 @subsection Function Handles |
|
792 |
|
793 @DOCSTRING(functions) |
|
794 |
|
795 @DOCSTRING(func2str) |
|
796 |
|
797 @DOCSTRING(str2func) |
|
798 |
|
799 @node Inline Functions |
|
800 @subsection Inline Functions |
|
801 |
|
802 @DOCSTRING(inline) |
|
803 |
|
804 @DOCSTRING(argnames) |
|
805 |
|
806 @DOCSTRING(formula) |
|
807 |
|
808 @DOCSTRING(vectorize) |
|
809 |
4167
|
810 @node Organization of Functions |
3294
|
811 @section Organization of Functions Distributed with Octave |
|
812 |
|
813 Many of Octave's standard functions are distributed as function files. |
|
814 They are loosely organized by topic, in subdirectories of |
|
815 @file{@var{octave-home}/lib/octave/@var{version}/m}, to make it easier |
|
816 to find them. |
|
817 |
|
818 The following is a list of all the function file subdirectories, and the |
|
819 types of functions you will find there. |
|
820 |
|
821 @table @file |
|
822 @item audio |
|
823 Functions for playing and recording sounds. |
|
824 |
|
825 @item control |
|
826 Functions for design and simulation of automatic control systems. |
|
827 |
|
828 @item elfun |
|
829 Elementary functions. |
|
830 |
|
831 @item general |
|
832 Miscellaneous matrix manipulations, like @code{flipud}, @code{rot90}, |
|
833 and @code{triu}, as well as other basic functions, like |
4029
|
834 @code{ismatrix}, @code{nargchk}, etc. |
3294
|
835 |
|
836 @item image |
|
837 Image processing tools. These functions require the X Window System. |
|
838 |
|
839 @item io |
|
840 Input-ouput functions. |
|
841 |
|
842 @item linear-algebra |
|
843 Functions for linear algebra. |
|
844 |
|
845 @item miscellaneous |
|
846 Functions that don't really belong anywhere else. |
|
847 |
|
848 @item plot |
|
849 A set of functions that implement the @sc{Matlab}-like plotting functions. |
|
850 |
|
851 @item polynomial |
|
852 Functions for manipulating polynomials. |
|
853 |
|
854 @item set |
|
855 Functions for creating and manipulating sets of unique values. |
|
856 |
|
857 @item signal |
|
858 Functions for signal processing applications. |
|
859 |
|
860 @item specfun |
|
861 Special functions. |
|
862 |
|
863 @item special-matrix |
|
864 Functions that create special matrix forms. |
|
865 |
|
866 @item startup |
|
867 Octave's system-wide startup file. |
|
868 |
|
869 @item statistics |
|
870 Statistical functions. |
|
871 |
|
872 @item strings |
|
873 Miscellaneous string-handling functions. |
|
874 |
|
875 @item time |
|
876 Functions related to time keeping. |
|
877 @end table |