# NAG Library Routine Document

## 1Purpose

f06faf computes the cosine of the angle between two real vectors.

## 2Specification

Fortran Interface
 Function f06faf ( n, j, tolx, x, incx, toly, y, incy)
 Real (Kind=nag_wp) :: f06faf Integer, Intent (In) :: n, j, incx, incy Real (Kind=nag_wp), Intent (In) :: tolx, x(*), toly, y(*)
#include nagmk26.h
 double f06faf_ ( const Integer *n, const Integer *j, const double *tolx, const double x[], const Integer *incx, const double *toly, const double y[], const Integer *incy)

## 3Description

f06faf returns, via the function name, the cosine of the angle between two $n$-element real vectors $x$ and $y$, given by the expression
 $xTy x2y2 .$
If $1\le j\le n$, $y$ is taken to be the unit vector ${e}_{j}$, in which case the array y is not referenced.
If ${‖x‖}_{2}\le \mathit{tolx}$, the routine returns $2.0$; if ${‖x‖}_{2}>\mathit{tolx}$ but ${‖y‖}_{2}\le \mathit{tol}y$, the routine returns $-2.0$; otherwise the value returned is in the range $\left(-1.0,1.0\right)$.

None.

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of elements in $x$ and $y$.
2:     $\mathbf{j}$ – IntegerInput
On entry: if the vector $y$ is supplied in y, j should be set to $0$. Otherwise, j specifies the index $j$ of the unit vector ${e}_{j}$ to be used as $y$.
3:     $\mathbf{tolx}$ – Real (Kind=nag_wp)Input
On entry: the value $\mathit{tolx}$, used to determine whether ${‖x‖}_{2}$ is effectively zero.
If tolx is negative, the value zero is used.
4:     $\mathbf{x}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the dimension of the array x must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,1+\left({\mathbf{n}}-1\right)×\left|{\mathbf{incx}}\right|\right)$.
On entry: the $n$-element vector $x$.
If ${\mathbf{incx}}>0$, ${x}_{\mathit{i}}$ must be stored in ${\mathbf{x}}\left(1+\left(\mathit{i}-1\right)×{\mathbf{incx}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
If ${\mathbf{incx}}<0$, ${x}_{\mathit{i}}$ must be stored in ${\mathbf{x}}\left(1-\left({\mathbf{n}}-\mathit{i}\right)×{\mathbf{incx}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
Intermediate elements of x are not referenced.
5:     $\mathbf{incx}$ – IntegerInput
On entry: the increment in the subscripts of x between successive elements of $x$.
6:     $\mathbf{toly}$ – Real (Kind=nag_wp)Input
On entry: the value $\mathit{toly}$, used to determine whether ${‖y‖}_{2}$ is effectively zero.
If toly is negative, the value zero is used.
7:     $\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the dimension of the array y must be at least $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,1+\left({\mathbf{n}}-1\right)×\left|{\mathbf{incy}}\right|\right)$.
On entry: if $1\le {\mathbf{j}}\le {\mathbf{n}}$, y is not referenced. Otherwise, y holds the vector $y$.
If ${\mathbf{incy}}>0$, ${y}_{\mathit{i}}$ must be stored in ${\mathbf{y}}\left(1+\left(\mathit{i}-1\right)×{\mathbf{incy}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
If ${\mathbf{incy}}<0$, ${y}_{\mathit{i}}$ must be stored in ${\mathbf{y}}\left(1-\left({\mathbf{n}}-\mathit{i}\right)×{\mathbf{incy}}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
Intermediate elements of y are not referenced.
8:     $\mathbf{incy}$ – IntegerInput
On entry: the increment in the subscripts of y between successive elements of $y$.

None.

Not applicable.

## 8Parallelism and Performance

f06faf is not threaded in any implementation.