home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / info / octave.info-2 < prev    next >
Encoding:
GNU Info File  |  1996-10-12  |  48.6 KB  |  1,511 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: Startup Files,  Prev: Command Line Options,  Up: Invoking Octave
  21.  
  22. Startup Files
  23. =============
  24.  
  25.    When Octave starts, it looks for commands to execute from the
  26. following files:
  27.  
  28. `OCTAVE_HOME/lib/octave/VERSION/startup/octaverc'
  29.      Where `OCTAVE_HOME' is the directory in which all of Octave is
  30.      installed (the default is `/usr/local'), and `VERSION' is the
  31.      version number of Octave.  This file is provided so that changes
  32.      to the default Octave environment can be made globally for all
  33.      users.  Some care should be taken when making changes to this
  34.      file, since all users of Octave at your site will be affected.
  35.  
  36. `~/.octaverc'
  37.      This file is normally used to make personal changes to the default
  38.      Octave environment.
  39.  
  40. `.octaverc'
  41.      This file can be used to make changes to the default Octave
  42.      environment for a particular project.  Octave searches for this
  43.      file after it reads `~/.octaverc', so any use of the `cd' command
  44.      in the `~/.octaverc' file will affect the directory that Octave
  45.      searches for the file `.octaverc'.
  46.  
  47.      If you start Octave in your home directory, it will avoid executing
  48.      commands from `~/.octaverc' twice.
  49.  
  50.    A message will be displayed as each of these files is read if you
  51. invoke Octave with the `--verbose' option but without the `--silent'
  52. option.
  53.  
  54.    Startup files may contain any valid Octave commands, including
  55. multiple function definitions.
  56.  
  57. 
  58. File: octave.info,  Node: Expressions,  Next: Statements,  Prev: Invoking Octave,  Up: Top
  59.  
  60. Expressions
  61. ***********
  62.  
  63.    Expressions are the basic building block of statements in Octave.  An
  64. expression evaluates to a value, which you can print, test, store in a
  65. variable, pass to a function, or assign a new value to a variable with
  66. an assignment operator.
  67.  
  68.    An expression can serve as a statement on its own.  Most other kinds
  69. of statements contain one or more expressions which specify data to be
  70. operated on.  As in other languages, expressions in Octave include
  71. variables, array references, constants, and function calls, as well as
  72. combinations of these with various operators.
  73.  
  74. * Menu:
  75.  
  76. * Constant Expressions::
  77. * Matrices::
  78. * Ranges::
  79. * Variables::
  80. * Index Expressions::
  81. * Data Structures::
  82. * Calling Functions::
  83. * Global Variables::
  84. * Keywords::
  85. * Arithmetic Ops::
  86. * Comparison Ops::
  87. * Boolean Expressions::
  88. * Assignment Ops::
  89. * Increment Ops::
  90. * Operator Precedence::
  91.  
  92. 
  93. File: octave.info,  Node: Constant Expressions,  Next: Matrices,  Prev: Expressions,  Up: Expressions
  94.  
  95. Constant Expressions
  96. ====================
  97.  
  98.    The simplest type of expression is the "constant", which always has
  99. the same value.  There are two types of constants: numeric constants and
  100. string constants.
  101.  
  102. * Menu:
  103.  
  104. * Numeric Constants::
  105. * String Constants::
  106.  
  107. 
  108. File: octave.info,  Node: Numeric Constants,  Next: String Constants,  Prev: Constant Expressions,  Up: Constant Expressions
  109.  
  110. Numeric Constants
  111. -----------------
  112.  
  113.    A "numeric constant" may be a scalar, a vector, or a matrix, and it
  114. may contain complex values.
  115.  
  116.    The simplest form of a numeric constant, a scalar, is a single number
  117. that can be an integer, a decimal fraction, a number in scientific
  118. (exponential) notation, or a complex number.  Note that all numeric
  119. values are represented within Octave in double-precision floating point
  120. format (complex constants are stored as pairs of double-precision
  121. floating point values).  Here are some examples of real-valued numeric
  122. constants, which all have the same value:
  123.  
  124.      105
  125.      1.05e+2
  126.      1050e-1
  127.  
  128.    To specify complex constants, you can write an expression of the form
  129.  
  130.      3 + 4i
  131.      3.0 + 4.0i
  132.      0.3e1 + 40e-1i
  133.  
  134.    all of which are equivalent.  The letter `i' in the previous example
  135. stands for the pure imaginary constant, defined as   `sqrt (-1)'.
  136.  
  137.    For Octave to recognize a value as the imaginary part of a complex
  138. constant, a space must not appear between the number and the `i'.  If
  139. it does, Octave will print an error message, like this:
  140.  
  141.      octave:13> 3 + 4 i
  142.      
  143.      parse error:
  144.      
  145.        3 + 4 i
  146.              ^
  147.  
  148.    You may also use `j', `I', or `J' in place of the `i' above.  All
  149. four forms are equivalent.
  150.  
  151. 
  152. File: octave.info,  Node: String Constants,  Prev: Numeric Constants,  Up: Constant Expressions
  153.  
  154. String Constants
  155. ----------------
  156.  
  157.    A "string constant" consists of a sequence of characters enclosed in
  158. either double-quote or single-quote marks.  For example, both of the
  159. following expressions
  160.  
  161.      "parrot"
  162.      'parrot'
  163.  
  164. represent the string whose contents are `parrot'.  Strings in Octave
  165. can be of any length.
  166.  
  167.    Since the single-quote mark is also used for the transpose operator
  168. (*note Arithmetic Ops::.) but double-quote marks have no other purpose
  169. in Octave, it is best to use double-quote marks to denote strings.
  170.  
  171.    Some characters cannot be included literally in a string constant.
  172. You represent them instead with "escape sequences", which are character
  173. sequences beginning with a backslash (`\').
  174.  
  175.    One use of an escape sequence is to include a double-quote
  176. (single-quote) character in a string constant that has been defined
  177. using double-quote (single-quote) marks.  Since a plain double-quote
  178. would end the string, you must use `\"' to represent a single
  179. double-quote character as a part of the string.  The backslash character
  180. itself is another character that cannot be included normally.  You must
  181. write `\\' to put one backslash in the string.  Thus, the string whose
  182. contents are the two characters `"\' must be written `"\"\\"'.
  183.  
  184.    Another use of backslash is to represent unprintable characters such
  185. as newline.  While there is nothing to stop you from writing most of
  186. these characters directly in a string constant, they may look ugly.
  187.  
  188.    Here is a table of all the escape sequences used in Octave.  They are
  189. the same as those used in the C programming langauge.
  190.  
  191. `\\'
  192.      Represents a literal backslash, `\'.
  193.  
  194. `\"'
  195.      Represents a literal double-quote character, `"'.
  196.  
  197. `\''
  198.      Represents a literal single-quote character, `''.
  199.  
  200. `\a'
  201.      Represents the "alert" character, control-g, ASCII code 7.
  202.  
  203. `\b'
  204.      Represents a backspace, control-h, ASCII code 8.
  205.  
  206. `\f'
  207.      Represents a formfeed, control-l, ASCII code 12.
  208.  
  209. `\n'
  210.      Represents a newline, control-j, ASCII code 10.
  211.  
  212. `\r'
  213.      Represents a carriage return, control-m, ASCII code 13.
  214.  
  215. `\t'
  216.      Represents a horizontal tab, control-i, ASCII code 9.
  217.  
  218. `\v'
  219.      Represents a vertical tab, control-k, ASCII code 11.
  220.  
  221.    Strings may be concatenated using the notation for defining matrices.
  222. For example, the expression
  223.  
  224.      [ "foo" , "bar" , "baz" ]
  225.  
  226. produces the string whose contents are `foobarbaz'.  The next section
  227. explains more about how to create matrices.
  228.  
  229. 
  230. File: octave.info,  Node: Matrices,  Next: Ranges,  Prev: Constant Expressions,  Up: Expressions
  231.  
  232. Matrices
  233. ========
  234.  
  235.    It is easy to define a matrix of values in Octave.  The size of the
  236. matrix is determined automatically, so it is not necessary to explicitly
  237. state the dimensions.  The expression
  238.  
  239.      a = [1, 2; 3, 4]
  240.  
  241. results in the matrix
  242.  
  243.      a =
  244.      
  245.        1  2
  246.        3  4
  247.  
  248.    The commas which separate the elements on a row may be omitted, and
  249. the semicolon that marks the beginning of a new row may be replaced by
  250. one or more new lines.  The expression
  251.  
  252.      a = [ 1 2
  253.            3 4 ]
  254.  
  255. is equivalent to the one above.
  256.  
  257.    Elements of a matrix may be arbitrary expressions, provided that the
  258. dimensions all agree.  For example, given the above matrix, the
  259. expression
  260.  
  261.      [ a, a ]
  262.  
  263. produces the matrix
  264.  
  265.      ans =
  266.      
  267.        1  2  1  2
  268.        3  4  3  4
  269.  
  270. but the expression
  271.  
  272.      [ a 1 ]
  273.  
  274. produces the error
  275.  
  276.      error: number of rows must match
  277.  
  278.    Inside the square brackets that delimit a matrix expression, Octave
  279. looks at the surrounding context to determine whether spaces should be
  280. converted into element separators, or simply ignored, so commands like
  281.  
  282.      [ linspace (1, 2) ]
  283.  
  284. will work.  However, some possible sources of confusion remain.  For
  285. example, in the expression
  286.  
  287.      [ 1 - 1 ]
  288.  
  289. the `-' is treated as a binary operator and the result is the scalar 0,
  290. but in the expression
  291.  
  292.      [ 1 -1 ]
  293.  
  294. the `-' is treated as a unary operator and the result is the vector `[
  295. 1 -1 ]'.
  296.  
  297.    Given `a = 1', the expression
  298.  
  299.      [ 1 a' ]
  300.  
  301. results in the single quote character `'' being treated as a transpose
  302. operator and the result is the vector `[ 1 1 ]', but the expression
  303.  
  304.      [ 1 a ' ]
  305.  
  306. produces the error message
  307.  
  308.      error: unterminated string constant
  309.  
  310. because to not do so would make it impossible to correctly parse the
  311. valid expression
  312.  
  313.      [ a 'foo' ]
  314.  
  315.    For clarity, it is probably best to always use commas and semicolons
  316. to separate matrix elements and rows.  It is possible to enforce this
  317. style by setting the built-in variable `whitespace_in_literal_matrix' to
  318. `"ignore"'.  *Note Built-in Variables::.
  319.  
  320. * Menu:
  321.  
  322. * Empty Matrices::
  323.  
  324. 
  325. File: octave.info,  Node: Empty Matrices,  Prev: Matrices,  Up: Matrices
  326.  
  327. Empty Matrices
  328. --------------
  329.  
  330.    A matrix may have one or both dimensions zero, and operations on
  331. empty matrices are handled as described by Carl de Boor in `An Empty
  332. Exercise', SIGNUM, Volume 25, pages 2-6, 1990 and C. N. Nett and W. M.
  333. Haddad, in `A System-Theoretic Appropriate Realization of the Empty
  334. Matrix Concept', IEEE Transactions on Automatic Control, Volume 38,
  335. Number 5, May 1993.  Briefly, given a scalar `s', and an M by N matrix
  336. `M(mxn)', and an M by N empty matrix `[](mxn)' (with either one or both
  337. dimensions equal to zero), the following are true:
  338.  
  339.      s * [](mxn) = [](mxn) * s = [](mxn)
  340.      
  341.          [](mxn) + [](mxn) = [](mxn)
  342.      
  343.          [](0xm) * M(mxn) = [](0xn)
  344.      
  345.          M(mxn) * [](nx0) = [](mx0)
  346.      
  347.          [](mx0) * [](0xn) = 0(mxn)
  348.  
  349.    By default, dimensions of the empty matrix are now printed along
  350. with the empty matrix symbol, `[]'.  For example:
  351.  
  352.      octave:13> zeros (3, 0)
  353.      ans =
  354.      
  355.      [](3x0)
  356.  
  357.    The built-in variable `print_empty_dimensions' controls this
  358. behavior (*note User Preferences::.).
  359.  
  360.    Empty matrices may also be used in assignment statements as a
  361. convenient way to delete rows or columns of matrices.  *Note Assignment
  362. Expressions: Assignment Ops.
  363.  
  364. 
  365. File: octave.info,  Node: Ranges,  Next: Variables,  Prev: Matrices,  Up: Expressions
  366.  
  367. Ranges
  368. ======
  369.  
  370.    A "range" is a convenient way to write a row vector with evenly
  371. spaced elements.  A range constant is defined by the value of the first
  372. element in the range, an optional value for the increment between
  373. elements, and a maximum value which the elements of the range will not
  374. exceed.  The base, increment, and limit are separated by colons (the
  375. `:' character) and may contain any arithmetic expressions and function
  376. calls.  If the increment is omitted, it is assumed to be 1.  For
  377. example, the range
  378.  
  379.      1 : 5
  380.  
  381. defines the set of values `[ 1 2 3 4 5 ]' (the increment has been
  382. omitted, so it is taken as 1), and the range
  383.  
  384.      1 : 3 : 5
  385.  
  386. defines the set of values `[ 1 4 ]'.  In this case, the base value is
  387. 1, the increment is 3, and the limit is 5.
  388.  
  389.    Although a range constant specifies a row vector, Octave does *not*
  390. convert range constants to vectors unless it is necessary to do so.
  391. This allows you to write a constant like `1 : 10000' without using up
  392. 80,000 bytes of storage on a typical 32-bit workstation.
  393.  
  394.    Note that the upper (or lower, if the increment is negative) bound on
  395. the range is not always included in the set of values, and that ranges
  396. defined by floating point values can produce surprising results because
  397. Octave uses floating point arithmetic to compute the values in the
  398. range.  If it is important to include the endpoints of a range and the
  399. number of elements is known, you should use the `linspace' function
  400. instead (*note Special Matrices::.).
  401.  
  402. 
  403. File: octave.info,  Node: Variables,  Next: Index Expressions,  Prev: Ranges,  Up: Expressions
  404.  
  405. Variables
  406. =========
  407.  
  408.    Variables let you give names to values and refer to them later.  You
  409. have already seen variables in many of the examples.  The name of a
  410. variable must be a sequence of letters, digits and underscores, but it
  411. may not begin with a digit.  Octave does not enforce a limit on the
  412. length of variable names, but it is seldom useful to have variables
  413. with names longer than about 30 characters.  The following are all
  414. valid variable names
  415.  
  416.      x
  417.      x15
  418.      __foo_bar_baz__
  419.      fucnrdthsucngtagdjb
  420.  
  421. Case is significant in variable names.  The symbols `a' and `A' are
  422. distinct variables.
  423.  
  424.    A variable name is a valid expression by itself.  It represents the
  425. variable's current value.  Variables are given new values with
  426. "assignment operators" and "increment operators".  *Note Assignment
  427. Expressions: Assignment Ops.
  428.  
  429.    A number of variables have special built-in meanings.  For example,
  430. `PWD' holds the current working directory, and `pi' names the ratio of
  431. the circumference of a circle to its diameter. *Note Built-in
  432. Variables::, for a list of all the predefined variables.  Some of these
  433. built-in symbols are constants and may not be changed.  Others can be
  434. used and assigned just like all other variables, but their values are
  435. also used or changed automatically by Octave.
  436.  
  437.    Variables in Octave can be assigned either numeric or string values.
  438. Variables may not be used before they have been given a value.  Doing so
  439. results in an error.
  440.  
  441. 
  442. File: octave.info,  Node: Index Expressions,  Next: Data Structures,  Prev: Variables,  Up: Expressions
  443.  
  444. Index Expressions
  445. =================
  446.  
  447.    An "index expression" allows you to reference or extract selected
  448. elements of a matrix or vector.
  449.  
  450.    Indices may be scalars, vectors, ranges, or the special operator
  451. `:', which may be used to select entire rows or columns.
  452.  
  453.    Vectors are indexed using a single expression.  Matrices require two
  454. indices unless the value of the built-in variable `do_fortran_indexing'
  455. is `"true"', in which case a matrix may also be indexed by a single
  456. expression (*note User Preferences::.).
  457.  
  458.    Given the matrix
  459.  
  460.      a = [1, 2; 3, 4]
  461.  
  462. all of the following expressions are equivalent
  463.  
  464.      a (1, [1, 2])
  465.      a (1, 1:2)
  466.      a (1, :)
  467.  
  468. and select the first row of the matrix.
  469.  
  470.    A special form of indexing may be used to select elements of a
  471. matrix or vector.  If the indices are vectors made up of only ones and
  472. zeros, the result is a new matrix whose elements correspond to the
  473. elements of the index vector that are equal to one.  For example,
  474.  
  475.      a = [1, 2; 3, 4];
  476.      a ([1, 0], :)
  477.  
  478. selects the first row of the matrix `a'.
  479.  
  480.    This operation can be useful for selecting elements of a matrix
  481. based on some condition, since the comparison operators return matrices
  482. of ones and zeros.
  483.  
  484.    Unfortunately, this special zero-one form of indexing leads to a
  485. conflict with the standard indexing operation.  For example, should the
  486. following statements
  487.  
  488.      a = [1, 2; 3, 4];
  489.      a ([1, 1], :)
  490.  
  491. return the original matrix, or the matrix formed by selecting the first
  492. row twice?  Although this conflict is not likely to arise very often in
  493. practice, you may select the behavior you prefer by setting the built-in
  494. variable `prefer_zero_one_indexing' (*note User Preferences::.).
  495.  
  496.    Finally, indexing a scalar with a vector of ones can be used to
  497. create a vector the same size as the the index vector, with each
  498. element equal to the value of the original scalar.  For example, the
  499. following statements
  500.  
  501.      a = 13;
  502.      a ([1, 1, 1, 1])
  503.  
  504. produce a vector whose four elements are all equal to 13.
  505.  
  506.    Similarly, indexing a scalar with two vectors of ones can be used to
  507. create a matrix.  For example the following statements
  508.  
  509.      a = 13;
  510.      a ([1, 1], [1, 1, 1])
  511.  
  512. create a 2 by 3 matrix with all elements equal to 13.
  513.  
  514.    This is an obscure notation and should be avoided.  It is better to
  515. use the function `ones' to generate a matrix of the appropriate size
  516. whose elements are all one, and then to scale it to produce the desired
  517. result.  *Note Special Matrices::.
  518.  
  519. 
  520. File: octave.info,  Node: Data Structures,  Next: Calling Functions,  Prev: Index Expressions,  Up: Expressions
  521.  
  522. Data Structures
  523. ===============
  524.  
  525.    Octave includes a limited amount of support for organizing data in
  526. structures.  The current implementation uses an associative array with
  527. indices limited to strings, but the syntax is more like C-style
  528. structures.  Here are some examples of using data structures in Octave.
  529.  
  530.    Elements of structures can be of any value type.
  531.  
  532.      octave:1> x.a = 1; x.b = [1, 2; 3, 4]; x.c = "string";
  533.      octave:2> x.a
  534.      x.a = 1
  535.      octave:3> x.b
  536.      x.b =
  537.      
  538.        1  2
  539.        3  4
  540.      
  541.      octave:4> x.c
  542.      x.c = string
  543.  
  544.    Structures may be copied.
  545.  
  546.      octave:1> y = x
  547.      y =
  548.      
  549.      <structure: a b c>
  550.  
  551.    Note that when the value of a structure is printed, Octave only
  552. displays the names of the elements.  This prevents long and confusing
  553. output from large deeply nested structures, but makes it more difficult
  554. to view the values of simple structures, so this behavior may change in
  555. a future version of Octave.
  556.  
  557.    Since structures are themselves values, structure elements may
  558. reference other structures.  The following statements change the value
  559. of the element `b' of the structure `x' to be a data structure
  560. containing the single element `d', which has a value of 3.
  561.  
  562.      octave:1> x.b.d = 3
  563.      x.b.d = 3
  564.      octave:2> x.b
  565.      x.b =
  566.      
  567.      <structure: d>
  568.      
  569.      octave:3> x.b.d
  570.      x.b.d = 3
  571.  
  572.    Functions can return structures.  For example, the following function
  573. separates the real and complex parts of a matrix and stores them in two
  574. elements of the same structure variable.
  575.  
  576.      octave:1> function y = f (x)
  577.      > y.re = real (x);
  578.      > y.im = imag (x);
  579.      > endfunction
  580.  
  581.    When called with a complex-valued argument, `f' returns the data
  582. structure containing the real and imaginary parts of the original
  583. function argument.
  584.  
  585.      octave:1> f (rand (3) + rand (3) * I);
  586.      ans =
  587.      
  588.      <structure: im re>
  589.      
  590.      octave:3> ans.im
  591.      ans.im =
  592.      
  593.        0.093411  0.229690  0.627585
  594.        0.415128  0.221706  0.850341
  595.        0.894990  0.343265  0.384018
  596.      
  597.      octave:4> ans.re
  598.      ans.re =
  599.      
  600.        0.56234  0.14797  0.26416
  601.        0.72120  0.62691  0.20910
  602.        0.89211  0.25175  0.21081
  603.  
  604.    Function return lists can include structure elements, and they may be
  605. indexed like any other variable.
  606.  
  607.      octave:1> [x.u, x.s(2:3,2:3), x.v] = svd ([1, 2; 3, 4])
  608.      x.u =
  609.      
  610.        -0.40455  -0.91451
  611.        -0.91451   0.40455
  612.      
  613.      x.s =
  614.      
  615.        0.00000  0.00000  0.00000
  616.        0.00000  5.46499  0.00000
  617.        0.00000  0.00000  0.36597
  618.      
  619.      x.v =
  620.      
  621.        -0.57605   0.81742
  622.        -0.81742  -0.57605
  623.      
  624.      octave:8> x
  625.      x =
  626.      
  627.      <structure: s u v>
  628.  
  629.    You can also use the function `is_struct' to determine whether a
  630. given value is a data structure.  For example
  631.  
  632.      is_struct (x)
  633.  
  634. returns 1 if the value of the variable X is a data structure.
  635.  
  636.    This feature should be considered experimental, but you should
  637. expect it to work.  Suggestions for ways to improve it are welcome.
  638.  
  639. 
  640. File: octave.info,  Node: Calling Functions,  Next: Global Variables,  Prev: Data Structures,  Up: Expressions
  641.  
  642. Calling Functions
  643. =================
  644.  
  645.    A "function" is a name for a particular calculation.  Because it has
  646. a name, you can ask for it by name at any point in the program.  For
  647. example, the function `sqrt' computes the square root of a number.
  648.  
  649.    A fixed set of functions are "built-in", which means they are
  650. available in every Octave program.  The `sqrt' function is one of
  651. these.  In addition, you can define your own functions.  *Note
  652. Functions and Scripts::, for information about how to do this.
  653.  
  654.    The way to use a function is with a "function call" expression,
  655. which consists of the function name followed by a list of "arguments"
  656. in parentheses. The arguments are expressions which give the raw
  657. materials for the calculation that the function will do.  When there is
  658. more than one argument, they are separated by commas.  If there are no
  659. arguments, you can omit the parentheses, but it is a good idea to
  660. include them anyway, to clearly indicate that a function call was
  661. intended.  Here are some examples:
  662.  
  663.      sqrt (x^2 + y^2)      # One argument
  664.      ones (n, m)           # Two arguments
  665.      rand ()               # No arguments
  666.  
  667.    Each function expects a particular number of arguments.  For
  668. example, the `sqrt' function must be called with a single argument, the
  669. number to take the square root of:
  670.  
  671.      sqrt (ARGUMENT)
  672.  
  673.    Some of the built-in functions take a variable number of arguments,
  674. depending on the particular usage, and their behavior is different
  675. depending on the number of arguments supplied.
  676.  
  677.    Like every other expression, the function call has a value, which is
  678. computed by the function based on the arguments you give it.  In this
  679. example, the value of `sqrt (ARGUMENT)' is the square root of the
  680. argument.  A function can also have side effects, such as assigning the
  681. values of certain variables or doing input or output operations.
  682.  
  683.    Unlike most languages, functions in Octave may return multiple
  684. values.  For example, the following statement
  685.  
  686.      [u, s, v] = svd (a)
  687.  
  688. computes the singular value decomposition of the matrix `a' and assigns
  689. the three result matrices to `u', `s', and `v'.
  690.  
  691.    The left side of a multiple assignment expression is itself a list of
  692. expressions, and is allowed to be a list of variable names or index
  693. expressions.  See also *Note Index Expressions::, and *Note Assignment
  694. Ops::.
  695.  
  696. * Menu:
  697.  
  698. * Call by Value::
  699. * Recursion::
  700.  
  701. 
  702. File: octave.info,  Node: Call by Value,  Next: Recursion,  Prev: Calling Functions,  Up: Calling Functions
  703.  
  704. Call by Value
  705. -------------
  706.  
  707.    In Octave, unlike Fortran, function arguments are passed by value,
  708. which means that each argument in a function call is evaluated and
  709. assigned to a temporary location in memory before being passed to the
  710. function.  There is currently no way to specify that a function
  711. parameter should be passed by reference instead of by value.  This
  712. means that it is impossible to directly alter the value of function
  713. parameter in the calling function.  It can only change the local copy
  714. within the function body.  For example, the function
  715.  
  716.      function f (x, n)
  717.        while (n-- > 0)
  718.          disp (x);
  719.        endwhile
  720.      endfunction
  721.  
  722. displays the value of the first argument N times.  In this function,
  723. the variable N is used as a temporary variable without having to worry
  724. that its value might also change in the calling function.  Call by
  725. value is also useful because it is always possible to pass constants
  726. for any function parameter without first having to determine that the
  727. function will not attempt to modify the parameter.
  728.  
  729.    The caller may use a variable as the expression for the argument, but
  730. the called function does not know this: it only knows what value the
  731. argument had.  For example, given a function called as
  732.  
  733.      foo = "bar";
  734.      fcn (foo)
  735.  
  736. you should not think of the argument as being "the variable `foo'."
  737. Instead, think of the argument as the string value, `"bar"'.
  738.  
  739. 
  740. File: octave.info,  Node: Recursion,  Prev: Call by Value,  Up: Calling Functions
  741.  
  742. Recursion
  743. ---------
  744.  
  745.    Recursive function calls are allowed.  A "recursive function" is one
  746. which calls itself, either directly or indirectly.  For example, here is
  747. an inefficient(1) way to compute the factorial of a given integer:
  748.  
  749.      function retval = fact (n)
  750.        if (n > 0)
  751.          retval = n * fact (n-1);
  752.        else
  753.          retval = 1;
  754.        endif
  755.      endfunction
  756.  
  757.    This function is recursive because it calls itself directly.  It
  758. eventually terminates because each time it calls itself, it uses an
  759. argument that is one less than was used for the previous call.  Once the
  760. argument is no longer greater than zero, it does not call itself, and
  761. the recursion ends.
  762.  
  763.    There is currently no limit on the recursion depth, so infinite
  764. recursion is possible.  If this happens, Octave will consume more and
  765. more memory attempting to store intermediate values for each function
  766. call context until there are no more resources available.  This is
  767. obviously undesirable, and will probably be fixed in some future version
  768. of Octave by allowing users to specify a maximum allowable recursion
  769. depth.
  770.  
  771.    ---------- Footnotes ----------
  772.  
  773.    (1)  It would be much better to use `prod (1:n)', or `gamma (n+1)'
  774. instead, after first checking to ensure that the value `n' is actually
  775. a positive integer.
  776.  
  777. 
  778. File: octave.info,  Node: Global Variables,  Next: Keywords,  Prev: Calling Functions,  Up: Expressions
  779.  
  780. Global Variables
  781. ================
  782.  
  783.    A variable that has been declared "global" may be accessed from
  784. within a function body without having to pass it as a formal parameter.
  785.  
  786.    A variable may be declared global using a `global' declaration
  787. statement.  The following statements are all global declarations.
  788.  
  789.      global a
  790.      global b = 2
  791.      global c = 3, d, e = 5
  792.  
  793.    It is necessary declare a variable as global within a function body
  794. in order to access it.  For example,
  795.  
  796.      global x
  797.      function f ()
  798.      x = 1;
  799.      endfunction
  800.      f ()
  801.  
  802. does *not* set the value of the global variable `x' to 1.  In order to
  803. change the value of the global variable `x', you must also declare it
  804. to be global within the function body, like this
  805.  
  806.      function f ()
  807.        global x;
  808.        x = 1;
  809.      endfunction
  810.  
  811.    Passing a global variable in a function parameter list will make a
  812. local copy and not modify the global value.  For example:
  813.  
  814.      octave:1> function f (x)
  815.      > x = 3
  816.      > endfunction
  817.      octave:2> global x = 0
  818.      octave:3> x              # This is the value of the global variable.
  819.      x = 0
  820.      octave:4> f (x)
  821.      x = 3                    # The value of the local variable x is 3.
  822.      octave:5> x              # But it was a *copy* so the global variable
  823.      x = 0                    # remains unchanged.
  824.  
  825. 
  826. File: octave.info,  Node: Keywords,  Next: Arithmetic Ops,  Prev: Global Variables,  Up: Expressions
  827.  
  828. Keywords
  829. ========
  830.  
  831.    The following identifiers are keywords, and may not be used as
  832. variable or function names:
  833.  
  834.      break       endfor         function    return
  835.      continue    endfunction    global      while
  836.      else        endif          gplot
  837.      elseif      endwhile       gsplot
  838.      end         for            if
  839.  
  840.    The following command-like functions are also keywords, and may not
  841. be used as variable or function names:
  842.  
  843.      casesen   document       history       set
  844.      cd        edit_history   load          show
  845.      clear     help           ls            who
  846.      dir       format         run_history   save
  847.  
  848. 
  849. File: octave.info,  Node: Arithmetic Ops,  Next: Comparison Ops,  Prev: Keywords,  Up: Expressions
  850.  
  851. Arithmetic Operators
  852. ====================
  853.  
  854.    The following arithmetic operators are available, and work on scalars
  855. and matrices.
  856.  
  857. `X + Y'
  858.      Addition.  If both operands are matrices, the number of rows and
  859.      columns must both agree.  If one operand is a scalar, its value is
  860.      added to all the elements of the other operand.
  861.  
  862. `X .+ Y'
  863.      Element by element addition.  This operator is equivalent to `+'.
  864.  
  865. `X - Y'
  866.      Subtraction.  If both operands are matrices, the number of rows and
  867.      columns of both must agree.
  868.  
  869. `X .- Y'
  870.      Element by element subtraction.  This operator is equivalent to
  871.      `-'.
  872.  
  873. `X * Y'
  874.      Matrix multiplication.  The number of columns of `x' must agree
  875.      with the number of rows of `y'.
  876.  
  877. `X .* Y'
  878.      Element by element multiplication.  If both operands are matrices,
  879.      the number of rows and columns must both agree.
  880.  
  881. `X / Y'
  882.      Right division.  This is conceptually equivalent to the expression
  883.  
  884.           (inverse (y') * x')'
  885.  
  886.      but it is computed without forming the inverse of `y''.
  887.  
  888.      If the system is not square, or if the coefficient matrix is
  889.      singular, a minimum norm solution is computed.
  890.  
  891. `X ./ Y'
  892.      Element by element right division.
  893.  
  894. `X \ Y'
  895.      Left division.  This is conceptually equivalent to the expression
  896.  
  897.           inverse (x) * y
  898.  
  899.      but it is computed without forming the inverse of `x'.
  900.  
  901.      If the system is not square, or if the coefficient matrix is
  902.      singular, a minimum norm solution is computed.
  903.  
  904. `X .\ Y'
  905.      Element by element left division.  Each element of `y' is divided
  906.      by each corresponding element of `x'.
  907.  
  908. `X ^ Y'
  909. `X ** Y'
  910.      Power operator.  If X and Y are both scalars, this operator
  911.      returns X raised to the power Y.  If X is a scalar and Y is a
  912.      square matrix, the result is computed using an eigenvalue
  913.      expansion.  If X is a square matrix. the result is computed by
  914.      repeated multiplication if Y is an integer, and by an eigenvalue
  915.      expansion if Y is not an integer.  An error results if both X and
  916.      Y are matrices.
  917.  
  918.      The implementation of this operator needs to be improved.
  919.  
  920. `X .^ Y'
  921. `X .** Y'
  922.      Element by element power operator.  If both operands are matrices,
  923.      the number of rows and columns must both agree.
  924.  
  925. `-X'
  926.      Negation.
  927.  
  928. `+X'
  929.      Unary plus.  This operator has no effect on the operand.
  930.  
  931. `X''
  932.      Complex conjugate transpose.  For real arguments, this operator is
  933.      the same as the transpose operator.  For complex arguments, this
  934.      operator is equivalent to the expression
  935.  
  936.           conj (x.')
  937.  
  938. `X.''
  939.      Transpose.
  940.  
  941.    Note that because Octave's element by element operators begin with a
  942. `.', there is a possible ambiguity for statements like
  943.  
  944.      1./m
  945.  
  946. because the period could be interpreted either as part of the constant
  947. or as part of the operator.  To resolve this conflict, Octave treats the
  948. expression as if you had typed
  949.  
  950.      (1) ./ m
  951.  
  952. and not
  953.  
  954.      (1.) / m
  955.  
  956. Although this is inconsistent with the normal behavior of Octave's
  957. lexer, which usually prefers to break the input into tokens by
  958. preferring the longest possible match at any given point, it is more
  959. useful in this case.
  960.  
  961. 
  962. File: octave.info,  Node: Comparison Ops,  Next: Boolean Expressions,  Prev: Arithmetic Ops,  Up: Expressions
  963.  
  964. Comparison Operators
  965. ====================
  966.  
  967.    "Comparison operators" compare numeric values for relationships such
  968. as equality.  They are written using *relational operators*, which are
  969. a superset of those in C.
  970.  
  971.    All of Octave's comparison operators return a value of 1 if the
  972. comparison is true, or 0 if it is false.  For matrix values, they all
  973. work on an element-by-element basis.  For example, evaluating the
  974. expression
  975.  
  976.      [1, 2; 3, 4] == [1, 3; 2, 4]
  977.  
  978. returns the result
  979.  
  980.      ans =
  981.      
  982.        1  0
  983.        0  1
  984.  
  985. `X < Y'
  986.      True if X is less than Y.
  987.  
  988. `X <= Y'
  989.      True if X is less than or equal to Y.
  990.  
  991. `X == Y'
  992.      True if X is equal to Y.
  993.  
  994. `X >= Y'
  995.      True if X is greater than or equal to Y.
  996.  
  997. `X > Y'
  998.      True if X is greater than Y.
  999.  
  1000. `X != Y'
  1001. `X ~= Y'
  1002. `X <> Y'
  1003.      True if X is not equal to Y.
  1004.  
  1005.    For matrix and vector arguments, the above table should be read as
  1006. "an element of the result matrix (vector) is true if the corresponding
  1007. elements of the argument matrices (vectors) satisfy the specified
  1008. condition"
  1009.  
  1010.    String comparisons should be performed with the `strcmp' function,
  1011. not with the comparison operators listed above.  *Note Calling
  1012. Functions::.
  1013.  
  1014. 
  1015. File: octave.info,  Node: Boolean Expressions,  Next: Assignment Ops,  Prev: Comparison Ops,  Up: Expressions
  1016.  
  1017. Boolean Expressions
  1018. ===================
  1019.  
  1020. * Menu:
  1021.  
  1022. * Element-by-element Boolean Operators::
  1023. * Short-circuit Boolean Operators::
  1024.  
  1025. 
  1026. File: octave.info,  Node: Element-by-element Boolean Operators,  Next: Short-circuit Boolean Operators,  Prev: Boolean Expressions,  Up: Boolean Expressions
  1027.  
  1028. Element-by-element Boolean Operators
  1029. ------------------------------------
  1030.  
  1031.    An element-by-element "boolean expression" is a combination of
  1032. comparison expressions or matching expressions, using the boolean
  1033. operators "or" (`|'), "and" (`&'), and "not" (`!'), along with
  1034. parentheses to control nesting.  The truth of the boolean expression is
  1035. computed by combining the truth values of the corresponding elements of
  1036. the component expressions.  A value is considered to be false if it is
  1037. zero, and true otherwise.
  1038.  
  1039.    Element-by-element boolean expressions can be used wherever
  1040. comparison expressions can be used.  They can be used in `if' and
  1041. `while' statements.  However, before being used in the condition of an
  1042. `if' or `while' statement, an implicit conversion from a matrix value to
  1043. a scalar value occurs using the equivalent of `all (all (X))'. That is,
  1044. a value used as the condition in an `if' or `while' statement is only
  1045. true if *all* of its elements are nonzero.
  1046.  
  1047.    Like comparison operations, each element of an element-by-element
  1048. boolean expression also has a numeric value (1 if true, 0 if false) that
  1049. comes into play if the result of the boolean expression is stored in a
  1050. variable, or used in arithmetic.
  1051.  
  1052.    Here are descriptions of the three element-by-element boolean
  1053. operators.
  1054.  
  1055. `BOOLEAN1 & BOOLEAN2'
  1056.      Elements of the result are true if both corresponding elements of
  1057.      BOOLEAN1 and BOOLEAN2 are true.
  1058.  
  1059. `BOOLEAN1 | BOOLEAN2'
  1060.      Elements of the result are true if either of the corresponding
  1061.      elements of BOOLEAN1 or BOOLEAN2 is true.
  1062.  
  1063. `! BOOLEAN'
  1064. `~ BOOLEAN'
  1065.      Each element of the result is true if the corresponding element of
  1066.      BOOLEAN is false.
  1067.  
  1068.    For matrix operands, these operators work on an element-by-element
  1069. basis.  For example, the expression
  1070.  
  1071.      [1, 0; 0, 1] & [1, 0; 2, 3]
  1072.  
  1073. returns a two by two identity matrix.
  1074.  
  1075.    For the binary operators, the dimensions of the operands must
  1076. conform if both are matrices.  If one of the operands is a scalar and
  1077. the other a matrix, the operator is applied to the scalar and each
  1078. element of the matrix.
  1079.  
  1080.    For the binary element-by-element boolean operators, both
  1081. subexpressions BOOLEAN1 and BOOLEAN2 are evaluated before computing the
  1082. result.  This can make a difference when the expressions have side
  1083. effects.  For example, in the expression
  1084.  
  1085.      a & b++
  1086.  
  1087. the value of the variable B is incremented even if the variable A is
  1088. zero.
  1089.  
  1090.    This behavior is necessary for the boolean operators to work as
  1091. described for matrix-valued operands.
  1092.  
  1093. 
  1094. File: octave.info,  Node: Short-circuit Boolean Operators,  Prev: Element-by-element Boolean Operators,  Up: Boolean Expressions
  1095.  
  1096. Short-circuit Boolean Operators
  1097. -------------------------------
  1098.  
  1099.    Combined with the implicit conversion to scalar values in `if' and
  1100. `while' conditions, Octave's element-by-element boolean operators are
  1101. often sufficient for performing most logical operations.  However, it
  1102. is sometimes desirable to stop evaluating a boolean expression as soon
  1103. as the overall truth value can be determined.  Octave's "short-circuit"
  1104. boolean operators work this way.
  1105.  
  1106. `BOOLEAN1 && BOOLEAN2'
  1107.      The expression BOOLEAN1 is evaluated and converted to a scalar
  1108.      using the equivalent of the operation `all (all (BOOLEAN1))'.  If
  1109.      it is false, the result of the expression is 0.  If it is true, the
  1110.      expression BOOLEAN2 is evaluated and converted to a scalar using
  1111.      the equivalent of the operation `all (all (BOOLEAN1))'.  If it is
  1112.      true, the result of the expression is 1.  Otherwise, the result of
  1113.      the expression is 0.
  1114.  
  1115. `BOOLEAN1 || BOOLEAN2'
  1116.      The expression BOOLEAN1 is evaluated and converted to a scalar
  1117.      using the equivalent of the operation `all (all (BOOLEAN1))'.  If
  1118.      it is true, the result of the expression is 1.  If it is false, the
  1119.      expression BOOLEAN2 is evaluated and converted to a scalar using
  1120.      the equivalent of the operation `all (all (BOOLEAN1))'.  If it is
  1121.      true, the result of the expression is 1.  Otherwise, the result of
  1122.      the expression is 0.
  1123.  
  1124.    The fact that both operands may not be evaluated before determining
  1125. the overall truth value of the expression can be important.  For
  1126. example, in the expression
  1127.  
  1128.      a && b++
  1129.  
  1130. the value of the variable B is only incremented if the variable A is
  1131. nonzero.
  1132.  
  1133.    This can be used to write somewhat more concise code.  For example,
  1134. it is possible write
  1135.  
  1136.      function f (a, b, c)
  1137.        if (nargin > 2 && isstr (c))
  1138.          ...
  1139.  
  1140. instead of having to use two `if' statements to avoid attempting to
  1141. evaluate an argument that doesn't exist.
  1142.  
  1143.      function f (a, b, c)
  1144.        if (nargin > 2)
  1145.          if (isstr (c))
  1146.            ...
  1147.  
  1148. 
  1149. File: octave.info,  Node: Assignment Ops,  Next: Increment Ops,  Prev: Boolean Expressions,  Up: Expressions
  1150.  
  1151. Assignment Expressions
  1152. ======================
  1153.  
  1154.    An "assignment" is an expression that stores a new value into a
  1155. variable.  For example, the following expression assigns the value 1 to
  1156. the variable `z':
  1157.  
  1158.      z = 1
  1159.  
  1160.    After this expression is executed, the variable `z' has the value 1.
  1161. Whatever old value `z' had before the assignment is forgotten.
  1162.  
  1163.    Assignments can store string values also.  For example, the following
  1164. expression would store the value `"this food is good"' in the variable
  1165. `message':
  1166.  
  1167.      thing = "food"
  1168.      predicate = "good"
  1169.      message = [ "this " , thing , " is " , predicate ]
  1170.  
  1171. (This also illustrates concatenation of strings.)
  1172.  
  1173.    The `=' sign is called an "assignment operator".  It is the simplest
  1174. assignment operator because the value of the right-hand operand is
  1175. stored unchanged.
  1176.  
  1177.    Most operators (addition, concatenation, and so on) have no effect
  1178. except to compute a value.  If you ignore the value, you might as well
  1179. not use the operator.  An assignment operator is different.  It does
  1180. produce a value, but even if you ignore the value, the assignment still
  1181. makes itself felt through the alteration of the variable.  We call this
  1182. a "side effect".
  1183.  
  1184.    The left-hand operand of an assignment need not be a variable (*note
  1185. Variables::.).  It can also be an element of a matrix (*note Index
  1186. Expressions::.) or a list of return values (*note Calling
  1187. Functions::.).  These are all called "lvalues", which means they can
  1188. appear on the left-hand side of an assignment operator.  The right-hand
  1189. operand may be any expression.  It produces the new value which the
  1190. assignment stores in the specified variable, matrix element, or list of
  1191. return values.
  1192.  
  1193.    It is important to note that variables do *not* have permanent types.
  1194. The type of a variable is simply the type of whatever value it happens
  1195. to hold at the moment.  In the following program fragment, the variable
  1196. `foo' has a numeric value at first, and a string value later on:
  1197.  
  1198.      octave:13> foo = 1
  1199.      foo = 1
  1200.      octave:13> foo = "bar"
  1201.      foo = bar
  1202.  
  1203. When the second assignment gives `foo' a string value, the fact that it
  1204. previously had a numeric value is forgotten.
  1205.  
  1206.    Assigning an empty matrix `[]' works in most cases to allow you to
  1207. delete rows or columns of matrices and vectors.  *Note Empty Matrices::.
  1208. For example, given a 4 by 5 matrix A, the assignment
  1209.  
  1210.      A (3, :) = []
  1211.  
  1212. deletes the third row of A, and the assignment
  1213.  
  1214.      A (:, 1:2:5) = []
  1215.  
  1216. deletes the first, third, and fifth columns.
  1217.  
  1218.    An assignment is an expression, so it has a value.  Thus, `z = 1' as
  1219. an expression has the value 1.  One consequence of this is that you can
  1220. write multiple assignments together:
  1221.  
  1222.      x = y = z = 0
  1223.  
  1224. stores the value 0 in all three variables.  It does this because the
  1225. value of `z = 0', which is 0, is stored into `y', and then the value of
  1226. `y = z = 0', which is 0, is stored into `x'.
  1227.  
  1228.    This is also true of assignments to lists of values, so the
  1229. following is a valid expression
  1230.  
  1231.      [a, b, c] = [u, s, v] = svd (a)
  1232.  
  1233. that is exactly equivalent to
  1234.  
  1235.      [u, s, v] = svd (a)
  1236.      a = u
  1237.      b = s
  1238.      c = v
  1239.  
  1240.    In expressions like this, the number of values in each part of the
  1241. expression need not match.  For example, the expression
  1242.  
  1243.      [a, b, c, d] = [u, s, v] = svd (a)
  1244.  
  1245. is equivalent to the expression above, except that the value of the
  1246. variable `d' is left unchanged, and the expression
  1247.  
  1248.      [a, b] = [u, s, v] = svd (a)
  1249.  
  1250. is equivalent to
  1251.  
  1252.      [u, s, v] = svd (a)
  1253.      a = u
  1254.      b = s
  1255.  
  1256.    You can use an assignment anywhere an expression is called for.  For
  1257. example, it is valid to write `x != (y = 1)' to set `y' to 1 and then
  1258. test whether `x' equals 1.  But this style tends to make programs hard
  1259. to read.  Except in a one-shot program, you should rewrite it to get
  1260. rid of such nesting of assignments.  This is never very hard.
  1261.  
  1262. 
  1263. File: octave.info,  Node: Increment Ops,  Next: Operator Precedence,  Prev: Assignment Ops,  Up: Expressions
  1264.  
  1265. Increment Operators
  1266. ===================
  1267.  
  1268.    *Increment operators* increase or decrease the value of a variable
  1269. by 1.  The operator to increment a variable is written as `++'.  It may
  1270. be used to increment a variable either before or after taking its value.
  1271.  
  1272.    For example, to pre-increment the variable X, you would write `++X'.
  1273. This would add one to X and then return the new value of X as the
  1274. result of the expression.  It is exactly the same as the expression `X
  1275. = X + 1'.
  1276.  
  1277.    To post-increment a variable X, you would write `X++'.  This adds
  1278. one to the variable X, but returns the value that X had prior to
  1279. incrementing it.  For example, if X is equal to 2, the result of the
  1280. expression `X++' is 2, and the new value of X is 3.
  1281.  
  1282.    For matrix and vector arguments, the increment and decrement
  1283. operators work on each element of the operand.
  1284.  
  1285.    Here is a list of all the increment and decrement expressions.
  1286.  
  1287. `++X'
  1288.      This expression increments the variable X.  The value of the
  1289.      expression is the *new* value of X.  It is equivalent to the
  1290.      expression `X = X + 1'.
  1291.  
  1292. `--X'
  1293.      This expression decrements the variable X.  The value of the
  1294.      expression is the *new* value of X.  It is equivalent to the
  1295.      expression `X = X - 1'.
  1296.  
  1297. `X++'
  1298.      This expression causes the variable X to be incremented.  The
  1299.      value of the expression is the *old* value of X.
  1300.  
  1301. `X--'
  1302.      This expression causes the variable X to be decremented.  The
  1303.      value of the expression is the *old* value of X.
  1304.  
  1305.    It is not currently possible to increment index expressions.  For
  1306. example, you might expect that the expression `V(4)++' would increment
  1307. the fourth element of the vector V, but instead it results in a parse
  1308. error.  This problem may be fixed in a future release of Octave.
  1309.  
  1310. 
  1311. File: octave.info,  Node: Operator Precedence,  Prev: Increment Ops,  Up: Expressions
  1312.  
  1313. Operator Precedence
  1314. ===================
  1315.  
  1316.    "Operator precedence" determines how operators are grouped, when
  1317. different operators appear close by in one expression.  For example,
  1318. `*' has higher precedence than `+'.  Thus, the expression `a + b * c'
  1319. means to multiply `b' and `c', and then add `a' to the product (i.e.,
  1320. `a + (b * c)').
  1321.  
  1322.    You can overrule the precedence of the operators by using
  1323. parentheses.  You can think of the precedence rules as saying where the
  1324. parentheses are assumed if you do not write parentheses yourself.  In
  1325. fact, it is wise to use parentheses whenever you have an unusual
  1326. combination of operators, because other people who read the program may
  1327. not remember what the precedence is in this case.  You might forget as
  1328. well, and then you too could make a mistake.  Explicit parentheses will
  1329. help prevent any such mistake.
  1330.  
  1331.    When operators of equal precedence are used together, the leftmost
  1332. operator groups first, except for the assignment, and exponentiation
  1333. operators, which group in the opposite order.  Thus, the expression `a
  1334. - b + c' groups as `(a - b) + c', but the expression `a = b = c' groups
  1335. as `a = (b = c)'.
  1336.  
  1337.    The precedence of prefix unary operators is important when another
  1338. operator follows the operand.  For example, `-x^2' means `-(x^2)',
  1339. because `-' has lower precedence than `^'.
  1340.  
  1341.    Here is a table of the operators in Octave, in order of increasing
  1342. precedence.
  1343.  
  1344. `statement separators'
  1345.      `;', `,'.
  1346.  
  1347. `assignment'
  1348.      `='.  This operator groups right to left.
  1349.  
  1350. `logical "or" and "and"'
  1351.      `||', `&&'.
  1352.  
  1353. `element-wise "or" and "and"'
  1354.      `|', `&'.
  1355.  
  1356. `relational'
  1357.      `<', `<=', `==', `>=', `>', `!=', `~=', `<>'.
  1358.  
  1359. `colon'
  1360.      `:'.
  1361.  
  1362. `add, subtract'
  1363.      `+', `-'.
  1364.  
  1365. `multiply, divide'
  1366.      `*', `/', `\', `.\', `.*', `./'.
  1367.  
  1368. `transpose'
  1369.      `'', `.''
  1370.  
  1371. `unary plus, minus, increment, decrement, and ``not'''
  1372.      `+', `-', `++', `--', `!', `~'.
  1373.  
  1374. `exponentiation'
  1375.      `^', `**', `.^', `.**'.
  1376.  
  1377. 
  1378. File: octave.info,  Node: Statements,  Next: Functions and Scripts,  Prev: Expressions,  Up: Top
  1379.  
  1380. Statements
  1381. **********
  1382.  
  1383.    "Control statements" such as `if', `while', and so on control the
  1384. flow of execution in Octave programs.  All the control statements start
  1385. with special keywords such as `if' and `while', to distinguish them
  1386. from simple expressions.
  1387.  
  1388.    Many control statements contain other statements; for example, the
  1389. `if' statement contains another statement which may or may not be
  1390. executed.  Each control statement has a corresponding "end" statement
  1391. that marks the end of the end of the control statement.  For example,
  1392. the keyword `endif' marks the end of an `if' statement, and `endwhile'
  1393. marks the end of a `while' statement.  You can use the keyword `end'
  1394. anywhere a more specific end keyword is expected, but using the more
  1395. specific keywords is preferred because if you use them, Octave is able
  1396. to provide better diagnostics for mismatched or missing end tokens.
  1397.  
  1398.    The list of statements contained between keywords like `if' or
  1399. `while' and the corresponding end statement is called the "body" of a
  1400. control statement.
  1401.  
  1402. * Menu:
  1403.  
  1404. * The if Statement::
  1405. * The while Statement::
  1406. * The for Statement::
  1407. * The break Statement::
  1408. * The continue Statement::
  1409. * The unwind_protect Statement::
  1410. * Continuation Lines::
  1411.  
  1412. 
  1413. File: octave.info,  Node: The if Statement,  Next: The while Statement,  Prev: Statements,  Up: Statements
  1414.  
  1415. The `if' Statement
  1416. ==================
  1417.  
  1418.    The `if' statement is Octave's decision-making statement.  There are
  1419. three basic forms of an `if' statement.  In its simplest form, it looks
  1420. like this:
  1421.  
  1422.      if (CONDITION) THEN-BODY endif
  1423.  
  1424. CONDITION is an expression that controls what the rest of the statement
  1425. will do.  The THEN-BODY is executed only if CONDITION is true.
  1426.  
  1427.    The condition in an `if' statement is considered true if its value
  1428. is non-zero, and false if its value is zero.  If the value of the
  1429. conditional expression in an `if' statement is a vector or a matrix, it
  1430. is considered true only if *all* of the elements are non-zero.
  1431.  
  1432.    The second form of an if statement looks like this:
  1433.  
  1434.      if (CONDITION) THEN-BODY else ELSE-BODY endif
  1435.  
  1436. If CONDITION is true, THEN-BODY is executed; otherwise, ELSE-BODY is
  1437. executed.
  1438.  
  1439.    Here is an example:
  1440.  
  1441.      if (rem (x, 2) == 0)
  1442.        printf ("x is even\n");
  1443.      else
  1444.        printf ("x is odd\n");
  1445.      endif
  1446.  
  1447.    In this example, if the expression `rem (x, 2) == 0' is true (that
  1448. is, the value of `x' is divisible by 2), then the first `printf'
  1449. statement is evaluated, otherwise the second `printf' statement is
  1450. evaluated.
  1451.  
  1452.    The third and most general form of the `if' statement allows
  1453. multiple decisions to be combined in a single statement.  It looks like
  1454. this:
  1455.  
  1456.      if (CONDITION) THEN-BODY elseif (CONDITION) ELSEIF-BODY else ELSE-BODY endif
  1457.  
  1458. Any number of `elseif' clauses may appear.  Each condition is tested in
  1459. turn, and if one is found to be true, its corresponding BODY is
  1460. executed.  If none of the conditions are true and the `else' clause is
  1461. present, its body is executed.  Only one `else' clause may appear, and
  1462. it must be the last part of the satement.
  1463.  
  1464.    In the following example, if the first condition is true (that is,
  1465. the value of `x' is divisible by 2), then the first `printf' statement
  1466. is executed.  If it is false, then the second condition is tested, and
  1467. if it is true (that is, the value of `x' is divisible by 3), then the
  1468. second `printf' statement is executed.  Otherwise, the third `printf'
  1469. statement is performed.
  1470.  
  1471.      if (rem (x, 2) == 0)
  1472.        printf ("x is even\n");
  1473.      elseif (rem (x, 3) == 0)
  1474.        printf ("x is odd and divisible by 3\n");
  1475.      else
  1476.        printf ("x is odd\n");
  1477.      endif
  1478.  
  1479.    Note that the `elseif' keyword must not be spelled `else if', as is
  1480. allowed in Fortran.  If it is, the space between the `else' and `if'
  1481. will tell Octave to treat this as a new `if' statement within another
  1482. `if' statement's `else' clause.  For example, if you write
  1483.  
  1484.      if (C1)
  1485.        BODY-1
  1486.      else if (C2)
  1487.        BODY-2
  1488.      endif
  1489.  
  1490. Octave will expect additional input to complete the first `if'
  1491. statement.  If you are using Octave interactively, it will continue to
  1492. prompt you for additional input.  If Octave is reading this input from a
  1493. file, it may complain about missing or mismatched `end' statements, or,
  1494. if you have not used the more specific `end' statements (`endif',
  1495. `endfor', etc.), it may simply produce incorrect results, without
  1496. producing any warning messages.
  1497.  
  1498.    It is much easier to see the error if we rewrite the statements above
  1499. like this,
  1500.  
  1501.      if (C1)
  1502.        BODY-1
  1503.      else
  1504.        if (C2)
  1505.          BODY-2
  1506.        endif
  1507.  
  1508. using the indentation to show how Octave groups the statements.  *Note
  1509. Functions and Scripts::.
  1510.  
  1511.