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 |
4167
|
228 @node Multiple Return Values |
3294
|
229 @section Multiple Return Values |
|
230 |
|
231 Unlike many other computer languages, Octave allows you to define |
|
232 functions that return more than one value. The syntax for defining |
|
233 functions that return multiple values is |
|
234 |
|
235 @example |
|
236 function [@var{ret-list}] = @var{name} (@var{arg-list}) |
|
237 @var{body} |
|
238 endfunction |
|
239 @end example |
|
240 |
|
241 @noindent |
|
242 where @var{name}, @var{arg-list}, and @var{body} have the same meaning |
|
243 as before, and @var{ret-list} is a comma-separated list of variable |
|
244 names that will hold the values returned from the function. The list of |
|
245 return values must have at least one element. If @var{ret-list} has |
|
246 only one element, this form of the @code{function} statement is |
|
247 equivalent to the form described in the previous section. |
|
248 |
|
249 Here is an example of a function that returns two values, the maximum |
|
250 element of a vector and the index of its first occurrence in the vector. |
|
251 |
|
252 @example |
|
253 @group |
|
254 function [max, idx] = vmax (v) |
|
255 idx = 1; |
|
256 max = v (idx); |
|
257 for i = 2:length (v) |
|
258 if (v (i) > max) |
|
259 max = v (i); |
|
260 idx = i; |
|
261 endif |
|
262 endfor |
|
263 endfunction |
|
264 @end group |
|
265 @end example |
|
266 |
|
267 In this particular case, the two values could have been returned as |
|
268 elements of a single array, but that is not always possible or |
|
269 convenient. The values to be returned may not have compatible |
|
270 dimensions, and it is often desirable to give the individual return |
|
271 values distinct names. |
|
272 |
|
273 In addition to setting @code{nargin} each time a function is called, |
|
274 Octave also automatically initializes @code{nargout} to the number of |
|
275 values that are expected to be returned. This allows you to write |
|
276 functions that behave differently depending on the number of values that |
|
277 the user of the function has requested. The implicit assignment to the |
|
278 built-in variable @code{ans} does not figure in the count of output |
|
279 arguments, so the value of @code{nargout} may be zero. |
|
280 |
|
281 The @code{svd} and @code{lu} functions are examples of built-in |
|
282 functions that behave differently depending on the value of |
|
283 @code{nargout}. |
|
284 |
|
285 It is possible to write functions that only set some return values. For |
|
286 example, calling the function |
|
287 |
|
288 @example |
|
289 function [x, y, z] = f () |
|
290 x = 1; |
|
291 z = 2; |
|
292 endfunction |
|
293 @end example |
|
294 |
|
295 @noindent |
|
296 as |
|
297 |
|
298 @example |
|
299 [a, b, c] = f () |
|
300 @end example |
|
301 |
|
302 @noindent |
|
303 produces: |
|
304 |
|
305 @example |
|
306 a = 1 |
|
307 |
|
308 b = [](0x0) |
|
309 |
|
310 c = 2 |
|
311 @end example |
|
312 |
|
313 @noindent |
6501
|
314 along with a warning. |
3294
|
315 |
4700
|
316 @DOCSTRING(nargout) |
3294
|
317 |
3371
|
318 @DOCSTRING(nargchk) |
3294
|
319 |
4167
|
320 @node Variable-length Argument Lists |
3294
|
321 @section Variable-length Argument Lists |
4933
|
322 @cindex variable-length argument lists |
3294
|
323 @cindex @code{...} |
|
324 |
4167
|
325 @node Variable-length Return Lists |
3294
|
326 @section Variable-length Return Lists |
4933
|
327 @cindex variable-length return lists |
3294
|
328 @cindex @code{...} |
|
329 |
4167
|
330 @node Returning From a Function |
3294
|
331 @section Returning From a Function |
|
332 |
|
333 The body of a user-defined function can contain a @code{return} statement. |
|
334 This statement returns control to the rest of the Octave program. It |
|
335 looks like this: |
|
336 |
|
337 @example |
|
338 return |
|
339 @end example |
|
340 |
|
341 Unlike the @code{return} statement in C, Octave's @code{return} |
|
342 statement cannot be used to return a value from a function. Instead, |
|
343 you must assign values to the list of return variables that are part of |
|
344 the @code{function} statement. The @code{return} statement simply makes |
|
345 it easier to exit a function from a deeply nested loop or conditional |
|
346 statement. |
|
347 |
|
348 Here is an example of a function that checks to see if any elements of a |
|
349 vector are nonzero. |
|
350 |
|
351 @example |
|
352 @group |
|
353 function retval = any_nonzero (v) |
|
354 retval = 0; |
|
355 for i = 1:length (v) |
|
356 if (v (i) != 0) |
|
357 retval = 1; |
|
358 return; |
|
359 endif |
|
360 endfor |
|
361 printf ("no nonzero elements found\n"); |
|
362 endfunction |
|
363 @end group |
|
364 @end example |
|
365 |
|
366 Note that this function could not have been written using the |
|
367 @code{break} statement to exit the loop once a nonzero value is found |
|
368 without adding extra logic to avoid printing the message if the vector |
|
369 does contain a nonzero element. |
|
370 |
5763
|
371 @deffn {Keyword} return |
3294
|
372 When Octave encounters the keyword @code{return} inside a function or |
5016
|
373 script, it returns control to the caller immediately. At the top level, |
3294
|
374 the return statement is ignored. A @code{return} statement is assumed |
|
375 at the end of every function definition. |
5763
|
376 @end deffn |
3294
|
377 |
3371
|
378 @DOCSTRING(return_last_computed_value) |
3294
|
379 |
4167
|
380 @node Function Files |
3294
|
381 @section Function Files |
|
382 @cindex function file |
|
383 |
|
384 Except for simple one-shot programs, it is not practical to have to |
|
385 define all the functions you need each time you need them. Instead, you |
|
386 will normally want to save them in a file so that you can easily edit |
|
387 them, and save them for use at a later time. |
|
388 |
|
389 Octave does not require you to load function definitions from files |
|
390 before using them. You simply need to put the function definitions in a |
|
391 place where Octave can find them. |
|
392 |
|
393 When Octave encounters an identifier that is undefined, it first looks |
|
394 for variables or functions that are already compiled and currently |
|
395 listed in its symbol table. If it fails to find a definition there, it |
6477
|
396 searches the list of directories specified by the internal variable |
3294
|
397 @code{LOADPATH} for files ending in @file{.m} that have the same base |
|
398 name as the undefined identifier.@footnote{The @samp{.m} suffix was |
|
399 chosen for compatibility with @sc{Matlab}.} Once Octave finds a file |
|
400 with a name that matches, the contents of the file are read. If it |
|
401 defines a @emph{single} function, it is compiled and executed. |
|
402 @xref{Script Files}, for more information about how you can define more |
|
403 than one function in a single file. |
|
404 |
|
405 When Octave defines a function from a function file, it saves the full |
|
406 name of the file it read and the time stamp on the file. After |
|
407 that, it checks the time stamp on the file every time it needs the |
|
408 function. If the time stamp indicates that the file has changed since |
|
409 the last time it was read, Octave reads it again. |
|
410 |
|
411 Checking the time stamp allows you to edit the definition of a function |
|
412 while Octave is running, and automatically use the new function |
|
413 definition without having to restart your Octave session. Checking the |
|
414 time stamp every time a function is used is rather inefficient, but it |
|
415 has to be done to ensure that the correct function definition is used. |
|
416 |
|
417 To avoid degrading performance unnecessarily by checking the time stamps |
|
418 on functions that are not likely to change, Octave assumes that function |
|
419 files in the directory tree |
|
420 @file{@var{octave-home}/share/octave/@var{version}/m} |
|
421 will not change, so it doesn't have to check their time stamps every time the |
|
422 functions defined in those files are used. This is normally a very good |
|
423 assumption and provides a significant improvement in performance for the |
|
424 function files that are distributed with Octave. |
|
425 |
|
426 If you know that your own function files will not change while you are |
|
427 running Octave, you can improve performance by setting the variable |
|
428 @code{ignore_function_time_stamp} to @code{"all"}, so that Octave will |
|
429 ignore the time stamps for all function files. Setting it to |
|
430 @code{"system"} gives the default behavior. If you set it to anything |
|
431 else, Octave will check the time stamps on all function files. |
|
432 |
5775
|
433 @c FIXME -- note about time stamps on files in NFS environments? |
3294
|
434 |
6502
|
435 @DOCSTRING(addpath) |
|
436 |
|
437 @DOCSTRING(genpath) |
|
438 |
|
439 @DOCSTRING(rmpath) |
|
440 |
|
441 @DOCSTRING(savepath) |
|
442 |
6477
|
443 @DOCSTRING(path) |
3294
|
444 |
6502
|
445 @DOCSTRING(pathdef) |
|
446 |
|
447 @DOCSTRING(pathsep) |
|
448 |
3428
|
449 @DOCSTRING(rehash) |
|
450 |
|
451 @DOCSTRING(file_in_loadpath) |
|
452 |
3371
|
453 @DOCSTRING(ignore_function_time_stamp) |
3294
|
454 |
4167
|
455 @node Script Files |
3294
|
456 @section Script Files |
|
457 |
|
458 A script file is a file containing (almost) any sequence of Octave |
|
459 commands. It is read and evaluated just as if you had typed each |
|
460 command at the Octave prompt, and provides a convenient way to perform a |
|
461 sequence of commands that do not logically belong inside a function. |
|
462 |
|
463 Unlike a function file, a script file must @emph{not} begin with the |
|
464 keyword @code{function}. If it does, Octave will assume that it is a |
|
465 function file, and that it defines a single function that should be |
|
466 evaluated as soon as it is defined. |
|
467 |
|
468 A script file also differs from a function file in that the variables |
|
469 named in a script file are not local variables, but are in the same |
|
470 scope as the other variables that are visible on the command line. |
|
471 |
|
472 Even though a script file may not begin with the @code{function} |
|
473 keyword, it is possible to define more than one function in a single |
|
474 script file and load (but not execute) all of them at once. To do |
|
475 this, the first token in the file (ignoring comments and other white |
|
476 space) must be something other than @code{function}. If you have no |
|
477 other statements to evaluate, you can use a statement that has no |
|
478 effect, like this: |
|
479 |
|
480 @example |
|
481 @group |
|
482 # Prevent Octave from thinking that this |
|
483 # is a function file: |
|
484 |
|
485 1; |
|
486 |
|
487 # Define function one: |
|
488 |
|
489 function one () |
|
490 ... |
|
491 @end group |
|
492 @end example |
|
493 |
|
494 To have Octave read and compile these functions into an internal form, |
6477
|
495 you need to make sure that the file is in Octave's @code{LOADPATH} |
|
496 (accessible through the @code{path} function), then simply type the |
|
497 base name of the file that contains the commands. (Octave uses the |
|
498 same rules to search for script files as it does to search for |
|
499 function files.) |
3294
|
500 |
|
501 If the first token in a file (ignoring comments) is @code{function}, |
|
502 Octave will compile the function and try to execute it, printing a |
|
503 message warning about any non-whitespace characters that appear after |
|
504 the function definition. |
|
505 |
|
506 Note that Octave does not try to look up the definition of any identifier |
|
507 until it needs to evaluate it. This means that Octave will compile the |
|
508 following statements if they appear in a script file, or are typed at |
|
509 the command line, |
|
510 |
|
511 @example |
|
512 @group |
|
513 # not a function file: |
|
514 1; |
|
515 function foo () |
|
516 do_something (); |
|
517 endfunction |
|
518 function do_something () |
|
519 do_something_else (); |
|
520 endfunction |
|
521 @end group |
|
522 @end example |
|
523 |
|
524 @noindent |
|
525 even though the function @code{do_something} is not defined before it is |
|
526 referenced in the function @code{foo}. This is not an error because |
|
527 Octave does not need to resolve all symbols that are referenced by a |
|
528 function until the function is actually evaluated. |
|
529 |
|
530 Since Octave doesn't look for definitions until they are needed, the |
|
531 following code will always print @samp{bar = 3} whether it is typed |
|
532 directly on the command line, read from a script file, or is part of a |
|
533 function body, even if there is a function or script file called |
6477
|
534 @file{bar.m} in Octave's path. |
3294
|
535 |
|
536 @example |
|
537 @group |
|
538 eval ("bar = 3"); |
|
539 bar |
|
540 @end group |
|
541 @end example |
|
542 |
|
543 Code like this appearing within a function body could fool Octave if |
|
544 definitions were resolved as the function was being compiled. It would |
|
545 be virtually impossible to make Octave clever enough to evaluate this |
|
546 code in a consistent fashion. The parser would have to be able to |
|
547 perform the call to @code{eval} at compile time, and that would be |
|
548 impossible unless all the references in the string to be evaluated could |
|
549 also be resolved, and requiring that would be too restrictive (the |
|
550 string might come from user input, or depend on things that are not |
|
551 known until the function is evaluated). |
|
552 |
|
553 Although Octave normally executes commands from script files that have |
|
554 the name @file{@var{file}.m}, you can use the function @code{source} to |
|
555 execute commands from any file. |
|
556 |
3371
|
557 @DOCSTRING(source) |
3294
|
558 |
4167
|
559 @node Dynamically Linked Functions |
3294
|
560 @section Dynamically Linked Functions |
|
561 @cindex dynamic linking |
|
562 |
|
563 On some systems, Octave can dynamically load and execute functions |
|
564 written in C++. Octave can only directly call functions written in C++, |
|
565 but you can also load functions written in other languages |
|
566 by calling them from a simple wrapper function written in C++. |
|
567 |
|
568 Here is an example of how to write a C++ function that Octave can load, |
|
569 with commentary. The source for this function is included in the source |
|
570 distributions of Octave, in the file @file{examples/oregonator.cc}. It |
|
571 defines the same set of differential equations that are used in the |
|
572 example problem of @ref{Ordinary Differential Equations}. By running |
|
573 that example and this one, we can compare the execution times to see |
|
574 what sort of increase in speed you can expect by using dynamically |
|
575 linked functions. |
|
576 |
|
577 The function defined in @file{oregonator.cc} contains just 8 statements, |
|
578 and is not much different than the code defined in the corresponding |
|
579 M-file (also distributed with Octave in the file |
|
580 @file{examples/oregonator.m}). |
|
581 |
|
582 Here is the complete text of @file{oregonator.cc}: |
|
583 |
|
584 just |
|
585 |
|
586 @example |
|
587 @group |
|
588 #include <octave/oct.h> |
|
589 |
|
590 DEFUN_DLD (oregonator, args, , |
|
591 "The `oregonator'.") |
|
592 @{ |
|
593 ColumnVector dx (3); |
|
594 |
3760
|
595 ColumnVector x (args(0).vector_value ()); |
3294
|
596 |
|
597 dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0) |
|
598 - 8.375e-06*pow (x(0), 2)); |
|
599 |
|
600 dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27; |
|
601 |
|
602 dx(2) = 0.161*(x(0) - x(2)); |
|
603 |
|
604 return octave_value (dx); |
|
605 @} |
|
606 @end group |
|
607 @end example |
|
608 |
|
609 The first line of the file, |
|
610 |
|
611 @example |
|
612 #include <octave/oct.h> |
|
613 @end example |
|
614 |
|
615 @noindent |
|
616 includes declarations for all of Octave's internal functions that you |
|
617 will need. If you need other functions from the standard C++ or C |
|
618 libraries, you can include the necessary headers here. |
|
619 |
|
620 The next two lines |
|
621 @example |
|
622 @group |
|
623 DEFUN_DLD (oregonator, args, , |
|
624 "The `oregonator'.") |
|
625 @end group |
|
626 @end example |
|
627 |
|
628 @noindent |
|
629 declares the function. The macro @code{DEFUN_DLD} and the macros that |
|
630 it depends on are defined in the files @file{defun-dld.h}, |
|
631 @file{defun.h}, and @file{defun-int.h} (these files are included in the |
|
632 header file @file{octave/oct.h}). |
|
633 |
|
634 Note that the third parameter to @code{DEFUN_DLD} (@code{nargout}) is |
3686
|
635 not used, so it is omitted from the list of arguments in order to |
|
636 avoid the warning from gcc about an unused function parameter. |
|
637 |
|
638 The next line, |
|
639 |
|
640 @example |
|
641 ColumnVector dx (3); |
|
642 @end example |
3294
|
643 |
|
644 @noindent |
|
645 simply declares an object to store the right hand sides of the |
3686
|
646 differential equation, and the statement |
3294
|
647 |
|
648 @example |
3760
|
649 ColumnVector x (args(0).vector_value ()); |
3294
|
650 @end example |
|
651 |
|
652 @noindent |
3760
|
653 extracts a vector from the first input argument. The |
|
654 @code{vector_value} method is used so that the user of the function |
|
655 can pass either a row or column vector. The @code{ColumnVector} |
|
656 constructor is needed because the ODE class requires a column |
|
657 vector. The variable @code{args} is passed to functions defined with |
|
658 @code{DEFUN_DLD} as an @code{octave_value_list} object, which includes |
|
659 methods for getting the length of the list and extracting individual |
|
660 elements. |
3294
|
661 |
|
662 In this example, we don't check for errors, but that is not difficult. |
|
663 All of the Octave's built-in functions do some form of checking on their |
|
664 arguments, so you can check the source code for those functions for |
|
665 examples of various strategies for verifying that the correct number and |
|
666 types of arguments have been supplied. |
|
667 |
|
668 The next statements |
|
669 |
|
670 @example |
|
671 @group |
|
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 |
5016
|
682 define the right-hand side of the differential equation. Finally, we |
3294
|
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 |
5775
|
742 @c FIXME -- is there a better place for this? |
3428
|
743 |
|
744 @DOCSTRING(variables_can_hide_functions) |
|
745 |
3294
|
746 Additional examples for writing dynamically linked functions are |
|
747 available in the files in the @file{src} directory of the Octave |
|
748 distribution. Currently, this includes the files |
|
749 |
|
750 @example |
|
751 @group |
|
752 balance.cc fft2.cc inv.cc qzval.cc |
|
753 chol.cc filter.cc log.cc schur.cc |
|
754 colloc.cc find.cc lsode.cc sort.cc |
|
755 dassl.cc fsolve.cc lu.cc svd.cc |
|
756 det.cc givens.cc minmax.cc syl.cc |
|
757 eig.cc hess.cc pinv.cc |
|
758 expm.cc ifft.cc qr.cc |
|
759 fft.cc ifft2.cc quad.cc |
|
760 @end group |
|
761 @end example |
|
762 |
|
763 @noindent |
|
764 These files use the macro @code{DEFUN_DLD_BUILTIN} instead of |
|
765 @code{DEFUN_DLD}. The difference between these two macros is just that |
|
766 @code{DEFUN_DLD_BUILTIN} can define a built-in function that is not |
|
767 dynamically loaded if the operating system does not support dynamic |
|
768 linking. To define your own dynamically linked functions you should use |
|
769 @code{DEFUN_DLD}. |
|
770 |
|
771 There is currently no detailed description of all the functions that you |
|
772 can call in a built-in function. For the time being, you will have to |
|
773 read the source code for Octave. |
|
774 |
4933
|
775 @node Function Handles and Inline |
|
776 @section Function Handles and Inline |
|
777 @cindex handle, function handles |
|
778 @cindex inline, inline functions |
|
779 |
|
780 This is a place holder for the description of function handles and |
|
781 inline functions. |
|
782 |
|
783 @menu |
|
784 * Function Handles:: |
|
785 * Inline Functions:: |
|
786 @end menu |
|
787 |
|
788 @node Function Handles |
|
789 @subsection Function Handles |
|
790 |
|
791 @DOCSTRING(functions) |
|
792 |
|
793 @DOCSTRING(func2str) |
|
794 |
|
795 @DOCSTRING(str2func) |
|
796 |
|
797 @node Inline Functions |
|
798 @subsection Inline Functions |
|
799 |
|
800 @DOCSTRING(inline) |
|
801 |
|
802 @DOCSTRING(argnames) |
|
803 |
|
804 @DOCSTRING(formula) |
|
805 |
|
806 @DOCSTRING(vectorize) |
|
807 |
4167
|
808 @node Organization of Functions |
3294
|
809 @section Organization of Functions Distributed with Octave |
|
810 |
|
811 Many of Octave's standard functions are distributed as function files. |
|
812 They are loosely organized by topic, in subdirectories of |
|
813 @file{@var{octave-home}/lib/octave/@var{version}/m}, to make it easier |
|
814 to find them. |
|
815 |
|
816 The following is a list of all the function file subdirectories, and the |
|
817 types of functions you will find there. |
|
818 |
|
819 @table @file |
|
820 @item audio |
|
821 Functions for playing and recording sounds. |
|
822 |
|
823 @item control |
|
824 Functions for design and simulation of automatic control systems. |
|
825 |
|
826 @item elfun |
|
827 Elementary functions. |
|
828 |
|
829 @item general |
|
830 Miscellaneous matrix manipulations, like @code{flipud}, @code{rot90}, |
|
831 and @code{triu}, as well as other basic functions, like |
4029
|
832 @code{ismatrix}, @code{nargchk}, etc. |
3294
|
833 |
|
834 @item image |
|
835 Image processing tools. These functions require the X Window System. |
|
836 |
|
837 @item io |
|
838 Input-ouput functions. |
|
839 |
|
840 @item linear-algebra |
|
841 Functions for linear algebra. |
|
842 |
|
843 @item miscellaneous |
|
844 Functions that don't really belong anywhere else. |
|
845 |
|
846 @item plot |
|
847 A set of functions that implement the @sc{Matlab}-like plotting functions. |
|
848 |
|
849 @item polynomial |
|
850 Functions for manipulating polynomials. |
|
851 |
|
852 @item set |
|
853 Functions for creating and manipulating sets of unique values. |
|
854 |
|
855 @item signal |
|
856 Functions for signal processing applications. |
|
857 |
|
858 @item specfun |
|
859 Special functions. |
|
860 |
|
861 @item special-matrix |
|
862 Functions that create special matrix forms. |
|
863 |
|
864 @item startup |
|
865 Octave's system-wide startup file. |
|
866 |
|
867 @item statistics |
|
868 Statistical functions. |
|
869 |
|
870 @item strings |
|
871 Miscellaneous string-handling functions. |
|
872 |
|
873 @item time |
|
874 Functions related to time keeping. |
|
875 @end table |