Mercurial > jwe > octave
annotate doc/interpreter/expr.txi @ 22620:74fb14727a30
doc: Remove DOCSTRING references to non-existent functions (bug #49329).
* expr.txi: Remove DOCSTRING entries for allow_noninteger_range_as_index and
do_braindead_shortcircuit_evaluation.
* grammar.txi: Remove mention of allow_noninteger_range_as_index.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 13 Oct 2016 08:20:10 -0700 |
parents | bac0d6f07a3e |
children | 3a2b891d0b33 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
20745
diff
changeset
|
1 @c Copyright (C) 1996-2016 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 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
137 An array with @samp{n} dimensions can be indexed using @samp{m} |
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 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
142 For the ordinary and most common case, @w{@code{m == n}}, and each |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
143 index corresponds to its respective dimension. If @w{@code{m < n}} |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
144 and every index is less than the size of the array in the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
145 @math{i^{th}} dimension, @code{m(i) < n(i)}, then the index expression |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
146 is padded with trailing singleton dimensions (@code{[ones (m-n, 1)]}). |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
147 If @w{@code{m < n}} but one of the indices @code{m(i)} is outside the |
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 |
4167 | 322 @node Calling Functions |
3294 | 323 @section Calling Functions |
324 | |
325 A @dfn{function} is a name for a particular calculation. Because it has | |
326 a name, you can ask for it by name at any point in the program. For | |
327 example, the function @code{sqrt} computes the square root of a number. | |
328 | |
329 A fixed set of functions are @dfn{built-in}, which means they are | |
330 available in every Octave program. The @code{sqrt} function is one of | |
331 these. In addition, you can define your own functions. | |
332 @xref{Functions and Scripts}, for information about how to do this. | |
333 | |
334 @cindex arguments in function call | |
335 The way to use a function is with a @dfn{function call} expression, | |
336 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
|
337 @dfn{arguments} in parentheses. The arguments are expressions which give |
3294 | 338 the raw materials for the calculation that the function will do. When |
339 there is more than one argument, they are separated by commas. If there | |
340 are no arguments, you can omit the parentheses, but it is a good idea to | |
341 include them anyway, to clearly indicate that a function call was | |
342 intended. Here are some examples: | |
343 | |
344 @example | |
345 @group | |
346 sqrt (x^2 + y^2) # @r{One argument} | |
347 ones (n, m) # @r{Two arguments} | |
348 rand () # @r{No arguments} | |
349 @end group | |
350 @end example | |
351 | |
352 Each function expects a particular number of arguments. For example, the | |
353 @code{sqrt} function must be called with a single argument, the number | |
354 to take the square root of: | |
355 | |
356 @example | |
357 sqrt (@var{argument}) | |
358 @end example | |
359 | |
360 Some of the built-in functions take a variable number of arguments, | |
361 depending on the particular usage, and their behavior is different | |
362 depending on the number of arguments supplied. | |
363 | |
364 Like every other expression, the function call has a value, which is | |
365 computed by the function based on the arguments you give it. In this | |
366 example, the value of @code{sqrt (@var{argument})} is the square root of | |
367 the argument. A function can also have side effects, such as assigning | |
368 the values of certain variables or doing input or output operations. | |
369 | |
370 Unlike most languages, functions in Octave may return multiple values. | |
371 For example, the following statement | |
372 | |
373 @example | |
374 [u, s, v] = svd (a) | |
375 @end example | |
376 | |
377 @noindent | |
378 computes the singular value decomposition of the matrix @code{a} and | |
379 assigns the three result matrices to @code{u}, @code{s}, and @code{v}. | |
380 | |
381 The left side of a multiple assignment expression is itself a list of | |
382 expressions, and is allowed to be a list of variable names or index | |
383 expressions. See also @ref{Index Expressions}, and @ref{Assignment Ops}. | |
384 | |
385 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
386 * Call by Value:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
387 * Recursion:: |
3294 | 388 @end menu |
389 | |
4167 | 390 @node Call by Value |
3294 | 391 @subsection Call by Value |
392 | |
393 In Octave, unlike Fortran, function arguments are passed by value, which | |
394 means that each argument in a function call is evaluated and assigned to | |
395 a temporary location in memory before being passed to the function. | |
396 There is currently no way to specify that a function parameter should be | |
397 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
|
398 impossible to directly alter the value of a function parameter in the |
3294 | 399 calling function. It can only change the local copy within the function |
400 body. For example, the function | |
401 | |
402 @example | |
403 @group | |
404 function f (x, n) | |
405 while (n-- > 0) | |
406 disp (x); | |
407 endwhile | |
408 endfunction | |
409 @end group | |
410 @end example | |
411 | |
412 @noindent | |
413 displays the value of the first argument @var{n} times. In this | |
414 function, the variable @var{n} is used as a temporary variable without | |
415 having to worry that its value might also change in the calling | |
416 function. Call by value is also useful because it is always possible to | |
417 pass constants for any function parameter without first having to | |
418 determine that the function will not attempt to modify the parameter. | |
419 | |
420 The caller may use a variable as the expression for the argument, but | |
421 the called function does not know this: it only knows what value the | |
422 argument had. For example, given a function called as | |
423 | |
424 @example | |
425 @group | |
426 foo = "bar"; | |
427 fcn (foo) | |
428 @end group | |
429 @end example | |
430 | |
431 @noindent | |
432 you should not think of the argument as being ``the variable | |
433 @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
|
434 @qcode{"bar"}. |
3294 | 435 |
436 Even though Octave uses pass-by-value semantics for function arguments, | |
437 values are not copied unnecessarily. For example, | |
438 | |
439 @example | |
440 @group | |
441 x = rand (1000); | |
442 f (x); | |
443 @end group | |
444 @end example | |
445 | |
446 @noindent | |
447 does not actually force two 1000 by 1000 element matrices to exist | |
448 @emph{unless} the function @code{f} modifies the value of its | |
449 argument. Then Octave must create a copy to avoid changing the | |
450 value outside the scope of the function @code{f}, or attempting (and | |
451 probably failing!) to modify the value of a constant or the value of a | |
452 temporary result. | |
453 | |
4167 | 454 @node Recursion |
3294 | 455 @subsection Recursion |
456 @cindex factorial function | |
457 | |
6939 | 458 With some restrictions@footnote{Some of Octave's functions are |
3294 | 459 implemented in terms of functions that cannot be called recursively. |
460 For example, the ODE solver @code{lsode} is ultimately implemented in a | |
461 Fortran subroutine that cannot be called recursively, so @code{lsode} | |
462 should not be called either directly or indirectly from within the | |
463 user-supplied function that @code{lsode} requires. Doing so will result | |
6642 | 464 in an error.}, recursive function calls are allowed. A |
3294 | 465 @dfn{recursive function} is one which calls itself, either directly or |
466 indirectly. For example, here is an inefficient@footnote{It would be | |
467 much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead, | |
468 after first checking to ensure that the value @code{n} is actually a | |
469 positive integer.} way to compute the factorial of a given integer: | |
470 | |
471 @example | |
472 @group | |
473 function retval = fact (n) | |
474 if (n > 0) | |
475 retval = n * fact (n-1); | |
476 else | |
477 retval = 1; | |
478 endif | |
479 endfunction | |
480 @end group | |
481 @end example | |
482 | |
483 This function is recursive because it calls itself directly. It | |
484 eventually terminates because each time it calls itself, it uses an | |
485 argument that is one less than was used for the previous call. Once the | |
486 argument is no longer greater than zero, it does not call itself, and | |
487 the recursion ends. | |
488 | |
489 The built-in variable @code{max_recursion_depth} specifies a limit to | |
490 the recursion depth and prevents Octave from recursing infinitely. | |
491 | |
3371 | 492 @DOCSTRING(max_recursion_depth) |
3294 | 493 |
4167 | 494 @node Arithmetic Ops |
3294 | 495 @section Arithmetic Operators |
496 @cindex arithmetic operators | |
497 @cindex operators, arithmetic | |
498 @cindex addition | |
499 @cindex subtraction | |
500 @cindex multiplication | |
501 @cindex matrix multiplication | |
502 @cindex division | |
503 @cindex quotient | |
504 @cindex negation | |
505 @cindex unary minus | |
506 @cindex exponentiation | |
507 @cindex transpose | |
508 @cindex Hermitian operator | |
509 @cindex transpose, complex-conjugate | |
510 @cindex complex-conjugate transpose | |
511 | |
512 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
|
513 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
|
514 (@pxref{Broadcasting}). |
3294 | 515 |
13780
990762e784fe
Fix confusing interaction between @code macro and transpose operator (') in documentation (Bug #34661).
Rik <octave@nomad.inbox5.com>
parents:
12627
diff
changeset
|
516 @table @asis |
3294 | 517 @item @var{x} + @var{y} |
518 @opindex + | |
519 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
|
520 must both agree, or they must be broadcastable to the same shape. |
3294 | 521 |
522 @item @var{x} .+ @var{y} | |
523 @opindex .+ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
524 Element-by-element addition. This operator is equivalent to @code{+}. |
3294 | 525 |
526 @item @var{x} - @var{y} | |
527 @opindex - | |
528 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
|
529 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
|
530 shape. |
3294 | 531 |
532 @item @var{x} .- @var{y} | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
533 Element-by-element subtraction. This operator is equivalent to @code{-}. |
3294 | 534 |
535 @item @var{x} * @var{y} | |
536 @opindex * | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
537 Matrix multiplication. The number of columns of @var{x} must agree with |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
538 the number of rows of @var{y}, 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
|
539 shape. |
3294 | 540 |
541 @item @var{x} .* @var{y} | |
542 @opindex .* | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
543 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
|
544 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
|
545 broadcastable to the same shape. |
3294 | 546 |
547 @item @var{x} / @var{y} | |
548 @opindex / | |
549 Right division. This is conceptually equivalent to the expression | |
550 | |
551 @example | |
552 (inverse (y') * x')' | |
553 @end example | |
554 | |
555 @noindent | |
556 but it is computed without forming the inverse of @var{y'}. | |
557 | |
558 If the system is not square, or if the coefficient matrix is singular, | |
559 a minimum norm solution is computed. | |
560 | |
561 @item @var{x} ./ @var{y} | |
562 @opindex ./ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
563 Element-by-element right division. |
3294 | 564 |
565 @item @var{x} \ @var{y} | |
566 @opindex \ | |
567 Left division. This is conceptually equivalent to the expression | |
568 | |
569 @example | |
570 inverse (x) * y | |
571 @end example | |
572 | |
573 @noindent | |
574 but it is computed without forming the inverse of @var{x}. | |
575 | |
576 If the system is not square, or if the coefficient matrix is singular, | |
577 a minimum norm solution is computed. | |
578 | |
579 @item @var{x} .\ @var{y} | |
580 @opindex .\ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
581 Element-by-element left division. Each element of @var{y} is divided |
3294 | 582 by each corresponding element of @var{x}. |
583 | |
17170
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
584 @item @var{x} ^ @var{y} |
3294 | 585 @itemx @var{x} ** @var{y} |
586 @opindex ** | |
587 @opindex ^ | |
588 Power operator. If @var{x} and @var{y} are both scalars, this operator | |
589 returns @var{x} raised to the power @var{y}. If @var{x} is a scalar and | |
590 @var{y} is a square matrix, the result is computed using an eigenvalue | |
7001 | 591 expansion. If @var{x} is a square matrix, the result is computed by |
3294 | 592 repeated multiplication if @var{y} is an integer, and by an eigenvalue |
593 expansion if @var{y} is not an integer. An error results if both | |
594 @var{x} and @var{y} are matrices. | |
595 | |
596 The implementation of this operator needs to be improved. | |
597 | |
17170
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
598 @item @var{x} .^ @var{y} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
599 @itemx @var{x} .** @var{y} |
3294 | 600 @opindex .** |
601 @opindex .^ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
602 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
|
603 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
|
604 broadcastable to the same shape. If several complex results are |
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
605 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
|
606 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
|
607 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
|
608 real result is preferred. |
3294 | 609 |
610 @item -@var{x} | |
611 @opindex - | |
612 Negation. | |
613 | |
614 @item +@var{x} | |
615 @opindex + | |
616 Unary plus. This operator has no effect on the operand. | |
617 | |
618 @item @var{x}' | |
619 @opindex ' | |
620 Complex conjugate transpose. For real arguments, this operator is the | |
621 same as the transpose operator. For complex arguments, this operator is | |
622 equivalent to the expression | |
623 | |
624 @example | |
625 conj (x.') | |
626 @end example | |
627 | |
628 @item @var{x}.' | |
629 @opindex .' | |
630 Transpose. | |
631 @end table | |
632 | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
633 Note that because Octave's element-by-element operators begin with a |
3294 | 634 @samp{.}, there is a possible ambiguity for statements like |
635 | |
636 @example | |
637 1./m | |
638 @end example | |
639 | |
640 @noindent | |
641 because the period could be interpreted either as part of the constant | |
642 or as part of the operator. To resolve this conflict, Octave treats the | |
643 expression as if you had typed | |
644 | |
645 @example | |
646 (1) ./ m | |
647 @end example | |
648 | |
649 @noindent | |
650 and not | |
651 | |
652 @example | |
653 (1.) / m | |
654 @end example | |
655 | |
656 @noindent | |
657 Although this is inconsistent with the normal behavior of Octave's | |
658 lexer, which usually prefers to break the input into tokens by | |
659 preferring the longest possible match at any given point, it is more | |
660 useful in this case. | |
661 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
662 @opindex ' |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
663 @DOCSTRING(ctranspose) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
664 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
665 @opindex .\ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
666 @DOCSTRING(ldivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
667 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
668 @opindex - |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
669 @DOCSTRING(minus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
670 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
671 @opindex \ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
672 @DOCSTRING(mldivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
673 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
674 @opindex ** |
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 @DOCSTRING(mpower) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
677 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
678 @opindex / |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
679 @DOCSTRING(mrdivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
680 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
681 @opindex * |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
682 @DOCSTRING(mtimes) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
683 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
684 @opindex + |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
685 @DOCSTRING(plus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
686 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
687 @opindex .** |
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 @DOCSTRING(power) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
690 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
691 @opindex ./ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
692 @DOCSTRING(rdivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
693 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
694 @opindex .* |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
695 @DOCSTRING(times) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
696 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
697 @opindex .' |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
698 @DOCSTRING(transpose) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
699 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
700 @opindex - |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
701 @DOCSTRING(uminus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
702 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
703 @opindex + |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
704 @DOCSTRING(uplus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
705 |
4167 | 706 @node Comparison Ops |
3294 | 707 @section Comparison Operators |
708 @cindex comparison expressions | |
709 @cindex expressions, comparison | |
710 @cindex relational operators | |
711 @cindex operators, relational | |
712 @cindex less than operator | |
713 @cindex greater than operator | |
714 @cindex equality operator | |
715 @cindex tests for equality | |
716 @cindex equality, tests for | |
717 | |
718 @dfn{Comparison operators} compare numeric values for relationships | |
719 such as equality. They are written using | |
720 @emph{relational operators}. | |
721 | |
722 All of Octave's comparison operators return a value of 1 if the | |
723 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
|
724 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
|
725 @xref{Broadcasting}. For example: |
3294 | 726 |
727 @example | |
728 @group | |
729 [1, 2; 3, 4] == [1, 3; 2, 4] | |
730 @result{} 1 0 | |
731 0 1 | |
732 @end group | |
733 @end example | |
734 | |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
735 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
|
736 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
|
737 in turn, and the result is the same size as the matrix. |
3294 | 738 |
739 @table @code | |
740 @item @var{x} < @var{y} | |
741 @opindex < | |
742 True if @var{x} is less than @var{y}. | |
743 | |
744 @item @var{x} <= @var{y} | |
745 @opindex <= | |
746 True if @var{x} is less than or equal to @var{y}. | |
747 | |
748 @item @var{x} == @var{y} | |
749 @opindex == | |
750 True if @var{x} is equal to @var{y}. | |
751 | |
752 @item @var{x} >= @var{y} | |
753 @opindex >= | |
754 True if @var{x} is greater than or equal to @var{y}. | |
755 | |
756 @item @var{x} > @var{y} | |
757 @opindex > | |
758 True if @var{x} is greater than @var{y}. | |
759 | |
17170
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
760 @item @var{x} != @var{y} |
3294 | 761 @itemx @var{x} ~= @var{y} |
762 @opindex != | |
763 @opindex ~= | |
764 True if @var{x} is not equal to @var{y}. | |
765 @end table | |
766 | |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
767 For complex numbers, the following ordering is defined: |
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
768 @var{z1} < @var{z2} |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
14856
diff
changeset
|
769 if and only if |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
770 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
771 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9578
diff
changeset
|
772 @group |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18842
diff
changeset
|
773 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
|
774 || (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
|
775 @end group |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
776 @end example |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
777 |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
778 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
|
779 @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
|
780 parts. |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
781 |
3294 | 782 String comparisons may also be performed with the @code{strcmp} |
783 function, not with the comparison operators listed above. | |
784 @xref{Strings}. | |
785 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
786 @opindex == |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
787 @DOCSTRING(eq) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
788 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
789 @opindex >= |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
790 @DOCSTRING(ge) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
791 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
792 @opindex > |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
793 @DOCSTRING(gt) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
794 |
6550 | 795 @DOCSTRING(isequal) |
796 | |
16935
a7b2fc7fe1a9
binocdf.m: Reverse calling convention to betaincinv to preserve accuracy when p =~ 1.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
797 @DOCSTRING(isequaln) |
6550 | 798 |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
799 @opindex <= |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
800 @DOCSTRING(le) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
801 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
802 @opindex < |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
803 @DOCSTRING(lt) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
804 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
805 @opindex != |
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 @DOCSTRING(ne) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
808 |
4167 | 809 @node Boolean Expressions |
3294 | 810 @section Boolean Expressions |
811 @cindex expressions, boolean | |
812 @cindex boolean expressions | |
813 @cindex expressions, logical | |
814 @cindex logical expressions | |
815 @cindex operators, boolean | |
816 @cindex boolean operators | |
817 @cindex logical operators | |
818 @cindex operators, logical | |
819 @cindex and operator | |
820 @cindex or operator | |
821 @cindex not operator | |
822 | |
823 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
824 * Element-by-element Boolean Operators:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
825 * Short-circuit Boolean Operators:: |
3294 | 826 @end menu |
827 | |
4167 | 828 @node Element-by-element Boolean Operators |
3294 | 829 @subsection Element-by-element Boolean Operators |
830 @cindex element-by-element evaluation | |
831 | |
832 An @dfn{element-by-element boolean expression} is a combination of | |
833 comparison expressions using the boolean | |
834 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}), | |
835 along with parentheses to control nesting. The truth of the boolean | |
836 expression is computed by combining the truth values of the | |
837 corresponding elements of the component expressions. A value is | |
838 considered to be false if it is zero, and true otherwise. | |
839 | |
840 Element-by-element boolean expressions can be used wherever comparison | |
841 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
|
842 statements. However, a matrix value used as the condition in an |
3294 | 843 @code{if} or @code{while} statement is only true if @emph{all} of its |
844 elements are nonzero. | |
845 | |
846 Like comparison operations, each element of an element-by-element | |
847 boolean expression also has a numeric value (1 if true, 0 if false) that | |
848 comes into play if the result of the boolean expression is stored in a | |
849 variable, or used in arithmetic. | |
850 | |
851 Here are descriptions of the three element-by-element boolean operators. | |
852 | |
853 @table @code | |
854 @item @var{boolean1} & @var{boolean2} | |
855 @opindex & | |
856 Elements of the result are true if both corresponding elements of | |
857 @var{boolean1} and @var{boolean2} are true. | |
858 | |
859 @item @var{boolean1} | @var{boolean2} | |
860 @opindex | | |
861 Elements of the result are true if either of the corresponding elements | |
862 of @var{boolean1} or @var{boolean2} is true. | |
863 | |
17170
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
864 @item ! @var{boolean} |
3294 | 865 @itemx ~ @var{boolean} |
866 @opindex ~ | |
867 @opindex ! | |
868 Each element of the result is true if the corresponding element of | |
869 @var{boolean} is false. | |
870 @end table | |
871 | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
872 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
|
873 expression |
3294 | 874 |
875 @example | |
876 [1, 0; 0, 1] & [1, 0; 2, 3] | |
877 @end example | |
878 | |
879 @noindent | |
880 returns a two by two identity matrix. | |
881 | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
882 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
|
883 In particular, if one of the operands is a scalar and the other a |
3294 | 884 matrix, the operator is applied to the scalar and each element of the |
885 matrix. | |
886 | |
887 For the binary element-by-element boolean operators, both subexpressions | |
888 @var{boolean1} and @var{boolean2} are evaluated before computing the | |
889 result. This can make a difference when the expressions have side | |
890 effects. For example, in the expression | |
891 | |
892 @example | |
893 a & b++ | |
894 @end example | |
895 | |
896 @noindent | |
897 the value of the variable @var{b} is incremented even if the variable | |
898 @var{a} is zero. | |
899 | |
900 This behavior is necessary for the boolean operators to work as | |
901 described for matrix-valued operands. | |
902 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
903 @opindex & |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
904 @DOCSTRING(and) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
905 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
906 @opindex ~ |
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 @DOCSTRING(not) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
909 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
910 @opindex | |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
911 @DOCSTRING(or) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
912 |
4167 | 913 @node Short-circuit Boolean Operators |
3294 | 914 @subsection Short-circuit Boolean Operators |
915 @cindex short-circuit evaluation | |
916 | |
917 Combined with the implicit conversion to scalar values in @code{if} and | |
918 @code{while} conditions, Octave's element-by-element boolean operators | |
919 are often sufficient for performing most logical operations. However, | |
920 it is sometimes desirable to stop evaluating a boolean expression as | |
921 soon as the overall truth value can be determined. Octave's | |
922 @dfn{short-circuit} boolean operators work this way. | |
923 | |
924 @table @code | |
925 @item @var{boolean1} && @var{boolean2} | |
926 @opindex && | |
927 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 928 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 929 If it is false, the result of the overall expression is 0. If it is |
930 true, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 931 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
|
932 (@var{boolean2}(:))}. If it is true, the result of the overall expression |
3294 | 933 is 1. Otherwise, the result of the overall expression is 0. |
934 | |
6632 | 935 @strong{Warning:} there is one exception to the rule of evaluating |
936 @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
|
937 empty matrix. The truth value of an empty matrix is always @code{false} |
6632 | 938 so @code{[] && true} evaluates to @code{false} even though |
939 @code{all ([])} is @code{true}. | |
940 | |
3294 | 941 @item @var{boolean1} || @var{boolean2} |
942 @opindex || | |
943 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 944 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 945 If it is true, the result of the overall expression is 1. If it is |
946 false, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 947 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
|
948 (@var{boolean2}(:))}. If it is true, the result of the overall expression |
3294 | 949 is 1. Otherwise, the result of the overall expression is 0. |
6632 | 950 |
951 @strong{Warning:} the truth value of an empty matrix is always @code{false}, | |
952 see the previous list item for details. | |
3294 | 953 @end table |
954 | |
955 The fact that both operands may not be evaluated before determining the | |
956 overall truth value of the expression can be important. For example, in | |
957 the expression | |
958 | |
959 @example | |
960 a && b++ | |
961 @end example | |
962 | |
963 @noindent | |
964 the value of the variable @var{b} is only incremented if the variable | |
965 @var{a} is nonzero. | |
966 | |
967 This can be used to write somewhat more concise code. For example, it | |
968 is possible write | |
969 | |
970 @example | |
971 @group | |
972 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
973 if (nargin > 2 && ischar (c)) |
3294 | 974 @dots{} |
975 @end group | |
976 @end example | |
977 | |
978 @noindent | |
979 instead of having to use two @code{if} statements to avoid attempting to | |
980 evaluate an argument that doesn't exist. For example, without the | |
981 short-circuit feature, it would be necessary to write | |
982 | |
983 @example | |
984 @group | |
985 function f (a, b, c) | |
986 if (nargin > 2) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
987 if (ischar (c)) |
3294 | 988 @dots{} |
989 @end group | |
990 @end example | |
991 | |
6632 | 992 @noindent |
3294 | 993 Writing |
994 | |
995 @example | |
996 @group | |
997 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
998 if (nargin > 2 & ischar (c)) |
3294 | 999 @dots{} |
1000 @end group | |
1001 @end example | |
1002 | |
1003 @noindent | |
1004 would result in an error if @code{f} were called with one or two | |
1005 arguments because Octave would be forced to try to evaluate both of the | |
1006 operands for the operator @samp{&}. | |
1007 | |
12543
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1008 @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
|
1009 @samp{|} to short-circuit when used in the truth expression for @code{if} and |
22620
74fb14727a30
doc: Remove DOCSTRING references to non-existent functions (bug #49329).
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
1010 @code{while} statements. Octave behaves the same way for compatibility, |
74fb14727a30
doc: Remove DOCSTRING references to non-existent functions (bug #49329).
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
1011 however, the use of the @samp{&} and @samp{|} operators in this way is |
74fb14727a30
doc: Remove DOCSTRING references to non-existent functions (bug #49329).
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
1012 strongly discouraged and a warning will be issued. Instead, you should use |
74fb14727a30
doc: Remove DOCSTRING references to non-existent functions (bug #49329).
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
1013 the @samp{&&} and @samp{||} operators that always have short-circuit behavior. |
20745
c4f79fc9281a
doc: Resolve two undocumented functions in manual.
Rik <rik@octave.org>
parents:
20659
diff
changeset
|
1014 |
12543
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1015 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
|
1016 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
|
1017 function. |
10308 | 1018 |
11552
6b6e9051ecb8
Add merge/ifelse function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1019 @DOCSTRING(merge) |
10308 | 1020 |
4167 | 1021 @node Assignment Ops |
3294 | 1022 @section Assignment Expressions |
1023 @cindex assignment expressions | |
1024 @cindex assignment operators | |
1025 @cindex operators, assignment | |
1026 @cindex expressions, assignment | |
1027 | |
1028 @opindex = | |
1029 | |
1030 An @dfn{assignment} is an expression that stores a new value into a | |
1031 variable. For example, the following expression assigns the value 1 to | |
1032 the variable @code{z}: | |
1033 | |
1034 @example | |
1035 z = 1 | |
1036 @end example | |
1037 | |
6632 | 1038 @noindent |
3294 | 1039 After this expression is executed, the variable @code{z} has the value 1. |
1040 Whatever old value @code{z} had before the assignment is forgotten. | |
1041 The @samp{=} sign is called an @dfn{assignment operator}. | |
1042 | |
1043 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
|
1044 expression would store the value @qcode{"this food is good"} in the |
3294 | 1045 variable @code{message}: |
1046 | |
1047 @example | |
1048 @group | |
1049 thing = "food" | |
1050 predicate = "good" | |
1051 message = [ "this " , thing , " is " , predicate ] | |
1052 @end group | |
1053 @end example | |
1054 | |
1055 @noindent | |
1056 (This also illustrates concatenation of strings.) | |
1057 | |
1058 @cindex side effect | |
1059 Most operators (addition, concatenation, and so on) have no effect | |
1060 except to compute a value. If you ignore the value, you might as well | |
1061 not use the operator. An assignment operator is different. It does | |
1062 produce a value, but even if you ignore the value, the assignment still | |
1063 makes itself felt through the alteration of the variable. We call this | |
1064 a @dfn{side effect}. | |
1065 | |
1066 @cindex lvalue | |
1067 The left-hand operand of an assignment need not be a variable | |
1068 (@pxref{Variables}). It can also be an element of a matrix | |
1069 (@pxref{Index Expressions}) or a list of return values | |
1070 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which | |
1071 means they can appear on the left-hand side of an assignment operator. | |
1072 The right-hand operand may be any expression. It produces the new value | |
1073 which the assignment stores in the specified variable, matrix element, | |
1074 or list of return values. | |
1075 | |
1076 It is important to note that variables do @emph{not} have permanent types. | |
1077 The type of a variable is simply the type of whatever value it happens | |
1078 to hold at the moment. In the following program fragment, the variable | |
1079 @code{foo} has a numeric value at first, and a string value later on: | |
1080 | |
1081 @example | |
1082 @group | |
1083 octave:13> foo = 1 | |
1084 foo = 1 | |
1085 octave:13> foo = "bar" | |
1086 foo = bar | |
1087 @end group | |
1088 @end example | |
1089 | |
1090 @noindent | |
1091 When the second assignment gives @code{foo} a string value, the fact that | |
1092 it previously had a numeric value is forgotten. | |
1093 | |
1094 Assignment of a scalar to an indexed matrix sets all of the elements | |
1095 that are referenced by the indices to the scalar value. For example, if | |
1096 @code{a} is a matrix with at least two columns, | |
1097 | |
1098 @example | |
1099 @group | |
1100 a(:, 2) = 5 | |
1101 @end group | |
1102 @end example | |
1103 | |
1104 @noindent | |
1105 sets all the elements in the second column of @code{a} to 5. | |
1106 | |
1107 Assigning an empty matrix @samp{[]} works in most cases to allow you to | |
1108 delete rows or columns of matrices and vectors. @xref{Empty Matrices}. | |
1109 For example, given a 4 by 5 matrix @var{A}, the assignment | |
1110 | |
1111 @example | |
1112 A (3, :) = [] | |
1113 @end example | |
1114 | |
1115 @noindent | |
1116 deletes the third row of @var{A}, and the assignment | |
1117 | |
1118 @example | |
1119 A (:, 1:2:5) = [] | |
1120 @end example | |
1121 | |
1122 @noindent | |
6672 | 1123 deletes the first, third, and fifth columns. |
3294 | 1124 |
1125 An assignment is an expression, so it has a value. Thus, @code{z = 1} | |
1126 as an expression has the value 1. One consequence of this is that you | |
1127 can write multiple assignments together: | |
1128 | |
1129 @example | |
1130 x = y = z = 0 | |
1131 @end example | |
1132 | |
1133 @noindent | |
1134 stores the value 0 in all three variables. It does this because the | |
1135 value of @code{z = 0}, which is 0, is stored into @code{y}, and then | |
1136 the value of @code{y = z = 0}, which is 0, is stored into @code{x}. | |
1137 | |
1138 This is also true of assignments to lists of values, so the following is | |
1139 a valid expression | |
1140 | |
1141 @example | |
1142 [a, b, c] = [u, s, v] = svd (a) | |
1143 @end example | |
1144 | |
1145 @noindent | |
1146 that is exactly equivalent to | |
1147 | |
1148 @example | |
1149 @group | |
1150 [u, s, v] = svd (a) | |
1151 a = u | |
1152 b = s | |
1153 c = v | |
1154 @end group | |
1155 @end example | |
1156 | |
1157 In expressions like this, the number of values in each part of the | |
1158 expression need not match. For example, the expression | |
1159 | |
1160 @example | |
1161 [a, b] = [u, s, v] = svd (a) | |
1162 @end example | |
1163 | |
1164 @noindent | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18842
diff
changeset
|
1165 is equivalent to |
3294 | 1166 |
1167 @example | |
1168 @group | |
1169 [u, s, v] = svd (a) | |
1170 a = u | |
1171 b = s | |
1172 @end group | |
1173 @end example | |
1174 | |
6632 | 1175 @noindent |
1176 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
|
1177 not exceed the number of values on the right side. For example, the |
6632 | 1178 following will produce an error. |
1179 | |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
1180 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
1181 @group |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7768
diff
changeset
|
1182 [a, b, c, d] = [u, s, v] = svd (a); |
7031 | 1183 @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
|
1184 @end group |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
1185 @end example |
6632 | 1186 |
10209
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1187 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
|
1188 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
|
1189 anywhere: |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1190 |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1191 @example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1192 @group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1193 [~, s, v] = svd (a); |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1194 @end group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1195 @end example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1196 |
10228 | 1197 This is cleaner and more memory efficient than using a dummy variable. |
1198 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
|
1199 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
|
1200 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
|
1201 |
6642 | 1202 @opindex += |
1203 A very common programming pattern is to increment an existing variable | |
1204 with a given value, like this | |
1205 | |
1206 @example | |
1207 a = a + 2; | |
1208 @end example | |
1209 | |
1210 @noindent | |
1211 This can be written in a clearer and more condensed form using the | |
1212 @code{+=} operator | |
1213 | |
1214 @example | |
1215 a += 2; | |
1216 @end example | |
1217 | |
1218 @noindent | |
1219 @opindex -= | |
1220 @opindex *= | |
1221 @opindex /= | |
1222 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
|
1223 multiplication (@code{*=}), and division (@code{/=}). An expression |
6642 | 1224 of the form |
1225 | |
1226 @example | |
1227 @var{expr1} @var{op}= @var{expr2} | |
1228 @end example | |
1229 | |
1230 @noindent | |
1231 is evaluated as | |
1232 | |
1233 @example | |
1234 @var{expr1} = (@var{expr1}) @var{op} (@var{expr2}) | |
1235 @end example | |
1236 | |
1237 @noindent | |
18842
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1238 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
|
1239 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
|
1240 @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
|
1241 is evaluated as |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1242 |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1243 @example |
19152
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19131
diff
changeset
|
1244 @group |
18842
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1245 @var{temp} = @var{expr2} |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1246 @var{expr1} = (@var{expr1}) @var{op} @var{temp} |
19152
0f9c5a15c8fa
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19131
diff
changeset
|
1247 @end group |
18842
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1248 @end example |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1249 |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1250 @noindent |
e6a4fa91a2f1
doc: Clarify order of evaluation of compound assignment expressions
Mike Miller <mtmiller@ieee.org>
parents:
17744
diff
changeset
|
1251 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
|
1252 result of evaluating @var{expr2}. So, the expression |
6642 | 1253 |
1254 @example | |
1255 a *= b+1 | |
1256 @end example | |
1257 | |
1258 @noindent | |
1259 is evaluated as | |
1260 | |
1261 @example | |
1262 a = a * (b+1) | |
1263 @end example | |
1264 | |
1265 @noindent | |
1266 and @emph{not} | |
1267 | |
1268 @example | |
1269 a = a * b + 1 | |
1270 @end example | |
1271 | |
3294 | 1272 You can use an assignment anywhere an expression is called for. For |
1273 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1 | |
1274 and then test whether @code{x} equals 1. But this style tends to make | |
1275 programs hard to read. Except in a one-shot program, you should rewrite | |
1276 it to get rid of such nesting of assignments. This is never very hard. | |
1277 | |
1278 @cindex increment operator | |
1279 @cindex decrement operator | |
1280 @cindex operators, increment | |
1281 @cindex operators, decrement | |
1282 | |
4167 | 1283 @node Increment Ops |
3294 | 1284 @section Increment Operators |
1285 | |
1286 @emph{Increment operators} increase or decrease the value of a variable | |
1287 by 1. The operator to increment a variable is written as @samp{++}. It | |
1288 may be used to increment a variable either before or after taking its | |
1289 value. | |
1290 | |
1291 For example, to pre-increment the variable @var{x}, you would write | |
1292 @code{++@var{x}}. This would add one to @var{x} and then return the new | |
1293 value of @var{x} as the result of the expression. It is exactly the | |
1294 same as the expression @code{@var{x} = @var{x} + 1}. | |
1295 | |
1296 To post-increment a variable @var{x}, you would write @code{@var{x}++}. | |
1297 This adds one to the variable @var{x}, but returns the value that | |
1298 @var{x} had prior to incrementing it. For example, if @var{x} is equal | |
1299 to 2, the result of the expression @code{@var{x}++} is 2, and the new | |
1300 value of @var{x} is 3. | |
1301 | |
1302 For matrix and vector arguments, the increment and decrement operators | |
1303 work on each element of the operand. | |
1304 | |
1305 Here is a list of all the increment and decrement expressions. | |
1306 | |
1307 @table @code | |
1308 @item ++@var{x} | |
1309 @opindex ++ | |
1310 This expression increments the variable @var{x}. The value of the | |
1311 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1312 expression @code{@var{x} = @var{x} + 1}. | |
1313 | |
1314 @item --@var{x} | |
1315 @opindex @code{--} | |
1316 This expression decrements the variable @var{x}. The value of the | |
1317 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1318 expression @code{@var{x} = @var{x} - 1}. | |
1319 | |
1320 @item @var{x}++ | |
1321 @opindex ++ | |
1322 This expression causes the variable @var{x} to be incremented. The | |
1323 value of the expression is the @emph{old} value of @var{x}. | |
1324 | |
1325 @item @var{x}-- | |
1326 @opindex @code{--} | |
1327 This expression causes the variable @var{x} to be decremented. The | |
1328 value of the expression is the @emph{old} value of @var{x}. | |
1329 @end table | |
1330 | |
4167 | 1331 @node Operator Precedence |
3294 | 1332 @section Operator Precedence |
1333 @cindex operator precedence | |
1334 | |
1335 @dfn{Operator precedence} determines how operators are grouped, when | |
1336 different operators appear close by in one expression. For example, | |
1337 @samp{*} has higher precedence than @samp{+}. Thus, the expression | |
1338 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add | |
1339 @code{a} to the product (i.e., @code{a + (b * c)}). | |
1340 | |
1341 You can overrule the precedence of the operators by using parentheses. | |
1342 You can think of the precedence rules as saying where the parentheses | |
1343 are assumed if you do not write parentheses yourself. In fact, it is | |
1344 wise to use parentheses whenever you have an unusual combination of | |
1345 operators, because other people who read the program may not remember | |
1346 what the precedence is in this case. You might forget as well, and then | |
1347 you too could make a mistake. Explicit parentheses will help prevent | |
1348 any such mistake. | |
1349 | |
1350 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
|
1351 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
|
1352 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
|
1353 @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
|
1354 @code{a = (b = c)}. |
3294 | 1355 |
1356 The precedence of prefix unary operators is important when another | |
1357 operator follows the operand. For example, @code{-x^2} means | |
1358 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}. | |
1359 | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1360 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
|
1361 precedence. Unless noted, all operators group left to right. |
3294 | 1362 |
1363 @table @code | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1364 @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
|
1365 @samp{()} @samp{@{@}} @samp{.} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1366 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1367 @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
|
1368 @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 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
|
1371 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1372 @item transpose and exponentiation |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1373 @samp{'} @samp{.'} @samp{^} @samp{**} @samp{.^} @samp{.**} |
3294 | 1374 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1375 @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
|
1376 @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
|
1377 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1378 @item multiply and divide |
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{./} |
3294 | 1380 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1381 @item add, subtract |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1382 @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 colon |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1385 @samp{:} |
3294 | 1386 |
1387 @item relational | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1388 @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
|
1389 @samp{~=} |
3294 | 1390 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1391 @item element-wise "and" |
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 "or" |
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 logical "and" |
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 "or" |
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 assignment |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1404 @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
|
1405 @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
|
1406 @samp{&=} |
3294 | 1407 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1408 These operators group right to left. |
3294 | 1409 @end table |