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