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