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