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