# NAG Library Function Document

## 1Purpose

nag_dgebal (f08nhc) balances a real general matrix in order to improve the accuracy of computed eigenvalues and/or eigenvectors.

## 2Specification

 #include #include
 void nag_dgebal (Nag_OrderType order, Nag_JobType job, Integer n, double a[], Integer pda, Integer *ilo, Integer *ihi, double scale[], NagError *fail)

## 3Description

nag_dgebal (f08nhc) balances a real general matrix $A$. The term ‘balancing’ covers two steps, each of which involves a similarity transformation of $A$. The function can perform either or both of these steps.
1. The function first attempts to permute $A$ to block upper triangular form by a similarity transformation:
 $PAPT = A′ = A11′ A12′ A13′ 0 A22′ A23′ 0 0 A33′$
where $P$ is a permutation matrix, and ${A}_{11}^{\prime }$ and ${A}_{33}^{\prime }$ are upper triangular. Then the diagonal elements of ${A}_{11}^{\prime }$ and ${A}_{33}^{\prime }$ are eigenvalues of $A$. The rest of the eigenvalues of $A$ are the eigenvalues of the central diagonal block ${A}_{22}^{\prime }$, in rows and columns ${i}_{\mathrm{lo}}$ to ${i}_{\mathrm{hi}}$. Subsequent operations to compute the eigenvalues of $A$ (or its Schur factorization) need only be applied to these rows and columns; this can save a significant amount of work if ${i}_{\mathrm{lo}}>1$ and ${i}_{\mathrm{hi}}. If no suitable permutation exists (as is often the case), the function sets ${i}_{\mathrm{lo}}=1$ and ${i}_{\mathrm{hi}}=n$, and ${A}_{22}^{\prime }$ is the whole of $A$.
2. The function applies a diagonal similarity transformation to ${A}^{\prime }$, to make the rows and columns of ${A}_{22}^{\prime }$ as close in norm as possible:
 $A′′ = DA′D-1 = I 0 0 0 D22 0 0 0 I A11′ A12′ A13′ 0 A22′ A23′ 0 0 A33′ I 0 0 0 D22-1 0 0 0 I .$
This scaling can reduce the norm of the matrix (i.e., $‖{A}_{22}^{\prime \prime }‖<‖{A}_{22}^{\prime }‖$) and hence reduce the effect of rounding errors on the accuracy of computed eigenvalues and eigenvectors.

## 4References

Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## 5Arguments

1:    $\mathbf{order}$Nag_OrderTypeInput
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.3.1.3 in How to Use the NAG Library and its Documentation for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2:    $\mathbf{job}$Nag_JobTypeInput
On entry: indicates whether $A$ is to be permuted and/or scaled (or neither).
${\mathbf{job}}=\mathrm{Nag_DoNothing}$
$A$ is neither permuted nor scaled (but values are assigned to ilo, ihi and scale).
${\mathbf{job}}=\mathrm{Nag_Permute}$
$A$ is permuted but not scaled.
${\mathbf{job}}=\mathrm{Nag_Scale}$
$A$ is scaled but not permuted.
${\mathbf{job}}=\mathrm{Nag_DoBoth}$
$A$ is both permuted and scaled.
Constraint: ${\mathbf{job}}=\mathrm{Nag_DoNothing}$, $\mathrm{Nag_Permute}$, $\mathrm{Nag_Scale}$ or $\mathrm{Nag_DoBoth}$.
3:    $\mathbf{n}$IntegerInput
On entry: $n$, the order of the matrix $A$.
Constraint: ${\mathbf{n}}\ge 0$.
4:    $\mathbf{a}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array a must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pda}}×{\mathbf{n}}\right)$.
Where ${\mathbf{A}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{a}}\left[\left(j-1\right)×{\mathbf{pda}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{a}}\left[\left(i-1\right)×{\mathbf{pda}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the $n$ by $n$ matrix $A$.
On exit: a is overwritten by the balanced matrix. If ${\mathbf{job}}=\mathrm{Nag_DoNothing}$, a is not referenced.
5:    $\mathbf{pda}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array a.
Constraint: ${\mathbf{pda}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
6:    $\mathbf{ilo}$Integer *Output
7:    $\mathbf{ihi}$Integer *Output
On exit: the values ${i}_{\mathrm{lo}}$ and ${i}_{\mathrm{hi}}$ such that on exit ${\mathbf{A}}\left(i,j\right)$ is zero if $i>j$ and $1\le j<{i}_{\mathrm{lo}}$ or ${i}_{\mathrm{hi}}.
If ${\mathbf{job}}=\mathrm{Nag_DoNothing}$ or $\mathrm{Nag_Scale}$, ${i}_{\mathrm{lo}}=1$ and ${i}_{\mathrm{hi}}=n$.
8:    $\mathbf{scale}\left[{\mathbf{n}}\right]$doubleOutput
On exit: details of the permutations and scaling factors applied to $A$. More precisely, if ${p}_{j}$ is the index of the row and column interchanged with row and column $j$ and ${d}_{j}$ is the scaling factor used to balance row and column $j$ then
 $scale[j-1] = pj, j=1,2,…,ilo-1 dj, j=ilo,ilo+1,…,ihi and pj, j=ihi+1,ihi+2,…,n.$
The order in which the interchanges are made is $n$ to ${i}_{\mathrm{hi}}+1$ then $1$ to ${i}_{\mathrm{lo}}-1$.
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
On entry, ${\mathbf{pda}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pda}}>0$.
NE_INT_2
On entry, ${\mathbf{pda}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pda}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The errors are negligible.

## 8Parallelism and Performance

nag_dgebal (f08nhc) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

If the matrix $A$ is balanced by nag_dgebal (f08nhc), then any eigenvectors computed subsequently are eigenvectors of the matrix ${A}^{\prime \prime }$ (see Section 3) and hence nag_dgebak (f08njc) must then be called to transform them back to eigenvectors of $A$.
If the Schur vectors of $A$ are required, then this function must not be called with ${\mathbf{job}}=\mathrm{Nag_Scale}$ or $\mathrm{Nag_DoBoth}$, because then the balancing transformation is not orthogonal. If this function is called with ${\mathbf{job}}=\mathrm{Nag_Permute}$, then any Schur vectors computed subsequently are Schur vectors of the matrix ${A}^{\prime \prime }$, and nag_dgebak (f08njc) must be called (with ${\mathbf{side}}=\mathrm{Nag_RightSide}$) to transform them back to Schur vectors of $A$.
The total number of floating-point operations is approximately proportional to ${n}^{2}$.
The complex analogue of this function is nag_zgebal (f08nvc).

## 10Example

This example computes all the eigenvalues and right eigenvectors of the matrix $A$, where
 $A = 5.14 0.91 0.00 -32.80 0.91 0.20 0.00 34.50 1.90 0.80 -0.40 -3.00 -0.33 0.35 0.00 0.66 .$
The program first calls nag_dgebal (f08nhc) to balance the matrix; it then computes the Schur factorization of the balanced matrix, by reduction to Hessenberg form and the $QR$ algorithm. Then it calls nag_dtrevc (f08qkc) to compute the right eigenvectors of the balanced matrix, and finally calls nag_dgebak (f08njc) to transform the eigenvectors back to eigenvectors of the original matrix $A$.

### 10.1Program Text

Program Text (f08nhce.c)

### 10.2Program Data

Program Data (f08nhce.d)

### 10.3Program Results

Program Results (f08nhce.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017