e04vj may be used before e04vh to determine the sparsity pattern for the Jacobian.

# Syntax

C#
```public static void e04vj(
int nf,
int n,
E04..::..E04VJ_USRFUN usrfun,
int[] iafun,
int[] javar,
out int nea,
double[] a,
int[] igfun,
int[] jgvar,
out int neg,
double[] x,
double[] xlow,
double[] xupp,
E04..::..e04vhOptions options,
out int ifail
)```
Visual Basic
```Public Shared Sub e04vj ( _
nf As Integer, _
n As Integer, _
usrfun As E04..::..E04VJ_USRFUN, _
iafun As Integer(), _
javar As Integer(), _
<OutAttribute> ByRef nea As Integer, _
a As Double(), _
igfun As Integer(), _
jgvar As Integer(), _
<OutAttribute> ByRef neg As Integer, _
x As Double(), _
xlow As Double(), _
xupp As Double(), _
options As E04..::..e04vhOptions, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void e04vj(
int nf,
int n,
E04..::..E04VJ_USRFUN^ usrfun,
array<int>^ iafun,
array<int>^ javar,
[OutAttribute] int% nea,
array<double>^ a,
array<int>^ igfun,
array<int>^ jgvar,
[OutAttribute] int% neg,
array<double>^ x,
array<double>^ xlow,
array<double>^ xupp,
E04..::..e04vhOptions^ options,
[OutAttribute] int% ifail
)```
F#
```static member e04vj :
nf : int *
n : int *
usrfun : E04..::..E04VJ_USRFUN *
iafun : int[] *
javar : int[] *
nea : int byref *
a : float[] *
igfun : int[] *
jgvar : int[] *
neg : int byref *
x : float[] *
xlow : float[] *
xupp : float[] *
options : E04..::..e04vhOptions *
ifail : int byref -> unit
```

#### Parameters

nf
Type: System..::..Int32
On entry: $\mathit{nf}$, the number of problem functions in $F\left(x\right)$, including the objective function (if any) and the linear and nonlinear constraints. Simple upper and lower bounds on $x$ can be defined using the parameters xlow and xupp and should not be included in $F$.
Constraint: ${\mathbf{nf}}>0$.
n
Type: System..::..Int32
On entry: $n$, the number of variables.
Constraint: ${\mathbf{n}}>0$.
usrfun
Type: NagLibrary..::..E04..::..E04VJ_USRFUN
usrfun must define the problem functions $F\left(x\right)$. This method is passed to e04vj as the external parameter usrfun.

A delegate of type E04VJ_USRFUN.

iafun
Type: array<System..::..Int32>[]()[][]
An array of size [lena]
On exit: define the coordinates $\left(i,j\right)$ and values ${A}_{ij}$ of the nonzero elements of the linear part $A$ of the function $F\left(x\right)=f\left(x\right)+Ax$.
In particular, nea triples $\left({\mathbf{iafun}}\left[k-1\right],{\mathbf{javar}}\left[k-1\right],{\mathbf{a}}\left[k-1\right]\right)$ define the row and column indices $i={\mathbf{iafun}}\left[k-1\right]$ and $j={\mathbf{javar}}\left[k-1\right]$ of the element ${A}_{ij}={\mathbf{a}}\left[k-1\right]$.
javar
Type: array<System..::..Int32>[]()[][]
An array of size [lena]
On exit: define the coordinates $\left(i,j\right)$ and values ${A}_{ij}$ of the nonzero elements of the linear part $A$ of the function $F\left(x\right)=f\left(x\right)+Ax$.
In particular, nea triples $\left({\mathbf{iafun}}\left[k-1\right],{\mathbf{javar}}\left[k-1\right],{\mathbf{a}}\left[k-1\right]\right)$ define the row and column indices $i={\mathbf{iafun}}\left[k-1\right]$ and $j={\mathbf{javar}}\left[k-1\right]$ of the element ${A}_{ij}={\mathbf{a}}\left[k-1\right]$.
nea
Type: System..::..Int32%
On exit: is the number of nonzero entries in $A$ such that $F\left(x\right)=f\left(x\right)+Ax$.
a
Type: array<System..::..Double>[]()[][]
An array of size [lena]
On exit: define the coordinates $\left(i,j\right)$ and values ${A}_{ij}$ of the nonzero elements of the linear part $A$ of the function $F\left(x\right)=f\left(x\right)+Ax$.
In particular, nea triples $\left({\mathbf{iafun}}\left[k-1\right],{\mathbf{javar}}\left[k-1\right],{\mathbf{a}}\left[k-1\right]\right)$ define the row and column indices $i={\mathbf{iafun}}\left[k-1\right]$ and $j={\mathbf{javar}}\left[k-1\right]$ of the element ${A}_{ij}={\mathbf{a}}\left[k-1\right]$.
igfun
Type: array<System..::..Int32>[]()[][]
An array of size [leng]
On exit: define the coordinates $\left(i,j\right)$ of the nonzero elements of $G$, the nonlinear part of the derivatives $J\left(x\right)=G\left(x\right)+A$ of the function $F\left(x\right)=f\left(x\right)+Ax$.
jgvar
Type: array<System..::..Int32>[]()[][]
An array of size [leng]
On exit: define the coordinates $\left(i,j\right)$ of the nonzero elements of $G$, the nonlinear part of the derivatives $J\left(x\right)=G\left(x\right)+A$ of the function $F\left(x\right)=f\left(x\right)+Ax$.
neg
Type: System..::..Int32%
On exit: the number of nonzero entries in $G$.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: an initial estimate of the variables $x$. The contents of $x$ will be used by e04vj in the call of usrfun, and so each element of x should be within the bounds given by xlow and xupp.
xlow
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: contain the lower and upper bounds ${l}_{x}$ and ${u}_{x}$ on the variables $x$.
To specify a nonexistent lower bound ${\left[{l}_{x}\right]}_{j}=-\infty$, set ${\mathbf{xlow}}\left[j-1\right]\le -\mathit{bigbnd}$, where $\mathit{bigbnd}$ is the optional parameter Infinite Bound Size. To specify a nonexistent upper bound ${\mathbf{xupp}}\left[j-1\right]\ge \mathit{bigbnd}$.
To fix the $j$th variable (say, ${x}_{j}=\beta$, where $\left|\beta \right|<\mathit{bigbnd}$), set ${\mathbf{xlow}}\left[j-1\right]={\mathbf{xupp}}\left[j-1\right]=\beta$.
xupp
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: contain the lower and upper bounds ${l}_{x}$ and ${u}_{x}$ on the variables $x$.
To specify a nonexistent lower bound ${\left[{l}_{x}\right]}_{j}=-\infty$, set ${\mathbf{xlow}}\left[j-1\right]\le -\mathit{bigbnd}$, where $\mathit{bigbnd}$ is the optional parameter Infinite Bound Size. To specify a nonexistent upper bound ${\mathbf{xupp}}\left[j-1\right]\ge \mathit{bigbnd}$.
To fix the $j$th variable (say, ${x}_{j}=\beta$, where $\left|\beta \right|<\mathit{bigbnd}$), set ${\mathbf{xlow}}\left[j-1\right]={\mathbf{xupp}}\left[j-1\right]=\beta$.
options
Type: NagLibrary..::..E04..::..e04vhOptions
An Object of type E04.e04vjOptions. Used to configure optional parameters to this method.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

When using e04vh, if you set the optional parameter ${\mathbf{Derivative Option}}=0$ and usrfun provides none of the derivatives, you may need to call e04vj to determine the input arrays iafun, javar, a, igfun and jgvar. These arrays define the pattern of nonzeros in the Jacobian matrix. A typical sequence of calls could be
e04vj determines the sparsity pattern for the Jacobian and identifies the constant elements automatically. To do so, e04vj approximates the problem functions, $F\left(x\right)$, at three random perturbations of the given initial point $x$. If an element of the approximate Jacobian is the same at all three points, then it is taken to be constant. If it is zero, it is taken to be identically zero. Since the random points are not chosen close together, the heuristic will correctly classify the Jacobian elements in the vast majority of cases. In general, e04vj finds that the Jacobian can be permuted to the form:
 $GxA3A2A4,$
where ${A}_{2}$, ${A}_{3}$ and ${A}_{4}$ are constant. Note that $G\left(x\right)$ might contain elements that are also constant, but e04vj must classify them as nonlinear. This is because e04vh ‘removes’ linear variables from the calculation of $F$ by setting them to zero before calling usrfun. A knowledgeable user would be able to move such elements from $F\left(x\right)$ in usrfun and enter them as part of iafun, javar and a for e04vh.

# References

Hock W and Schittkowski K (1981) Test Examples for Nonlinear Programming Codes. Lecture Notes in Economics and Mathematical Systems 187 Springer–Verlag

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
The initialization method (E04VGF not in this release) has not been called or at least one of lencwleniw or lenrw is less than $600$.
${\mathbf{ifail}}=2$
An input parameter is invalid. The output message provides more details of the invalid parameter.
${\mathbf{ifail}}=3$
Undefined user-supplied function.
You have indicated that the problem functions are undefined by setting ${\mathbf{status}}=-1$ on exit from usrfun. This exit occurs if e04vj is unable to find a point at which the functions are defined.
${\mathbf{ifail}}=4$
User requested termination.
You have indicated the wish to terminate the call to e04vj by setting status to a value $\text{}<-1$ on exit from usrfun.
${\mathbf{ifail}}=5$
Either lena or leng is too small, resulting in insufficient array to store the Jacobian information. Increase lena and/or leng
${\mathbf{ifail}}=6$
Unable to estimate the Jacobian structure.
${\mathbf{ifail}}=7$
${\mathbf{ifail}}=8$
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-1000$
The array lengths are not the same for arrays
${\mathbf{ifail}}=-1000$
The array lengths are not the same for arrays
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

Not applicable.

None.

None.

# Example

It is a reformulation of Problem 74 from Hock and Schittkowski (1981) and involves the minimization of the nonlinear function
 $fx=10-6x33+23×10-6x43+3x3+2x4$
subject to the bounds
 $-0.55≤x1≤0.55,-0.55≤x2≤0.55,0≤x3≤1200,0≤x4≤1200,$
to the nonlinear constraints
 $1000sin-x1-0.25+1000sin-x2-0.25-x3=-894.8,1000sinx1-0.25+1000sinx1-x2-0.25-x4=-894.8,1000sinx2-0.25+1000sinx2-x1-0.25=-1294.8,$
and to the linear constraints
 $-x1+x2≥-0.55,-x1-x2≥-0.55.$
The initial point, which is infeasible, is
 $x0=0,0,0,0T,$
and $f\left({x}_{0}\right)=0$.
The optimal solution (to five figures) is
 $x*=0.11887,-0.39623,679.94,1026.0T,$
and $f\left({x}^{*}\right)=5126.4$. All the nonlinear constraints are active at the solution.
The formulation of the problem combines the constraints and the objective into a single vector ($F$).
 $F=1000sin-x1-0.25+1000sin-x2-0.25-x31000sinx1-0.25+1000sinx1-x2-0.25-x41000sinx2-0.25+1000sinx2-x1-0.25-x1+x2x1-x210-6x33+23×10-6x43+3x3+2x4$

Example program (C#): e04vje.cs

Example program data: e04vje.d

Example program results: e04vje.r