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