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