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 |
4476
|
155 simply ignored, so an expression like |
3294
|
156 |
|
157 @example |
|
158 @group |
|
159 a = [ 1 2 |
|
160 3 4 ] |
|
161 @end group |
|
162 @end example |
|
163 |
|
164 @noindent |
|
165 will work. However, some possible sources of confusion remain. For |
|
166 example, in the expression |
|
167 |
|
168 @example |
|
169 [ 1 - 1 ] |
|
170 @end example |
|
171 |
|
172 @noindent |
|
173 the @samp{-} is treated as a binary operator and the result is the |
|
174 scalar 0, but in the expression |
|
175 |
|
176 @example |
|
177 [ 1 -1 ] |
|
178 @end example |
|
179 |
|
180 @noindent |
|
181 the @samp{-} is treated as a unary operator and the result is the |
4476
|
182 vector @code{[ 1, -1 ]}. Similarly, the expression |
|
183 |
|
184 @example |
|
185 [ sin (pi) ] |
|
186 @end example |
|
187 |
|
188 @noindent |
|
189 will be parsed as |
|
190 |
|
191 @example |
|
192 [ sin, (pi) ] |
|
193 @end example |
3294
|
194 |
4476
|
195 @noindent |
|
196 and will result in an error since the @code{sin} function will be |
|
197 called with no arguments. To get around this, you must omit the space |
|
198 between @code{sin} and the opening parenthesis, or enclose the |
|
199 expression in a set of parentheses: |
|
200 |
|
201 @example |
|
202 [ (sin (pi)) ] |
|
203 @end example |
|
204 |
|
205 Whitespace surrounding the single quote character (@samp{'}, used as a |
|
206 transpose operator and for delimiting character strings) can also cause |
|
207 confusion. Given @code{a = 1}, the expression |
3294
|
208 |
|
209 @example |
|
210 [ 1 a' ] |
|
211 @end example |
|
212 |
|
213 @noindent |
4476
|
214 results in the single quote character being treated as a |
3294
|
215 transpose operator and the result is the vector @code{[ 1, 1 ]}, but the |
|
216 expression |
|
217 |
|
218 @example |
|
219 [ 1 a ' ] |
|
220 @end example |
|
221 |
|
222 @noindent |
|
223 produces the error message |
|
224 |
|
225 @example |
|
226 error: unterminated string constant |
|
227 @end example |
|
228 |
|
229 @noindent |
4476
|
230 because to not do so would cause trouble when parsing the valid expression |
3294
|
231 |
|
232 @example |
|
233 [ a 'foo' ] |
|
234 @end example |
|
235 |
|
236 For clarity, it is probably best to always use commas and semicolons to |
4476
|
237 separate matrix elements and rows. |
3294
|
238 |
3428
|
239 @DOCSTRING(warn_separator_insert) |
|
240 |
3294
|
241 When you type a matrix or the name of a variable whose value is a |
|
242 matrix, Octave responds by printing the matrix in with neatly aligned |
|
243 rows and columns. If the rows of the matrix are too large to fit on the |
|
244 screen, Octave splits the matrix and displays a header before each |
|
245 section to indicate which columns are being displayed. You can use the |
|
246 following variables to control the format of the output. |
|
247 |
3321
|
248 @DOCSTRING(output_max_field_width) |
3294
|
249 |
3321
|
250 @DOCSTRING(output_precision) |
3294
|
251 |
|
252 It is possible to achieve a wide range of output styles by using |
|
253 different values of @code{output_precision} and |
|
254 @code{output_max_field_width}. Reasonable combinations can be set using |
|
255 the @code{format} function. @xref{Basic Input and Output}. |
|
256 |
3321
|
257 @DOCSTRING(split_long_rows) |
3294
|
258 |
|
259 Octave automatically switches to scientific notation when values become |
|
260 very large or very small. This guarantees that you will see several |
|
261 significant figures for every value in a matrix. If you would prefer to |
|
262 see all values in a matrix printed in a fixed point format, you can set |
|
263 the built-in variable @code{fixed_point_format} to a nonzero value. But |
|
264 doing so is not recommended, because it can produce output that can |
|
265 easily be misinterpreted. |
|
266 |
3321
|
267 @DOCSTRING(fixed_point_format) |
3294
|
268 |
|
269 @menu |
|
270 * Empty Matrices:: |
|
271 @end menu |
|
272 |
4167
|
273 @node Empty Matrices |
3294
|
274 @subsection Empty Matrices |
|
275 |
|
276 A matrix may have one or both dimensions zero, and operations on empty |
|
277 matrices are handled as described by Carl de Boor in @cite{An Empty |
|
278 Exercise}, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. |
|
279 Haddad, in @cite{A System-Theoretic Appropriate Realization of the Empty |
|
280 Matrix Concept}, IEEE Transactions on Automatic Control, Volume 38, |
|
281 Number 5, May 1993. |
|
282 @iftex |
|
283 @tex |
|
284 Briefly, given a scalar $s$, an $m\times n$ matrix $M_{m\times n}$, |
|
285 and an $m\times n$ empty matrix $[\,]_{m\times n}$ (with either one or |
|
286 both dimensions equal to zero), the following are true: |
|
287 $$ |
|
288 \eqalign{% |
|
289 s \cdot [\,]_{m\times n} = [\,]_{m\times n} \cdot s &= [\,]_{m\times n}\cr |
|
290 [\,]_{m\times n} + [\,]_{m\times n} &= [\,]_{m\times n}\cr |
|
291 [\,]_{0\times m} \cdot M_{m\times n} &= [\,]_{0\times n}\cr |
|
292 M_{m\times n} \cdot [\,]_{n\times 0} &= [\,]_{m\times 0}\cr |
|
293 [\,]_{m\times 0} \cdot [\,]_{0\times n} &= 0_{m\times n}} |
|
294 $$ |
|
295 @end tex |
|
296 @end iftex |
|
297 @ifinfo |
|
298 Briefly, given a scalar @var{s}, an @var{m} by |
|
299 @var{n} matrix @code{M(mxn)}, and an @var{m} by @var{n} empty matrix |
|
300 @code{[](mxn)} (with either one or both dimensions equal to zero), the |
|
301 following are true: |
|
302 |
|
303 @example |
|
304 @group |
|
305 s * [](mxn) = [](mxn) * s = [](mxn) |
|
306 |
|
307 [](mxn) + [](mxn) = [](mxn) |
|
308 |
|
309 [](0xm) * M(mxn) = [](0xn) |
|
310 |
|
311 M(mxn) * [](nx0) = [](mx0) |
|
312 |
|
313 [](mx0) * [](0xn) = 0(mxn) |
|
314 @end group |
|
315 @end example |
|
316 @end ifinfo |
|
317 |
|
318 By default, dimensions of the empty matrix are printed along with the |
|
319 empty matrix symbol, @samp{[]}. The built-in variable |
|
320 @code{print_empty_dimensions} controls this behavior. |
|
321 |
3321
|
322 @DOCSTRING(print_empty_dimensions) |
3294
|
323 |
|
324 Empty matrices may also be used in assignment statements as a convenient |
|
325 way to delete rows or columns of matrices. |
|
326 @xref{Assignment Ops, ,Assignment Expressions}. |
|
327 |
4460
|
328 @DOCSTRING(warn_empty_list_elements) |
3294
|
329 |
|
330 When Octave parses a matrix expression, it examines the elements of the |
|
331 list to determine whether they are all constants. If they are, it |
|
332 replaces the list with a single matrix constant. |
|
333 |
4167
|
334 @node Ranges |
3294
|
335 @section Ranges |
|
336 @cindex range expressions |
|
337 @cindex expression, range |
|
338 |
3920
|
339 @opindex colon |
3294
|
340 |
|
341 A @dfn{range} is a convenient way to write a row vector with evenly |
|
342 spaced elements. A range expression is defined by the value of the first |
|
343 element in the range, an optional value for the increment between |
|
344 elements, and a maximum value which the elements of the range will not |
|
345 exceed. The base, increment, and limit are separated by colons (the |
|
346 @samp{:} character) and may contain any arithmetic expressions and |
|
347 function calls. If the increment is omitted, it is assumed to be 1. |
|
348 For example, the range |
|
349 |
|
350 @example |
|
351 1 : 5 |
|
352 @end example |
|
353 |
|
354 @noindent |
|
355 defines the set of values @samp{[ 1, 2, 3, 4, 5 ]}, and the range |
|
356 |
|
357 @example |
|
358 1 : 3 : 5 |
|
359 @end example |
|
360 |
|
361 @noindent |
|
362 defines the set of values @samp{[ 1, 4 ]}. |
|
363 |
|
364 Although a range constant specifies a row vector, Octave does @emph{not} |
|
365 convert range constants to vectors unless it is necessary to do so. |
|
366 This allows you to write a constant like @samp{1 : 10000} without using |
|
367 80,000 bytes of storage on a typical 32-bit workstation. |
|
368 |
|
369 Note that the upper (or lower, if the increment is negative) bound on |
|
370 the range is not always included in the set of values, and that ranges |
|
371 defined by floating point values can produce surprising results because |
|
372 Octave uses floating point arithmetic to compute the values in the |
|
373 range. If it is important to include the endpoints of a range and the |
|
374 number of elements is known, you should use the @code{linspace} function |
|
375 instead (@pxref{Special Utility Matrices}). |
|
376 |
|
377 When Octave parses a range expression, it examines the elements of the |
|
378 expression to determine whether they are all constants. If they are, it |
|
379 replaces the range expression with a single range constant. |
|
380 |
4167
|
381 @node Logical Values |
3428
|
382 @section Logical Values |
|
383 |
|
384 @DOCSTRING(true) |
|
385 |
|
386 @DOCSTRING(false) |
|
387 |
4167
|
388 @node Predicates for Numeric Objects |
3294
|
389 @section Predicates for Numeric Objects |
|
390 |
3428
|
391 @DOCSTRING(isnumeric) |
|
392 |
|
393 @DOCSTRING(isreal) |
|
394 |
4029
|
395 @DOCSTRING(iscomplex) |
3428
|
396 |
4029
|
397 @DOCSTRING(ismatrix) |
3294
|
398 |
4029
|
399 @DOCSTRING(isvector) |
3294
|
400 |
4029
|
401 @DOCSTRING(isscalar) |
3294
|
402 |
4029
|
403 @DOCSTRING(issquare) |
3294
|
404 |
4029
|
405 @DOCSTRING(issymmetric) |
3428
|
406 |
4029
|
407 @DOCSTRING(isbool) |