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