Mercurial > octave
annotate doc/interpreter/numbers.txi @ 7989:23c248d415b5
Various doc fixes. Readd cellidx
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 29 Jul 2008 17:01:43 +0200 |
parents | bbaa5d7d0143 |
children | 30629059b72d |
rev | line source |
---|---|
6778 | 1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
5 @c Octave is free software; you can redistribute it and/or modify it | |
6 @c under the terms of the GNU General Public License as published by the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c for more details. | |
14 @c | |
15 @c You should have received a copy of the GNU General Public License | |
16 @c along with Octave; see the file COPYING. If not, see | |
17 @c <http://www.gnu.org/licenses/>. | |
3294 | 18 |
4167 | 19 @node Numeric Data Types |
3294 | 20 @chapter Numeric Data Types |
21 @cindex numeric constant | |
22 @cindex numeric value | |
23 | |
24 A @dfn{numeric constant} may be a scalar, a vector, or a matrix, and it | |
25 may contain complex values. | |
26 | |
27 The simplest form of a numeric constant, a scalar, is a single number | |
28 that can be an integer, a decimal fraction, a number in scientific | |
6620 | 29 (exponential) notation, or a complex number. Note that by default numeric |
3294 | 30 constants are represented within Octave in double-precision floating |
31 point format (complex constants are stored as pairs of double-precision | |
6620 | 32 floating point values). It is however possible to represent real |
33 integers as described in @ref{Integer Data Types}. Here are some examples | |
34 of real-valued numeric constants, which all have the same value: | |
3294 | 35 |
36 @example | |
37 @group | |
38 105 | |
39 1.05e+2 | |
40 1050e-1 | |
41 @end group | |
42 @end example | |
43 | |
44 To specify complex constants, you can write an expression of the form | |
45 | |
46 @example | |
47 @group | |
48 3 + 4i | |
49 3.0 + 4.0i | |
50 0.3e1 + 40e-1i | |
51 @end group | |
52 @end example | |
53 | |
6620 | 54 @noindent |
3294 | 55 all of which are equivalent. The letter @samp{i} in the previous example |
56 stands for the pure imaginary constant, defined as | |
57 @iftex | |
58 @tex | |
59 $\sqrt{-1}$. | |
60 @end tex | |
61 @end iftex | |
6620 | 62 @ifnottex |
3294 | 63 @code{sqrt (-1)}. |
6620 | 64 @end ifnottex |
3294 | 65 |
66 For Octave to recognize a value as the imaginary part of a complex | |
67 constant, a space must not appear between the number and the @samp{i}. | |
68 If it does, Octave will print an error message, like this: | |
69 | |
70 @example | |
71 @group | |
72 octave:13> 3 + 4 i | |
73 | |
74 parse error: | |
75 | |
76 3 + 4 i | |
77 ^ | |
78 @end group | |
79 @end example | |
80 | |
6620 | 81 @noindent |
3294 | 82 You may also use @samp{j}, @samp{I}, or @samp{J} in place of the |
83 @samp{i} above. All four forms are equivalent. | |
84 | |
6549 | 85 @DOCSTRING(double) |
86 | |
87 @DOCSTRING(complex) | |
88 | |
3294 | 89 @menu |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
90 * Matrices:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
91 * Ranges:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
92 * Single Precision Data Types:: |
6549 | 93 * Integer Data Types:: |
6781 | 94 * Bit Manipulations:: |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
95 * Logical Values:: |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
96 * Promotion and Demotion of Data Types:: |
3294 | 97 * Predicates for Numeric Objects:: |
98 @end menu | |
99 | |
4167 | 100 @node Matrices |
3294 | 101 @section Matrices |
102 @cindex matrices | |
103 | |
104 @opindex [ | |
105 @opindex ] | |
106 @opindex ; | |
107 @opindex , | |
108 | |
109 It is easy to define a matrix of values in Octave. The size of the | |
110 matrix is determined automatically, so it is not necessary to explicitly | |
111 state the dimensions. The expression | |
112 | |
113 @example | |
114 a = [1, 2; 3, 4] | |
115 @end example | |
116 | |
117 @noindent | |
118 results in the matrix | |
119 @iftex | |
120 @tex | |
121 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ | |
122 @end tex | |
123 @end iftex | |
6620 | 124 @ifnottex |
3294 | 125 |
126 @example | |
127 @group | |
128 | |
129 / \ | |
130 | 1 2 | | |
131 a = | | | |
132 | 3 4 | | |
133 \ / | |
134 | |
135 @end group | |
136 @end example | |
6620 | 137 @end ifnottex |
3294 | 138 |
139 Elements of a matrix may be arbitrary expressions, provided that the | |
140 dimensions all make sense when combining the various pieces. For | |
141 example, given the above matrix, the expression | |
142 | |
143 @example | |
144 [ a, a ] | |
145 @end example | |
146 | |
147 @noindent | |
148 produces the matrix | |
149 | |
150 @example | |
151 @group | |
152 ans = | |
153 | |
154 1 2 1 2 | |
155 3 4 3 4 | |
156 @end group | |
157 @end example | |
158 | |
159 @noindent | |
160 but the expression | |
161 | |
162 @example | |
163 [ a, 1 ] | |
164 @end example | |
165 | |
166 @noindent | |
167 produces the error | |
168 | |
169 @example | |
170 error: number of rows must match near line 13, column 6 | |
171 @end example | |
172 | |
173 @noindent | |
174 (assuming that this expression was entered as the first thing on line | |
175 13, of course). | |
176 | |
177 Inside the square brackets that delimit a matrix expression, Octave | |
178 looks at the surrounding context to determine whether spaces and newline | |
179 characters should be converted into element and row separators, or | |
4476 | 180 simply ignored, so an expression like |
3294 | 181 |
182 @example | |
183 @group | |
184 a = [ 1 2 | |
185 3 4 ] | |
186 @end group | |
187 @end example | |
188 | |
189 @noindent | |
190 will work. However, some possible sources of confusion remain. For | |
191 example, in the expression | |
192 | |
193 @example | |
194 [ 1 - 1 ] | |
195 @end example | |
196 | |
197 @noindent | |
198 the @samp{-} is treated as a binary operator and the result is the | |
199 scalar 0, but in the expression | |
200 | |
201 @example | |
202 [ 1 -1 ] | |
203 @end example | |
204 | |
205 @noindent | |
206 the @samp{-} is treated as a unary operator and the result is the | |
4476 | 207 vector @code{[ 1, -1 ]}. Similarly, the expression |
208 | |
209 @example | |
210 [ sin (pi) ] | |
211 @end example | |
212 | |
213 @noindent | |
214 will be parsed as | |
215 | |
216 @example | |
217 [ sin, (pi) ] | |
218 @end example | |
3294 | 219 |
4476 | 220 @noindent |
221 and will result in an error since the @code{sin} function will be | |
222 called with no arguments. To get around this, you must omit the space | |
223 between @code{sin} and the opening parenthesis, or enclose the | |
224 expression in a set of parentheses: | |
225 | |
226 @example | |
227 [ (sin (pi)) ] | |
228 @end example | |
229 | |
230 Whitespace surrounding the single quote character (@samp{'}, used as a | |
231 transpose operator and for delimiting character strings) can also cause | |
232 confusion. Given @code{a = 1}, the expression | |
3294 | 233 |
234 @example | |
235 [ 1 a' ] | |
236 @end example | |
237 | |
238 @noindent | |
4476 | 239 results in the single quote character being treated as a |
3294 | 240 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
241 expression | |
242 | |
243 @example | |
244 [ 1 a ' ] | |
245 @end example | |
246 | |
247 @noindent | |
248 produces the error message | |
249 | |
250 @example | |
251 error: unterminated string constant | |
252 @end example | |
253 | |
254 @noindent | |
6620 | 255 because not doing so would cause trouble when parsing the valid expression |
3294 | 256 |
257 @example | |
258 [ a 'foo' ] | |
259 @end example | |
260 | |
261 For clarity, it is probably best to always use commas and semicolons to | |
4476 | 262 separate matrix elements and rows. |
3294 | 263 |
264 When you type a matrix or the name of a variable whose value is a | |
265 matrix, Octave responds by printing the matrix in with neatly aligned | |
266 rows and columns. If the rows of the matrix are too large to fit on the | |
267 screen, Octave splits the matrix and displays a header before each | |
268 section to indicate which columns are being displayed. You can use the | |
269 following variables to control the format of the output. | |
270 | |
3321 | 271 @DOCSTRING(output_max_field_width) |
3294 | 272 |
3321 | 273 @DOCSTRING(output_precision) |
3294 | 274 |
275 It is possible to achieve a wide range of output styles by using | |
276 different values of @code{output_precision} and | |
277 @code{output_max_field_width}. Reasonable combinations can be set using | |
278 the @code{format} function. @xref{Basic Input and Output}. | |
279 | |
3321 | 280 @DOCSTRING(split_long_rows) |
3294 | 281 |
282 Octave automatically switches to scientific notation when values become | |
283 very large or very small. This guarantees that you will see several | |
284 significant figures for every value in a matrix. If you would prefer to | |
285 see all values in a matrix printed in a fixed point format, you can set | |
286 the built-in variable @code{fixed_point_format} to a nonzero value. But | |
287 doing so is not recommended, because it can produce output that can | |
288 easily be misinterpreted. | |
289 | |
3321 | 290 @DOCSTRING(fixed_point_format) |
3294 | 291 |
292 @menu | |
293 * Empty Matrices:: | |
294 @end menu | |
295 | |
4167 | 296 @node Empty Matrices |
3294 | 297 @subsection Empty Matrices |
298 | |
299 A matrix may have one or both dimensions zero, and operations on empty | |
300 matrices are handled as described by Carl de Boor in @cite{An Empty | |
301 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. | |
302 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty | |
303 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, | |
304 Number 5, May 1993. | |
305 @iftex | |
306 @tex | |
307 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, | |
308 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or | |
309 both dimensions equal to zero), the following are true: | |
310 $$ | |
311 \eqalign{% | |
312 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr | |
313 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr | |
314 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr | |
315 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr | |
316 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} | |
317 $$ | |
318 @end tex | |
319 @end iftex | |
6620 | 320 @ifnottex |
3294 | 321 Briefly, given a scalar @var{s}, an @var{m} by |
322 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix | |
323 @code{[](mxn)} (with either one or both dimensions equal to zero), the | |
324 following are true: | |
325 | |
326 @example | |
327 @group | |
328 s * [](mxn) = [](mxn) * s = [](mxn) | |
329 | |
330 [](mxn) + [](mxn) = [](mxn) | |
331 | |
332 [](0xm) * M(mxn) = [](0xn) | |
333 | |
334 M(mxn) * [](nx0) = [](mx0) | |
335 | |
336 [](mx0) * [](0xn) = 0(mxn) | |
337 @end group | |
338 @end example | |
6620 | 339 @end ifnottex |
3294 | 340 |
341 By default, dimensions of the empty matrix are printed along with the | |
342 empty matrix symbol, @samp{[]}. The built-in variable | |
343 @code{print_empty_dimensions} controls this behavior. | |
344 | |
3321 | 345 @DOCSTRING(print_empty_dimensions) |
3294 | 346 |
347 Empty matrices may also be used in assignment statements as a convenient | |
348 way to delete rows or columns of matrices. | |
349 @xref{Assignment Ops, ,Assignment Expressions}. | |
350 | |
351 When Octave parses a matrix expression, it examines the elements of the | |
352 list to determine whether they are all constants. If they are, it | |
353 replaces the list with a single matrix constant. | |
354 | |
4167 | 355 @node Ranges |
3294 | 356 @section Ranges |
357 @cindex range expressions | |
358 @cindex expression, range | |
359 | |
3920 | 360 @opindex colon |
3294 | 361 |
362 A @dfn{range} is a convenient way to write a row vector with evenly | |
363 spaced elements. A range expression is defined by the value of the first | |
364 element in the range, an optional value for the increment between | |
365 elements, and a maximum value which the elements of the range will not | |
366 exceed. The base, increment, and limit are separated by colons (the | |
367 @samp{:} character) and may contain any arithmetic expressions and | |
368 function calls. If the increment is omitted, it is assumed to be 1. | |
369 For example, the range | |
370 | |
371 @example | |
372 1 : 5 | |
373 @end example | |
374 | |
375 @noindent | |
376 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range | |
377 | |
378 @example | |
379 1 : 3 : 5 | |
380 @end example | |
381 | |
382 @noindent | |
383 defines the set of values @samp{[ 1, 4 ]}. | |
384 | |
385 Although a range constant specifies a row vector, Octave does @emph{not} | |
386 convert range constants to vectors unless it is necessary to do so. | |
387 This allows you to write a constant like @samp{1 : 10000} without using | |
388 80,000 bytes of storage on a typical 32-bit workstation. | |
389 | |
390 Note that the upper (or lower, if the increment is negative) bound on | |
391 the range is not always included in the set of values, and that ranges | |
392 defined by floating point values can produce surprising results because | |
393 Octave uses floating point arithmetic to compute the values in the | |
394 range. If it is important to include the endpoints of a range and the | |
395 number of elements is known, you should use the @code{linspace} function | |
396 instead (@pxref{Special Utility Matrices}). | |
397 | |
398 When Octave parses a range expression, it examines the elements of the | |
399 expression to determine whether they are all constants. If they are, it | |
400 replaces the range expression with a single range constant. | |
401 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
402 @node Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
403 @section Single Precision Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
404 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
405 Octave includes support for single precision data types, and most of the |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
406 functions in Octave accept single precision values and return single |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
407 precion answers. A single precision variable is created with the |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
408 @code{sample} function. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
409 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
410 @DOCSTRING(single) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
411 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
412 for example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
413 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
414 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
415 sngl = single (rand (2, 2)) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
416 @result{} sngl = 0.37569 0.92982 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
417 0.11962 0.50876 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
418 class (sngl) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
419 @result{} single |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
420 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
421 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
422 Many functions can also return single precision values directly. For |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
423 example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
424 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
425 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
426 ones (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
427 zeros (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
428 eye (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
429 rand (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
430 NaN (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
431 NA (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
432 Inf (2, 2, "single") |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
433 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
434 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
435 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
436 will all return single precision matrices. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
437 |
6549 | 438 @node Integer Data Types |
439 @section Integer Data Types | |
440 | |
6620 | 441 Octave supports integer matrices as an alternative to using double |
442 precision. It is possible to use both signed and unsigned integers | |
443 represented by 8, 16, 32, or 64 bits. It should be noted that most | |
444 computations require floating point data, meaning that integers will | |
445 often change type when involved in numeric computations. For this | |
446 reason integers are most often used to store data, and not for | |
447 calculations. | |
448 | |
449 In general most integer matrices are created by casting | |
450 existing matrices to integers. The following example shows how to cast | |
451 a matrix into 32 bit integers. | |
452 | |
453 @example | |
454 float = rand (2, 2) | |
455 @result{} float = 0.37569 0.92982 | |
456 0.11962 0.50876 | |
457 integer = int32 (float) | |
458 @result{} integer = 0 1 | |
459 0 1 | |
460 @end example | |
461 | |
462 @noindent | |
463 As can be seen, floating point values are rounded to the nearest integer | |
464 when converted. | |
465 | |
6549 | 466 @DOCSTRING(isinteger) |
467 | |
468 @DOCSTRING(int8) | |
469 | |
470 @DOCSTRING(uint8) | |
471 | |
472 @DOCSTRING(int16) | |
473 | |
474 @DOCSTRING(uint16) | |
475 | |
476 @DOCSTRING(int32) | |
477 | |
478 @DOCSTRING(uint32) | |
479 | |
480 @DOCSTRING(int64) | |
481 | |
482 @DOCSTRING(uint64) | |
483 | |
484 @DOCSTRING(intmax) | |
485 | |
486 @DOCSTRING(intmin) | |
487 | |
6624 | 488 @menu |
489 * Integer Arithmetic:: | |
490 @end menu | |
491 | |
6620 | 492 @node Integer Arithmetic |
493 @subsection Integer Arithmetic | |
494 | |
495 While many numerical computations can't be carried out in integers, | |
496 Octave does support basic operations like addition and multiplication | |
497 on integers. The operators @code{+}, @code{-}, @code{.*}, and @code{./} | |
498 works on integers of the same type. So, it is possible to add two 32 bit | |
499 integers, but not to add a 32 bit integer and a 16 bit integer. | |
500 | |
6715 | 501 The arithmetic operations on integers are performed by casting the |
502 integer values to double precision values, performing the operation, and | |
503 then re-casting the values back to the original integer type. As the | |
504 double precision type of Octave is only capable of representing integers | |
505 with up to 53 bits of precision, it is not possible to perform | |
506 arithmetic of the 64 bit integer types. | |
507 | |
6620 | 508 When doing integer arithmetic one should consider the possibility of |
509 underflow and overflow. This happens when the result of the computation | |
510 can't be represented using the chosen integer type. As an example it is | |
511 not possible to represent the result of @math{10 - 20} when using | |
512 unsigned integers. Octave makes sure that the result of integer | |
513 computations is the integer that is closest to the true result. So, the | |
514 result of @math{10 - 20} when using unsigned integers is zero. | |
515 | |
516 When doing integer division Octave will round the result to the nearest | |
517 integer. This is different from most programming languages, where the | |
518 result is often floored to the nearest integer. So, the result of | |
519 @code{int32(5)./int32(8)} is @code{1}. | |
520 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
521 @DOCSTRING(idivide) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
522 |
6781 | 523 @node Bit Manipulations |
524 @section Bit Manipulations | |
525 | |
526 Octave provides a number of functions for the manipulation of numeric | |
527 values on a bit by bit basis. The basic functions to set and obtain the | |
528 values of individual bits are @code{bitset} and @code{bitget}. | |
529 | |
530 @DOCSTRING(bitset) | |
531 | |
532 @DOCSTRING(bitget) | |
533 | |
534 The arguments to all of Octave's bitwise operations can be scalar or | |
535 arrays, except for @code{bitcmp}, whose @var{k} argument must a | |
536 scalar. In the case where more than one argument is an array, then all | |
537 arguments must have the same shape, and the bitwise operator is applied | |
538 to each of the elements of the argument individually. If at least one | |
539 argument is a scalar and one an array, then the scalar argument is | |
540 duplicated. Therefore | |
541 | |
542 @example | |
543 bitget (100, 8:-1:1) | |
544 @end example | |
545 | |
546 is the same as | |
547 | |
548 @example | |
549 bitget (100 * ones (1, 8), 8:-1:1) | |
550 @end example | |
551 | |
552 It should be noted that all values passed to the bit manipulation | |
553 functions of Octave are treated as integers. Therefore, even though the | |
554 example for @code{bitset} above passes the floating point value | |
555 @code{10}, it is treated as the bits @code{[1, 0, 1, 0]} rather than the | |
556 bits of the native floating point format representation of @code{10}. | |
557 | |
558 As the maximum number that can be represented by a number is important | |
559 for bit manipulation, particularly when forming masks, Octave supplies | |
560 the function @code{bitmax}. | |
561 | |
562 @DOCSTRING(bitmax) | |
563 | |
564 This is the double precision version of the functions @code{intmax}, | |
565 previously discussed. | |
566 | |
567 Octave also include the basic bitwise 'and', 'or' and 'exclusive or' | |
568 operators. | |
569 | |
570 @DOCSTRING(bitand) | |
571 | |
572 @DOCSTRING(bitor) | |
573 | |
574 @DOCSTRING(bitxor) | |
575 | |
576 The bitwise 'not' operator is unary operator that performs a logial | |
577 negation of each of the bits of the value. For this to make sense, the | |
578 mask against which the value is negated must be defined. Octave's | |
579 bitwise 'not' operator is @code{bitcmp}. | |
580 | |
581 @DOCSTRING(bitcmp) | |
582 | |
583 Octave also includes the ability to left and right values bitwise. | |
584 | |
585 @DOCSTRING(bitshift) | |
586 | |
587 Bits that are shifted out of either end of the value are lost. Octave | |
588 also uses arithmetic shifts, where the sign bit of the value is keep | |
589 during a right shift. For example | |
590 | |
591 @example | |
592 @group | |
593 bitshift (-10, -1) | |
594 @result{} -5 | |
595 bitshift (int8 (-1), -1) | |
596 @result{} -1 | |
597 @end group | |
598 @end example | |
599 | |
600 Note that @code{bitshift (int8 (-1), -1)} is @code{-1} since the bit | |
601 representation of @code{-1} in the @code{int8} data type is @code{[1, 1, | |
602 1, 1, 1, 1, 1, 1]}. | |
603 | |
4167 | 604 @node Logical Values |
3428 | 605 @section Logical Values |
606 | |
6620 | 607 Octave has built-in support for logical values, i.e. variables that |
608 are either @code{true} or @code{false}. When comparing two variables, | |
609 the result will be a logical value whose value depends on whether or | |
610 not the comparison is true. | |
611 | |
612 The basic logical operations are @code{&}, @code{|}, and @code{!}, | |
613 that corresponds to ``Logical And'', ``Logical Or'', and ``Logical | |
614 Negation''. These operations all follow the rules of logic. | |
615 | |
616 It is also possible to use logical values as part of standard numerical | |
617 calculations. In this case @code{true} is converted to @code{1}, and | |
618 @code{false} to 0, both represented using double precision floating | |
619 point numbers. So, the result of @code{true*22 - false/6} is @code{22}. | |
620 | |
621 Logical values can also be used to index matrices and cell arrays. | |
622 When indexing with a logical array the result will be a vector containing | |
623 the values corresponding to @code{true} parts of the logical array. | |
624 The following example illustrates this. | |
625 | |
626 @example | |
627 data = [ 1, 2; 3, 4 ]; | |
628 idx = (data <= 2); | |
629 data(idx) | |
630 @result{} ans = [ 1; 4 ] | |
631 @end example | |
632 | |
633 @noindent | |
634 Instead of creating the @code{idx} array it is possible to replace | |
635 @code{data(idx)} with @code{data( data <= 2 )} in the above code. | |
636 | |
637 Besides when doing comparisons, logical values can be constructed by | |
638 casting numeric objects to logical values, or by using the @code{true} | |
639 or @code{false} functions. | |
640 | |
6549 | 641 @DOCSTRING(logical) |
642 | |
3428 | 643 @DOCSTRING(true) |
644 | |
645 @DOCSTRING(false) | |
646 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
647 @node Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
648 @section Promotion and Demotion of Data Types |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
649 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
650 Many operators and functions can work with mixed data types. For example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
651 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
652 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
653 uint8 (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
654 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
655 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
656 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
657 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
658 where the above operator works with an 8-bit integer and a double precision |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
659 value and returns an 8-bit integer value. Note that the type is demoted |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
660 to an 8-bit integer, rather than promoted to a double precision value as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
661 might be expected. The reason is that if Octave promoted values in |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
662 expressions like the above with all numerical constants would need to be |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
663 explicitly cast to the appropriate data type type like |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
664 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
665 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
666 uint8 (1) + uint8 (1) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
667 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
668 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
669 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
670 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
671 which becomes difficult for the user to apply uniformly and might allow |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
672 hard to find bugs to be introduced. The same applies to single precision |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
673 values where a mixed operation such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
674 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
675 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
676 single (1) + 1 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
677 @result{} 2 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
678 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
679 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
680 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
681 returns a single precision value. The mixed operations that are valid |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
682 and their returned data types are |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
683 |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
684 @multitable @columnfractions .2 .3 .3 .2 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
685 @item @tab Mixed Operation @tab Result @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
686 @item @tab double OP single @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
687 @item @tab double OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
688 @item @tab double OP char @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
689 @item @tab double OP logical @tab double @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
690 @item @tab single OP integer @tab integer @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
691 @item @tab single OP char @tab single @tab |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
692 @item @tab single OP logical @tab single @tab |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
693 @end multitable |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
694 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
695 The same logic applies to functions with mixed arguments such as |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
696 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
697 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
698 min (single (1), 0) |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
699 @result{} 0 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
700 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
701 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
702 @noindent |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
703 where the returned value is single precision. |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
704 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
705 In the case of mixed type indexed assignments, the type is not |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
706 changed. For example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
707 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
708 @example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
709 x = ones (2, 2); |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
710 x (1, 1) = single (2) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
711 @result{} x = 2 1 |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
712 1 1 |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
713 @end example |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
714 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
715 @noindent |
7989
23c248d415b5
Various doc fixes. Readd cellidx
David Bateman <dbateman@free.fr>
parents:
7984
diff
changeset
|
716 where @code{x} remains of the double precision type. |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7638
diff
changeset
|
717 |
4167 | 718 @node Predicates for Numeric Objects |
3294 | 719 @section Predicates for Numeric Objects |
720 | |
6620 | 721 Since the type of a variable may change during the execution of a |
722 program, it can be necessary to type checking at run-time. Doing this | |
723 also allows you to change the behaviour of a function depending on the | |
724 type of the input. As an example, this naive implementation of @code{abs} | |
7001 | 725 returns the absolute value of the input if it is a real number, and the |
6620 | 726 length of the input if it is a complex number. |
727 | |
728 @example | |
729 function a = abs (x) | |
730 if (isreal (x)) | |
731 a = sign (x) .* x; | |
732 elseif (iscomplex (x)) | |
733 a = sqrt (real(x).^2 + imag(x).^2); | |
734 endif | |
735 endfunction | |
736 @end example | |
737 | |
738 The following functions are available for determining the type of a | |
739 variable. | |
740 | |
3428 | 741 @DOCSTRING(isnumeric) |
742 | |
743 @DOCSTRING(isreal) | |
744 | |
7638
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7018
diff
changeset
|
745 @DOCSTRING(isfloat) |
2df457529cfa
implement expm1 and log1p functions
Jaroslav Hajek <highegg@gmail.com>
parents:
7018
diff
changeset
|
746 |
4029 | 747 @DOCSTRING(iscomplex) |
3428 | 748 |
4029 | 749 @DOCSTRING(ismatrix) |
3294 | 750 |
4029 | 751 @DOCSTRING(isvector) |
3294 | 752 |
4029 | 753 @DOCSTRING(isscalar) |
3294 | 754 |
4029 | 755 @DOCSTRING(issquare) |
3294 | 756 |
4029 | 757 @DOCSTRING(issymmetric) |
3428 | 758 |
6550 | 759 @DOCSTRING(isdefinite) |
760 | |
6688 | 761 @DOCSTRING(islogical) |
6550 | 762 |
763 @DOCSTRING(isprime) |