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