home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / info / octave.info-3 < prev    next >
Encoding:
GNU Info File  |  1996-10-12  |  35.9 KB  |  1,019 lines

  1. This is Info file octave.info, produced by Makeinfo-1.64 from the input
  2. file octave.texi.
  3.  
  4.    Copyright (C) 1993, 1994, 1995 John W. Eaton.
  5.  
  6.    Permission is granted to make and distribute verbatim copies of this
  7. manual provided the copyright notice and this permission notice are
  8. preserved on all copies.
  9.  
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided that
  12. the entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14.  
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions.
  18.  
  19. 
  20. File: octave.info,  Node: The while Statement,  Next: The for Statement,  Prev: The if Statement,  Up: Statements
  21.  
  22. The `while' Statement
  23. =====================
  24.  
  25.    In programming, a "loop" means a part of a program that is (or at
  26. least can be) executed two or more times in succession.
  27.  
  28.    The `while' statement is the simplest looping statement in Octave.
  29. It repeatedly executes a statement as long as a condition is true.  As
  30. with the condition in an `if' statement, the condition in a `while'
  31. statement is considered true if its value is non-zero, and false if its
  32. value is zero.  If the value of the conditional expression in an `if'
  33. statement is a vector or a matrix, it is considered true only if *all*
  34. of the elements are non-zero.
  35.  
  36.    Octave's `while' statement looks like this:
  37.  
  38.      while (CONDITION)
  39.        BODY
  40.      endwhile
  41.  
  42. Here BODY is a statement or list of statements that we call the "body"
  43. of the loop, and CONDITION is an expression that controls how long the
  44. loop keeps running.
  45.  
  46.    The first thing the `while' statement does is test CONDITION.  If
  47. CONDITION is true, it executes the statement BODY.  After BODY has been
  48. executed, CONDITION is tested again, and if it is still true, BODY is
  49. executed again.  This process repeats until CONDITION is no longer
  50. true.  If CONDITION is initially false, the body of the loop is never
  51. executed.
  52.  
  53.    This example creates a variable `fib' that contains the elements of
  54. the Fibonacci sequence.
  55.  
  56.      fib = ones (1, 10);
  57.      i = 3;
  58.      while (i <= 10)
  59.        fib (i) = fib (i-1) + fib (i-2);
  60.        i++;
  61.      endwhile
  62.  
  63. Here the body of the loop contains two statements.
  64.  
  65.    The loop works like this: first, the value of `i' is set to 3.
  66. Then, the `while' tests whether `i' is less than or equal to 10.  This
  67. is the case when `i' equals 3, so the value of the `i'-th element of
  68. `fib' is set to the sum of the previous two values in the sequence.
  69. Then the `i++' increments the value of `i' and the loop repeats.  The
  70. loop terminates when `i' reaches 11.
  71.  
  72.    A newline is not required between the condition and the body; but
  73. using one makes the program clearer unless the body is very simple.
  74.  
  75. 
  76. File: octave.info,  Node: The for Statement,  Next: The break Statement,  Prev: The while Statement,  Up: Statements
  77.  
  78. The `for' Statement
  79. ===================
  80.  
  81.    The `for' statement makes it more convenient to count iterations of a
  82. loop.  The general form of the `for' statement looks like this:
  83.  
  84.      for VAR = EXPRESSION
  85.        BODY
  86.      endfor
  87.  
  88. The assignment expression in the `for' statement works a bit
  89. differently than Octave's normal assignment statement.  Instead of
  90. assigning the complete result of the expression, it assigns each column
  91. of the expression to VAR in turn.  If EXPRESSION is either a row vector
  92. or a scalar, the value of VAR will be a scalar each time the loop body
  93. is executed.  If VAR is a column vector or a matrix, VAR will be a
  94. column vector each time the loop body is executed.
  95.  
  96.    The following example shows another way to create a vector containing
  97. the first ten elements of the Fibonacci sequence, this time using the
  98. `for' statement:
  99.  
  100.      fib = ones (1, 10);
  101.      for i = 3:10
  102.        fib (i) = fib (i-1) + fib (i-2);
  103.      endfor
  104.  
  105. This code works by first evaluating the expression `3:10', to produce a
  106. range of values from 3 to 10 inclusive.  Then the variable `i' is
  107. assigned the first element of the range and the body of the loop is
  108. executed once.  When the end of the loop body is reached, the next
  109. value in the range is assigned to the variable `i', and the loop body
  110. is executed again.  This process continues until there are no more
  111. elements to assign.
  112.  
  113.    In the `for' statement, BODY stands for any statement or list of
  114. statements.
  115.  
  116.    Although it is possible to rewrite all `for' loops as `while' loops,
  117. the Octave language has both statements because often a `for' loop is
  118. both less work to type and more natural to think of.  Counting the
  119. number of iterations is very common in loops and it can be easier to
  120. think of this counting as part of looping rather than as something to
  121. do inside the loop.
  122.  
  123. 
  124. File: octave.info,  Node: The break Statement,  Next: The continue Statement,  Prev: The for Statement,  Up: Statements
  125.  
  126. The `break' Statement
  127. =====================
  128.  
  129.    The `break' statement jumps out of the innermost `for' or `while'
  130. loop that encloses it.  The `break' statement may only be used within
  131. the body of a loop.  The following example finds the smallest divisor
  132. of a given integer, and also identifies prime numbers:
  133.  
  134.      num = 103;
  135.      div = 2;
  136.      while (div*div <= num)
  137.        if (rem (num, div) == 0)
  138.          break;
  139.        endif
  140.        div++;
  141.      endwhile
  142.      if (rem (num, div) == 0)
  143.        printf ("Smallest divisor of %d is %d\n", num, div)
  144.      else
  145.        printf ("%d is prime\n", num);
  146.      endif
  147.  
  148.    When the remainder is zero in the first `while' statement, Octave
  149. immediately "breaks out" of the loop.  This means that Octave proceeds
  150. immediately to the statement following the loop and continues
  151. processing.  (This is very different from the `exit' statement which
  152. stops the entire Octave program.)
  153.  
  154.    Here is another program equivalent to the previous one.  It
  155. illustrates how the CONDITION of a `while' statement could just as well
  156. be replaced with a `break' inside an `if':
  157.  
  158.      num = 103;
  159.      div = 2;
  160.      while (1)
  161.        if (rem (num, div) == 0)
  162.          printf ("Smallest divisor of %d is %d\n", num, div);
  163.          break;
  164.        endif
  165.        div++;
  166.        if (div*div > num)
  167.          printf ("%d is prime\n", num);
  168.          break;
  169.        endif
  170.      endwhile
  171.  
  172. 
  173. File: octave.info,  Node: The continue Statement,  Next: The unwind_protect Statement,  Prev: The break Statement,  Up: Statements
  174.  
  175. The `continue' Statement
  176. ========================
  177.  
  178.    The `continue' statement, like `break', is used only inside `for' or
  179. `while' loops.  It skips over the rest of the loop body, causing the
  180. next cycle around the loop to begin immediately.  Contrast this with
  181. `break', which jumps out of the loop altogether.  Here is an example:
  182.  
  183.      # print elements of a vector of random
  184.      # integers that are even.
  185.      
  186.      # first, create a row vector of 10 random
  187.      # integers with values between 0 and 100:
  188.      
  189.      vec = round (rand (1, 10) * 100);
  190.      
  191.      # print what we're interested in:
  192.      
  193.      for x = vec
  194.        if (rem (x, 2) != 0)
  195.          continue;
  196.        endif
  197.        printf ("%d\n", x);
  198.      endfor
  199.  
  200.    If one of the elements of VEC is an odd number, this example skips
  201. the print statement for that element, and continues back to the first
  202. statement in the loop.
  203.  
  204.    This is not a practical example of the `continue' statement, but it
  205. should give you a clear understanding of how it works.  Normally, one
  206. would probably write the loop like this:
  207.  
  208.      for x = vec
  209.        if (rem (x, 2) == 0)
  210.          printf ("%d\n", x);
  211.        endif
  212.      endfor
  213.  
  214. 
  215. File: octave.info,  Node: The unwind_protect Statement,  Next: Continuation Lines,  Prev: The continue Statement,  Up: Statements
  216.  
  217. The `unwind_protect' Statement
  218. ==============================
  219.  
  220.    Octave supports a limited form of exception handling modelled after
  221. the unwind-protect form of Lisp.
  222.  
  223.    The general form of an `unwind_protect' block looks like this:
  224.  
  225.      unwind_protect
  226.        BODY
  227.      unwind_protect_cleanup
  228.        CLEANUP
  229.      end_unwind_protect
  230.  
  231. Where BODY and CLEANUP are both optional and may contain any Octave
  232. expressions or commands.  The statements in CLEANUP are guaranteed to
  233. be executed regardless of how control exits BODY.
  234.  
  235.    This is useful to protect temporary changes to global variables from
  236. possible errors.  For example, the following code will always restore
  237. the original value of the built-in variable `do_fortran_indexing' even
  238. if an error occurs while performing the indexing operation.
  239.  
  240.      save_do_fortran_indexing = do_fortran_indexing;
  241.      unwind_protect
  242.        do_fortran_indexing = "true";
  243.        elt = a (idx)
  244.      unwind_protect_cleanup
  245.        do_fortran_indexing = save_do_fortran_indexing;
  246.      end_unwind_protect
  247.  
  248.    Without `unwind_protect', the value of DO_FORTRAN_INDEXING would not
  249. be restored if an error occurs while performing the indexing operation
  250. because evaluation would stop at the point of the error and the
  251. statement to restore the value would not be executed.
  252.  
  253. 
  254. File: octave.info,  Node: Continuation Lines,  Prev: The unwind_protect Statement,  Up: Statements
  255.  
  256. Continuation Lines
  257. ==================
  258.  
  259.    In the Octave language, most statements end with a newline character
  260. and you must tell Octave to ignore the newline character in order to
  261. continue a statement from one line to the next.  Lines that end with the
  262. characters `...' or `\' are joined with the following line before they
  263. are divided into tokens by Octave's parser.  For example, the lines
  264.  
  265.      x = long_variable_name ...
  266.          + longer_variable_name \
  267.          - 42
  268.  
  269. form a single statement.  The backslash character on the second line
  270. above is interpreted a continuation character, *not* as a division
  271. operator.
  272.  
  273.    For continuation lines that do not occur inside string constants,
  274. whitespace and comments may appear between the continuation marker and
  275. the newline character.  For example, the statement
  276.  
  277.      x = long_variable_name ...     % comment one
  278.          + longer_variable_name \   % comment two
  279.          - 42                       % last comment
  280.  
  281. is equivalent to the one shown above.
  282.  
  283.    In some cases, Octave will allow you to continue lines without
  284. having to specify continuation characters.  For example, it is possible
  285. to write statements like
  286.  
  287.      if (big_long_variable_name == other_long_variable_name
  288.          || not_so_short_variable_name > 4
  289.          && y > x)
  290.        some (code, here);
  291.      endif
  292.  
  293. without having to clutter up the if statement with continuation
  294. characters.
  295.  
  296. 
  297. File: octave.info,  Node: Functions and Scripts,  Next: Built-in Variables,  Prev: Statements,  Up: Top
  298.  
  299. Functions and Script Files
  300. **************************
  301.  
  302.    Complicated Octave programs can often be simplified by defining
  303. functions.  Functions can be defined directly on the command line during
  304. interactive Octave sessions, or in external files, and can be called
  305. just like built-in ones.
  306.  
  307. * Menu:
  308.  
  309. * Defining Functions::
  310. * Multiple Return Values::
  311. * Variable-length Argument Lists::
  312. * Variable-length Return Lists::
  313. * Returning From a Function::
  314. * Function Files::
  315. * Script Files::
  316. * Dynamically Linked Functions::
  317. * Organization of Functions::
  318.  
  319. 
  320. File: octave.info,  Node: Defining Functions,  Next: Multiple Return Values,  Prev: Functions and Scripts,  Up: Functions and Scripts
  321.  
  322. Defining Functions
  323. ==================
  324.  
  325.    In its simplest form, the definition of a function named NAME looks
  326. like this:
  327.  
  328.      function NAME
  329.        BODY
  330.      endfunction
  331.  
  332. A valid function name is like a valid variable name: a sequence of
  333. letters, digits and underscores, not starting with a digit.  Functions
  334. share the same pool of names as variables.
  335.  
  336.    The function BODY consists of Octave statements.  It is the most
  337. important part of the definition, because it says what the function
  338. should actually *do*.
  339.  
  340.    For example, here is a function that, when executed, will ring the
  341. bell on your terminal (assuming that it is possible to do so):
  342.  
  343.      function wakeup
  344.        printf ("\a");
  345.      endfunction
  346.  
  347.    The `printf' statement (*note Input and Output::.) simply tells
  348. Octave to print the string `"\a"'.  The special character `\a' stands
  349. for the alert character (ASCII 7).  *Note String Constants::.
  350.  
  351.    Once this function is defined, you can ask Octave to evaluate it by
  352. typing the name of the function.
  353.  
  354.    Normally, you will want to pass some information to the functions you
  355. define.  The syntax for passing parameters to a function in Octave is
  356.  
  357.      function NAME (ARG-LIST)
  358.        BODY
  359.      endfunction
  360.  
  361. where ARG-LIST is a comma-separated list of the function's arguments.
  362. When the function is called, the argument names are used to hold the
  363. argument values given in the call.  The list of arguments may be empty,
  364. in which case this form is equivalent to the one shown above.
  365.  
  366.    To print a message along with ringing the bell, you might modify the
  367. `beep' to look like this:
  368.  
  369.      function wakeup (message)
  370.        printf ("\a%s\n", message);
  371.      endfunction
  372.  
  373.    Calling this function using a statement like this
  374.  
  375.      wakeup ("Rise and shine!");
  376.  
  377. will cause Octave to ring your terminal's bell and print the message
  378. `Rise and shine!', followed by a newline character (the `\n' in the
  379. first argument to the `printf' statement).
  380.  
  381.    In most cases, you will also want to get some information back from
  382. the functions you define.  Here is the syntax for writing a function
  383. that returns a single value:
  384.  
  385.      function RET-VAR = NAME (ARG-LIST)
  386.        BODY
  387.      endfunction
  388.  
  389. The symbol RET-VAR is the name of the variable that will hold the value
  390. to be returned by the function.  This variable must be defined before
  391. the end of the function body in order for the function to return a
  392. value.
  393.  
  394.    For example, here is a function that computes the average of the
  395. elements of a vector:
  396.  
  397.      function retval = avg (v)
  398.        retval = sum (v) / length (v);
  399.      endfunction
  400.  
  401.    If we had written `avg' like this instead,
  402.  
  403.      function retval = avg (v)
  404.        if (is_vector (v))
  405.          retval = sum (v) / length (v);
  406.        endif
  407.      endfunction
  408.  
  409. and then called the function with a matrix instead of a vector as the
  410. argument, Octave would have printed an error message like this:
  411.  
  412.      error: `retval' undefined near line 1 column 10
  413.      error: evaluating index expression near line 7, column 1
  414.  
  415. because the body of the `if' statement was never executed, and `retval'
  416. was never defined.  To prevent obscure errors like this, it is a good
  417. idea to always make sure that the return variables will always have
  418. values, and to produce meaningful error messages when problems are
  419. encountered.  For example, `avg' could have been written like this:
  420.  
  421.      function retval = avg (v)
  422.        retval = 0;
  423.        if (is_vector (v))
  424.          retval = sum (v) / length (v);
  425.        else
  426.          error ("avg: expecting vector argument");
  427.        endif
  428.      endfunction
  429.  
  430.    There is still one additional problem with this function.  What if
  431. it is called without an argument?  Without additional error checking,
  432. Octave will probably print an error message that won't really help you
  433. track down the source of the error.  To allow you to catch errors like
  434. this, Octave provides each function with an automatic variable called
  435. `nargin'.  Each time a function is called, `nargin' is automatically
  436. initialized to the number of arguments that have actually been passed
  437. to the function.  For example, we might rewrite the `avg' function like
  438. this:
  439.  
  440.      function retval = avg (v)
  441.        retval = 0;
  442.        if (nargin != 1)
  443.          error ("usage: avg (vector)");
  444.        endif
  445.        if (is_vector (v))
  446.          retval = sum (v) / length (v);
  447.        else
  448.          error ("avg: expecting vector argument");
  449.        endif
  450.      endfunction
  451.  
  452.    Although Octave does not consider it an error if you call a function
  453. with more arguments than were expected, doing so is probably an error,
  454. so we check for that possibility too, and issue the error message if
  455. either too few or too many arguments have been provided.
  456.  
  457.    The body of a user-defined function can contain a `return'
  458. statement.  This statement returns control to the rest of the Octave
  459. program.  A `return' statement is assumed at the end of every function
  460. definition.
  461.  
  462. 
  463. File: octave.info,  Node: Multiple Return Values,  Next: Variable-length Argument Lists,  Prev: Defining Functions,  Up: Functions and Scripts
  464.  
  465. Multiple Return Values
  466. ======================
  467.  
  468.    Unlike many other computer languages, Octave allows you to define
  469. functions that return more than one value.  The syntax for defining
  470. functions that return multiple values is
  471.  
  472.      function [RET-LIST] = NAME (ARG-LIST)
  473.        BODY
  474.      endfunction
  475.  
  476. where NAME, ARG-LIST, and BODY have the same meaning as before, and
  477. RET-LIST is a comma-separated list of variable names that will hold the
  478. values returned from the function.  The list of return values must have
  479. at least one element.  If RET-LIST has only one element, this form of
  480. the `function' statement is equivalent to the form described in the
  481. previous section.
  482.  
  483.    Here is an example of a function that returns two values, the maximum
  484. element of a vector and the index of its first occurrence in the vector.
  485.  
  486.      function [max, idx] = vmax (v)
  487.        idx = 1;
  488.        max = v (idx);
  489.        for i = 2:length (v)
  490.          if (v (i) > max)
  491.            max = v (i);
  492.            idx = i;
  493.          endif
  494.        endfor
  495.      endfunction
  496.  
  497.    In this particular case, the two values could have been returned as
  498. elements of a single array, but that is not always possible or
  499. convenient.  The values to be returned may not have compatible
  500. dimensions, and it is often desirable to give the individual return
  501. values distinct names.
  502.  
  503.    In addition to setting `nargin' each time a function is called,
  504. Octave also automatically initializes `nargout' to the number of values
  505. that are expected to be returned.  This allows you to write functions
  506. that behave differently depending on the number of values that the user
  507. of the function has requested.  The implicit assignment to the built-in
  508. variable `ans' does not figure in the count of output arguments, so the
  509. value of `nargout' may be zero.
  510.  
  511.    The `svd' and `lu' functions are examples of built-in functions that
  512. behave differently depending on the value of `nargout'.
  513.  
  514.    It is possible to write functions that only set some return values.
  515. For example, calling the function
  516.  
  517.      function [x, y, z] = f ()
  518.        x = 1;
  519.        z = 2;
  520.      endfunction
  521.  
  522. as
  523.  
  524.      [a, b, c] = f ()
  525.  
  526. produces:
  527.  
  528.      a = 1
  529.      
  530.      b = [](0x0)
  531.      
  532.      c = 2
  533.  
  534. 
  535. File: octave.info,  Node: Variable-length Argument Lists,  Next: Variable-length Return Lists,  Prev: Multiple Return Values,  Up: Functions and Scripts
  536.  
  537. Variable-length Argument Lists
  538. ==============================
  539.  
  540.    Octave has a real mechanism for handling functions that take an
  541. unspecified number of arguments, so it is not necessary to place an
  542. upper bound on the number of optional arguments that a function can
  543. accept.
  544.  
  545.    Here is an example of a function that uses the new syntax to print a
  546. header followed by an unspecified number of values:
  547.  
  548.      function foo (heading, ...)
  549.        disp (heading);
  550.        va_start ();
  551.        while (--nargin)
  552.          disp (va_arg ());
  553.        endwhile
  554.      endfunction
  555.  
  556.    The ellipsis that marks the variable argument list may only appear
  557. once and must be the last element in the list of arguments.
  558.  
  559.    Calling `va_start()' positions an internal pointer to the first
  560. unnamed argument and allows you to cycle through the arguments more than
  561. once.  It is not necessary to call `va_start()' if you do not plan to
  562. cycle through the arguments more than once.
  563.  
  564.    The function `va_arg()' returns the value of the next available
  565. argument and moves the internal pointer to the next argument.  It is an
  566. error to call `va_arg()' when there are no more arguments available.
  567.  
  568.    Sometimes it is useful to be able to pass all unnamed arguments to
  569. another function.  The keyword ALL_VA_ARGS makes this very easy to do.
  570. For example, given the functions
  571.  
  572.      function f (...)
  573.        while (nargin--)
  574.          disp (va_arg ())
  575.        endwhile
  576.      endfunction
  577.      function g (...)
  578.        f ("begin", all_va_args, "end")
  579.      endfunction
  580.  
  581. the statement
  582.  
  583.      g (1, 2, 3)
  584.  
  585. prints
  586.  
  587.      begin
  588.      1
  589.      2
  590.      3
  591.      end
  592.  
  593.    The keyword `all_va_args' always stands for the entire list of
  594. optional argument, so it is possible to use it more than once within the
  595. same function without having to call `var_start ()'.  It can only be
  596. used within functions that take a variable number of arguments.  It is
  597. an error to use it in other contexts.
  598.  
  599. 
  600. File: octave.info,  Node: Variable-length Return Lists,  Next: Returning From a Function,  Prev: Variable-length Argument Lists,  Up: Functions and Scripts
  601.  
  602. Variable-length Return Lists
  603. ============================
  604.  
  605.    Octave also has a real mechanism for handling functions that return
  606. an unspecified number of values, so it is no longer necessary to place
  607. an upper bound on the number of outputs that a function can produce.
  608.  
  609.    Here is an example of a function that uses the new syntax to produce
  610. N values:
  611.  
  612.      function [...] = foo (n, x)
  613.        for i = 1:n
  614.          vr_val (i * x);
  615.        endfor
  616.      endfunction
  617.  
  618.    Each time `vr_val()' is called, it places the value of its argument
  619. at the end of the list of values to return from the function.  Once
  620. `vr_val()' has been called, there is no way to go back to the beginning
  621. of the list and rewrite any of the return values.
  622.  
  623.    As with variable argument lists, the ellipsis that marks the variable
  624. return list may only appear once and must be the last element in the
  625. list of returned values.
  626.  
  627. 
  628. File: octave.info,  Node: Returning From a Function,  Next: Function Files,  Prev: Variable-length Return Lists,  Up: Functions and Scripts
  629.  
  630. Returning From a Function
  631. =========================
  632.  
  633.    The body of a user-defined function can contain a `return' statement.
  634. This statement returns control to the rest of the Octave program.  It
  635. looks like this:
  636.  
  637.      return
  638.  
  639.    Unlike the `return' statement in C, Octave's `return' statement
  640. cannot be used to return a value from a function.  Instead, you must
  641. assign values to the list of return variables that are part of the
  642. `function' statement.  The `return' statement simply makes it easier to
  643. exit a function from a deeply nested loop or conditional statement.
  644.  
  645.    Here is an example of a function that checks to see if any elements
  646. of a vector are nonzero.
  647.  
  648.      function retval = any_nonzero (v)
  649.        retval = 0;
  650.        for i = 1:length (v)
  651.          if (v (i) != 0)
  652.            retval = 1;
  653.            return;
  654.          endif
  655.        endfor
  656.        printf ("no nonzero elements found\n");
  657.      endfunction
  658.  
  659.    Note that this function could not have been written using the
  660. `break' statement to exit the loop once a nonzero value is found
  661. without adding extra logic to avoid printing the message if the vector
  662. does contain a nonzero element.
  663.  
  664. 
  665. File: octave.info,  Node: Function Files,  Next: Script Files,  Prev: Returning From a Function,  Up: Functions and Scripts
  666.  
  667. Function Files
  668. ==============
  669.  
  670.    Except for simple one-shot programs, it is not practical to have to
  671. define all the functions you need each time you need them.  Instead, you
  672. will normally want to save them in a file so that you can easily edit
  673. them, and save them for use at a later time.
  674.  
  675.    Octave does not require you to load function definitions from files
  676. before using them.  You simply need to put the function definitions in a
  677. place where Octave can find them.
  678.  
  679.    When Octave encounters an identifier that is undefined, it first
  680. looks for variables or functions that are already compiled and currently
  681. listed in its symbol table.  If it fails to find a definition there, it
  682. searches the list of directories specified by the built-in variable
  683. `LOADPATH' for files ending in `.m' that have the same base name as the
  684. undefined identifier.(1)  *Note User Preferences:: for a description of
  685. `LOADPATH'.  Once Octave finds a file with a name that matches, the
  686. contents of the file are read.  If it defines a *single* function, it
  687. is compiled and executed.  *Note Script Files::, for more information
  688. about how you can define more than one function in a single file.
  689.  
  690.    When Octave defines a function from a function file, it saves the
  691. full name of the file it read and the time stamp on the file.  After
  692. that, it checks the time stamp on the file every time it needs the
  693. function.  If the time stamp indicates that the file has changed since
  694. the last time it was read, Octave reads it again.
  695.  
  696.    Checking the time stamp allows you to edit the definition of a
  697. function while Octave is running, and automatically use the new function
  698. definition without having to restart your Octave session.  Checking the
  699. time stamp every time a function is used is rather inefficient, but it
  700. has to be done to ensure that the correct function definition is used.
  701.  
  702.    Octave assumes that function files in the
  703. `/usr/local/lib/octave/1.1.1' directory tree will not change, so it
  704. doesn't have to check their time stamps every time the functions
  705. defined in those files are used.  This is normally a very good
  706. assumption and provides a significant improvement in performance for the
  707. function files that are distributed with Octave.
  708.  
  709.    If you know that your own function files will not change while you
  710. are running Octave, you can improve performance by setting the variable
  711. `ignore_function_time_stamp' to `"all"', so that Octave will ignore the
  712. time stamps for all function files.  Setting it to `"system"' gives the
  713. default behavior.  If you set it to anything else, Octave will check
  714. the time stamps on all function files.
  715.  
  716.    ---------- Footnotes ----------
  717.  
  718.    (1)  The `.m' suffix was chosen for compatibility with MATLAB.
  719.  
  720. 
  721. File: octave.info,  Node: Script Files,  Next: Dynamically Linked Functions,  Prev: Function Files,  Up: Functions and Scripts
  722.  
  723. Script Files
  724. ============
  725.  
  726.    A script file is a file containing (almost) any sequence of Octave
  727. commands.  It is read and evaluated just as if you had typed each
  728. command at the Octave prompt, and provides a convenient way to perform a
  729. sequence of commands that do not logically belong inside a function.
  730.  
  731.    Unlike a function file, a script file must *not* begin with the
  732. keyword `function'.  If it does, Octave will assume that it is a
  733. function file, and that it defines a single function that should be
  734. evaluated as soon as it is defined.
  735.  
  736.    A script file also differs from a function file in that the variables
  737. named in a script file are not local variables, but are in the same
  738. scope as the other variables that are visible on the command line.
  739.  
  740.    Even though a script file may not begin with the `function' keyword,
  741. it is possible to define more than one function in a single script file
  742. and load (but not execute) all of them at once.  To do this, the first
  743. token in the file (ignoring comments and other white space) must be
  744. something other than `function'.  If you have no other statements to
  745. evaluate, you can use a statement that has no effect, like this:
  746.  
  747.      # Prevent Octave from thinking that this
  748.      # is a function file:
  749.      
  750.      1;
  751.      
  752.      # Define function one:
  753.      
  754.      function one ()
  755.        ...
  756.  
  757.    To have Octave read and compile these functions into an internal
  758. form, you need to make sure that the file is in Octave's `LOADPATH',
  759. then simply type the base name of the file that contains the commands.
  760. (Octave uses the same rules to search for script files as it does to
  761. search for function files.)
  762.  
  763.    If the first token in a file (ignoring comments) is `function',
  764. Octave will compile the function and try to execute it, printing a
  765. message warning about any non-whitespace characters that appear after
  766. the function definition.
  767.  
  768.    Note that Octave does not try to lookup the definition of any
  769. identifier until it needs to evaluate it.  This means that Octave will
  770. compile the following statements if they appear in a script file, or
  771. are typed at the command line,
  772.  
  773.      # not a function file:
  774.      1;
  775.      function foo ()
  776.        do_something ();
  777.      endfunction
  778.      function do_something ()
  779.        do_something_else ();
  780.      endfunction
  781.  
  782. even though the function `do_something' is not defined before it is
  783. referenced in the function `foo'.  This is not an error because the
  784. Octave does not need to resolve all symbols that are referenced by a
  785. function until the function is actually evaluated.
  786.  
  787.    Since Octave doesn't look for definitions until they are needed, the
  788. following code will always print `bar = 3' whether it is typed directly
  789. on the command line, read from a script file, or is part of a function
  790. body, even if there is a function or script file called `bar.m' in
  791. Octave's `LOADPATH'.
  792.  
  793.      eval ("bar = 3");
  794.      bar
  795.  
  796.    Code like this appearing within a function body could fool Octave if
  797. definitions were resolved as the function was being compiled.  It would
  798. be virtually impossible to make Octave clever enough to evaluate this
  799. code in a consistent fashion.  The parser would have to be able to
  800. perform the `eval ()' statement at compile time, and that would be
  801. impossible unless all the references in the string to be evaluated could
  802. also be resolved, and requiring that would be too restrictive (the
  803. string might come from user input, or depend on things that are not
  804. known until the function is evaluated).
  805.  
  806. 
  807. File: octave.info,  Node: Dynamically Linked Functions,  Next: Organization of Functions,  Prev: Script Files,  Up: Functions and Scripts
  808.  
  809. Dynamically Linked Functions
  810. ============================
  811.  
  812.    On some systems, Octave can dynamically load and execute functions
  813. written in C++ or other compiled languages.  This currently only works
  814. on systems that have a working version of the GNU dynamic linker,
  815. `dld'. Unfortunately, `dld' does not work on very many systems, but
  816. someone is working on making `dld' use the GNU Binary File Descriptor
  817. library, `BFD', so that may soon change.  In any case, it should not be
  818. too hard to make Octave's dynamic linking features work on other
  819. systems using system-specific dynamic linking facilities.
  820.  
  821.    Here is an example of how to write a C++ function that Octave can
  822. load.
  823.  
  824.      #include <iostream.h>
  825.      
  826.      #include "defun-dld.h"
  827.      #include "tree-const.h"
  828.      
  829.      DEFUN_DLD ("hello", Fhello, Shello, -1, -1,
  830.        "hello (...)\n\
  831.      \n\
  832.      Print greeting followed by the values of all the arguments passed.\n\
  833.      Returns all the arguments passed.")
  834.      {
  835.        Octave_object retval;
  836.        cerr << "Hello, world!\n";
  837.        int nargin = args.length ();
  838.        for (int i = 1; i < nargin; i++)
  839.          retval (nargin-i-1) = args(i).eval (1);
  840.        return retval;
  841.      }
  842.  
  843.    Octave's dynamic linking features currently have the following
  844. limitations.
  845.  
  846.    * Dynamic linking only works on systems that support the GNU dynamic
  847.      linker, `dld'.
  848.  
  849.    * Clearing dynamically linked functions doesn't work.
  850.  
  851.    * Configuring Octave with `--enable-lite-kernel' seems to mostly work
  852.      to make nonessential built-in functions dynamically loaded, but
  853.      there also seem to be some problems.  For example, fsolve seems to
  854.      always return `info == 3'.  This is difficult to debug since `gdb'
  855.      won't seem to allow breakpoints to be set inside dynamically loaded
  856.      functions.
  857.  
  858.    * Octave uses a lot of memory if the dynamically linked functions are
  859.      compiled to include debugging symbols.  This appears to be a
  860.      limitation with `dld', and can be avoided by not using `-g' to
  861.      compile functions that will be linked dynamically.
  862.  
  863.    If you would like to volunteer to help improve Octave's ability to
  864. dynamically link externally compiled functions, please contact
  865. `bug-octave@che.utexas.edu'.
  866.  
  867. 
  868. File: octave.info,  Node: Organization of Functions,  Prev: Dynamically Linked Functions,  Up: Functions and Scripts
  869.  
  870. Organization of Functions Distributed with Octave
  871. =================================================
  872.  
  873.    Many of Octave's standard functions are distributed as function
  874. files.  They are loosely organized by topic, in subdirectories of
  875. `OCTAVE_HOME/lib/octave/VERSION/m', to make it easier to find them.
  876.  
  877.    The following is a list of all the function file subdirectories, and
  878. the types of functions you will find there.
  879.  
  880. `control'
  881.      Functions for design and simulation of automatic control systems.
  882.  
  883. `elfun'
  884.      Elementary functions.
  885.  
  886. `general'
  887.      Miscellaneous matrix manipulations, like `flipud', `rot90', and
  888.      `triu', as well as other basic functions, like `is_matrix',
  889.      `nargchk', etc.
  890.  
  891. `image'
  892.      Image processing tools.  These functions require the X Window
  893.      System.
  894.  
  895. `linear-algebra'
  896.      Functions for linear algebra.
  897.  
  898. `miscellaneous'
  899.      Functions that don't really belong anywhere else.
  900.  
  901. `plot'
  902.      A set of functions that implement the MATLAB-like plotting
  903.      functions.
  904.  
  905. `polynomial'
  906.      Functions for manipulating polynomials.
  907.  
  908. `set'
  909.      Functions for creating and manipulating sets of unique values.
  910.  
  911. `signal'
  912.      Functions for signal processing applications.
  913.  
  914. `specfun'
  915.      Special functions.
  916.  
  917. `special-matrix'
  918.      Functions that create special matrix forms.
  919.  
  920. `startup'
  921.      Octave's system-wide startup file.
  922.  
  923. `statistics'
  924.      Statistical functions.
  925.  
  926. `strings'
  927.      Miscellaneous string-handling functions.
  928.  
  929.    *Note User Preferences:: for an explanation of the built-in variable
  930. `LOADPATH', and *Note Function Files:: for a description of the way
  931. Octave resolves undefined variable and function names.
  932.  
  933. 
  934. File: octave.info,  Node: Built-in Variables,  Next: Arithmetic,  Prev: Functions and Scripts,  Up: Top
  935.  
  936. Built-in Variables
  937. ******************
  938.  
  939.    Most Octave variables are available for you to use for your own
  940. purposes; they never change except when your program assigns values to
  941. them, and never affect anything except when your program examines them.
  942.  
  943.    A few variables have special built-in meanings.  Some of them, like
  944. `pi' and `eps' provide useful predefined constant values.  Others, like
  945. `do_fortran_indexing' and `page_screen_output' are examined
  946. automatically by Octave, so that you can to tell Octave how to do
  947. certain things.  There are also two special variables, `ans' and `PWD',
  948. that are set automatically by Octave and carry information from the
  949. internal workings of Octave to your program.
  950.  
  951.    This chapter documents all the built-in variables of Octave.  Most
  952. of them are also documented in the chapters that describe functions
  953. that use them, or are affected by their values.
  954.  
  955. * Menu:
  956.  
  957. * Predefined Constants::
  958. * User Preferences::
  959. * Other Built-in Variables::
  960. * Summary of Preference Variables::
  961.  
  962. 
  963. File: octave.info,  Node: Predefined Constants,  Next: User Preferences,  Prev: Built-in Variables,  Up: Built-in Variables
  964.  
  965. Predefined Constants
  966. ====================
  967.  
  968. `I, i, J, j'
  969.      A pure imaginary number, defined as   `sqrt (-1)'.  The `I' and
  970.      `J' forms are true constants, and cannot be modified.  The `i' and
  971.      `j' forms are like ordinary variables, and may be used for other
  972.      purposes.  However, unlike other variables, they once again assume
  973.      their special predefined values if they are cleared *Note
  974.      Miscellaneous Utilities::.
  975.  
  976. `Inf, inf'
  977.      Infinity.  This is the result of an operation like 1/0, or an
  978.      operation that results in a floating point overflow.
  979.  
  980. `NaN, nan'
  981.      Not a number.  This is the result of an operation like `0/0', or
  982.      `Inf - Inf', or any operation with a NaN.
  983.  
  984. `SEEK_SET'
  985. `SEEK_CUR'
  986. `SEEK_END'
  987.      These variables may be used as the optional third argument for the
  988.      function `fseek'.
  989.  
  990. `eps'
  991.      The machine precision.  More precisely, `eps' is the smallest value
  992.      such that `1+eps' is not equal to 1.  This number is
  993.      system-dependent.  On machines that support 64 bit IEEE floating
  994.      point arithmetic, `eps' is approximately  2.2204e-16.
  995.  
  996. `pi'
  997.      The ratio of the circumference of a circle to its diameter.
  998.      Internally, `pi' is computed as `4.0 * atan (1.0)'.
  999.  
  1000. `realmax'
  1001.      The largest floating point number that is representable.  The
  1002.      actual value is system-dependent.  On machines that support 64 bit
  1003.      IEEE floating point arithmetic, `realmax' is approximately
  1004.      1.7977e+308
  1005.  
  1006. `realmin'
  1007.      The smallest floating point number that is representable.  The
  1008.      actual value is system-dependent.  On machines that support 64 bit
  1009.      IEEE floating point arithmetic, `realmin' is approximately
  1010.      2.2251e-308
  1011.  
  1012. `stdin'
  1013. `stdout'
  1014. `stderr'
  1015.      These variables are the file numbers corresponding to the standard
  1016.      input, standard output, and standard error streams.  These streams
  1017.      are preconnected and available when Octave starts.
  1018.  
  1019.