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