### 21.1 Creating and Manipulating Diagonal/Permutation Matrices

A diagonal matrix is defined as a matrix that has zero entries outside the main diagonal; that is, `D(i,j) == 0` if `i != j`. Most often, square diagonal matrices are considered; however, the definition can equally be applied to non-square matrices, in which case we usually speak of a rectangular diagonal matrix.

A permutation matrix is defined as a square matrix that has a single element equal to unity in each row and each column; all other elements are zero. That is, there exists a permutation (vector) `p` such that `P(i,j) == 1` if `j == p(i)` and `P(i,j) == 0` otherwise.

Octave provides special treatment of real and complex rectangular diagonal matrices, as well as permutation matrices. They are stored as special objects, using efficient storage and algorithms, facilitating writing both readable and efficient matrix algebra expressions in the Octave language. The special treatment may be disabled by using the functions disable_diagonal_matrix and disable_permutation_matrix.

Built-in Function: val = disable_diagonal_matrix ()
Built-in Function: old_val = disable_diagonal_matrix (new_val)
Built-in Function: disable_diagonal_matrix (new_val, "local")

  Query or set the internal variable that controls whether diagonal matrices are stored in a special space-efficient format. The default value is true. If this option is disabled Octave will store diagonal matrices as full matrices.

  When called from inside a function with the `"local"` option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

  See also: disable_range, disable_permutation_matrix.

Built-in Function: val = disable_permutation_matrix ()
Built-in Function: old_val = disable_permutation_matrix (new_val)
Built-in Function: disable_permutation_matrix (new_val, "local")

  Query or set the internal variable that controls whether permutation matrices are stored in a special space-efficient format. The default value is true. If this option is disabled Octave will store permutation matrices as full matrices.

  When called from inside a function with the `"local"` option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

  See also: disable_range, disable_diagonal_matrix.

The space savings are significant as demonstrated by the following code.

    x = diag (rand (10, 1));
    xf = full (x);
    sizeof (x)
    ⇒ 80
    sizeof (xf)
    ⇒ 800