annotate doc/interpreter/expr.txi @ 12546:39ca02387a32

Improve docstrings for a number of functions.
author Rik <octave@nomad.inbox5.com>
date Mon, 28 Mar 2011 16:38:13 -0700
parents 3ba6e702e6fd
children 99babbf683ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11431
diff changeset
1 @c Copyright (C) 1996-2011 John W. Eaton
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
4 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
5 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
6 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
7 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
8 @c your option) any later version.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
9 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
13 @c for more details.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
14 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
16 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7001
diff changeset
17 @c <http://www.gnu.org/licenses/>.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
19 @node Expressions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
20 @chapter Expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
21 @cindex expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
22
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
23 Expressions are the basic building block of statements in Octave. An
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
24 expression evaluates to a value, which you can print, test, store in a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
25 variable, pass to a function, or assign a new value to a variable with
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
26 an assignment operator.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
27
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
28 An expression can serve as a statement on its own. Most other kinds of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
29 statements contain one or more expressions which specify data to be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
30 operated on. As in other languages, expressions in Octave include
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
31 variables, array references, constants, and function calls, as well as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
32 combinations of these with various operators.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
33
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
34 @menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
35 * Index Expressions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
36 * Calling Functions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
37 * Arithmetic Ops::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
38 * Comparison Ops::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
39 * Boolean Expressions::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
40 * Assignment Ops::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
41 * Increment Ops::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
42 * Operator Precedence::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
43 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
44
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
45 @node Index Expressions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
46 @section Index Expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
47
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
48 @opindex (
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
49 @opindex )
11403
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
50 @opindex :
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
51
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
52 An @dfn{index expression} allows you to reference or extract selected
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
53 elements of a matrix or vector.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
54
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
55 Indices may be scalars, vectors, ranges, or the special operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
56 @samp{:}, which may be used to select entire rows or columns.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
57
5679
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
58 Vectors are indexed using a single index expression. Matrices may be
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
59 indexed using one or two indices. When using a single index
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
60 expression, the elements of the matrix are taken in column-first order;
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
61 the dimensions of the output match those of the index expression. For
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
62 example,
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
63
5679
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
64 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
65 @group
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
66 a(2) # a scalar
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
67 a(1:2) # a row vector
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
68 a([1; 2]) # a column vector
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
69 @end group
5679
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
70 @end example
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
71
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
72 As a special case, when a colon is used as a single index, the output
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
73 is a column vector containing all the elements of the vector or matrix.
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
74 For example:
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
75
5679
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
76 @example
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
77 a(:) # a column vector
5679
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
78 @end example
297b82335c7b [project @ 2006-03-16 16:44:12 by jwe]
jwe
parents: 5016
diff changeset
79
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
80 Given the matrix
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
81
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
83 a = [1, 2; 3, 4]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
84 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
86 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
87 all of the following expressions are equivalent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
88
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
89 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
90 @group
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
91 a(1, [1, 2])
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
92 a(1, 1:2)
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
93 a(1, :)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98 and select the first row of the matrix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99
9159
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
100 In general, an array with @samp{n} dimensions can be indexed using @samp{m}
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
101 indices. If @code{n == m}, each index corresponds to its respective dimension.
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
102 The set of index tuples determining the result is formed by the Cartesian
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
103 product of the index vectors (or ranges or scalars). If @code{n < m}, then the
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
104 array is padded by trailing singleton dimensions. If @code{n > m}, the last
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
105 @code{n-m+1} dimensions are folded into a single dimension with extent equal to
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
106 product of extents of the original dimensions.
9159
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
107
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4461
diff changeset
108 @c FIXED -- sections on variable prefer_zero_one_indexing were removed
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4461
diff changeset
110 Indexing a scalar with a vector of ones can be used to create a
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111 vector the same size as the index vector, with each element equal to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112 the value of the original scalar. For example, the following statements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 a = 13;
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
117 a(ones (1, 4))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
119 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
120
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
121 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
122 produce a vector whose four elements are all equal to 13.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
123
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
124 Similarly, indexing a scalar with two vectors of ones can be used to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
125 create a matrix. For example the following statements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
128 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
129 a = 13;
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12544
diff changeset
130 a(ones (1, 2), ones (1, 3))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
131 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 create a 2 by 3 matrix with all elements equal to 13.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136
9159
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
137 The last example could also be written as
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
138
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
139 @example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
140 @group
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
141 13 (ones (2, 3))
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
142 @end group
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
143 @end example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
144
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
145 It should be, noted that @code{ones (1, n)} (a row vector of ones) results in a
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
146 range (with zero increment), and is therefore more efficient when used in index
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
147 expression than other forms of @dfn{ones}. In particular, when @samp{r} is a
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
148 row vector, the expressions
9159
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
149
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
150 @example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
151 r(ones (1, n), :)
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
152 @end example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
153
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
154 @example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
155 r(ones (n, 1), :)
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
156 @end example
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
157
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10828
diff changeset
158 @noindent
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
159 will produce identical results, but the first one will be significantly
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
160 faster, at least for @samp{r} and @samp{n} large enough. The reason is that
9159
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
161 in the first case the index is kept in a compressed form, which allows Octave
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
162 to choose a more efficient algorithm to handle the expression.
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
163
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
164 In general, for an user unaware of these subtleties, it is best to use
c07cbffb82e3 update indexing docs
Jaroslav Hajek <highegg@gmail.com>
parents: 9153
diff changeset
165 the function @dfn{repmat} for spreading arrays into bigger ones.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
166
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
167 It is also possible to create a matrix with different values. The
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6778
diff changeset
168 following example creates a 10 dimensional row vector @math{a} containing
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
169 the values
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
170 @tex
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
171 $a_i = \sqrt{i}$.
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
172 @end tex
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
173 @ifnottex
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
174 a(i) = sqrt(i).
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
175 @end ifnottex
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
176
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
177 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
178 @group
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
179 for i = 1:10
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
180 a(i) = sqrt (i);
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
181 endfor
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
182 @end group
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
183 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
184
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
185 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
186 Note that it is quite inefficient to create a vector using a loop like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
187 the one shown in the example above. In this particular case, it would
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
188 have been much more efficient to use the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
189
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
190 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
191 a = sqrt (1:10);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
192 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
193
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
194 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
195 thus avoiding the loop entirely. In cases where a loop is still
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
196 required, or a number of values must be combined to form a larger
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
197 matrix, it is generally much faster to set the size of the matrix first,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
198 and then insert elements using indexing commands. For example, given a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
199 matrix @code{a},
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
200
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
201 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
202 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
203 [nr, nc] = size (a);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
204 x = zeros (nr, n * nc);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
205 for i = 1:n
3602
83889344cbef [project @ 2000-02-25 08:52:20 by jwe]
jwe
parents: 3428
diff changeset
206 x(:,(i-1)*nc+1:i*nc) = a;
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
207 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
208 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
209 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
210
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
211 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
212 is considerably faster than
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
213
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
214 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
215 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
216 x = a;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
217 for i = 1:n-1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
218 x = [x, a];
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
219 endfor
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
220 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
221 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
222
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
223 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
224 particularly for large matrices because Octave does not have to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
225 repeatedly resize the result.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
226
6549
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6501
diff changeset
227 @DOCSTRING(sub2ind)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6501
diff changeset
228
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6501
diff changeset
229 @DOCSTRING(ind2sub)
5a5a09d7deb8 [project @ 2007-04-20 06:55:29 by jwe]
jwe
parents: 6501
diff changeset
230
11431
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11403
diff changeset
231 @DOCSTRING(isindex)
0d9640d755b1 Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents: 11403
diff changeset
232
12544
3ba6e702e6fd Add allow_noninteger_range_as_index to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12543
diff changeset
233 @DOCSTRING(allow_noninteger_range_as_index)
3ba6e702e6fd Add allow_noninteger_range_as_index to documentation.
Rik <octave@nomad.inbox5.com>
parents: 12543
diff changeset
234
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
235 @node Calling Functions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
236 @section Calling Functions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
237
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
238 A @dfn{function} is a name for a particular calculation. Because it has
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
239 a name, you can ask for it by name at any point in the program. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
240 example, the function @code{sqrt} computes the square root of a number.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
241
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
242 A fixed set of functions are @dfn{built-in}, which means they are
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
243 available in every Octave program. The @code{sqrt} function is one of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
244 these. In addition, you can define your own functions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
245 @xref{Functions and Scripts}, for information about how to do this.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
246
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
247 @cindex arguments in function call
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
248 The way to use a function is with a @dfn{function call} expression,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
249 which consists of the function name followed by a list of
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
250 @dfn{arguments} in parentheses. The arguments are expressions which give
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
251 the raw materials for the calculation that the function will do. When
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
252 there is more than one argument, they are separated by commas. If there
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
253 are no arguments, you can omit the parentheses, but it is a good idea to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
254 include them anyway, to clearly indicate that a function call was
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
255 intended. Here are some examples:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
256
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
257 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
258 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
259 sqrt (x^2 + y^2) # @r{One argument}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
260 ones (n, m) # @r{Two arguments}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
261 rand () # @r{No arguments}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
262 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
263 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
264
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
265 Each function expects a particular number of arguments. For example, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
266 @code{sqrt} function must be called with a single argument, the number
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
267 to take the square root of:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
268
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
269 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
270 sqrt (@var{argument})
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
271 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
272
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
273 Some of the built-in functions take a variable number of arguments,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
274 depending on the particular usage, and their behavior is different
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
275 depending on the number of arguments supplied.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
276
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
277 Like every other expression, the function call has a value, which is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
278 computed by the function based on the arguments you give it. In this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
279 example, the value of @code{sqrt (@var{argument})} is the square root of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
280 the argument. A function can also have side effects, such as assigning
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
281 the values of certain variables or doing input or output operations.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
282
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
283 Unlike most languages, functions in Octave may return multiple values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
284 For example, the following statement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
285
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
286 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
287 [u, s, v] = svd (a)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
288 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
289
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
290 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
291 computes the singular value decomposition of the matrix @code{a} and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
292 assigns the three result matrices to @code{u}, @code{s}, and @code{v}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
293
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
294 The left side of a multiple assignment expression is itself a list of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
295 expressions, and is allowed to be a list of variable names or index
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
296 expressions. See also @ref{Index Expressions}, and @ref{Assignment Ops}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
297
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
298 @menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
299 * Call by Value::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
300 * Recursion::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
301 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
302
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
303 @node Call by Value
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
304 @subsection Call by Value
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
305
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
306 In Octave, unlike Fortran, function arguments are passed by value, which
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
307 means that each argument in a function call is evaluated and assigned to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
308 a temporary location in memory before being passed to the function.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
309 There is currently no way to specify that a function parameter should be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
310 passed by reference instead of by value. This means that it is
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 8235
diff changeset
311 impossible to directly alter the value of a function parameter in the
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
312 calling function. It can only change the local copy within the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
313 body. For example, the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
314
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
315 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
316 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
317 function f (x, n)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
318 while (n-- > 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
319 disp (x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
320 endwhile
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
321 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
322 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
323 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
324
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
325 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
326 displays the value of the first argument @var{n} times. In this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
327 function, the variable @var{n} is used as a temporary variable without
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
328 having to worry that its value might also change in the calling
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
329 function. Call by value is also useful because it is always possible to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
330 pass constants for any function parameter without first having to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
331 determine that the function will not attempt to modify the parameter.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
332
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
333 The caller may use a variable as the expression for the argument, but
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
334 the called function does not know this: it only knows what value the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
335 argument had. For example, given a function called as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
336
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
337 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
338 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
339 foo = "bar";
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
340 fcn (foo)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
341 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
342 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
343
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
344 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
345 you should not think of the argument as being ``the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
346 @code{foo}.'' Instead, think of the argument as the string value,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
347 @code{"bar"}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
348
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
349 Even though Octave uses pass-by-value semantics for function arguments,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
350 values are not copied unnecessarily. For example,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
351
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
352 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
353 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
354 x = rand (1000);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
355 f (x);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
356 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
357 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
358
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
359 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
360 does not actually force two 1000 by 1000 element matrices to exist
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
361 @emph{unless} the function @code{f} modifies the value of its
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
362 argument. Then Octave must create a copy to avoid changing the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
363 value outside the scope of the function @code{f}, or attempting (and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
364 probably failing!) to modify the value of a constant or the value of a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
365 temporary result.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
366
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
367 @node Recursion
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
368 @subsection Recursion
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
369 @cindex factorial function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
370
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6778
diff changeset
371 With some restrictions@footnote{Some of Octave's functions are
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
372 implemented in terms of functions that cannot be called recursively.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
373 For example, the ODE solver @code{lsode} is ultimately implemented in a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
374 Fortran subroutine that cannot be called recursively, so @code{lsode}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
375 should not be called either directly or indirectly from within the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
376 user-supplied function that @code{lsode} requires. Doing so will result
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
377 in an error.}, recursive function calls are allowed. A
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
378 @dfn{recursive function} is one which calls itself, either directly or
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
379 indirectly. For example, here is an inefficient@footnote{It would be
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
380 much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
381 after first checking to ensure that the value @code{n} is actually a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
382 positive integer.} way to compute the factorial of a given integer:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
383
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
384 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
385 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
386 function retval = fact (n)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
387 if (n > 0)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
388 retval = n * fact (n-1);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
389 else
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
390 retval = 1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
391 endif
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
392 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
393 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
394 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
395
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
396 This function is recursive because it calls itself directly. It
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
397 eventually terminates because each time it calls itself, it uses an
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
398 argument that is one less than was used for the previous call. Once the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
399 argument is no longer greater than zero, it does not call itself, and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
400 the recursion ends.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
401
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
402 The built-in variable @code{max_recursion_depth} specifies a limit to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
403 the recursion depth and prevents Octave from recursing infinitely.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
404
3371
86873384cd10 [project @ 1999-11-21 17:31:07 by jwe]
jwe
parents: 3294
diff changeset
405 @DOCSTRING(max_recursion_depth)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
406
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
407 @node Arithmetic Ops
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
408 @section Arithmetic Operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
409 @cindex arithmetic operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
410 @cindex operators, arithmetic
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
411 @cindex addition
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
412 @cindex subtraction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
413 @cindex multiplication
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
414 @cindex matrix multiplication
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
415 @cindex division
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
416 @cindex quotient
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
417 @cindex negation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
418 @cindex unary minus
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
419 @cindex exponentiation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
420 @cindex transpose
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
421 @cindex Hermitian operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
422 @cindex transpose, complex-conjugate
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
423 @cindex complex-conjugate transpose
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
424
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
425 The following arithmetic operators are available, and work on scalars
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
426 and matrices.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
427
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
428 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
429 @item @var{x} + @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
430 @opindex +
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
431 Addition. If both operands are matrices, the number of rows and columns
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
432 must both agree. If one operand is a scalar, its value is added to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
433 all the elements of the other operand.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
434
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
435 @item @var{x} .+ @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
436 @opindex .+
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
437 Element by element addition. This operator is equivalent to @code{+}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
438
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
439 @item @var{x} - @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
440 @opindex -
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
441 Subtraction. If both operands are matrices, the number of rows and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
442 columns of both must agree.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
443
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
444 @item @var{x} .- @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
445 Element by element subtraction. This operator is equivalent to @code{-}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
446
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
447 @item @var{x} * @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
448 @opindex *
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
449 Matrix multiplication. The number of columns of @var{x} must agree
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
450 with the number of rows of @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
451
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
452 @item @var{x} .* @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
453 @opindex .*
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
454 Element by element multiplication. If both operands are matrices, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
455 number of rows and columns must both agree.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
456
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
457 @item @var{x} / @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
458 @opindex /
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
459 Right division. This is conceptually equivalent to the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
460
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
461 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
462 (inverse (y') * x')'
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
463 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
464
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
465 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
466 but it is computed without forming the inverse of @var{y'}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
467
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
468 If the system is not square, or if the coefficient matrix is singular,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
469 a minimum norm solution is computed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
470
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
471 @item @var{x} ./ @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
472 @opindex ./
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
473 Element by element right division.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
474
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
475 @item @var{x} \ @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
476 @opindex \
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
477 Left division. This is conceptually equivalent to the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
478
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
479 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
480 inverse (x) * y
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
481 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
482
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
483 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
484 but it is computed without forming the inverse of @var{x}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
485
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
486 If the system is not square, or if the coefficient matrix is singular,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
487 a minimum norm solution is computed.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
488
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
489 @item @var{x} .\ @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
490 @opindex .\
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
491 Element by element left division. Each element of @var{y} is divided
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
492 by each corresponding element of @var{x}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
493
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
494 @item @var{x} ^ @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
495 @itemx @var{x} ** @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
496 @opindex **
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
497 @opindex ^
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
498 Power operator. If @var{x} and @var{y} are both scalars, this operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
499 returns @var{x} raised to the power @var{y}. If @var{x} is a scalar and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
500 @var{y} is a square matrix, the result is computed using an eigenvalue
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
501 expansion. If @var{x} is a square matrix, the result is computed by
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
502 repeated multiplication if @var{y} is an integer, and by an eigenvalue
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
503 expansion if @var{y} is not an integer. An error results if both
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
504 @var{x} and @var{y} are matrices.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
505
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
506 The implementation of this operator needs to be improved.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
507
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
508 @item @var{x} .^ @var{y}
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
509 @itemx @var{x} .** @var{y}
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
510 @opindex .**
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
511 @opindex .^
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
512 Element by element power operator. If both operands are matrices, the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
513 number of rows and columns must both agree.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
514
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
515 @item -@var{x}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
516 @opindex -
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
517 Negation.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
518
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
519 @item +@var{x}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
520 @opindex +
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
521 Unary plus. This operator has no effect on the operand.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
522
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
523 @item @var{x}'
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
524 @opindex '
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
525 Complex conjugate transpose. For real arguments, this operator is the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
526 same as the transpose operator. For complex arguments, this operator is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
527 equivalent to the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
528
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
529 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
530 conj (x.')
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
531 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
532
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
533 @item @var{x}.'
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
534 @opindex .'
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
535 Transpose.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
536 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
537
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
538 Note that because Octave's element by element operators begin with a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
539 @samp{.}, there is a possible ambiguity for statements like
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
540
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
541 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
542 1./m
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
543 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
544
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
545 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
546 because the period could be interpreted either as part of the constant
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
547 or as part of the operator. To resolve this conflict, Octave treats the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
548 expression as if you had typed
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
549
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
550 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
551 (1) ./ m
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
552 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
553
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
554 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
555 and not
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
556
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
557 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
558 (1.) / m
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
559 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
560
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
561 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
562 Although this is inconsistent with the normal behavior of Octave's
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
563 lexer, which usually prefers to break the input into tokens by
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
564 preferring the longest possible match at any given point, it is more
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
565 useful in this case.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
566
11403
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
567 @opindex '
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
568 @DOCSTRING(ctranspose)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
569
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
570 @opindex .\
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
571 @DOCSTRING(ldivide)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
572
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
573 @opindex -
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
574 @DOCSTRING(minus)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
575
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
576 @opindex \
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
577 @DOCSTRING(mldivide)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
578
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
579 @opindex **
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
580 @opindex ^
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
581 @DOCSTRING(mpower)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
582
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
583 @opindex /
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
584 @DOCSTRING(mrdivide)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
585
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
586 @opindex *
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
587 @DOCSTRING(mtimes)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
588
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
589 @opindex +
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
590 @DOCSTRING(plus)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
591
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
592 @opindex .**
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
593 @opindex .^
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
594 @DOCSTRING(power)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
595
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
596 @opindex ./
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
597 @DOCSTRING(rdivide)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
598
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
599 @opindex .*
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
600 @DOCSTRING(times)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
601
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
602 @opindex .'
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
603 @DOCSTRING(transpose)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
604
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
605 @opindex -
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
606 @DOCSTRING(uminus)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
607
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
608 @opindex +
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
609 @DOCSTRING(uplus)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
610
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
611 @node Comparison Ops
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
612 @section Comparison Operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
613 @cindex comparison expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
614 @cindex expressions, comparison
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
615 @cindex relational operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
616 @cindex operators, relational
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
617 @cindex less than operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
618 @cindex greater than operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
619 @cindex equality operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
620 @cindex tests for equality
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
621 @cindex equality, tests for
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
622
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
623 @dfn{Comparison operators} compare numeric values for relationships
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
624 such as equality. They are written using
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
625 @emph{relational operators}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
626
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
627 All of Octave's comparison operators return a value of 1 if the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
628 comparison is true, or 0 if it is false. For matrix values, they all
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
629 work on an element-by-element basis. For example:
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
630
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
631 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
632 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
633 [1, 2; 3, 4] == [1, 3; 2, 4]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
634 @result{} 1 0
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
635 0 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
636 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
637 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
638
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
639 If one operand is a scalar and the other is a matrix, the scalar is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
640 compared to each element of the matrix in turn, and the result is the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
641 same size as the matrix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
642
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
643 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
644 @item @var{x} < @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
645 @opindex <
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
646 True if @var{x} is less than @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
647
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
648 @item @var{x} <= @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
649 @opindex <=
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
650 True if @var{x} is less than or equal to @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
651
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
652 @item @var{x} == @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
653 @opindex ==
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
654 True if @var{x} is equal to @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
655
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
656 @item @var{x} >= @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
657 @opindex >=
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
658 True if @var{x} is greater than or equal to @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
659
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
660 @item @var{x} > @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
661 @opindex >
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
662 True if @var{x} is greater than @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
663
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
664 @item @var{x} != @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
665 @itemx @var{x} ~= @var{y}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
666 @opindex !=
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
667 @opindex ~=
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
668 True if @var{x} is not equal to @var{y}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
669 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
670
9578
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
671 For complex numbers, the following ordering is defined:
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
672 @var{z1} < @var{z2}
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
673 iff
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
674
9578
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
675 @example
9758
09da0bd91412 Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents: 9578
diff changeset
676 @group
9578
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
677 abs(@var{z1}) < abs(@var{z2})
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
678 || (abs(@var{z1}) == abs(@var{z2}) && arg(@var{z1}) < arg(@var{z2}))
9758
09da0bd91412 Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents: 9578
diff changeset
679 @end group
9578
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
680 @end example
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
681
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
682 This is consistent with the ordering used by @dfn{max}, @dfn{min} and
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
683 @dfn{sort}, but is not consistent with @sc{matlab}, which only compares the real
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
684 parts.
9578
7dafdb8b062f refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
685
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
686 String comparisons may also be performed with the @code{strcmp}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
687 function, not with the comparison operators listed above.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
688 @xref{Strings}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
689
11403
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
690 @opindex ==
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
691 @DOCSTRING(eq)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
692
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
693 @opindex >=
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
694 @DOCSTRING(ge)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
695
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
696 @opindex >
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
697 @DOCSTRING(gt)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
698
6550
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
699 @DOCSTRING(isequal)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
700
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
701 @DOCSTRING(isequalwithequalnans)
1b7a6061a05d [project @ 2007-04-20 07:40:26 by jwe]
jwe
parents: 6549
diff changeset
702
11403
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
703 @opindex <=
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
704 @DOCSTRING(le)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
705
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
706 @opindex <
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
707 @DOCSTRING(lt)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
708
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
709 @opindex !=
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
710 @opindex ~=
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
711 @DOCSTRING(ne)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
712
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
713 @node Boolean Expressions
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
714 @section Boolean Expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
715 @cindex expressions, boolean
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
716 @cindex boolean expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
717 @cindex expressions, logical
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
718 @cindex logical expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
719 @cindex operators, boolean
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
720 @cindex boolean operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
721 @cindex logical operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
722 @cindex operators, logical
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
723 @cindex and operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
724 @cindex or operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
725 @cindex not operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
726
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
727 @menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
728 * Element-by-element Boolean Operators::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
729 * Short-circuit Boolean Operators::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
730 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
731
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
732 @node Element-by-element Boolean Operators
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
733 @subsection Element-by-element Boolean Operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
734 @cindex element-by-element evaluation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
735
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
736 An @dfn{element-by-element boolean expression} is a combination of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
737 comparison expressions using the boolean
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
738 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}),
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
739 along with parentheses to control nesting. The truth of the boolean
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
740 expression is computed by combining the truth values of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
741 corresponding elements of the component expressions. A value is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
742 considered to be false if it is zero, and true otherwise.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
743
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
744 Element-by-element boolean expressions can be used wherever comparison
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
745 expressions can be used. They can be used in @code{if} and @code{while}
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
746 statements. However, a matrix value used as the condition in an
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
747 @code{if} or @code{while} statement is only true if @emph{all} of its
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
748 elements are nonzero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
749
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
750 Like comparison operations, each element of an element-by-element
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
751 boolean expression also has a numeric value (1 if true, 0 if false) that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
752 comes into play if the result of the boolean expression is stored in a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
753 variable, or used in arithmetic.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
754
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
755 Here are descriptions of the three element-by-element boolean operators.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
756
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
757 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
758 @item @var{boolean1} & @var{boolean2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
759 @opindex &
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
760 Elements of the result are true if both corresponding elements of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
761 @var{boolean1} and @var{boolean2} are true.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
762
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
763 @item @var{boolean1} | @var{boolean2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
764 @opindex |
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
765 Elements of the result are true if either of the corresponding elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
766 of @var{boolean1} or @var{boolean2} is true.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
767
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
768 @item ! @var{boolean}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
769 @itemx ~ @var{boolean}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
770 @opindex ~
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
771 @opindex !
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
772 Each element of the result is true if the corresponding element of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
773 @var{boolean} is false.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
774 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
775
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
776 For matrix operands, these operators work on an element-by-element
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
777 basis. For example, the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
778
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
779 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
780 [1, 0; 0, 1] & [1, 0; 2, 3]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
781 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
782
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
783 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
784 returns a two by two identity matrix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
785
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
786 For the binary operators, the dimensions of the operands must conform if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
787 both are matrices. If one of the operands is a scalar and the other a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
788 matrix, the operator is applied to the scalar and each element of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
789 matrix.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
790
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
791 For the binary element-by-element boolean operators, both subexpressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
792 @var{boolean1} and @var{boolean2} are evaluated before computing the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
793 result. This can make a difference when the expressions have side
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
794 effects. For example, in the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
795
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
796 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
797 a & b++
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
798 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
799
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
800 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
801 the value of the variable @var{b} is incremented even if the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
802 @var{a} is zero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
803
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
804 This behavior is necessary for the boolean operators to work as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
805 described for matrix-valued operands.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
806
11403
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
807 @opindex &
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
808 @DOCSTRING(and)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
809
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
810 @opindex ~
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
811 @opindex !
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
812 @DOCSTRING(not)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
813
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
814 @opindex |
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
815 @DOCSTRING(or)
b8b08b1ac21f Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents: 10846
diff changeset
816
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
817 @node Short-circuit Boolean Operators
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
818 @subsection Short-circuit Boolean Operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
819 @cindex short-circuit evaluation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
820
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
821 Combined with the implicit conversion to scalar values in @code{if} and
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
822 @code{while} conditions, Octave's element-by-element boolean operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
823 are often sufficient for performing most logical operations. However,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
824 it is sometimes desirable to stop evaluating a boolean expression as
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
825 soon as the overall truth value can be determined. Octave's
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
826 @dfn{short-circuit} boolean operators work this way.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
827
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
828 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
829 @item @var{boolean1} && @var{boolean2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
830 @opindex &&
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
831 The expression @var{boolean1} is evaluated and converted to a scalar
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
832 using the equivalent of the operation @code{all (@var{boolean1}(:))}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
833 If it is false, the result of the overall expression is 0. If it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
834 true, the expression @var{boolean2} is evaluated and converted to a
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
835 scalar using the equivalent of the operation @code{all
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
836 (@var{boolean1}(:))}. If it is true, the result of the overall expression
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
837 is 1. Otherwise, the result of the overall expression is 0.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
838
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
839 @strong{Warning:} there is one exception to the rule of evaluating
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
840 @code{all (@var{boolean1}(:))}, which is when @code{boolean1} is the
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
841 empty matrix. The truth value of an empty matrix is always @code{false}
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
842 so @code{[] && true} evaluates to @code{false} even though
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
843 @code{all ([])} is @code{true}.
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
844
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
845 @item @var{boolean1} || @var{boolean2}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
846 @opindex ||
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
847 The expression @var{boolean1} is evaluated and converted to a scalar
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
848 using the equivalent of the operation @code{all (@var{boolean1}(:))}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
849 If it is true, the result of the overall expression is 1. If it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
850 false, the expression @var{boolean2} is evaluated and converted to a
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
851 scalar using the equivalent of the operation @code{all
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
852 (@var{boolean1}(:))}. If it is true, the result of the overall expression
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
853 is 1. Otherwise, the result of the overall expression is 0.
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
854
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
855 @strong{Warning:} the truth value of an empty matrix is always @code{false},
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
856 see the previous list item for details.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
857 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
858
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
859 The fact that both operands may not be evaluated before determining the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
860 overall truth value of the expression can be important. For example, in
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
861 the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
862
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
863 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
864 a && b++
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
865 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
866
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
867 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
868 the value of the variable @var{b} is only incremented if the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
869 @var{a} is nonzero.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
870
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
871 This can be used to write somewhat more concise code. For example, it
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
872 is possible write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
873
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
874 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
875 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
876 function f (a, b, c)
7768
a2d9f325b65a Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents: 7594
diff changeset
877 if (nargin > 2 && ischar (c))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
878 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
879 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
880 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
881
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
882 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
883 instead of having to use two @code{if} statements to avoid attempting to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
884 evaluate an argument that doesn't exist. For example, without the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
885 short-circuit feature, it would be necessary to write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
886
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
887 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
888 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
889 function f (a, b, c)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
890 if (nargin > 2)
7768
a2d9f325b65a Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents: 7594
diff changeset
891 if (ischar (c))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
892 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
893 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
894 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
895
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
896 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
897 Writing
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
898
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
899 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
900 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
901 function f (a, b, c)
7768
a2d9f325b65a Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents: 7594
diff changeset
902 if (nargin > 2 & ischar (c))
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
903 @dots{}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
904 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
905 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
906
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
907 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
908 would result in an error if @code{f} were called with one or two
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
909 arguments because Octave would be forced to try to evaluate both of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
910 operands for the operator @samp{&}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
911
12543
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
912 @sc{matlab} has special behavior that allows the operators @samp{&} and
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
913 @samp{|} to short-circuit when used in the truth expression for @code{if} and
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
914 @code{while} statements. The Octave parser may be instructed to behave in the
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
915 same manner, but it's use is strongly discouraged.
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
916
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
917 @DOCSTRING(do_braindead_shortcircuit_evaluation)
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
918
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
919 Finally, the ternary operator (?:) is not supported in Octave. If
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
920 short-circuiting is not important, it can be replaced by the @code{ifelse}
f60f755ebfe4 Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11552
diff changeset
921 function.
10308
0d928dd9eeb8 extend parseparams
Jaroslav Hajek <highegg@gmail.com>
parents: 10228
diff changeset
922
11552
6b6e9051ecb8 Add merge/ifelse function to documentation.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
923 @DOCSTRING(merge)
10308
0d928dd9eeb8 extend parseparams
Jaroslav Hajek <highegg@gmail.com>
parents: 10228
diff changeset
924
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
925 @node Assignment Ops
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
926 @section Assignment Expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
927 @cindex assignment expressions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
928 @cindex assignment operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
929 @cindex operators, assignment
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
930 @cindex expressions, assignment
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
931
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
932 @opindex =
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
933
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
934 An @dfn{assignment} is an expression that stores a new value into a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
935 variable. For example, the following expression assigns the value 1 to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
936 the variable @code{z}:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
937
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
938 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
939 z = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
940 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
941
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
942 @noindent
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
943 After this expression is executed, the variable @code{z} has the value 1.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
944 Whatever old value @code{z} had before the assignment is forgotten.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
945 The @samp{=} sign is called an @dfn{assignment operator}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
946
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
947 Assignments can store string values also. For example, the following
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
948 expression would store the value @code{"this food is good"} in the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
949 variable @code{message}:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
950
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
951 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
952 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
953 thing = "food"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
954 predicate = "good"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
955 message = [ "this " , thing , " is " , predicate ]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
956 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
957 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
958
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
959 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
960 (This also illustrates concatenation of strings.)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
961
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
962 @cindex side effect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
963 Most operators (addition, concatenation, and so on) have no effect
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
964 except to compute a value. If you ignore the value, you might as well
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
965 not use the operator. An assignment operator is different. It does
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
966 produce a value, but even if you ignore the value, the assignment still
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
967 makes itself felt through the alteration of the variable. We call this
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
968 a @dfn{side effect}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
969
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
970 @cindex lvalue
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
971 The left-hand operand of an assignment need not be a variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
972 (@pxref{Variables}). It can also be an element of a matrix
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
973 (@pxref{Index Expressions}) or a list of return values
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
974 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
975 means they can appear on the left-hand side of an assignment operator.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
976 The right-hand operand may be any expression. It produces the new value
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
977 which the assignment stores in the specified variable, matrix element,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
978 or list of return values.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
979
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
980 It is important to note that variables do @emph{not} have permanent types.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
981 The type of a variable is simply the type of whatever value it happens
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
982 to hold at the moment. In the following program fragment, the variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
983 @code{foo} has a numeric value at first, and a string value later on:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
984
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
985 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
986 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
987 octave:13> foo = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
988 foo = 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
989 octave:13> foo = "bar"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
990 foo = bar
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
991 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
992 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
993
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
994 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
995 When the second assignment gives @code{foo} a string value, the fact that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
996 it previously had a numeric value is forgotten.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
997
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
998 Assignment of a scalar to an indexed matrix sets all of the elements
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
999 that are referenced by the indices to the scalar value. For example, if
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1000 @code{a} is a matrix with at least two columns,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1001
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1002 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1003 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1004 a(:, 2) = 5
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1005 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1006 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1007
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1008 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1009 sets all the elements in the second column of @code{a} to 5.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1010
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1011 Assigning an empty matrix @samp{[]} works in most cases to allow you to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1012 delete rows or columns of matrices and vectors. @xref{Empty Matrices}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1013 For example, given a 4 by 5 matrix @var{A}, the assignment
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1014
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1015 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1016 A (3, :) = []
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1017 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1018
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1019 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1020 deletes the third row of @var{A}, and the assignment
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1021
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1022 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1023 A (:, 1:2:5) = []
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1024 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1025
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1026 @noindent
6672
2f0abcba669c [project @ 2007-05-30 06:42:22 by jwe]
jwe
parents: 6642
diff changeset
1027 deletes the first, third, and fifth columns.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1028
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1029 An assignment is an expression, so it has a value. Thus, @code{z = 1}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1030 as an expression has the value 1. One consequence of this is that you
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1031 can write multiple assignments together:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1032
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1033 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1034 x = y = z = 0
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1035 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1036
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1037 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1038 stores the value 0 in all three variables. It does this because the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1039 value of @code{z = 0}, which is 0, is stored into @code{y}, and then
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1040 the value of @code{y = z = 0}, which is 0, is stored into @code{x}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1041
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1042 This is also true of assignments to lists of values, so the following is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1043 a valid expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1044
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1045 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1046 [a, b, c] = [u, s, v] = svd (a)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1047 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1048
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1049 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1050 that is exactly equivalent to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1051
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1052 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1053 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1054 [u, s, v] = svd (a)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1055 a = u
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1056 b = s
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1057 c = v
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1058 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1059 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1060
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1061 In expressions like this, the number of values in each part of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1062 expression need not match. For example, the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1063
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1064 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1065 [a, b] = [u, s, v] = svd (a)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1066 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1067
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1068 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1069 is equivalent to
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1070
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1071 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1072 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1073 [u, s, v] = svd (a)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1074 a = u
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1075 b = s
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1076 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1077 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1078
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
1079 @noindent
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
1080 The number of values on the left side of the expression can, however,
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
1081 not exceed the number of values on the right side. For example, the
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
1082 following will produce an error.
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
1083
9153
5247e89688e1 Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents: 9037
diff changeset
1084 @example
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
1085 @group
8015
30629059b72d Update the manual to reflect the changes in error output
sh@sh-laptop
parents: 7768
diff changeset
1086 [a, b, c, d] = [u, s, v] = svd (a);
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7018
diff changeset
1087 @print{} error: element number 4 undefined in return list
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9159
diff changeset
1088 @end group
9153
5247e89688e1 Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents: 9037
diff changeset
1089 @end example
6632
fc235391e58f [project @ 2007-05-16 18:21:26 by jwe]
jwe
parents: 6550
diff changeset
1090
10209
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1091 The symbol @code{~} may be used as a placeholder in the list of lvalues,
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1092 indicating that the corresponding return value should be ignored and not stored
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1093 anywhere:
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1094
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1095 @example
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1096 @group
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1097 [~, s, v] = svd (a);
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1098 @end group
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1099 @end example
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1100
10228
fa0c28aff439 update documentation
Jaroslav Hajek <highegg@gmail.com>
parents: 10209
diff changeset
1101 This is cleaner and more memory efficient than using a dummy variable.
fa0c28aff439 update documentation
Jaroslav Hajek <highegg@gmail.com>
parents: 10209
diff changeset
1102 The @code{nargout} value for the right-hand side expression is not affected.
10828
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1103 If the assignment is used as an expression, the return value is a
322f43e0e170 Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
1104 comma-separated list with the ignored values dropped.
10209
ea0d83b4470b document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents: 9758
diff changeset
1105
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1106 @opindex +=
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1107 A very common programming pattern is to increment an existing variable
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1108 with a given value, like this
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1109
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1110 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1111 a = a + 2;
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1112 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1113
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1114 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1115 This can be written in a clearer and more condensed form using the
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1116 @code{+=} operator
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1117
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1118 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1119 a += 2;
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1120 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1121
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1122 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1123 @opindex -=
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1124 @opindex *=
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1125 @opindex /=
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1126 Similar operators also exist for subtraction (@code{-=}),
9037
4cb9f994dcec Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
1127 multiplication (@code{*=}), and division (@code{/=}). An expression
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1128 of the form
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1129
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1130 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1131 @var{expr1} @var{op}= @var{expr2}
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1132 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1133
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1134 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1135 is evaluated as
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1136
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1137 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1138 @var{expr1} = (@var{expr1}) @var{op} (@var{expr2})
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1139 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1140
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1141 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1142 where @var{op} can be either @code{+}, @code{-}, @code{*}, or @code{/}.
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1143 So, the expression
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1144
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1145 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1146 a *= b+1
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1147 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1148
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1149 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1150 is evaluated as
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1151
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1152 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1153 a = a * (b+1)
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1154 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1155
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1156 @noindent
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1157 and @emph{not}
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1158
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1159 @example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1160 a = a * b + 1
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1161 @end example
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1162
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1163 You can use an assignment anywhere an expression is called for. For
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1164 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1165 and then test whether @code{x} equals 1. But this style tends to make
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1166 programs hard to read. Except in a one-shot program, you should rewrite
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1167 it to get rid of such nesting of assignments. This is never very hard.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1168
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1169 @cindex increment operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1170 @cindex decrement operator
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1171 @cindex operators, increment
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1172 @cindex operators, decrement
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1173
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
1174 @node Increment Ops
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1175 @section Increment Operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1176
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1177 @emph{Increment operators} increase or decrease the value of a variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1178 by 1. The operator to increment a variable is written as @samp{++}. It
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1179 may be used to increment a variable either before or after taking its
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1180 value.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1181
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1182 For example, to pre-increment the variable @var{x}, you would write
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1183 @code{++@var{x}}. This would add one to @var{x} and then return the new
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1184 value of @var{x} as the result of the expression. It is exactly the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1185 same as the expression @code{@var{x} = @var{x} + 1}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1186
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1187 To post-increment a variable @var{x}, you would write @code{@var{x}++}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1188 This adds one to the variable @var{x}, but returns the value that
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1189 @var{x} had prior to incrementing it. For example, if @var{x} is equal
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1190 to 2, the result of the expression @code{@var{x}++} is 2, and the new
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1191 value of @var{x} is 3.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1192
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1193 For matrix and vector arguments, the increment and decrement operators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1194 work on each element of the operand.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1195
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1196 Here is a list of all the increment and decrement expressions.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1197
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1198 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1199 @item ++@var{x}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1200 @opindex ++
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1201 This expression increments the variable @var{x}. The value of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1202 expression is the @emph{new} value of @var{x}. It is equivalent to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1203 expression @code{@var{x} = @var{x} + 1}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1204
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1205 @item --@var{x}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1206 @opindex @code{--}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1207 This expression decrements the variable @var{x}. The value of the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1208 expression is the @emph{new} value of @var{x}. It is equivalent to the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1209 expression @code{@var{x} = @var{x} - 1}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1210
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1211 @item @var{x}++
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1212 @opindex ++
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1213 This expression causes the variable @var{x} to be incremented. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1214 value of the expression is the @emph{old} value of @var{x}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1215
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1216 @item @var{x}--
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1217 @opindex @code{--}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1218 This expression causes the variable @var{x} to be decremented. The
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1219 value of the expression is the @emph{old} value of @var{x}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1220 @end table
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1221
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3602
diff changeset
1222 @node Operator Precedence
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1223 @section Operator Precedence
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1224 @cindex operator precedence
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1225
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1226 @dfn{Operator precedence} determines how operators are grouped, when
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1227 different operators appear close by in one expression. For example,
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1228 @samp{*} has higher precedence than @samp{+}. Thus, the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1229 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1230 @code{a} to the product (i.e., @code{a + (b * c)}).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1231
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1232 You can overrule the precedence of the operators by using parentheses.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1233 You can think of the precedence rules as saying where the parentheses
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1234 are assumed if you do not write parentheses yourself. In fact, it is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1235 wise to use parentheses whenever you have an unusual combination of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1236 operators, because other people who read the program may not remember
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1237 what the precedence is in this case. You might forget as well, and then
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1238 you too could make a mistake. Explicit parentheses will help prevent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1239 any such mistake.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1240
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1241 When operators of equal precedence are used together, the leftmost
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1242 operator groups first, except for the assignment and exponentiation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1243 operators, which group in the opposite order. Thus, the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1244 @code{a - b + c} groups as @code{(a - b) + c}, but the expression
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1245 @code{a = b = c} groups as @code{a = (b = c)}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1246
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1247 The precedence of prefix unary operators is important when another
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1248 operator follows the operand. For example, @code{-x^2} means
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1249 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1250
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1251 Here is a table of the operators in Octave, in order of increasing
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1252 precedence.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1253
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1254 @table @code
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1255 @item statement separators
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1256 @samp{;}, @samp{,}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1257
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1258 @item assignment
6642
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1259 @samp{=}, @samp{+=}, @samp{-=}, @samp{*=},@samp{/=}. This operator
ffee6a1a10f3 [project @ 2007-05-21 19:58:43 by jwe]
jwe
parents: 6632
diff changeset
1260 groups right to left.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1261
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1262 @item logical "or" and "and"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1263 @samp{||}, @samp{&&}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1264
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1265 @item element-wise "or" and "and"
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1266 @samp{|}, @samp{&}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1267
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1268 @item relational
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1269 @samp{<}, @samp{<=}, @samp{==}, @samp{>=}, @samp{>}, @samp{!=},
7594
5acf21344dff remove <> from the manual
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
1270 @samp{~=}.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1271
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1272 @item colon
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1273 @samp{:}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1274
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1275 @item add, subtract
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1276 @samp{+}, @samp{-}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1277
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1278 @item multiply, divide
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1279 @samp{*}, @samp{/}, @samp{\}, @samp{.\}, @samp{.*}, @samp{./}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1280
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1281 @item transpose
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1282 @samp{'}, @samp{.'}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1283
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1284 @item unary plus, minus, increment, decrement, and ``not''
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1285 @samp{+}, @samp{-}, @samp{++}, @samp{--}, @samp{!}, @samp{~}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1286
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1287 @item exponentiation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1288 @samp{^}, @samp{**}, @samp{.^}, @samp{.**}.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
1289 @end table