3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node 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 |
|
15 (exponential) notation, or a complex number. Note that all numeric |
|
16 constants are represented within Octave in double-precision floating |
|
17 point format (complex constants are stored as pairs of double-precision |
|
18 floating point values). Here are some examples of real-valued numeric |
|
19 constants, which all have the same value: |
|
20 |
|
21 @example |
|
22 @group |
|
23 105 |
|
24 1.05e+2 |
|
25 1050e-1 |
|
26 @end group |
|
27 @end example |
|
28 |
|
29 To specify complex constants, you can write an expression of the form |
|
30 |
|
31 @example |
|
32 @group |
|
33 3 + 4i |
|
34 3.0 + 4.0i |
|
35 0.3e1 + 40e-1i |
|
36 @end group |
|
37 @end example |
|
38 |
|
39 all of which are equivalent. The letter @samp{i} in the previous example |
|
40 stands for the pure imaginary constant, defined as |
|
41 @iftex |
|
42 @tex |
|
43 $\sqrt{-1}$. |
|
44 @end tex |
|
45 @end iftex |
|
46 @ifinfo |
|
47 @code{sqrt (-1)}. |
|
48 @end ifinfo |
|
49 |
|
50 For Octave to recognize a value as the imaginary part of a complex |
|
51 constant, a space must not appear between the number and the @samp{i}. |
|
52 If it does, Octave will print an error message, like this: |
|
53 |
|
54 @example |
|
55 @group |
|
56 octave:13> 3 + 4 i |
|
57 |
|
58 parse error: |
|
59 |
|
60 3 + 4 i |
|
61 ^ |
|
62 @end group |
|
63 @end example |
|
64 |
|
65 You may also use @samp{j}, @samp{I}, or @samp{J} in place of the |
|
66 @samp{i} above. All four forms are equivalent. |
|
67 |
6549
|
68 @DOCSTRING(double) |
|
69 |
|
70 @DOCSTRING(single) |
|
71 |
|
72 @DOCSTRING(complex) |
|
73 |
3294
|
74 @menu |
|
75 * Matrices:: |
|
76 * Ranges:: |
6549
|
77 * Integer Data Types:: |
3428
|
78 * Logical Values:: |
3294
|
79 * Predicates for Numeric Objects:: |
|
80 @end menu |
|
81 |
4167
|
82 @node Matrices |
3294
|
83 @section Matrices |
|
84 @cindex matrices |
|
85 |
|
86 @opindex [ |
|
87 @opindex ] |
|
88 @opindex ; |
|
89 @opindex , |
|
90 |
|
91 It is easy to define a matrix of values in Octave. The size of the |
|
92 matrix is determined automatically, so it is not necessary to explicitly |
|
93 state the dimensions. The expression |
|
94 |
|
95 @example |
|
96 a = [1, 2; 3, 4] |
|
97 @end example |
|
98 |
|
99 @noindent |
|
100 results in the matrix |
|
101 @iftex |
|
102 @tex |
|
103 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ |
|
104 @end tex |
|
105 @end iftex |
|
106 @ifinfo |
|
107 |
|
108 @example |
|
109 @group |
|
110 |
|
111 / \ |
|
112 | 1 2 | |
|
113 a = | | |
|
114 | 3 4 | |
|
115 \ / |
|
116 |
|
117 @end group |
|
118 @end example |
|
119 @end ifinfo |
|
120 |
|
121 Elements of a matrix may be arbitrary expressions, provided that the |
|
122 dimensions all make sense when combining the various pieces. For |
|
123 example, given the above matrix, the expression |
|
124 |
|
125 @example |
|
126 [ a, a ] |
|
127 @end example |
|
128 |
|
129 @noindent |
|
130 produces the matrix |
|
131 |
|
132 @example |
|
133 @group |
|
134 ans = |
|
135 |
|
136 1 2 1 2 |
|
137 3 4 3 4 |
|
138 @end group |
|
139 @end example |
|
140 |
|
141 @noindent |
|
142 but the expression |
|
143 |
|
144 @example |
|
145 [ a, 1 ] |
|
146 @end example |
|
147 |
|
148 @noindent |
|
149 produces the error |
|
150 |
|
151 @example |
|
152 error: number of rows must match near line 13, column 6 |
|
153 @end example |
|
154 |
|
155 @noindent |
|
156 (assuming that this expression was entered as the first thing on line |
|
157 13, of course). |
|
158 |
|
159 Inside the square brackets that delimit a matrix expression, Octave |
|
160 looks at the surrounding context to determine whether spaces and newline |
|
161 characters should be converted into element and row separators, or |
4476
|
162 simply ignored, so an expression like |
3294
|
163 |
|
164 @example |
|
165 @group |
|
166 a = [ 1 2 |
|
167 3 4 ] |
|
168 @end group |
|
169 @end example |
|
170 |
|
171 @noindent |
|
172 will work. However, some possible sources of confusion remain. For |
|
173 example, in the expression |
|
174 |
|
175 @example |
|
176 [ 1 - 1 ] |
|
177 @end example |
|
178 |
|
179 @noindent |
|
180 the @samp{-} is treated as a binary operator and the result is the |
|
181 scalar 0, but in the expression |
|
182 |
|
183 @example |
|
184 [ 1 -1 ] |
|
185 @end example |
|
186 |
|
187 @noindent |
|
188 the @samp{-} is treated as a unary operator and the result is the |
4476
|
189 vector @code{[ 1, -1 ]}. Similarly, the expression |
|
190 |
|
191 @example |
|
192 [ sin (pi) ] |
|
193 @end example |
|
194 |
|
195 @noindent |
|
196 will be parsed as |
|
197 |
|
198 @example |
|
199 [ sin, (pi) ] |
|
200 @end example |
3294
|
201 |
4476
|
202 @noindent |
|
203 and will result in an error since the @code{sin} function will be |
|
204 called with no arguments. To get around this, you must omit the space |
|
205 between @code{sin} and the opening parenthesis, or enclose the |
|
206 expression in a set of parentheses: |
|
207 |
|
208 @example |
|
209 [ (sin (pi)) ] |
|
210 @end example |
|
211 |
|
212 Whitespace surrounding the single quote character (@samp{'}, used as a |
|
213 transpose operator and for delimiting character strings) can also cause |
|
214 confusion. Given @code{a = 1}, the expression |
3294
|
215 |
|
216 @example |
|
217 [ 1 a' ] |
|
218 @end example |
|
219 |
|
220 @noindent |
4476
|
221 results in the single quote character being treated as a |
3294
|
222 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
|
223 expression |
|
224 |
|
225 @example |
|
226 [ 1 a ' ] |
|
227 @end example |
|
228 |
|
229 @noindent |
|
230 produces the error message |
|
231 |
|
232 @example |
|
233 error: unterminated string constant |
|
234 @end example |
|
235 |
|
236 @noindent |
4476
|
237 because to not do so would cause trouble when parsing the valid expression |
3294
|
238 |
|
239 @example |
|
240 [ a 'foo' ] |
|
241 @end example |
|
242 |
|
243 For clarity, it is probably best to always use commas and semicolons to |
4476
|
244 separate matrix elements and rows. |
3294
|
245 |
|
246 When you type a matrix or the name of a variable whose value is a |
|
247 matrix, Octave responds by printing the matrix in with neatly aligned |
|
248 rows and columns. If the rows of the matrix are too large to fit on the |
|
249 screen, Octave splits the matrix and displays a header before each |
|
250 section to indicate which columns are being displayed. You can use the |
|
251 following variables to control the format of the output. |
|
252 |
3321
|
253 @DOCSTRING(output_max_field_width) |
3294
|
254 |
3321
|
255 @DOCSTRING(output_precision) |
3294
|
256 |
|
257 It is possible to achieve a wide range of output styles by using |
|
258 different values of @code{output_precision} and |
|
259 @code{output_max_field_width}. Reasonable combinations can be set using |
|
260 the @code{format} function. @xref{Basic Input and Output}. |
|
261 |
3321
|
262 @DOCSTRING(split_long_rows) |
3294
|
263 |
|
264 Octave automatically switches to scientific notation when values become |
|
265 very large or very small. This guarantees that you will see several |
|
266 significant figures for every value in a matrix. If you would prefer to |
|
267 see all values in a matrix printed in a fixed point format, you can set |
|
268 the built-in variable @code{fixed_point_format} to a nonzero value. But |
|
269 doing so is not recommended, because it can produce output that can |
|
270 easily be misinterpreted. |
|
271 |
3321
|
272 @DOCSTRING(fixed_point_format) |
3294
|
273 |
|
274 @menu |
|
275 * Empty Matrices:: |
|
276 @end menu |
|
277 |
4167
|
278 @node Empty Matrices |
3294
|
279 @subsection Empty Matrices |
|
280 |
|
281 A matrix may have one or both dimensions zero, and operations on empty |
|
282 matrices are handled as described by Carl de Boor in @cite{An Empty |
|
283 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. |
|
284 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty |
|
285 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, |
|
286 Number 5, May 1993. |
|
287 @iftex |
|
288 @tex |
|
289 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, |
|
290 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or |
|
291 both dimensions equal to zero), the following are true: |
|
292 $$ |
|
293 \eqalign{% |
|
294 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr |
|
295 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr |
|
296 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr |
|
297 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr |
|
298 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} |
|
299 $$ |
|
300 @end tex |
|
301 @end iftex |
|
302 @ifinfo |
|
303 Briefly, given a scalar @var{s}, an @var{m} by |
|
304 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix |
|
305 @code{[](mxn)} (with either one or both dimensions equal to zero), the |
|
306 following are true: |
|
307 |
|
308 @example |
|
309 @group |
|
310 s * [](mxn) = [](mxn) * s = [](mxn) |
|
311 |
|
312 [](mxn) + [](mxn) = [](mxn) |
|
313 |
|
314 [](0xm) * M(mxn) = [](0xn) |
|
315 |
|
316 M(mxn) * [](nx0) = [](mx0) |
|
317 |
|
318 [](mx0) * [](0xn) = 0(mxn) |
|
319 @end group |
|
320 @end example |
|
321 @end ifinfo |
|
322 |
|
323 By default, dimensions of the empty matrix are printed along with the |
|
324 empty matrix symbol, @samp{[]}. The built-in variable |
|
325 @code{print_empty_dimensions} controls this behavior. |
|
326 |
3321
|
327 @DOCSTRING(print_empty_dimensions) |
3294
|
328 |
|
329 Empty matrices may also be used in assignment statements as a convenient |
|
330 way to delete rows or columns of matrices. |
|
331 @xref{Assignment Ops, ,Assignment Expressions}. |
|
332 |
|
333 When Octave parses a matrix expression, it examines the elements of the |
|
334 list to determine whether they are all constants. If they are, it |
|
335 replaces the list with a single matrix constant. |
|
336 |
4167
|
337 @node Ranges |
3294
|
338 @section Ranges |
|
339 @cindex range expressions |
|
340 @cindex expression, range |
|
341 |
3920
|
342 @opindex colon |
3294
|
343 |
|
344 A @dfn{range} is a convenient way to write a row vector with evenly |
|
345 spaced elements. A range expression is defined by the value of the first |
|
346 element in the range, an optional value for the increment between |
|
347 elements, and a maximum value which the elements of the range will not |
|
348 exceed. The base, increment, and limit are separated by colons (the |
|
349 @samp{:} character) and may contain any arithmetic expressions and |
|
350 function calls. If the increment is omitted, it is assumed to be 1. |
|
351 For example, the range |
|
352 |
|
353 @example |
|
354 1 : 5 |
|
355 @end example |
|
356 |
|
357 @noindent |
|
358 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range |
|
359 |
|
360 @example |
|
361 1 : 3 : 5 |
|
362 @end example |
|
363 |
|
364 @noindent |
|
365 defines the set of values @samp{[ 1, 4 ]}. |
|
366 |
|
367 Although a range constant specifies a row vector, Octave does @emph{not} |
|
368 convert range constants to vectors unless it is necessary to do so. |
|
369 This allows you to write a constant like @samp{1 : 10000} without using |
|
370 80,000 bytes of storage on a typical 32-bit workstation. |
|
371 |
|
372 Note that the upper (or lower, if the increment is negative) bound on |
|
373 the range is not always included in the set of values, and that ranges |
|
374 defined by floating point values can produce surprising results because |
|
375 Octave uses floating point arithmetic to compute the values in the |
|
376 range. If it is important to include the endpoints of a range and the |
|
377 number of elements is known, you should use the @code{linspace} function |
|
378 instead (@pxref{Special Utility Matrices}). |
|
379 |
|
380 When Octave parses a range expression, it examines the elements of the |
|
381 expression to determine whether they are all constants. If they are, it |
|
382 replaces the range expression with a single range constant. |
|
383 |
6549
|
384 @node Integer Data Types |
|
385 @section Integer Data Types |
|
386 |
|
387 @DOCSTRING(isinteger) |
|
388 |
|
389 @DOCSTRING(int8) |
|
390 |
|
391 @DOCSTRING(uint8) |
|
392 |
|
393 @DOCSTRING(int16) |
|
394 |
|
395 @DOCSTRING(uint16) |
|
396 |
|
397 @DOCSTRING(int32) |
|
398 |
|
399 @DOCSTRING(uint32) |
|
400 |
|
401 @DOCSTRING(int64) |
|
402 |
|
403 @DOCSTRING(uint64) |
|
404 |
|
405 @DOCSTRING(intmax) |
|
406 |
|
407 @DOCSTRING(intmin) |
|
408 |
4167
|
409 @node Logical Values |
3428
|
410 @section Logical Values |
|
411 |
6549
|
412 @DOCSTRING(logical) |
|
413 |
3428
|
414 @DOCSTRING(true) |
|
415 |
|
416 @DOCSTRING(false) |
|
417 |
4167
|
418 @node Predicates for Numeric Objects |
3294
|
419 @section Predicates for Numeric Objects |
|
420 |
3428
|
421 @DOCSTRING(isnumeric) |
|
422 |
|
423 @DOCSTRING(isreal) |
|
424 |
4029
|
425 @DOCSTRING(iscomplex) |
3428
|
426 |
4029
|
427 @DOCSTRING(ismatrix) |
3294
|
428 |
4029
|
429 @DOCSTRING(isvector) |
3294
|
430 |
4029
|
431 @DOCSTRING(isscalar) |
3294
|
432 |
4029
|
433 @DOCSTRING(issquare) |
3294
|
434 |
4029
|
435 @DOCSTRING(issymmetric) |
3428
|
436 |
6550
|
437 @DOCSTRING(isdefinite) |
|
438 |
4029
|
439 @DOCSTRING(isbool) |
6550
|
440 |
|
441 @DOCSTRING(isprime) |