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