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