[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 4. Numeric Data Types

A 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:

    105
    1.05e+2
    1050e-1

To specify complex constants, you can write an expression of the form

    3 + 4i
    3.0 + 4.0i
    0.3e1 + 40e-1i

all of which are equivalent. The letter ``i`' in the previous example stands for the pure imaginary constant, defined as `sqrt (-1)`.

For Octave to recognize a value as the imaginary part of a complex constant, a space must not appear between the number and the ``i`'. If it does, Octave will print an error message, like this:

    octave:13> 3 + 4 i

    parse error:

      3 + 4 i
            ^

You may also use ``j`', ``I`', or ``J`' in place of the ``i`' above. All four forms are equivalent.

4.1 Matrices |  |
4.2 Ranges |  |
4.3 Logical Values |  |
4.4 Predicates for Numeric Objects |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.1 Matrices

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

    a = [1, 2; 3, 4]

results in the matrix

            /      \
            | 1  2 |
      a  =  |      |
            | 3  4 |
            \      /

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

    [ a, a ]

produces the matrix

    ans =

      1  2  1  2
      3  4  3  4

but the expression

    [ a, 1 ]

produces the error

    error: number of rows must match near line 13, column 6

(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

    a = [ 1 2
          3 4 ]

will work. However, some possible sources of confusion remain. For example, in the expression

    [ 1 - 1 ]

the ``-`' is treated as a binary operator and the result is the scalar 0, but in the expression

    [ 1 -1 ]

the ``-`' is treated as a unary operator and the result is the vector `[ 1, -1 ]`. Similarly, the expression

    [ sin (pi) ]

will be parsed as

    [ sin, (pi) ]

and will result in an error since the `sin` function will be called with no arguments. To get around this, you must omit the space between `sin` and the opening parenthesis, or enclose the expression in a set of parentheses:

    [ (sin (pi)) ]

Whitespace surrounding the single quote character (``'`', used as a transpose operator and for delimiting character strings) can also cause confusion. Given `a = 1`, the expression

    [ 1 a' ]

results in the single quote character being treated as a transpose operator and the result is the vector `[ 1, 1 ]`, but the expression

    [ 1 a ' ]

produces the error message

    error: unterminated string constant

because to not do so would cause trouble when parsing the valid expression

    [ a 'foo' ]

For clarity, it is probably best to always use commas and semicolons to separate matrix elements and rows.

Built-in Variable: warn_separator_insert
  Print warning if commas or semicolons might be inserted automatically in literal matrices.

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.

Built-in Variable: output_max_field_width
  This variable specifies the maximum width of a numeric output field. The default value is 10.

Built-in Variable: output_precision
  This variable specifies the minimum number of significant figures to display for numeric output. The default value is 5.

It is possible to achieve a wide range of output styles by using different values of `output_precision` and `output_max_field_width`. Reasonable combinations can be set using the `format` function. See section 16.1 Basic Input and Output.

Built-in Variable: split_long_rows
  For large matrices, Octave may not be able to display all the columns of a given row on one line of your screen. This can result in missing information or output that is nearly impossible to decipher, depending on whether your terminal truncates or wraps long lines.

  If the value of `split_long_rows` is nonzero, Octave will display the matrix in a series of smaller pieces, each of which can fit within the limits of your terminal width. Each set of rows is labeled so that you can easily see which columns are currently being displayed. For example:

      octave:13> rand (2,10)
      ans =

       Columns 1 through 6:

        0.75883  0.93290  0.40064  0.43818  0.94958  0.16467
        0.75697  0.51942  0.40031  0.61784  0.92309  0.40201

       Columns 7 through 10:

        0.90174  0.11854  0.72313  0.73326
        0.44672  0.94303  0.56564  0.82150

  The default value of `split_long_rows` is nonzero.

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 `fixed_point_format` to a nonzero value. But doing so is not recommended, because it can produce output that can easily be misinterpreted.

Built-in Variable: fixed_point_format
  If the value of this variable is nonzero, Octave will scale all values in a matrix so that the largest may be written with one leading digit. The scaling factor is printed on the first line of output. For example,

      octave:1> logspace (1, 7, 5)'
      ans =

        1.0e+07  *

        0.00000
        0.00003
        0.00100
        0.03162
        1.00000

  Notice that first value appears to be zero when it is actually 1. For this reason, you should be careful when setting `fixed_point_format` to a nonzero value.

  The default value of `fixed_point_format` is 0.

4.1.1 Empty Matrices |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.1.1 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 An Empty Exercise, SIGNUM, Volume 25, pages 2--6, 1990 and C. N. Nett and W. M. Haddad, in A System-Theoretic Appropriate Realization of the Empty Matrix Concept, IEEE Transactions on Automatic Control, Volume 38, Number 5, May 1993. Briefly, given a scalar s, an m by n matrix `M(mxn)`, and an m by n empty matrix `[](mxn)` (with either one or both dimensions equal to zero), the following are true:

    s * [](mxn) = [](mxn) * s = [](mxn)

        [](mxn) + [](mxn) = [](mxn)

        [](0xm) *  M(mxn) = [](0xn)

         M(mxn) * [](nx0) = [](mx0)

        [](mx0) * [](0xn) =  0(mxn)

By default, dimensions of the empty matrix are printed along with the empty matrix symbol, ``[]`'. The built-in variable `print_empty_dimensions` controls this behavior.

Built-in Variable: print_empty_dimensions
  If the value of `print_empty_dimensions` is nonzero, the dimensions of empty matrices are printed along with the empty matrix symbol, ``[]`'. For example, the expression

      zeros (3, 0)

  will print

      ans = [](3x0)

Empty matrices may also be used in assignment statements as a convenient way to delete rows or columns of matrices. See section Assignment Expressions.

Built-in Variable: warn_empty_list_elements
  If the value of `warn_empty_list_elements` is nonzero, print a warning when an empty matrix is found in a matrix list. For example,

      a = [1, [], 3, [], 5]

  The default value is 0.

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.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.2 Ranges

A 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 ``:`' 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

    1 : 5

defines the set of values ``[ 1, 2, 3, 4, 5 ]`', and the range

    1 : 3 : 5

defines the set of values ``[ 1, 4 ]`'.

Although a range constant specifies a row vector, Octave does not convert range constants to vectors unless it is necessary to do so. This allows you to write a constant like ``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 `linspace` function instead (see section 18.3 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.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.3 Logical Values

Built-in Variable: true
  Logical true value.

Built-in Variable: false
  Logical false value.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 4.4 Predicates for Numeric Objects

Built-in Function: isnumeric (x)
  Return nonzero if x is a numeric object.

Built-in Function: isreal (x)
  Return true if x is a real-valued numeric object.

Built-in Function: iscomplex (x)
  Return true if x is a complex-valued numeric object.

Built-in Function: ismatrix (a)
  Return 1 if a is a matrix. Otherwise, return 0.

Function File: isvector (a)
  Return 1 if a is a vector. Otherwise, return 0.

Function File: isscalar (a)
  Return 1 if a is a scalar. Otherwise, return 0.

Function File: issquare (x)
  If x is a square matrix, then return the dimension of x. Otherwise, return 0.

Function File: issymmetric (x, tol)
  If x is symmetric within the tolerance specified by tol, then return the dimension of x. Otherwise, return 0. If tol is omitted, use a tolerance equal to the machine precision.

Built-in Functio: isbool (x)
  Return true if x is a boolean object.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by John W. Eaton on March, 28 2005 using texi2html