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 |
|
68 @menu |
|
69 * Matrices:: |
|
70 * Ranges:: |
3428
|
71 * Logical Values:: |
3294
|
72 * Predicates for Numeric Objects:: |
|
73 @end menu |
|
74 |
4167
|
75 @node Matrices |
3294
|
76 @section Matrices |
|
77 @cindex matrices |
|
78 |
|
79 @opindex [ |
|
80 @opindex ] |
|
81 @opindex ; |
|
82 @opindex , |
|
83 |
|
84 It is easy to define a matrix of values in Octave. The size of the |
|
85 matrix is determined automatically, so it is not necessary to explicitly |
|
86 state the dimensions. The expression |
|
87 |
|
88 @example |
|
89 a = [1, 2; 3, 4] |
|
90 @end example |
|
91 |
|
92 @noindent |
|
93 results in the matrix |
|
94 @iftex |
|
95 @tex |
|
96 $$ a = \left[ \matrix{ 1 & 2 \cr 3 & 4 } \right] $$ |
|
97 @end tex |
|
98 @end iftex |
|
99 @ifinfo |
|
100 |
|
101 @example |
|
102 @group |
|
103 |
|
104 / \ |
|
105 | 1 2 | |
|
106 a = | | |
|
107 | 3 4 | |
|
108 \ / |
|
109 |
|
110 @end group |
|
111 @end example |
|
112 @end ifinfo |
|
113 |
|
114 Elements of a matrix may be arbitrary expressions, provided that the |
|
115 dimensions all make sense when combining the various pieces. For |
|
116 example, given the above matrix, the expression |
|
117 |
|
118 @example |
|
119 [ a, a ] |
|
120 @end example |
|
121 |
|
122 @noindent |
|
123 produces the matrix |
|
124 |
|
125 @example |
|
126 @group |
|
127 ans = |
|
128 |
|
129 1 2 1 2 |
|
130 3 4 3 4 |
|
131 @end group |
|
132 @end example |
|
133 |
|
134 @noindent |
|
135 but the expression |
|
136 |
|
137 @example |
|
138 [ a, 1 ] |
|
139 @end example |
|
140 |
|
141 @noindent |
|
142 produces the error |
|
143 |
|
144 @example |
|
145 error: number of rows must match near line 13, column 6 |
|
146 @end example |
|
147 |
|
148 @noindent |
|
149 (assuming that this expression was entered as the first thing on line |
|
150 13, of course). |
|
151 |
|
152 Inside the square brackets that delimit a matrix expression, Octave |
|
153 looks at the surrounding context to determine whether spaces and newline |
|
154 characters should be converted into element and row separators, or |
|
155 simply ignored, so commands like |
|
156 |
|
157 @example |
|
158 [ linspace (1, 2) ] |
|
159 @end example |
|
160 |
|
161 @noindent |
|
162 and |
|
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 |
|
189 vector @code{[ 1, -1 ]}. |
|
190 |
|
191 Given @code{a = 1}, the expression |
|
192 |
|
193 @example |
|
194 [ 1 a' ] |
|
195 @end example |
|
196 |
|
197 @noindent |
|
198 results in the single quote character @samp{'} being treated as a |
|
199 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
|
200 expression |
|
201 |
|
202 @example |
|
203 [ 1 a ' ] |
|
204 @end example |
|
205 |
|
206 @noindent |
|
207 produces the error message |
|
208 |
|
209 @example |
|
210 error: unterminated string constant |
|
211 @end example |
|
212 |
|
213 @noindent |
|
214 because to not do so would make it impossible to correctly parse the |
|
215 valid expression |
|
216 |
|
217 @example |
|
218 [ a 'foo' ] |
|
219 @end example |
|
220 |
|
221 For clarity, it is probably best to always use commas and semicolons to |
|
222 separate matrix elements and rows. It is possible to enforce this style |
|
223 by setting the built-in variable @code{whitespace_in_literal_matrix} to |
|
224 @code{"ignore"}. |
|
225 |
3321
|
226 @DOCSTRING(whitespace_in_literal_matrix) |
3294
|
227 |
3428
|
228 @DOCSTRING(warn_separator_insert) |
|
229 |
3294
|
230 When you type a matrix or the name of a variable whose value is a |
|
231 matrix, Octave responds by printing the matrix in with neatly aligned |
|
232 rows and columns. If the rows of the matrix are too large to fit on the |
|
233 screen, Octave splits the matrix and displays a header before each |
|
234 section to indicate which columns are being displayed. You can use the |
|
235 following variables to control the format of the output. |
|
236 |
3321
|
237 @DOCSTRING(output_max_field_width) |
3294
|
238 |
3321
|
239 @DOCSTRING(output_precision) |
3294
|
240 |
|
241 It is possible to achieve a wide range of output styles by using |
|
242 different values of @code{output_precision} and |
|
243 @code{output_max_field_width}. Reasonable combinations can be set using |
|
244 the @code{format} function. @xref{Basic Input and Output}. |
|
245 |
3321
|
246 @DOCSTRING(split_long_rows) |
3294
|
247 |
|
248 Octave automatically switches to scientific notation when values become |
|
249 very large or very small. This guarantees that you will see several |
|
250 significant figures for every value in a matrix. If you would prefer to |
|
251 see all values in a matrix printed in a fixed point format, you can set |
|
252 the built-in variable @code{fixed_point_format} to a nonzero value. But |
|
253 doing so is not recommended, because it can produce output that can |
|
254 easily be misinterpreted. |
|
255 |
3321
|
256 @DOCSTRING(fixed_point_format) |
3294
|
257 |
|
258 @menu |
|
259 * Empty Matrices:: |
|
260 @end menu |
|
261 |
4167
|
262 @node Empty Matrices |
3294
|
263 @subsection Empty Matrices |
|
264 |
|
265 A matrix may have one or both dimensions zero, and operations on empty |
|
266 matrices are handled as described by Carl de Boor in @cite{An Empty |
|
267 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. |
|
268 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty |
|
269 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, |
|
270 Number 5, May 1993. |
|
271 @iftex |
|
272 @tex |
|
273 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, |
|
274 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or |
|
275 both dimensions equal to zero), the following are true: |
|
276 $$ |
|
277 \eqalign{% |
|
278 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr |
|
279 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr |
|
280 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr |
|
281 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr |
|
282 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} |
|
283 $$ |
|
284 @end tex |
|
285 @end iftex |
|
286 @ifinfo |
|
287 Briefly, given a scalar @var{s}, an @var{m} by |
|
288 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix |
|
289 @code{[](mxn)} (with either one or both dimensions equal to zero), the |
|
290 following are true: |
|
291 |
|
292 @example |
|
293 @group |
|
294 s * [](mxn) = [](mxn) * s = [](mxn) |
|
295 |
|
296 [](mxn) + [](mxn) = [](mxn) |
|
297 |
|
298 [](0xm) * M(mxn) = [](0xn) |
|
299 |
|
300 M(mxn) * [](nx0) = [](mx0) |
|
301 |
|
302 [](mx0) * [](0xn) = 0(mxn) |
|
303 @end group |
|
304 @end example |
|
305 @end ifinfo |
|
306 |
|
307 By default, dimensions of the empty matrix are printed along with the |
|
308 empty matrix symbol, @samp{[]}. The built-in variable |
|
309 @code{print_empty_dimensions} controls this behavior. |
|
310 |
3321
|
311 @DOCSTRING(print_empty_dimensions) |
3294
|
312 |
|
313 Empty matrices may also be used in assignment statements as a convenient |
|
314 way to delete rows or columns of matrices. |
|
315 @xref{Assignment Ops, ,Assignment Expressions}. |
|
316 |
|
317 Octave will normally issue a warning if it finds an empty matrix in the |
|
318 list of elements that make up another matrix. You can use the variable |
|
319 @code{empty_list_elements_ok} to suppress the warning or to treat it as |
|
320 an error. |
|
321 |
3321
|
322 @DOCSTRING(empty_list_elements_ok) |
3294
|
323 |
|
324 When Octave parses a matrix expression, it examines the elements of the |
|
325 list to determine whether they are all constants. If they are, it |
|
326 replaces the list with a single matrix constant. |
|
327 |
3321
|
328 @DOCSTRING(propagate_empty_matrices) |
3294
|
329 |
4167
|
330 @node Ranges |
3294
|
331 @section Ranges |
|
332 @cindex range expressions |
|
333 @cindex expression, range |
|
334 |
3920
|
335 @opindex colon |
3294
|
336 |
|
337 A @dfn{range} is a convenient way to write a row vector with evenly |
|
338 spaced elements. A range expression is defined by the value of the first |
|
339 element in the range, an optional value for the increment between |
|
340 elements, and a maximum value which the elements of the range will not |
|
341 exceed. The base, increment, and limit are separated by colons (the |
|
342 @samp{:} character) and may contain any arithmetic expressions and |
|
343 function calls. If the increment is omitted, it is assumed to be 1. |
|
344 For example, the range |
|
345 |
|
346 @example |
|
347 1 : 5 |
|
348 @end example |
|
349 |
|
350 @noindent |
|
351 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range |
|
352 |
|
353 @example |
|
354 1 : 3 : 5 |
|
355 @end example |
|
356 |
|
357 @noindent |
|
358 defines the set of values @samp{[ 1, 4 ]}. |
|
359 |
|
360 Although a range constant specifies a row vector, Octave does @emph{not} |
|
361 convert range constants to vectors unless it is necessary to do so. |
|
362 This allows you to write a constant like @samp{1 : 10000} without using |
|
363 80,000 bytes of storage on a typical 32-bit workstation. |
|
364 |
|
365 Note that the upper (or lower, if the increment is negative) bound on |
|
366 the range is not always included in the set of values, and that ranges |
|
367 defined by floating point values can produce surprising results because |
|
368 Octave uses floating point arithmetic to compute the values in the |
|
369 range. If it is important to include the endpoints of a range and the |
|
370 number of elements is known, you should use the @code{linspace} function |
|
371 instead (@pxref{Special Utility Matrices}). |
|
372 |
|
373 When Octave parses a range expression, it examines the elements of the |
|
374 expression to determine whether they are all constants. If they are, it |
|
375 replaces the range expression with a single range constant. |
|
376 |
4167
|
377 @node Logical Values |
3428
|
378 @section Logical Values |
|
379 |
|
380 @DOCSTRING(true) |
|
381 |
|
382 @DOCSTRING(false) |
|
383 |
4167
|
384 @node Predicates for Numeric Objects |
3294
|
385 @section Predicates for Numeric Objects |
|
386 |
3428
|
387 @DOCSTRING(isnumeric) |
|
388 |
|
389 @DOCSTRING(isreal) |
|
390 |
4029
|
391 @DOCSTRING(iscomplex) |
3428
|
392 |
4029
|
393 @DOCSTRING(ismatrix) |
3294
|
394 |
4029
|
395 @DOCSTRING(isvector) |
3294
|
396 |
4029
|
397 @DOCSTRING(isscalar) |
3294
|
398 |
4029
|
399 @DOCSTRING(issquare) |
3294
|
400 |
4029
|
401 @DOCSTRING(issymmetric) |
3428
|
402 |
4029
|
403 @DOCSTRING(isbool) |