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