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