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 @itemx @var{x} <> @var{y} |
|
584 @opindex != |
|
585 @opindex ~= |
|
586 @opindex <> |
|
587 True if @var{x} is not equal to @var{y}. |
|
588 @end table |
|
589 |
|
590 String comparisons may also be performed with the @code{strcmp} |
|
591 function, not with the comparison operators listed above. |
|
592 @xref{Strings}. |
|
593 |
6550
|
594 @DOCSTRING(isequal) |
|
595 |
|
596 @DOCSTRING(isequalwithequalnans) |
|
597 |
4167
|
598 @node Boolean Expressions |
3294
|
599 @section Boolean Expressions |
|
600 @cindex expressions, boolean |
|
601 @cindex boolean expressions |
|
602 @cindex expressions, logical |
|
603 @cindex logical expressions |
|
604 @cindex operators, boolean |
|
605 @cindex boolean operators |
|
606 @cindex logical operators |
|
607 @cindex operators, logical |
|
608 @cindex and operator |
|
609 @cindex or operator |
|
610 @cindex not operator |
|
611 |
|
612 @menu |
|
613 * Element-by-element Boolean Operators:: |
|
614 * Short-circuit Boolean Operators:: |
|
615 @end menu |
|
616 |
4167
|
617 @node Element-by-element Boolean Operators |
3294
|
618 @subsection Element-by-element Boolean Operators |
|
619 @cindex element-by-element evaluation |
|
620 |
|
621 An @dfn{element-by-element boolean expression} is a combination of |
|
622 comparison expressions using the boolean |
|
623 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}), |
|
624 along with parentheses to control nesting. The truth of the boolean |
|
625 expression is computed by combining the truth values of the |
|
626 corresponding elements of the component expressions. A value is |
|
627 considered to be false if it is zero, and true otherwise. |
|
628 |
|
629 Element-by-element boolean expressions can be used wherever comparison |
|
630 expressions can be used. They can be used in @code{if} and @code{while} |
|
631 statements. However, if a matrix value used as the condition in an |
|
632 @code{if} or @code{while} statement is only true if @emph{all} of its |
|
633 elements are nonzero. |
|
634 |
|
635 Like comparison operations, each element of an element-by-element |
|
636 boolean expression also has a numeric value (1 if true, 0 if false) that |
|
637 comes into play if the result of the boolean expression is stored in a |
|
638 variable, or used in arithmetic. |
|
639 |
|
640 Here are descriptions of the three element-by-element boolean operators. |
|
641 |
|
642 @table @code |
|
643 @item @var{boolean1} & @var{boolean2} |
|
644 @opindex & |
|
645 Elements of the result are true if both corresponding elements of |
|
646 @var{boolean1} and @var{boolean2} are true. |
|
647 |
|
648 @item @var{boolean1} | @var{boolean2} |
|
649 @opindex | |
|
650 Elements of the result are true if either of the corresponding elements |
|
651 of @var{boolean1} or @var{boolean2} is true. |
|
652 |
|
653 @item ! @var{boolean} |
|
654 @itemx ~ @var{boolean} |
|
655 @opindex ~ |
|
656 @opindex ! |
|
657 Each element of the result is true if the corresponding element of |
|
658 @var{boolean} is false. |
|
659 @end table |
|
660 |
|
661 For matrix operands, these operators work on an element-by-element |
|
662 basis. For example, the expression |
|
663 |
|
664 @example |
|
665 [1, 0; 0, 1] & [1, 0; 2, 3] |
|
666 @end example |
|
667 |
|
668 @noindent |
|
669 returns a two by two identity matrix. |
|
670 |
|
671 For the binary operators, the dimensions of the operands must conform if |
|
672 both are matrices. If one of the operands is a scalar and the other a |
|
673 matrix, the operator is applied to the scalar and each element of the |
|
674 matrix. |
|
675 |
|
676 For the binary element-by-element boolean operators, both subexpressions |
|
677 @var{boolean1} and @var{boolean2} are evaluated before computing the |
|
678 result. This can make a difference when the expressions have side |
|
679 effects. For example, in the expression |
|
680 |
|
681 @example |
|
682 a & b++ |
|
683 @end example |
|
684 |
|
685 @noindent |
|
686 the value of the variable @var{b} is incremented even if the variable |
|
687 @var{a} is zero. |
|
688 |
|
689 This behavior is necessary for the boolean operators to work as |
|
690 described for matrix-valued operands. |
|
691 |
4167
|
692 @node Short-circuit Boolean Operators |
3294
|
693 @subsection Short-circuit Boolean Operators |
|
694 @cindex short-circuit evaluation |
|
695 |
|
696 Combined with the implicit conversion to scalar values in @code{if} and |
|
697 @code{while} conditions, Octave's element-by-element boolean operators |
|
698 are often sufficient for performing most logical operations. However, |
|
699 it is sometimes desirable to stop evaluating a boolean expression as |
|
700 soon as the overall truth value can be determined. Octave's |
|
701 @dfn{short-circuit} boolean operators work this way. |
|
702 |
|
703 @table @code |
|
704 @item @var{boolean1} && @var{boolean2} |
|
705 @opindex && |
|
706 The expression @var{boolean1} is evaluated and converted to a scalar |
6632
|
707 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294
|
708 If it is false, the result of the overall expression is 0. If it is |
|
709 true, the expression @var{boolean2} is evaluated and converted to a |
6632
|
710 scalar using the equivalent of the operation @code{all |
|
711 (@var{boolean1}(:))}. If it is true, the result of the overall expression |
3294
|
712 is 1. Otherwise, the result of the overall expression is 0. |
|
713 |
6632
|
714 @strong{Warning:} there is one exception to the rule of evaluating |
|
715 @code{all (@var{boolean1}(:))}, which is when @code{boolean1} is the |
|
716 empty matrix. The truth value of an empty matrix is always @code{false} |
|
717 so @code{[] && true} evaluates to @code{false} even though |
|
718 @code{all ([])} is @code{true}. |
|
719 |
3294
|
720 @item @var{boolean1} || @var{boolean2} |
|
721 @opindex || |
|
722 The expression @var{boolean1} is evaluated and converted to a scalar |
6632
|
723 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294
|
724 If it is true, the result of the overall expression is 1. If it is |
|
725 false, the expression @var{boolean2} is evaluated and converted to a |
6632
|
726 scalar using the equivalent of the operation @code{all |
|
727 (@var{boolean1}(:))}. If it is true, the result of the overall expression |
3294
|
728 is 1. Otherwise, the result of the overall expression is 0. |
6632
|
729 |
|
730 @strong{Warning:} the truth value of an empty matrix is always @code{false}, |
|
731 see the previous list item for details. |
3294
|
732 @end table |
|
733 |
|
734 The fact that both operands may not be evaluated before determining the |
|
735 overall truth value of the expression can be important. For example, in |
|
736 the expression |
|
737 |
|
738 @example |
|
739 a && b++ |
|
740 @end example |
|
741 |
|
742 @noindent |
|
743 the value of the variable @var{b} is only incremented if the variable |
|
744 @var{a} is nonzero. |
|
745 |
|
746 This can be used to write somewhat more concise code. For example, it |
|
747 is possible write |
|
748 |
|
749 @example |
|
750 @group |
|
751 function f (a, b, c) |
|
752 if (nargin > 2 && isstr (c)) |
|
753 @dots{} |
|
754 @end group |
|
755 @end example |
|
756 |
|
757 @noindent |
|
758 instead of having to use two @code{if} statements to avoid attempting to |
|
759 evaluate an argument that doesn't exist. For example, without the |
|
760 short-circuit feature, it would be necessary to write |
|
761 |
|
762 @example |
|
763 @group |
|
764 function f (a, b, c) |
|
765 if (nargin > 2) |
|
766 if (isstr (c)) |
|
767 @dots{} |
|
768 @end group |
|
769 @end example |
|
770 |
6632
|
771 @noindent |
3294
|
772 Writing |
|
773 |
|
774 @example |
|
775 @group |
|
776 function f (a, b, c) |
|
777 if (nargin > 2 & isstr (c)) |
|
778 @dots{} |
|
779 @end group |
|
780 @end example |
|
781 |
|
782 @noindent |
|
783 would result in an error if @code{f} were called with one or two |
|
784 arguments because Octave would be forced to try to evaluate both of the |
|
785 operands for the operator @samp{&}. |
|
786 |
4167
|
787 @node Assignment Ops |
3294
|
788 @section Assignment Expressions |
|
789 @cindex assignment expressions |
|
790 @cindex assignment operators |
|
791 @cindex operators, assignment |
|
792 @cindex expressions, assignment |
|
793 |
|
794 @opindex = |
|
795 |
|
796 An @dfn{assignment} is an expression that stores a new value into a |
|
797 variable. For example, the following expression assigns the value 1 to |
|
798 the variable @code{z}: |
|
799 |
|
800 @example |
|
801 z = 1 |
|
802 @end example |
|
803 |
6632
|
804 @noindent |
3294
|
805 After this expression is executed, the variable @code{z} has the value 1. |
|
806 Whatever old value @code{z} had before the assignment is forgotten. |
|
807 The @samp{=} sign is called an @dfn{assignment operator}. |
|
808 |
|
809 Assignments can store string values also. For example, the following |
|
810 expression would store the value @code{"this food is good"} in the |
|
811 variable @code{message}: |
|
812 |
|
813 @example |
|
814 @group |
|
815 thing = "food" |
|
816 predicate = "good" |
|
817 message = [ "this " , thing , " is " , predicate ] |
|
818 @end group |
|
819 @end example |
|
820 |
|
821 @noindent |
|
822 (This also illustrates concatenation of strings.) |
|
823 |
|
824 @cindex side effect |
|
825 Most operators (addition, concatenation, and so on) have no effect |
|
826 except to compute a value. If you ignore the value, you might as well |
|
827 not use the operator. An assignment operator is different. It does |
|
828 produce a value, but even if you ignore the value, the assignment still |
|
829 makes itself felt through the alteration of the variable. We call this |
|
830 a @dfn{side effect}. |
|
831 |
|
832 @cindex lvalue |
|
833 The left-hand operand of an assignment need not be a variable |
|
834 (@pxref{Variables}). It can also be an element of a matrix |
|
835 (@pxref{Index Expressions}) or a list of return values |
|
836 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which |
|
837 means they can appear on the left-hand side of an assignment operator. |
|
838 The right-hand operand may be any expression. It produces the new value |
|
839 which the assignment stores in the specified variable, matrix element, |
|
840 or list of return values. |
|
841 |
|
842 It is important to note that variables do @emph{not} have permanent types. |
|
843 The type of a variable is simply the type of whatever value it happens |
|
844 to hold at the moment. In the following program fragment, the variable |
|
845 @code{foo} has a numeric value at first, and a string value later on: |
|
846 |
|
847 @example |
|
848 @group |
|
849 octave:13> foo = 1 |
|
850 foo = 1 |
|
851 octave:13> foo = "bar" |
|
852 foo = bar |
|
853 @end group |
|
854 @end example |
|
855 |
|
856 @noindent |
|
857 When the second assignment gives @code{foo} a string value, the fact that |
|
858 it previously had a numeric value is forgotten. |
|
859 |
|
860 Assignment of a scalar to an indexed matrix sets all of the elements |
|
861 that are referenced by the indices to the scalar value. For example, if |
|
862 @code{a} is a matrix with at least two columns, |
|
863 |
|
864 @example |
|
865 @group |
|
866 a(:, 2) = 5 |
|
867 @end group |
|
868 @end example |
|
869 |
|
870 @noindent |
|
871 sets all the elements in the second column of @code{a} to 5. |
|
872 |
|
873 Assigning an empty matrix @samp{[]} works in most cases to allow you to |
|
874 delete rows or columns of matrices and vectors. @xref{Empty Matrices}. |
|
875 For example, given a 4 by 5 matrix @var{A}, the assignment |
|
876 |
|
877 @example |
|
878 A (3, :) = [] |
|
879 @end example |
|
880 |
|
881 @noindent |
|
882 deletes the third row of @var{A}, and the assignment |
|
883 |
|
884 @example |
|
885 A (:, 1:2:5) = [] |
|
886 @end example |
|
887 |
|
888 @noindent |
6672
|
889 deletes the first, third, and fifth columns. |
3294
|
890 |
|
891 An assignment is an expression, so it has a value. Thus, @code{z = 1} |
|
892 as an expression has the value 1. One consequence of this is that you |
|
893 can write multiple assignments together: |
|
894 |
|
895 @example |
|
896 x = y = z = 0 |
|
897 @end example |
|
898 |
|
899 @noindent |
|
900 stores the value 0 in all three variables. It does this because the |
|
901 value of @code{z = 0}, which is 0, is stored into @code{y}, and then |
|
902 the value of @code{y = z = 0}, which is 0, is stored into @code{x}. |
|
903 |
|
904 This is also true of assignments to lists of values, so the following is |
|
905 a valid expression |
|
906 |
|
907 @example |
|
908 [a, b, c] = [u, s, v] = svd (a) |
|
909 @end example |
|
910 |
|
911 @noindent |
|
912 that is exactly equivalent to |
|
913 |
|
914 @example |
|
915 @group |
|
916 [u, s, v] = svd (a) |
|
917 a = u |
|
918 b = s |
|
919 c = v |
|
920 @end group |
|
921 @end example |
|
922 |
|
923 In expressions like this, the number of values in each part of the |
|
924 expression need not match. For example, the expression |
|
925 |
|
926 @example |
|
927 [a, b] = [u, s, v] = svd (a) |
|
928 @end example |
|
929 |
|
930 @noindent |
|
931 is equivalent to |
|
932 |
|
933 @example |
|
934 @group |
|
935 [u, s, v] = svd (a) |
|
936 a = u |
|
937 b = s |
|
938 @end group |
|
939 @end example |
|
940 |
6632
|
941 @noindent |
|
942 The number of values on the left side of the expression can, however, |
|
943 not exceed the number of values on the right side. For example, the |
|
944 following will produce an error. |
|
945 |
|
946 @example |
|
947 [a, b, c, d] = [u, s, v] = svd (a) |
|
948 @print{} error: element number 4 undefined in return list |
|
949 error: evaluating assignment expression near line 8, column 15 |
|
950 @end example |
|
951 |
6642
|
952 @opindex += |
|
953 A very common programming pattern is to increment an existing variable |
|
954 with a given value, like this |
|
955 |
|
956 @example |
|
957 a = a + 2; |
|
958 @end example |
|
959 |
|
960 @noindent |
|
961 This can be written in a clearer and more condensed form using the |
|
962 @code{+=} operator |
|
963 |
|
964 @example |
|
965 a += 2; |
|
966 @end example |
|
967 |
|
968 @noindent |
|
969 @opindex -= |
|
970 @opindex *= |
|
971 @opindex /= |
|
972 Similar operators also exist for subtraction (@code{-=}), |
|
973 multiplication (@code{*=}), and division (@code{/=}). An expression |
|
974 of the form |
|
975 |
|
976 @example |
|
977 @var{expr1} @var{op}= @var{expr2} |
|
978 @end example |
|
979 |
|
980 @noindent |
|
981 is evaluated as |
|
982 |
|
983 @example |
|
984 @var{expr1} = (@var{expr1}) @var{op} (@var{expr2}) |
|
985 @end example |
|
986 |
|
987 @noindent |
|
988 where @var{op} can be either @code{+}, @code{-}, @code{*}, or @code{/}. |
|
989 So, the expression |
|
990 |
|
991 @example |
|
992 a *= b+1 |
|
993 @end example |
|
994 |
|
995 @noindent |
|
996 is evaluated as |
|
997 |
|
998 @example |
|
999 a = a * (b+1) |
|
1000 @end example |
|
1001 |
|
1002 @noindent |
|
1003 and @emph{not} |
|
1004 |
|
1005 @example |
|
1006 a = a * b + 1 |
|
1007 @end example |
|
1008 |
3294
|
1009 You can use an assignment anywhere an expression is called for. For |
|
1010 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1 |
|
1011 and then test whether @code{x} equals 1. But this style tends to make |
|
1012 programs hard to read. Except in a one-shot program, you should rewrite |
|
1013 it to get rid of such nesting of assignments. This is never very hard. |
|
1014 |
6550
|
1015 @DOCSTRING(subsasgn) |
|
1016 |
3294
|
1017 @cindex increment operator |
|
1018 @cindex decrement operator |
|
1019 @cindex operators, increment |
|
1020 @cindex operators, decrement |
|
1021 |
4167
|
1022 @node Increment Ops |
3294
|
1023 @section Increment Operators |
|
1024 |
|
1025 @emph{Increment operators} increase or decrease the value of a variable |
|
1026 by 1. The operator to increment a variable is written as @samp{++}. It |
|
1027 may be used to increment a variable either before or after taking its |
|
1028 value. |
|
1029 |
|
1030 For example, to pre-increment the variable @var{x}, you would write |
|
1031 @code{++@var{x}}. This would add one to @var{x} and then return the new |
|
1032 value of @var{x} as the result of the expression. It is exactly the |
|
1033 same as the expression @code{@var{x} = @var{x} + 1}. |
|
1034 |
|
1035 To post-increment a variable @var{x}, you would write @code{@var{x}++}. |
|
1036 This adds one to the variable @var{x}, but returns the value that |
|
1037 @var{x} had prior to incrementing it. For example, if @var{x} is equal |
|
1038 to 2, the result of the expression @code{@var{x}++} is 2, and the new |
|
1039 value of @var{x} is 3. |
|
1040 |
|
1041 For matrix and vector arguments, the increment and decrement operators |
|
1042 work on each element of the operand. |
|
1043 |
|
1044 Here is a list of all the increment and decrement expressions. |
|
1045 |
|
1046 @table @code |
|
1047 @item ++@var{x} |
|
1048 @opindex ++ |
|
1049 This expression increments 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 @code{--} |
|
1055 This expression decrements the variable @var{x}. The value of the |
|
1056 expression is the @emph{new} value of @var{x}. It is equivalent to the |
|
1057 expression @code{@var{x} = @var{x} - 1}. |
|
1058 |
|
1059 @item @var{x}++ |
|
1060 @opindex ++ |
|
1061 This expression causes the variable @var{x} to be incremented. The |
|
1062 value of the expression is the @emph{old} value of @var{x}. |
|
1063 |
|
1064 @item @var{x}-- |
|
1065 @opindex @code{--} |
|
1066 This expression causes the variable @var{x} to be decremented. The |
|
1067 value of the expression is the @emph{old} value of @var{x}. |
|
1068 @end table |
|
1069 |
4167
|
1070 @node Operator Precedence |
3294
|
1071 @section Operator Precedence |
|
1072 @cindex operator precedence |
|
1073 |
|
1074 @dfn{Operator precedence} determines how operators are grouped, when |
|
1075 different operators appear close by in one expression. For example, |
|
1076 @samp{*} has higher precedence than @samp{+}. Thus, the expression |
|
1077 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add |
|
1078 @code{a} to the product (i.e., @code{a + (b * c)}). |
|
1079 |
|
1080 You can overrule the precedence of the operators by using parentheses. |
|
1081 You can think of the precedence rules as saying where the parentheses |
|
1082 are assumed if you do not write parentheses yourself. In fact, it is |
|
1083 wise to use parentheses whenever you have an unusual combination of |
|
1084 operators, because other people who read the program may not remember |
|
1085 what the precedence is in this case. You might forget as well, and then |
|
1086 you too could make a mistake. Explicit parentheses will help prevent |
|
1087 any such mistake. |
|
1088 |
|
1089 When operators of equal precedence are used together, the leftmost |
|
1090 operator groups first, except for the assignment and exponentiation |
|
1091 operators, which group in the opposite order. Thus, the expression |
|
1092 @code{a - b + c} groups as @code{(a - b) + c}, but the expression |
|
1093 @code{a = b = c} groups as @code{a = (b = c)}. |
|
1094 |
|
1095 The precedence of prefix unary operators is important when another |
|
1096 operator follows the operand. For example, @code{-x^2} means |
|
1097 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}. |
|
1098 |
|
1099 Here is a table of the operators in Octave, in order of increasing |
|
1100 precedence. |
|
1101 |
|
1102 @table @code |
|
1103 @item statement separators |
|
1104 @samp{;}, @samp{,}. |
|
1105 |
|
1106 @item assignment |
6642
|
1107 @samp{=}, @samp{+=}, @samp{-=}, @samp{*=},@samp{/=}. This operator |
|
1108 groups right to left. |
3294
|
1109 |
|
1110 @item logical "or" and "and" |
|
1111 @samp{||}, @samp{&&}. |
|
1112 |
|
1113 @item element-wise "or" and "and" |
|
1114 @samp{|}, @samp{&}. |
|
1115 |
|
1116 @item relational |
|
1117 @samp{<}, @samp{<=}, @samp{==}, @samp{>=}, @samp{>}, @samp{!=}, |
|
1118 @samp{~=}, @samp{<>}. |
|
1119 |
|
1120 @item colon |
|
1121 @samp{:}. |
|
1122 |
|
1123 @item add, subtract |
|
1124 @samp{+}, @samp{-}. |
|
1125 |
|
1126 @item multiply, divide |
|
1127 @samp{*}, @samp{/}, @samp{\}, @samp{.\}, @samp{.*}, @samp{./}. |
|
1128 |
|
1129 @item transpose |
|
1130 @samp{'}, @samp{.'} |
|
1131 |
|
1132 @item unary plus, minus, increment, decrement, and ``not'' |
|
1133 @samp{+}, @samp{-}, @samp{++}, @samp{--}, @samp{!}, @samp{~}. |
|
1134 |
|
1135 @item exponentiation |
|
1136 @samp{^}, @samp{**}, @samp{.^}, @samp{.**}. |
|
1137 @end table |