6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Numeric Data Types |
3294
|
6 @chapter Numeric Data Types |
|
7 @cindex numeric constant |
|
8 @cindex numeric value |
|
9 |
|
10 A @dfn{numeric constant} may be a scalar, a vector, or a matrix, and it |
|
11 may contain complex values. |
|
12 |
|
13 The simplest form of a numeric constant, a scalar, is a single number |
|
14 that can be an integer, a decimal fraction, a number in scientific |
6620
|
15 (exponential) notation, or a complex number. Note that by default numeric |
3294
|
16 constants are represented within Octave in double-precision floating |
|
17 point format (complex constants are stored as pairs of double-precision |
6620
|
18 floating point values). It is however possible to represent real |
|
19 integers as described in @ref{Integer Data Types}. Here are some examples |
|
20 of real-valued numeric constants, which all have the same value: |
3294
|
21 |
|
22 @example |
|
23 @group |
|
24 105 |
|
25 1.05e+2 |
|
26 1050e-1 |
|
27 @end group |
|
28 @end example |
|
29 |
|
30 To specify complex constants, you can write an expression of the form |
|
31 |
|
32 @example |
|
33 @group |
|
34 3 + 4i |
|
35 3.0 + 4.0i |
|
36 0.3e1 + 40e-1i |
|
37 @end group |
|
38 @end example |
|
39 |
6620
|
40 @noindent |
3294
|
41 all of which are equivalent. The letter @samp{i} in the previous example |
|
42 stands for the pure imaginary constant, defined as |
|
43 @iftex |
|
44 @tex |
|
45 $\sqrt{-1}$. |
|
46 @end tex |
|
47 @end iftex |
6620
|
48 @ifnottex |
3294
|
49 @code{sqrt (-1)}. |
6620
|
50 @end ifnottex |
3294
|
51 |
|
52 For Octave to recognize a value as the imaginary part of a complex |
|
53 constant, a space must not appear between the number and the @samp{i}. |
|
54 If it does, Octave will print an error message, like this: |
|
55 |
|
56 @example |
|
57 @group |
|
58 octave:13> 3 + 4 i |
|
59 |
|
60 parse error: |
|
61 |
|
62 3 + 4 i |
|
63 ^ |
|
64 @end group |
|
65 @end example |
|
66 |
6620
|
67 @noindent |
3294
|
68 You may also use @samp{j}, @samp{I}, or @samp{J} in place of the |
|
69 @samp{i} above. All four forms are equivalent. |
|
70 |
6549
|
71 @DOCSTRING(double) |
|
72 |
|
73 @DOCSTRING(single) |
|
74 |
|
75 @DOCSTRING(complex) |
|
76 |
3294
|
77 @menu |
|
78 * Matrices:: |
|
79 * Ranges:: |
6549
|
80 * Integer Data Types:: |
3428
|
81 * Logical Values:: |
3294
|
82 * Predicates for Numeric Objects:: |
|
83 @end menu |
|
84 |
4167
|
85 @node Matrices |
3294
|
86 @section Matrices |
|
87 @cindex matrices |
|
88 |
|
89 @opindex [ |
|
90 @opindex ] |
|
91 @opindex ; |
|
92 @opindex , |
|
93 |
|
94 It is easy to define a matrix of values in Octave. The size of the |
|
95 matrix is determined automatically, so it is not necessary to explicitly |
|
96 state the dimensions. The expression |
|
97 |
|
98 @example |
|
99 a = [1, 2; 3, 4] |
|
100 @end example |
|
101 |
|
102 @noindent |
|
103 results in the matrix |
|
104 @iftex |
|
105 @tex |
|
106 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ |
|
107 @end tex |
|
108 @end iftex |
6620
|
109 @ifnottex |
3294
|
110 |
|
111 @example |
|
112 @group |
|
113 |
|
114 / \ |
|
115 | 1 2 | |
|
116 a = | | |
|
117 | 3 4 | |
|
118 \ / |
|
119 |
|
120 @end group |
|
121 @end example |
6620
|
122 @end ifnottex |
3294
|
123 |
|
124 Elements of a matrix may be arbitrary expressions, provided that the |
|
125 dimensions all make sense when combining the various pieces. For |
|
126 example, given the above matrix, the expression |
|
127 |
|
128 @example |
|
129 [ a, a ] |
|
130 @end example |
|
131 |
|
132 @noindent |
|
133 produces the matrix |
|
134 |
|
135 @example |
|
136 @group |
|
137 ans = |
|
138 |
|
139 1 2 1 2 |
|
140 3 4 3 4 |
|
141 @end group |
|
142 @end example |
|
143 |
|
144 @noindent |
|
145 but the expression |
|
146 |
|
147 @example |
|
148 [ a, 1 ] |
|
149 @end example |
|
150 |
|
151 @noindent |
|
152 produces the error |
|
153 |
|
154 @example |
|
155 error: number of rows must match near line 13, column 6 |
|
156 @end example |
|
157 |
|
158 @noindent |
|
159 (assuming that this expression was entered as the first thing on line |
|
160 13, of course). |
|
161 |
|
162 Inside the square brackets that delimit a matrix expression, Octave |
|
163 looks at the surrounding context to determine whether spaces and newline |
|
164 characters should be converted into element and row separators, or |
4476
|
165 simply ignored, so an expression like |
3294
|
166 |
|
167 @example |
|
168 @group |
|
169 a = [ 1 2 |
|
170 3 4 ] |
|
171 @end group |
|
172 @end example |
|
173 |
|
174 @noindent |
|
175 will work. However, some possible sources of confusion remain. For |
|
176 example, in the expression |
|
177 |
|
178 @example |
|
179 [ 1 - 1 ] |
|
180 @end example |
|
181 |
|
182 @noindent |
|
183 the @samp{-} is treated as a binary operator and the result is the |
|
184 scalar 0, but in the expression |
|
185 |
|
186 @example |
|
187 [ 1 -1 ] |
|
188 @end example |
|
189 |
|
190 @noindent |
|
191 the @samp{-} is treated as a unary operator and the result is the |
4476
|
192 vector @code{[ 1, -1 ]}. Similarly, the expression |
|
193 |
|
194 @example |
|
195 [ sin (pi) ] |
|
196 @end example |
|
197 |
|
198 @noindent |
|
199 will be parsed as |
|
200 |
|
201 @example |
|
202 [ sin, (pi) ] |
|
203 @end example |
3294
|
204 |
4476
|
205 @noindent |
|
206 and will result in an error since the @code{sin} function will be |
|
207 called with no arguments. To get around this, you must omit the space |
|
208 between @code{sin} and the opening parenthesis, or enclose the |
|
209 expression in a set of parentheses: |
|
210 |
|
211 @example |
|
212 [ (sin (pi)) ] |
|
213 @end example |
|
214 |
|
215 Whitespace surrounding the single quote character (@samp{'}, used as a |
|
216 transpose operator and for delimiting character strings) can also cause |
|
217 confusion. Given @code{a = 1}, the expression |
3294
|
218 |
|
219 @example |
|
220 [ 1 a' ] |
|
221 @end example |
|
222 |
|
223 @noindent |
4476
|
224 results in the single quote character being treated as a |
3294
|
225 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
|
226 expression |
|
227 |
|
228 @example |
|
229 [ 1 a ' ] |
|
230 @end example |
|
231 |
|
232 @noindent |
|
233 produces the error message |
|
234 |
|
235 @example |
|
236 error: unterminated string constant |
|
237 @end example |
|
238 |
|
239 @noindent |
6620
|
240 because not doing so would cause trouble when parsing the valid expression |
3294
|
241 |
|
242 @example |
|
243 [ a 'foo' ] |
|
244 @end example |
|
245 |
|
246 For clarity, it is probably best to always use commas and semicolons to |
4476
|
247 separate matrix elements and rows. |
3294
|
248 |
|
249 When you type a matrix or the name of a variable whose value is a |
|
250 matrix, Octave responds by printing the matrix in with neatly aligned |
|
251 rows and columns. If the rows of the matrix are too large to fit on the |
|
252 screen, Octave splits the matrix and displays a header before each |
|
253 section to indicate which columns are being displayed. You can use the |
|
254 following variables to control the format of the output. |
|
255 |
3321
|
256 @DOCSTRING(output_max_field_width) |
3294
|
257 |
3321
|
258 @DOCSTRING(output_precision) |
3294
|
259 |
|
260 It is possible to achieve a wide range of output styles by using |
|
261 different values of @code{output_precision} and |
|
262 @code{output_max_field_width}. Reasonable combinations can be set using |
|
263 the @code{format} function. @xref{Basic Input and Output}. |
|
264 |
3321
|
265 @DOCSTRING(split_long_rows) |
3294
|
266 |
|
267 Octave automatically switches to scientific notation when values become |
|
268 very large or very small. This guarantees that you will see several |
|
269 significant figures for every value in a matrix. If you would prefer to |
|
270 see all values in a matrix printed in a fixed point format, you can set |
|
271 the built-in variable @code{fixed_point_format} to a nonzero value. But |
|
272 doing so is not recommended, because it can produce output that can |
|
273 easily be misinterpreted. |
|
274 |
3321
|
275 @DOCSTRING(fixed_point_format) |
3294
|
276 |
|
277 @menu |
|
278 * Empty Matrices:: |
|
279 @end menu |
|
280 |
4167
|
281 @node Empty Matrices |
3294
|
282 @subsection Empty Matrices |
|
283 |
|
284 A matrix may have one or both dimensions zero, and operations on empty |
|
285 matrices are handled as described by Carl de Boor in @cite{An Empty |
|
286 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. |
|
287 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty |
|
288 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, |
|
289 Number 5, May 1993. |
|
290 @iftex |
|
291 @tex |
|
292 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, |
|
293 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or |
|
294 both dimensions equal to zero), the following are true: |
|
295 $$ |
|
296 \eqalign{% |
|
297 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr |
|
298 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr |
|
299 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr |
|
300 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr |
|
301 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} |
|
302 $$ |
|
303 @end tex |
|
304 @end iftex |
6620
|
305 @ifnottex |
3294
|
306 Briefly, given a scalar @var{s}, an @var{m} by |
|
307 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix |
|
308 @code{[](mxn)} (with either one or both dimensions equal to zero), the |
|
309 following are true: |
|
310 |
|
311 @example |
|
312 @group |
|
313 s * [](mxn) = [](mxn) * s = [](mxn) |
|
314 |
|
315 [](mxn) + [](mxn) = [](mxn) |
|
316 |
|
317 [](0xm) * M(mxn) = [](0xn) |
|
318 |
|
319 M(mxn) * [](nx0) = [](mx0) |
|
320 |
|
321 [](mx0) * [](0xn) = 0(mxn) |
|
322 @end group |
|
323 @end example |
6620
|
324 @end ifnottex |
3294
|
325 |
|
326 By default, dimensions of the empty matrix are printed along with the |
|
327 empty matrix symbol, @samp{[]}. The built-in variable |
|
328 @code{print_empty_dimensions} controls this behavior. |
|
329 |
3321
|
330 @DOCSTRING(print_empty_dimensions) |
3294
|
331 |
|
332 Empty matrices may also be used in assignment statements as a convenient |
|
333 way to delete rows or columns of matrices. |
|
334 @xref{Assignment Ops, ,Assignment Expressions}. |
|
335 |
|
336 When Octave parses a matrix expression, it examines the elements of the |
|
337 list to determine whether they are all constants. If they are, it |
|
338 replaces the list with a single matrix constant. |
|
339 |
4167
|
340 @node Ranges |
3294
|
341 @section Ranges |
|
342 @cindex range expressions |
|
343 @cindex expression, range |
|
344 |
3920
|
345 @opindex colon |
3294
|
346 |
|
347 A @dfn{range} is a convenient way to write a row vector with evenly |
|
348 spaced elements. A range expression is defined by the value of the first |
|
349 element in the range, an optional value for the increment between |
|
350 elements, and a maximum value which the elements of the range will not |
|
351 exceed. The base, increment, and limit are separated by colons (the |
|
352 @samp{:} character) and may contain any arithmetic expressions and |
|
353 function calls. If the increment is omitted, it is assumed to be 1. |
|
354 For example, the range |
|
355 |
|
356 @example |
|
357 1 : 5 |
|
358 @end example |
|
359 |
|
360 @noindent |
|
361 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range |
|
362 |
|
363 @example |
|
364 1 : 3 : 5 |
|
365 @end example |
|
366 |
|
367 @noindent |
|
368 defines the set of values @samp{[ 1, 4 ]}. |
|
369 |
|
370 Although a range constant specifies a row vector, Octave does @emph{not} |
|
371 convert range constants to vectors unless it is necessary to do so. |
|
372 This allows you to write a constant like @samp{1 : 10000} without using |
|
373 80,000 bytes of storage on a typical 32-bit workstation. |
|
374 |
|
375 Note that the upper (or lower, if the increment is negative) bound on |
|
376 the range is not always included in the set of values, and that ranges |
|
377 defined by floating point values can produce surprising results because |
|
378 Octave uses floating point arithmetic to compute the values in the |
|
379 range. If it is important to include the endpoints of a range and the |
|
380 number of elements is known, you should use the @code{linspace} function |
|
381 instead (@pxref{Special Utility Matrices}). |
|
382 |
|
383 When Octave parses a range expression, it examines the elements of the |
|
384 expression to determine whether they are all constants. If they are, it |
|
385 replaces the range expression with a single range constant. |
|
386 |
6549
|
387 @node Integer Data Types |
|
388 @section Integer Data Types |
|
389 |
6620
|
390 Octave supports integer matrices as an alternative to using double |
|
391 precision. It is possible to use both signed and unsigned integers |
|
392 represented by 8, 16, 32, or 64 bits. It should be noted that most |
|
393 computations require floating point data, meaning that integers will |
|
394 often change type when involved in numeric computations. For this |
|
395 reason integers are most often used to store data, and not for |
|
396 calculations. |
|
397 |
|
398 In general most integer matrices are created by casting |
|
399 existing matrices to integers. The following example shows how to cast |
|
400 a matrix into 32 bit integers. |
|
401 |
|
402 @example |
|
403 float = rand (2, 2) |
|
404 @result{} float = 0.37569 0.92982 |
|
405 0.11962 0.50876 |
|
406 integer = int32 (float) |
|
407 @result{} integer = 0 1 |
|
408 0 1 |
|
409 @end example |
|
410 |
|
411 @noindent |
|
412 As can be seen, floating point values are rounded to the nearest integer |
|
413 when converted. |
|
414 |
6549
|
415 @DOCSTRING(isinteger) |
|
416 |
|
417 @DOCSTRING(int8) |
|
418 |
|
419 @DOCSTRING(uint8) |
|
420 |
|
421 @DOCSTRING(int16) |
|
422 |
|
423 @DOCSTRING(uint16) |
|
424 |
|
425 @DOCSTRING(int32) |
|
426 |
|
427 @DOCSTRING(uint32) |
|
428 |
|
429 @DOCSTRING(int64) |
|
430 |
|
431 @DOCSTRING(uint64) |
|
432 |
|
433 @DOCSTRING(intmax) |
|
434 |
|
435 @DOCSTRING(intmin) |
|
436 |
6624
|
437 @menu |
|
438 * Integer Arithmetic:: |
|
439 @end menu |
|
440 |
6620
|
441 @node Integer Arithmetic |
|
442 @subsection Integer Arithmetic |
|
443 |
|
444 While many numerical computations can't be carried out in integers, |
|
445 Octave does support basic operations like addition and multiplication |
|
446 on integers. The operators @code{+}, @code{-}, @code{.*}, and @code{./} |
|
447 works on integers of the same type. So, it is possible to add two 32 bit |
|
448 integers, but not to add a 32 bit integer and a 16 bit integer. |
|
449 |
6715
|
450 The arithmetic operations on integers are performed by casting the |
|
451 integer values to double precision values, performing the operation, and |
|
452 then re-casting the values back to the original integer type. As the |
|
453 double precision type of Octave is only capable of representing integers |
|
454 with up to 53 bits of precision, it is not possible to perform |
|
455 arithmetic of the 64 bit integer types. |
|
456 |
6620
|
457 When doing integer arithmetic one should consider the possibility of |
|
458 underflow and overflow. This happens when the result of the computation |
|
459 can't be represented using the chosen integer type. As an example it is |
|
460 not possible to represent the result of @math{10 - 20} when using |
|
461 unsigned integers. Octave makes sure that the result of integer |
|
462 computations is the integer that is closest to the true result. So, the |
|
463 result of @math{10 - 20} when using unsigned integers is zero. |
|
464 |
|
465 When doing integer division Octave will round the result to the nearest |
|
466 integer. This is different from most programming languages, where the |
|
467 result is often floored to the nearest integer. So, the result of |
|
468 @code{int32(5)./int32(8)} is @code{1}. |
|
469 |
4167
|
470 @node Logical Values |
3428
|
471 @section Logical Values |
|
472 |
6620
|
473 Octave has built-in support for logical values, i.e. variables that |
|
474 are either @code{true} or @code{false}. When comparing two variables, |
|
475 the result will be a logical value whose value depends on whether or |
|
476 not the comparison is true. |
|
477 |
|
478 The basic logical operations are @code{&}, @code{|}, and @code{!}, |
|
479 that corresponds to ``Logical And'', ``Logical Or'', and ``Logical |
|
480 Negation''. These operations all follow the rules of logic. |
|
481 |
|
482 It is also possible to use logical values as part of standard numerical |
|
483 calculations. In this case @code{true} is converted to @code{1}, and |
|
484 @code{false} to 0, both represented using double precision floating |
|
485 point numbers. So, the result of @code{true*22 - false/6} is @code{22}. |
|
486 |
|
487 Logical values can also be used to index matrices and cell arrays. |
|
488 When indexing with a logical array the result will be a vector containing |
|
489 the values corresponding to @code{true} parts of the logical array. |
|
490 The following example illustrates this. |
|
491 |
|
492 @example |
|
493 data = [ 1, 2; 3, 4 ]; |
|
494 idx = (data <= 2); |
|
495 data(idx) |
|
496 @result{} ans = [ 1; 4 ] |
|
497 @end example |
|
498 |
|
499 @noindent |
|
500 Instead of creating the @code{idx} array it is possible to replace |
|
501 @code{data(idx)} with @code{data( data <= 2 )} in the above code. |
|
502 |
|
503 Besides when doing comparisons, logical values can be constructed by |
|
504 casting numeric objects to logical values, or by using the @code{true} |
|
505 or @code{false} functions. |
|
506 |
6549
|
507 @DOCSTRING(logical) |
|
508 |
3428
|
509 @DOCSTRING(true) |
|
510 |
|
511 @DOCSTRING(false) |
|
512 |
4167
|
513 @node Predicates for Numeric Objects |
3294
|
514 @section Predicates for Numeric Objects |
|
515 |
6620
|
516 Since the type of a variable may change during the execution of a |
|
517 program, it can be necessary to type checking at run-time. Doing this |
|
518 also allows you to change the behaviour of a function depending on the |
|
519 type of the input. As an example, this naive implementation of @code{abs} |
|
520 return the absolute value of the input if it is a real number, and the |
|
521 length of the input if it is a complex number. |
|
522 |
|
523 @example |
|
524 function a = abs (x) |
|
525 if (isreal (x)) |
|
526 a = sign (x) .* x; |
|
527 elseif (iscomplex (x)) |
|
528 a = sqrt (real(x).^2 + imag(x).^2); |
|
529 endif |
|
530 endfunction |
|
531 @end example |
|
532 |
|
533 The following functions are available for determining the type of a |
|
534 variable. |
|
535 |
3428
|
536 @DOCSTRING(isnumeric) |
|
537 |
|
538 @DOCSTRING(isreal) |
|
539 |
4029
|
540 @DOCSTRING(iscomplex) |
3428
|
541 |
4029
|
542 @DOCSTRING(ismatrix) |
3294
|
543 |
4029
|
544 @DOCSTRING(isvector) |
3294
|
545 |
4029
|
546 @DOCSTRING(isscalar) |
3294
|
547 |
4029
|
548 @DOCSTRING(issquare) |
3294
|
549 |
4029
|
550 @DOCSTRING(issymmetric) |
3428
|
551 |
6550
|
552 @DOCSTRING(isdefinite) |
|
553 |
6688
|
554 @DOCSTRING(islogical) |
6550
|
555 |
|
556 @DOCSTRING(isprime) |