# NAG C Library Function Document

## 1Purpose

nag_dggbal (f08whc) balances a pair of real square matrices $\left(A,B\right)$ of order $n$. Balancing usually improves the accuracy of computed generalized eigenvalues and eigenvectors.

## 2Specification

 #include #include
 void nag_dggbal (Nag_OrderType order, Nag_JobType job, Integer n, double a[], Integer pda, double b[], Integer pdb, Integer *ilo, Integer *ihi, double lscale[], double rscale[], NagError *fail)

## 3Description

Balancing may reduce the $1$-norms of the matrices and improve the accuracy of the computed eigenvalues and eigenvectors in the real generalized eigenvalue problem
 $Ax=λBx.$
nag_dggbal (f08whc) is usually the first step in the solution of the above generalized eigenvalue problem. Balancing is optional but it is highly recommended.
The term ‘balancing’ covers two steps, each of which involves similarity transformations on $A$ and $B$. The function can perform either or both of these steps. Both steps are optional.
1. The function first attempts to permute $A$ and $B$ to block upper triangular form by a similarity transformation:
 $PAPT=F= F11 F12 F13 F22 F23 F33$
 $PBPT=G= G11 G12 G13 G22 G23 G33$
where $P$ is a permutation matrix, ${F}_{11}$, ${F}_{33}$, ${G}_{11}$ and ${G}_{33}$ are upper triangular. Then the diagonal elements of the matrix pairs $\left({F}_{11},{G}_{11}\right)$ and $\left({F}_{33},{G}_{33}\right)$ are generalized eigenvalues of $\left(A,B\right)$. The rest of the generalized eigenvalues are given by the matrix pair $\left({F}_{22},{G}_{22}\right)$ which are in rows and columns ${i}_{\mathrm{lo}}$ to ${i}_{\mathrm{hi}}$. Subsequent operations to compute the generalized eigenvalues of $\left(A,B\right)$ need only be applied to the matrix pair $\left({F}_{22},{G}_{22}\right)$; 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$.
2. The function applies a diagonal similarity transformation to $\left(F,G\right)$, to make the rows and columns of $\left({F}_{22},{G}_{22}\right)$ as close in norm as possible:
 $DFD^= I 0 0 0 D22 0 0 0 I F11 F12 F13 F22 F23 F33 I 0 0 0 D^22 0 0 0 I$
 $DGD^= I 0 0 0 D22 0 0 0 I G11 G12 G13 G22 G23 G33 I 0 0 0 D^22 0 0 0 I$
This transformation usually improves the accuracy of computed generalized eigenvalues and eigenvectors.

## 4References

Ward R C (1981) Balancing the generalized eigenvalue problem SIAM J. Sci. Stat. Comp. 2 141–152

## 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: specifies the operations to be performed on matrices $A$ and $B$.
${\mathbf{job}}=\mathrm{Nag_DoNothing}$
No balancing is done. Initialize ${\mathbf{ilo}}=1$, ${\mathbf{ihi}}={\mathbf{n}}$, ${\mathbf{lscale}}\left[\mathit{i}-1\right]=1.0$ and ${\mathbf{rscale}}\left[\mathit{i}-1\right]=1.0$, for $\mathit{i}=1,2,\dots ,n$.
${\mathbf{job}}=\mathrm{Nag_Permute}$
Only permutations are used in balancing.
${\mathbf{job}}=\mathrm{Nag_Scale}$
Only scalings are are used in balancing.
${\mathbf{job}}=\mathrm{Nag_DoBoth}$
Both permutations and scalings are used in balancing.
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 matrices $A$ and $B$.
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{b}\left[\mathit{dim}\right]$doubleInput/Output
Note: the dimension, dim, of the array b must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pdb}}×{\mathbf{n}}\right)$.
Where ${\mathbf{B}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{b}}\left[\left(j-1\right)×{\mathbf{pdb}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{b}}\left[\left(i-1\right)×{\mathbf{pdb}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: the $n$ by $n$ matrix $B$.
On exit: b is overwritten by the balanced matrix. If ${\mathbf{job}}=\mathrm{Nag_DoNothing}$, b is not referenced.
7:    $\mathbf{pdb}$IntegerInput
On entry: the stride separating row or column elements (depending on the value of order) in the array b.
Constraint: ${\mathbf{pdb}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\right)$.
8:    $\mathbf{ilo}$Integer *Output
9:    $\mathbf{ihi}$Integer *Output
On exit: ${i}_{\mathrm{lo}}$ and ${i}_{\mathrm{hi}}$ are set such that ${\mathbf{A}}\left(i,j\right)=0$ and ${\mathbf{B}}\left(i,j\right)=0$ 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$.
10:  $\mathbf{lscale}\left[{\mathbf{n}}\right]$doubleOutput
On exit: details of the permutations and scaling factors applied to the left side of the matrices $A$ and $B$. If ${P}_{i}$ is the index of the row interchanged with row $i$ and ${d}_{i}$ is the scaling factor applied to row $i$, then
• ${\mathbf{lscale}}\left[\mathit{i}-1\right]={P}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{i}}_{\mathrm{lo}}-1$;
• ${\mathbf{lscale}}\left[\mathit{i}-1\right]={\mathit{d}}_{\mathit{i}}$, for $\mathit{i}={\mathit{i}}_{\mathrm{lo}},\dots ,{\mathit{i}}_{\mathrm{hi}}$;
• ${\mathbf{lscale}}\left[\mathit{i}-1\right]={P}_{\mathit{i}}$, for $\mathit{i}={\mathit{i}}_{\mathrm{hi}}+1,\dots ,n$.
The order in which the interchanges are made is $n$ to ${i}_{\mathrm{hi}}+1$, then $1$ to ${i}_{\mathrm{lo}}-1$.
11:  $\mathbf{rscale}\left[{\mathbf{n}}\right]$doubleOutput
On exit: details of the permutations and scaling factors applied to the right side of the matrices $A$ and $B$.
If ${P}_{j}$ is the index of the column interchanged with column $j$ and ${\stackrel{^}{d}}_{j}$ is the scaling factor applied to column $j$, then
• ${\mathbf{rscale}}\left[\mathit{j}-1\right]={P}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathit{i}}_{\mathrm{lo}}-1$;
• ${\mathbf{rscale}}\left[\mathit{j}-1\right]={\stackrel{^}{d}}_{\mathit{j}}$, for $\mathit{j}={i}_{\mathrm{lo}},\dots ,{i}_{\mathrm{hi}}$;
• ${\mathbf{rscale}}\left[\mathit{j}-1\right]={P}_{\mathit{j}}$, for $\mathit{j}={i}_{\mathrm{hi}}+1,\dots ,n$.
The order in which the interchanges are made is $n$ to ${i}_{\mathrm{hi}}+1$, then $1$ to ${i}_{\mathrm{lo}}-1$.
12:  $\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$.
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}>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)$.
On entry, ${\mathbf{pdb}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{pdb}}\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, compared to those in subsequent computations.

## 8Parallelism and Performance

nag_dggbal (f08whc) 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.

nag_dggbal (f08whc) is usually the first step in computing the real generalized eigenvalue problem but it is an optional step. The matrix $B$ is reduced to the upper triangular form using the $QR$ factorization function nag_dgeqrf (f08aec) and this orthogonal transformation $Q$ is applied to the matrix $A$ by calling nag_dormqr (f08agc). This is followed by nag_dgghrd (f08wec) which reduces the matrix pair into the generalized Hessenberg form.
If the matrix pair $\left(A,B\right)$ is balanced by this function, then any generalized eigenvectors computed subsequently are eigenvectors of the balanced matrix pair. In that case, to compute the generalized eigenvectors of the original matrix, nag_dggbak (f08wjc) must be called.
The total number of floating-point operations is approximately proportional to ${n}^{2}$.