# NAG FL Interfacef05aaf (real_​gram_​schmidt)

## 1Purpose

f05aaf applies the Schmidt orthogonalization process to $n$ vectors in $m$-dimensional space, $n\le m$.

## 2Specification

Fortran Interface
 Subroutine f05aaf ( a, lda, m, n1, n2, s, cc, icol,
 Integer, Intent (In) :: lda, m, n1, n2 Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: icol Real (Kind=nag_wp), Intent (Inout) :: a(lda,n2) Real (Kind=nag_wp), Intent (Out) :: s(n2), cc
C Header Interface
#include <nag.h>
 void f05aaf_ (double a[], const Integer *lda, const Integer *m, const Integer *n1, const Integer *n2, double s[], double *cc, Integer *icol, Integer *ifail)
The routine may be called by the names f05aaf or nagf_orthog_real_gram_schmidt.

## 3Description

f05aaf applies the Schmidt orthogonalization process to $n$ linearly independent vectors in $m$-dimensional space, $n\le m$. The effect of this process is to replace the original $n$ vectors by $n$ orthonormal vectors which have the property that the $\mathit{r}$th vector is linearly dependent on the first $\mathit{r}$ of the original vectors, and that the sum of squares of the elements of the $\mathit{r}$th vector is equal to $1$, for $\mathit{r}=1,2,\dots ,n$. Inner-products are accumulated using additional precision.
None.

## 5Arguments

1: $\mathbf{a}\left({\mathbf{lda}},{\mathbf{n2}}\right)$Real (Kind=nag_wp) array Input/Output
On entry: columns n1 to n2 contain the vectors to be orthogonalized. The vectors are stored by columns in elements $1$ to $m$.
On exit: these vectors are overwritten by the orthonormal vectors.
2: $\mathbf{lda}$Integer Input
On entry: the first dimension of the array a as declared in the (sub)program from which f05aaf is called.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}$.
3: $\mathbf{m}$Integer Input
On entry: $m$, the number of elements in each vector.
4: $\mathbf{n1}$Integer Input
5: $\mathbf{n2}$Integer Input
On entry: the indices of the first and last columns of $A$ to be orthogonalized.
Constraint: ${\mathbf{n1}}\le {\mathbf{n2}}$.
6: $\mathbf{s}\left({\mathbf{n2}}\right)$Real (Kind=nag_wp) array Workspace
7: $\mathbf{cc}$Real (Kind=nag_wp) Output
On exit: is used to indicate linear dependence of the original vectors. The nearer cc is to $1.0$, the more likely vector icol is dependent on vectors n1 to ${\mathbf{icol}}-1$. See Section 9.
8: $\mathbf{icol}$Integer Output
On exit: the column number corresponding to cc. See Section 9.
9: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{lda}}=〈\mathit{\text{value}}〉$, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lda}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{n1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n1}}\le {\mathbf{n2}}$.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

Innerproducts are accumulated using additional precision arithmetic and full machine accuracy should be obtained except when ${\mathbf{cc}}>0.99999$. (See Section 9.)

## 8Parallelism and Performance

f05aaf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

## 9Further Comments

The time taken by f05aaf is approximately proportional to $n{m}^{2}$, where $n={\mathbf{n2}}-{\mathbf{n1}}+1$.
Arguments cc and icol have been included to give some indication of whether or not the vectors are nearly linearly independent, and their values should always be tested on exit from the routine. cc will be in the range $\left[0.0,1.0\right]$ and the closer cc is to $1.0$, the more likely the vector icol is to be linearly dependent on vectors n1 to ${\mathbf{icol}}-1$. Theoretically, when the vectors are linearly dependent, cc should be exactly $1.0$. In practice, because of rounding errors, it may be difficult to decide whether or not a value of cc close to $1.0$ indicates linear dependence. As a general guide a value of ${\mathbf{cc}}>0.99999$ usually indicates linear dependence, but examples exist which give ${\mathbf{cc}}>0.99999$ for linearly independent vectors. If one of the original vectors is zero or if, possibly due to rounding errors, an exactly zero vector is produced by the Gram–Schmidt process, then cc is set exactly to $1.0$ and the vector is not, of course, normalized. If more than one such vector occurs then icol references the last of these vectors.
If you are concerned about testing for near linear dependence in a set of vectors you may wish to consider using routine f08kbf.

## 10Example

This example orthonormalizes columns 2, 3 and 4 of the matrix:
 $1 -2 3 1 -2 1 -2 -1 3 -2 1 5 4 1 5 3 .$

### 10.1Program Text

Program Text (f05aafe.f90)

### 10.2Program Data

Program Data (f05aafe.d)

### 10.3Program Results

Program Results (f05aafe.r)