Mercurial > octave-nkf
annotate doc/interpreter/expr.txi @ 10846:a4f482e66b65
Grammarcheck more of the documentation.
Use @noindent macro appropriately.
Limit line length to 80 characters.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 01 Aug 2010 20:22:17 -0700 |
parents | 322f43e0e170 |
children | b8b08b1ac21f |
rev | line source |
---|---|
7018 | 1 @c Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2006, |
9245 | 2 @c 2007, 2008, 2009 John W. Eaton |
7018 | 3 @c |
4 @c This file is part of Octave. | |
5 @c | |
6 @c Octave is free software; you can redistribute it and/or modify it | |
7 @c under the terms of the GNU General Public License as published by the | |
8 @c Free Software Foundation; either version 3 of the License, or (at | |
9 @c your option) any later version. | |
10 @c | |
11 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
12 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 @c for more details. | |
15 @c | |
16 @c You should have received a copy of the GNU General Public License | |
17 @c along with Octave; see the file COPYING. If not, see | |
18 @c <http://www.gnu.org/licenses/>. | |
3294 | 19 |
4167 | 20 @node Expressions |
3294 | 21 @chapter Expressions |
22 @cindex expressions | |
23 | |
24 Expressions are the basic building block of statements in Octave. An | |
25 expression evaluates to a value, which you can print, test, store in a | |
26 variable, pass to a function, or assign a new value to a variable with | |
27 an assignment operator. | |
28 | |
29 An expression can serve as a statement on its own. Most other kinds of | |
30 statements contain one or more expressions which specify data to be | |
31 operated on. As in other languages, expressions in Octave include | |
32 variables, array references, constants, and function calls, as well as | |
33 combinations of these with various operators. | |
34 | |
35 @menu | |
36 * Index Expressions:: | |
37 * Calling Functions:: | |
38 * Arithmetic Ops:: | |
39 * Comparison Ops:: | |
40 * Boolean Expressions:: | |
41 * Assignment Ops:: | |
42 * Increment Ops:: | |
43 * Operator Precedence:: | |
44 @end menu | |
45 | |
4167 | 46 @node Index Expressions |
3294 | 47 @section Index Expressions |
48 | |
49 @opindex ( | |
50 @opindex ) | |
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 | |
4167 | 231 @node Calling Functions |
3294 | 232 @section Calling Functions |
233 | |
234 A @dfn{function} is a name for a particular calculation. Because it has | |
235 a name, you can ask for it by name at any point in the program. For | |
236 example, the function @code{sqrt} computes the square root of a number. | |
237 | |
238 A fixed set of functions are @dfn{built-in}, which means they are | |
239 available in every Octave program. The @code{sqrt} function is one of | |
240 these. In addition, you can define your own functions. | |
241 @xref{Functions and Scripts}, for information about how to do this. | |
242 | |
243 @cindex arguments in function call | |
244 The way to use a function is with a @dfn{function call} expression, | |
245 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
|
246 @dfn{arguments} in parentheses. The arguments are expressions which give |
3294 | 247 the raw materials for the calculation that the function will do. When |
248 there is more than one argument, they are separated by commas. If there | |
249 are no arguments, you can omit the parentheses, but it is a good idea to | |
250 include them anyway, to clearly indicate that a function call was | |
251 intended. Here are some examples: | |
252 | |
253 @example | |
254 @group | |
255 sqrt (x^2 + y^2) # @r{One argument} | |
256 ones (n, m) # @r{Two arguments} | |
257 rand () # @r{No arguments} | |
258 @end group | |
259 @end example | |
260 | |
261 Each function expects a particular number of arguments. For example, the | |
262 @code{sqrt} function must be called with a single argument, the number | |
263 to take the square root of: | |
264 | |
265 @example | |
266 sqrt (@var{argument}) | |
267 @end example | |
268 | |
269 Some of the built-in functions take a variable number of arguments, | |
270 depending on the particular usage, and their behavior is different | |
271 depending on the number of arguments supplied. | |
272 | |
273 Like every other expression, the function call has a value, which is | |
274 computed by the function based on the arguments you give it. In this | |
275 example, the value of @code{sqrt (@var{argument})} is the square root of | |
276 the argument. A function can also have side effects, such as assigning | |
277 the values of certain variables or doing input or output operations. | |
278 | |
279 Unlike most languages, functions in Octave may return multiple values. | |
280 For example, the following statement | |
281 | |
282 @example | |
283 [u, s, v] = svd (a) | |
284 @end example | |
285 | |
286 @noindent | |
287 computes the singular value decomposition of the matrix @code{a} and | |
288 assigns the three result matrices to @code{u}, @code{s}, and @code{v}. | |
289 | |
290 The left side of a multiple assignment expression is itself a list of | |
291 expressions, and is allowed to be a list of variable names or index | |
292 expressions. See also @ref{Index Expressions}, and @ref{Assignment Ops}. | |
293 | |
294 @menu | |
295 * Call by Value:: | |
296 * Recursion:: | |
297 @end menu | |
298 | |
4167 | 299 @node Call by Value |
3294 | 300 @subsection Call by Value |
301 | |
302 In Octave, unlike Fortran, function arguments are passed by value, which | |
303 means that each argument in a function call is evaluated and assigned to | |
304 a temporary location in memory before being passed to the function. | |
305 There is currently no way to specify that a function parameter should be | |
306 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
|
307 impossible to directly alter the value of a function parameter in the |
3294 | 308 calling function. It can only change the local copy within the function |
309 body. For example, the function | |
310 | |
311 @example | |
312 @group | |
313 function f (x, n) | |
314 while (n-- > 0) | |
315 disp (x); | |
316 endwhile | |
317 endfunction | |
318 @end group | |
319 @end example | |
320 | |
321 @noindent | |
322 displays the value of the first argument @var{n} times. In this | |
323 function, the variable @var{n} is used as a temporary variable without | |
324 having to worry that its value might also change in the calling | |
325 function. Call by value is also useful because it is always possible to | |
326 pass constants for any function parameter without first having to | |
327 determine that the function will not attempt to modify the parameter. | |
328 | |
329 The caller may use a variable as the expression for the argument, but | |
330 the called function does not know this: it only knows what value the | |
331 argument had. For example, given a function called as | |
332 | |
333 @example | |
334 @group | |
335 foo = "bar"; | |
336 fcn (foo) | |
337 @end group | |
338 @end example | |
339 | |
340 @noindent | |
341 you should not think of the argument as being ``the variable | |
342 @code{foo}.'' Instead, think of the argument as the string value, | |
343 @code{"bar"}. | |
344 | |
345 Even though Octave uses pass-by-value semantics for function arguments, | |
346 values are not copied unnecessarily. For example, | |
347 | |
348 @example | |
349 @group | |
350 x = rand (1000); | |
351 f (x); | |
352 @end group | |
353 @end example | |
354 | |
355 @noindent | |
356 does not actually force two 1000 by 1000 element matrices to exist | |
357 @emph{unless} the function @code{f} modifies the value of its | |
358 argument. Then Octave must create a copy to avoid changing the | |
359 value outside the scope of the function @code{f}, or attempting (and | |
360 probably failing!) to modify the value of a constant or the value of a | |
361 temporary result. | |
362 | |
4167 | 363 @node Recursion |
3294 | 364 @subsection Recursion |
365 @cindex factorial function | |
366 | |
6939 | 367 With some restrictions@footnote{Some of Octave's functions are |
3294 | 368 implemented in terms of functions that cannot be called recursively. |
369 For example, the ODE solver @code{lsode} is ultimately implemented in a | |
370 Fortran subroutine that cannot be called recursively, so @code{lsode} | |
371 should not be called either directly or indirectly from within the | |
372 user-supplied function that @code{lsode} requires. Doing so will result | |
6642 | 373 in an error.}, recursive function calls are allowed. A |
3294 | 374 @dfn{recursive function} is one which calls itself, either directly or |
375 indirectly. For example, here is an inefficient@footnote{It would be | |
376 much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead, | |
377 after first checking to ensure that the value @code{n} is actually a | |
378 positive integer.} way to compute the factorial of a given integer: | |
379 | |
380 @example | |
381 @group | |
382 function retval = fact (n) | |
383 if (n > 0) | |
384 retval = n * fact (n-1); | |
385 else | |
386 retval = 1; | |
387 endif | |
388 endfunction | |
389 @end group | |
390 @end example | |
391 | |
392 This function is recursive because it calls itself directly. It | |
393 eventually terminates because each time it calls itself, it uses an | |
394 argument that is one less than was used for the previous call. Once the | |
395 argument is no longer greater than zero, it does not call itself, and | |
396 the recursion ends. | |
397 | |
398 The built-in variable @code{max_recursion_depth} specifies a limit to | |
399 the recursion depth and prevents Octave from recursing infinitely. | |
400 | |
3371 | 401 @DOCSTRING(max_recursion_depth) |
3294 | 402 |
4167 | 403 @node Arithmetic Ops |
3294 | 404 @section Arithmetic Operators |
405 @cindex arithmetic operators | |
406 @cindex operators, arithmetic | |
407 @cindex addition | |
408 @cindex subtraction | |
409 @cindex multiplication | |
410 @cindex matrix multiplication | |
411 @cindex division | |
412 @cindex quotient | |
413 @cindex negation | |
414 @cindex unary minus | |
415 @cindex exponentiation | |
416 @cindex transpose | |
417 @cindex Hermitian operator | |
418 @cindex transpose, complex-conjugate | |
419 @cindex complex-conjugate transpose | |
420 | |
421 The following arithmetic operators are available, and work on scalars | |
422 and matrices. | |
423 | |
424 @table @code | |
425 @item @var{x} + @var{y} | |
426 @opindex + | |
427 Addition. If both operands are matrices, the number of rows and columns | |
428 must both agree. If one operand is a scalar, its value is added to | |
429 all the elements of the other operand. | |
430 | |
431 @item @var{x} .+ @var{y} | |
432 @opindex .+ | |
433 Element by element addition. This operator is equivalent to @code{+}. | |
434 | |
435 @item @var{x} - @var{y} | |
436 @opindex - | |
437 Subtraction. If both operands are matrices, the number of rows and | |
438 columns of both must agree. | |
439 | |
440 @item @var{x} .- @var{y} | |
441 Element by element subtraction. This operator is equivalent to @code{-}. | |
442 | |
443 @item @var{x} * @var{y} | |
444 @opindex * | |
445 Matrix multiplication. The number of columns of @var{x} must agree | |
446 with the number of rows of @var{y}. | |
447 | |
448 @item @var{x} .* @var{y} | |
449 @opindex .* | |
450 Element by element multiplication. If both operands are matrices, the | |
451 number of rows and columns must both agree. | |
452 | |
453 @item @var{x} / @var{y} | |
454 @opindex / | |
455 Right division. This is conceptually equivalent to the expression | |
456 | |
457 @example | |
458 (inverse (y') * x')' | |
459 @end example | |
460 | |
461 @noindent | |
462 but it is computed without forming the inverse of @var{y'}. | |
463 | |
464 If the system is not square, or if the coefficient matrix is singular, | |
465 a minimum norm solution is computed. | |
466 | |
467 @item @var{x} ./ @var{y} | |
468 @opindex ./ | |
469 Element by element right division. | |
470 | |
471 @item @var{x} \ @var{y} | |
472 @opindex \ | |
473 Left division. This is conceptually equivalent to the expression | |
474 | |
475 @example | |
476 inverse (x) * y | |
477 @end example | |
478 | |
479 @noindent | |
480 but it is computed without forming the inverse of @var{x}. | |
481 | |
482 If the system is not square, or if the coefficient matrix is singular, | |
483 a minimum norm solution is computed. | |
484 | |
485 @item @var{x} .\ @var{y} | |
486 @opindex .\ | |
487 Element by element left division. Each element of @var{y} is divided | |
488 by each corresponding element of @var{x}. | |
489 | |
490 @item @var{x} ^ @var{y} | |
491 @itemx @var{x} ** @var{y} | |
492 @opindex ** | |
493 @opindex ^ | |
494 Power operator. If @var{x} and @var{y} are both scalars, this operator | |
495 returns @var{x} raised to the power @var{y}. If @var{x} is a scalar and | |
496 @var{y} is a square matrix, the result is computed using an eigenvalue | |
7001 | 497 expansion. If @var{x} is a square matrix, the result is computed by |
3294 | 498 repeated multiplication if @var{y} is an integer, and by an eigenvalue |
499 expansion if @var{y} is not an integer. An error results if both | |
500 @var{x} and @var{y} are matrices. | |
501 | |
502 The implementation of this operator needs to be improved. | |
503 | |
504 @item @var{x} .^ @var{y} | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
505 @itemx @var{x} .** @var{y} |
3294 | 506 @opindex .** |
507 @opindex .^ | |
508 Element by element power operator. If both operands are matrices, the | |
509 number of rows and columns must both agree. | |
510 | |
511 @item -@var{x} | |
512 @opindex - | |
513 Negation. | |
514 | |
515 @item +@var{x} | |
516 @opindex + | |
517 Unary plus. This operator has no effect on the operand. | |
518 | |
519 @item @var{x}' | |
520 @opindex ' | |
521 Complex conjugate transpose. For real arguments, this operator is the | |
522 same as the transpose operator. For complex arguments, this operator is | |
523 equivalent to the expression | |
524 | |
525 @example | |
526 conj (x.') | |
527 @end example | |
528 | |
529 @item @var{x}.' | |
530 @opindex .' | |
531 Transpose. | |
532 @end table | |
533 | |
534 Note that because Octave's element by element operators begin with a | |
535 @samp{.}, there is a possible ambiguity for statements like | |
536 | |
537 @example | |
538 1./m | |
539 @end example | |
540 | |
541 @noindent | |
542 because the period could be interpreted either as part of the constant | |
543 or as part of the operator. To resolve this conflict, Octave treats the | |
544 expression as if you had typed | |
545 | |
546 @example | |
547 (1) ./ m | |
548 @end example | |
549 | |
550 @noindent | |
551 and not | |
552 | |
553 @example | |
554 (1.) / m | |
555 @end example | |
556 | |
557 @noindent | |
558 Although this is inconsistent with the normal behavior of Octave's | |
559 lexer, which usually prefers to break the input into tokens by | |
560 preferring the longest possible match at any given point, it is more | |
561 useful in this case. | |
562 | |
4167 | 563 @node Comparison Ops |
3294 | 564 @section Comparison Operators |
565 @cindex comparison expressions | |
566 @cindex expressions, comparison | |
567 @cindex relational operators | |
568 @cindex operators, relational | |
569 @cindex less than operator | |
570 @cindex greater than operator | |
571 @cindex equality operator | |
572 @cindex tests for equality | |
573 @cindex equality, tests for | |
574 | |
575 @dfn{Comparison operators} compare numeric values for relationships | |
576 such as equality. They are written using | |
577 @emph{relational operators}. | |
578 | |
579 All of Octave's comparison operators return a value of 1 if the | |
580 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
|
581 work on an element-by-element basis. For example: |
3294 | 582 |
583 @example | |
584 @group | |
585 [1, 2; 3, 4] == [1, 3; 2, 4] | |
586 @result{} 1 0 | |
587 0 1 | |
588 @end group | |
589 @end example | |
590 | |
591 If one operand is a scalar and the other is a matrix, the scalar is | |
592 compared to each element of the matrix in turn, and the result is the | |
593 same size as the matrix. | |
594 | |
595 @table @code | |
596 @item @var{x} < @var{y} | |
597 @opindex < | |
598 True if @var{x} is less than @var{y}. | |
599 | |
600 @item @var{x} <= @var{y} | |
601 @opindex <= | |
602 True if @var{x} is less than or equal to @var{y}. | |
603 | |
604 @item @var{x} == @var{y} | |
605 @opindex == | |
606 True if @var{x} is equal to @var{y}. | |
607 | |
608 @item @var{x} >= @var{y} | |
609 @opindex >= | |
610 True if @var{x} is greater than or equal to @var{y}. | |
611 | |
612 @item @var{x} > @var{y} | |
613 @opindex > | |
614 True if @var{x} is greater than @var{y}. | |
615 | |
616 @item @var{x} != @var{y} | |
617 @itemx @var{x} ~= @var{y} | |
618 @opindex != | |
619 @opindex ~= | |
620 True if @var{x} is not equal to @var{y}. | |
621 @end table | |
622 | |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
623 For complex numbers, the following ordering is defined: |
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
624 @var{z1} < @var{z2} |
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
625 iff |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
626 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
627 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9578
diff
changeset
|
628 @group |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
629 abs(@var{z1}) < abs(@var{z2}) |
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
630 || (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
|
631 @end group |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
632 @end example |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
633 |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
634 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
|
635 @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
|
636 parts. |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
637 |
3294 | 638 String comparisons may also be performed with the @code{strcmp} |
639 function, not with the comparison operators listed above. | |
640 @xref{Strings}. | |
641 | |
6550 | 642 @DOCSTRING(isequal) |
643 | |
644 @DOCSTRING(isequalwithequalnans) | |
645 | |
4167 | 646 @node Boolean Expressions |
3294 | 647 @section Boolean Expressions |
648 @cindex expressions, boolean | |
649 @cindex boolean expressions | |
650 @cindex expressions, logical | |
651 @cindex logical expressions | |
652 @cindex operators, boolean | |
653 @cindex boolean operators | |
654 @cindex logical operators | |
655 @cindex operators, logical | |
656 @cindex and operator | |
657 @cindex or operator | |
658 @cindex not operator | |
659 | |
660 @menu | |
661 * Element-by-element Boolean Operators:: | |
662 * Short-circuit Boolean Operators:: | |
663 @end menu | |
664 | |
4167 | 665 @node Element-by-element Boolean Operators |
3294 | 666 @subsection Element-by-element Boolean Operators |
667 @cindex element-by-element evaluation | |
668 | |
669 An @dfn{element-by-element boolean expression} is a combination of | |
670 comparison expressions using the boolean | |
671 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}), | |
672 along with parentheses to control nesting. The truth of the boolean | |
673 expression is computed by combining the truth values of the | |
674 corresponding elements of the component expressions. A value is | |
675 considered to be false if it is zero, and true otherwise. | |
676 | |
677 Element-by-element boolean expressions can be used wherever comparison | |
678 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
|
679 statements. However, a matrix value used as the condition in an |
3294 | 680 @code{if} or @code{while} statement is only true if @emph{all} of its |
681 elements are nonzero. | |
682 | |
683 Like comparison operations, each element of an element-by-element | |
684 boolean expression also has a numeric value (1 if true, 0 if false) that | |
685 comes into play if the result of the boolean expression is stored in a | |
686 variable, or used in arithmetic. | |
687 | |
688 Here are descriptions of the three element-by-element boolean operators. | |
689 | |
690 @table @code | |
691 @item @var{boolean1} & @var{boolean2} | |
692 @opindex & | |
693 Elements of the result are true if both corresponding elements of | |
694 @var{boolean1} and @var{boolean2} are true. | |
695 | |
696 @item @var{boolean1} | @var{boolean2} | |
697 @opindex | | |
698 Elements of the result are true if either of the corresponding elements | |
699 of @var{boolean1} or @var{boolean2} is true. | |
700 | |
701 @item ! @var{boolean} | |
702 @itemx ~ @var{boolean} | |
703 @opindex ~ | |
704 @opindex ! | |
705 Each element of the result is true if the corresponding element of | |
706 @var{boolean} is false. | |
707 @end table | |
708 | |
709 For matrix operands, these operators work on an element-by-element | |
710 basis. For example, the expression | |
711 | |
712 @example | |
713 [1, 0; 0, 1] & [1, 0; 2, 3] | |
714 @end example | |
715 | |
716 @noindent | |
717 returns a two by two identity matrix. | |
718 | |
719 For the binary operators, the dimensions of the operands must conform if | |
720 both are matrices. If one of the operands is a scalar and the other a | |
721 matrix, the operator is applied to the scalar and each element of the | |
722 matrix. | |
723 | |
724 For the binary element-by-element boolean operators, both subexpressions | |
725 @var{boolean1} and @var{boolean2} are evaluated before computing the | |
726 result. This can make a difference when the expressions have side | |
727 effects. For example, in the expression | |
728 | |
729 @example | |
730 a & b++ | |
731 @end example | |
732 | |
733 @noindent | |
734 the value of the variable @var{b} is incremented even if the variable | |
735 @var{a} is zero. | |
736 | |
737 This behavior is necessary for the boolean operators to work as | |
738 described for matrix-valued operands. | |
739 | |
4167 | 740 @node Short-circuit Boolean Operators |
3294 | 741 @subsection Short-circuit Boolean Operators |
742 @cindex short-circuit evaluation | |
743 | |
744 Combined with the implicit conversion to scalar values in @code{if} and | |
745 @code{while} conditions, Octave's element-by-element boolean operators | |
746 are often sufficient for performing most logical operations. However, | |
747 it is sometimes desirable to stop evaluating a boolean expression as | |
748 soon as the overall truth value can be determined. Octave's | |
749 @dfn{short-circuit} boolean operators work this way. | |
750 | |
751 @table @code | |
752 @item @var{boolean1} && @var{boolean2} | |
753 @opindex && | |
754 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 755 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 756 If it is false, the result of the overall expression is 0. If it is |
757 true, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 758 scalar using the equivalent of the operation @code{all |
759 (@var{boolean1}(:))}. If it is true, the result of the overall expression | |
3294 | 760 is 1. Otherwise, the result of the overall expression is 0. |
761 | |
6632 | 762 @strong{Warning:} there is one exception to the rule of evaluating |
763 @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
|
764 empty matrix. The truth value of an empty matrix is always @code{false} |
6632 | 765 so @code{[] && true} evaluates to @code{false} even though |
766 @code{all ([])} is @code{true}. | |
767 | |
3294 | 768 @item @var{boolean1} || @var{boolean2} |
769 @opindex || | |
770 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 771 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 772 If it is true, the result of the overall expression is 1. If it is |
773 false, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 774 scalar using the equivalent of the operation @code{all |
775 (@var{boolean1}(:))}. If it is true, the result of the overall expression | |
3294 | 776 is 1. Otherwise, the result of the overall expression is 0. |
6632 | 777 |
778 @strong{Warning:} the truth value of an empty matrix is always @code{false}, | |
779 see the previous list item for details. | |
3294 | 780 @end table |
781 | |
782 The fact that both operands may not be evaluated before determining the | |
783 overall truth value of the expression can be important. For example, in | |
784 the expression | |
785 | |
786 @example | |
787 a && b++ | |
788 @end example | |
789 | |
790 @noindent | |
791 the value of the variable @var{b} is only incremented if the variable | |
792 @var{a} is nonzero. | |
793 | |
794 This can be used to write somewhat more concise code. For example, it | |
795 is possible write | |
796 | |
797 @example | |
798 @group | |
799 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
800 if (nargin > 2 && ischar (c)) |
3294 | 801 @dots{} |
802 @end group | |
803 @end example | |
804 | |
805 @noindent | |
806 instead of having to use two @code{if} statements to avoid attempting to | |
807 evaluate an argument that doesn't exist. For example, without the | |
808 short-circuit feature, it would be necessary to write | |
809 | |
810 @example | |
811 @group | |
812 function f (a, b, c) | |
813 if (nargin > 2) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
814 if (ischar (c)) |
3294 | 815 @dots{} |
816 @end group | |
817 @end example | |
818 | |
6632 | 819 @noindent |
3294 | 820 Writing |
821 | |
822 @example | |
823 @group | |
824 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
825 if (nargin > 2 & ischar (c)) |
3294 | 826 @dots{} |
827 @end group | |
828 @end example | |
829 | |
830 @noindent | |
831 would result in an error if @code{f} were called with one or two | |
832 arguments because Octave would be forced to try to evaluate both of the | |
833 operands for the operator @samp{&}. | |
834 | |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10308
diff
changeset
|
835 The ternary operator (?:) is not supported in Octave. If short-circuiting is |
10308 | 836 not important, it can be replaced by the @code{ifelse} function. |
837 | |
838 @DOCSTRING(ifelse) | |
839 | |
4167 | 840 @node Assignment Ops |
3294 | 841 @section Assignment Expressions |
842 @cindex assignment expressions | |
843 @cindex assignment operators | |
844 @cindex operators, assignment | |
845 @cindex expressions, assignment | |
846 | |
847 @opindex = | |
848 | |
849 An @dfn{assignment} is an expression that stores a new value into a | |
850 variable. For example, the following expression assigns the value 1 to | |
851 the variable @code{z}: | |
852 | |
853 @example | |
854 z = 1 | |
855 @end example | |
856 | |
6632 | 857 @noindent |
3294 | 858 After this expression is executed, the variable @code{z} has the value 1. |
859 Whatever old value @code{z} had before the assignment is forgotten. | |
860 The @samp{=} sign is called an @dfn{assignment operator}. | |
861 | |
862 Assignments can store string values also. For example, the following | |
863 expression would store the value @code{"this food is good"} in the | |
864 variable @code{message}: | |
865 | |
866 @example | |
867 @group | |
868 thing = "food" | |
869 predicate = "good" | |
870 message = [ "this " , thing , " is " , predicate ] | |
871 @end group | |
872 @end example | |
873 | |
874 @noindent | |
875 (This also illustrates concatenation of strings.) | |
876 | |
877 @cindex side effect | |
878 Most operators (addition, concatenation, and so on) have no effect | |
879 except to compute a value. If you ignore the value, you might as well | |
880 not use the operator. An assignment operator is different. It does | |
881 produce a value, but even if you ignore the value, the assignment still | |
882 makes itself felt through the alteration of the variable. We call this | |
883 a @dfn{side effect}. | |
884 | |
885 @cindex lvalue | |
886 The left-hand operand of an assignment need not be a variable | |
887 (@pxref{Variables}). It can also be an element of a matrix | |
888 (@pxref{Index Expressions}) or a list of return values | |
889 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which | |
890 means they can appear on the left-hand side of an assignment operator. | |
891 The right-hand operand may be any expression. It produces the new value | |
892 which the assignment stores in the specified variable, matrix element, | |
893 or list of return values. | |
894 | |
895 It is important to note that variables do @emph{not} have permanent types. | |
896 The type of a variable is simply the type of whatever value it happens | |
897 to hold at the moment. In the following program fragment, the variable | |
898 @code{foo} has a numeric value at first, and a string value later on: | |
899 | |
900 @example | |
901 @group | |
902 octave:13> foo = 1 | |
903 foo = 1 | |
904 octave:13> foo = "bar" | |
905 foo = bar | |
906 @end group | |
907 @end example | |
908 | |
909 @noindent | |
910 When the second assignment gives @code{foo} a string value, the fact that | |
911 it previously had a numeric value is forgotten. | |
912 | |
913 Assignment of a scalar to an indexed matrix sets all of the elements | |
914 that are referenced by the indices to the scalar value. For example, if | |
915 @code{a} is a matrix with at least two columns, | |
916 | |
917 @example | |
918 @group | |
919 a(:, 2) = 5 | |
920 @end group | |
921 @end example | |
922 | |
923 @noindent | |
924 sets all the elements in the second column of @code{a} to 5. | |
925 | |
926 Assigning an empty matrix @samp{[]} works in most cases to allow you to | |
927 delete rows or columns of matrices and vectors. @xref{Empty Matrices}. | |
928 For example, given a 4 by 5 matrix @var{A}, the assignment | |
929 | |
930 @example | |
931 A (3, :) = [] | |
932 @end example | |
933 | |
934 @noindent | |
935 deletes the third row of @var{A}, and the assignment | |
936 | |
937 @example | |
938 A (:, 1:2:5) = [] | |
939 @end example | |
940 | |
941 @noindent | |
6672 | 942 deletes the first, third, and fifth columns. |
3294 | 943 |
944 An assignment is an expression, so it has a value. Thus, @code{z = 1} | |
945 as an expression has the value 1. One consequence of this is that you | |
946 can write multiple assignments together: | |
947 | |
948 @example | |
949 x = y = z = 0 | |
950 @end example | |
951 | |
952 @noindent | |
953 stores the value 0 in all three variables. It does this because the | |
954 value of @code{z = 0}, which is 0, is stored into @code{y}, and then | |
955 the value of @code{y = z = 0}, which is 0, is stored into @code{x}. | |
956 | |
957 This is also true of assignments to lists of values, so the following is | |
958 a valid expression | |
959 | |
960 @example | |
961 [a, b, c] = [u, s, v] = svd (a) | |
962 @end example | |
963 | |
964 @noindent | |
965 that is exactly equivalent to | |
966 | |
967 @example | |
968 @group | |
969 [u, s, v] = svd (a) | |
970 a = u | |
971 b = s | |
972 c = v | |
973 @end group | |
974 @end example | |
975 | |
976 In expressions like this, the number of values in each part of the | |
977 expression need not match. For example, the expression | |
978 | |
979 @example | |
980 [a, b] = [u, s, v] = svd (a) | |
981 @end example | |
982 | |
983 @noindent | |
984 is equivalent to | |
985 | |
986 @example | |
987 @group | |
988 [u, s, v] = svd (a) | |
989 a = u | |
990 b = s | |
991 @end group | |
992 @end example | |
993 | |
6632 | 994 @noindent |
995 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
|
996 not exceed the number of values on the right side. For example, the |
6632 | 997 following will produce an error. |
998 | |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
999 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
1000 @group |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7768
diff
changeset
|
1001 [a, b, c, d] = [u, s, v] = svd (a); |
7031 | 1002 @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
|
1003 @end group |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
1004 @end example |
6632 | 1005 |
10209
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1006 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
|
1007 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
|
1008 anywhere: |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1009 |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1010 @example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1011 @group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1012 [~, s, v] = svd (a); |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1013 @end group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1014 @end example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1015 |
10228 | 1016 This is cleaner and more memory efficient than using a dummy variable. |
1017 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
|
1018 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
|
1019 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
|
1020 |
6642 | 1021 @opindex += |
1022 A very common programming pattern is to increment an existing variable | |
1023 with a given value, like this | |
1024 | |
1025 @example | |
1026 a = a + 2; | |
1027 @end example | |
1028 | |
1029 @noindent | |
1030 This can be written in a clearer and more condensed form using the | |
1031 @code{+=} operator | |
1032 | |
1033 @example | |
1034 a += 2; | |
1035 @end example | |
1036 | |
1037 @noindent | |
1038 @opindex -= | |
1039 @opindex *= | |
1040 @opindex /= | |
1041 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
|
1042 multiplication (@code{*=}), and division (@code{/=}). An expression |
6642 | 1043 of the form |
1044 | |
1045 @example | |
1046 @var{expr1} @var{op}= @var{expr2} | |
1047 @end example | |
1048 | |
1049 @noindent | |
1050 is evaluated as | |
1051 | |
1052 @example | |
1053 @var{expr1} = (@var{expr1}) @var{op} (@var{expr2}) | |
1054 @end example | |
1055 | |
1056 @noindent | |
1057 where @var{op} can be either @code{+}, @code{-}, @code{*}, or @code{/}. | |
1058 So, the expression | |
1059 | |
1060 @example | |
1061 a *= b+1 | |
1062 @end example | |
1063 | |
1064 @noindent | |
1065 is evaluated as | |
1066 | |
1067 @example | |
1068 a = a * (b+1) | |
1069 @end example | |
1070 | |
1071 @noindent | |
1072 and @emph{not} | |
1073 | |
1074 @example | |
1075 a = a * b + 1 | |
1076 @end example | |
1077 | |
3294 | 1078 You can use an assignment anywhere an expression is called for. For |
1079 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1 | |
1080 and then test whether @code{x} equals 1. But this style tends to make | |
1081 programs hard to read. Except in a one-shot program, you should rewrite | |
1082 it to get rid of such nesting of assignments. This is never very hard. | |
1083 | |
1084 @cindex increment operator | |
1085 @cindex decrement operator | |
1086 @cindex operators, increment | |
1087 @cindex operators, decrement | |
1088 | |
4167 | 1089 @node Increment Ops |
3294 | 1090 @section Increment Operators |
1091 | |
1092 @emph{Increment operators} increase or decrease the value of a variable | |
1093 by 1. The operator to increment a variable is written as @samp{++}. It | |
1094 may be used to increment a variable either before or after taking its | |
1095 value. | |
1096 | |
1097 For example, to pre-increment the variable @var{x}, you would write | |
1098 @code{++@var{x}}. This would add one to @var{x} and then return the new | |
1099 value of @var{x} as the result of the expression. It is exactly the | |
1100 same as the expression @code{@var{x} = @var{x} + 1}. | |
1101 | |
1102 To post-increment a variable @var{x}, you would write @code{@var{x}++}. | |
1103 This adds one to the variable @var{x}, but returns the value that | |
1104 @var{x} had prior to incrementing it. For example, if @var{x} is equal | |
1105 to 2, the result of the expression @code{@var{x}++} is 2, and the new | |
1106 value of @var{x} is 3. | |
1107 | |
1108 For matrix and vector arguments, the increment and decrement operators | |
1109 work on each element of the operand. | |
1110 | |
1111 Here is a list of all the increment and decrement expressions. | |
1112 | |
1113 @table @code | |
1114 @item ++@var{x} | |
1115 @opindex ++ | |
1116 This expression increments the variable @var{x}. The value of the | |
1117 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1118 expression @code{@var{x} = @var{x} + 1}. | |
1119 | |
1120 @item --@var{x} | |
1121 @opindex @code{--} | |
1122 This expression decrements the variable @var{x}. The value of the | |
1123 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1124 expression @code{@var{x} = @var{x} - 1}. | |
1125 | |
1126 @item @var{x}++ | |
1127 @opindex ++ | |
1128 This expression causes the variable @var{x} to be incremented. The | |
1129 value of the expression is the @emph{old} value of @var{x}. | |
1130 | |
1131 @item @var{x}-- | |
1132 @opindex @code{--} | |
1133 This expression causes the variable @var{x} to be decremented. The | |
1134 value of the expression is the @emph{old} value of @var{x}. | |
1135 @end table | |
1136 | |
4167 | 1137 @node Operator Precedence |
3294 | 1138 @section Operator Precedence |
1139 @cindex operator precedence | |
1140 | |
1141 @dfn{Operator precedence} determines how operators are grouped, when | |
1142 different operators appear close by in one expression. For example, | |
1143 @samp{*} has higher precedence than @samp{+}. Thus, the expression | |
1144 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add | |
1145 @code{a} to the product (i.e., @code{a + (b * c)}). | |
1146 | |
1147 You can overrule the precedence of the operators by using parentheses. | |
1148 You can think of the precedence rules as saying where the parentheses | |
1149 are assumed if you do not write parentheses yourself. In fact, it is | |
1150 wise to use parentheses whenever you have an unusual combination of | |
1151 operators, because other people who read the program may not remember | |
1152 what the precedence is in this case. You might forget as well, and then | |
1153 you too could make a mistake. Explicit parentheses will help prevent | |
1154 any such mistake. | |
1155 | |
1156 When operators of equal precedence are used together, the leftmost | |
1157 operator groups first, except for the assignment and exponentiation | |
1158 operators, which group in the opposite order. Thus, the expression | |
1159 @code{a - b + c} groups as @code{(a - b) + c}, but the expression | |
1160 @code{a = b = c} groups as @code{a = (b = c)}. | |
1161 | |
1162 The precedence of prefix unary operators is important when another | |
1163 operator follows the operand. For example, @code{-x^2} means | |
1164 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}. | |
1165 | |
1166 Here is a table of the operators in Octave, in order of increasing | |
1167 precedence. | |
1168 | |
1169 @table @code | |
1170 @item statement separators | |
1171 @samp{;}, @samp{,}. | |
1172 | |
1173 @item assignment | |
6642 | 1174 @samp{=}, @samp{+=}, @samp{-=}, @samp{*=},@samp{/=}. This operator |
1175 groups right to left. | |
3294 | 1176 |
1177 @item logical "or" and "and" | |
1178 @samp{||}, @samp{&&}. | |
1179 | |
1180 @item element-wise "or" and "and" | |
1181 @samp{|}, @samp{&}. | |
1182 | |
1183 @item relational | |
1184 @samp{<}, @samp{<=}, @samp{==}, @samp{>=}, @samp{>}, @samp{!=}, | |
7594 | 1185 @samp{~=}. |
3294 | 1186 |
1187 @item colon | |
1188 @samp{:}. | |
1189 | |
1190 @item add, subtract | |
1191 @samp{+}, @samp{-}. | |
1192 | |
1193 @item multiply, divide | |
1194 @samp{*}, @samp{/}, @samp{\}, @samp{.\}, @samp{.*}, @samp{./}. | |
1195 | |
1196 @item transpose | |
1197 @samp{'}, @samp{.'} | |
1198 | |
1199 @item unary plus, minus, increment, decrement, and ``not'' | |
1200 @samp{+}, @samp{-}, @samp{++}, @samp{--}, @samp{!}, @samp{~}. | |
1201 | |
1202 @item exponentiation | |
1203 @samp{^}, @samp{**}, @samp{.^}, @samp{.**}. | |
1204 @end table |