Skip to content

Commit c62aca4

Browse files
committed
Merge branch 'main' of https://github.com/numericalalgorithmsgroup/NAGJavaExamples into development
2 parents 98becff + c9cd217 commit c62aca4

File tree

18 files changed

+2422
-11
lines changed

18 files changed

+2422
-11
lines changed

QCQP/Readme.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
> ## Important Information
2-
> This repository can viewed as a regular github [repository](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/tree/main/QCQP) or as a [webpage](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/QCQP).
2+
> This file has a lot of Latex and GitHub currently cannot render it on Markdown files. You can read all the math clearly as a [webpage](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/QCQP/) or access this as a regular github [repository](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/tree/main/QCQP)
3+
>
4+
> The source of this example can be found [here](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/blob/main/QCQP/portfolioOptimizationQCQP.java).
35
>
46
> See the top directory of this repository for instructions to set up the [NAG Library for Java](https://github.com/numericalalgorithmsgroup/NAGJavaExamples).
57
68
# Quadratically constrained quadratic programming and its applications in portfolio optimization
79

8-
# Correct Rendering of this notebook
9-
This file has a lot of Latex and GitHub currently cannot render it on Markdown files. [Here](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/QCQP/) you can read it clearly.
10-
11-
The source of this example can be found [here](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/blob/main/QCQP/portfolioOptimizationQCQP.java).
12-
1310
# Introduction
1411

1512
Quadratically constrained quadratic programming (QCQP) is a type of optimization problem in which both the objective function and the constraints involve quadratic functions. A general QCQP problem has the following form

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
> ## Important Information
2-
> This repository is still under development.
3-
>
42
> This repository can viewed as a regular github [repository](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/) or as a [webpage](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/).
53
64
# Examples using the NAG Library for Java

nearest_correlation_matrices/Readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
> ## Important Information
2-
> This repository can viewed as a regular github [repository](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/tree/main/nearest_correlation_matrices) or as a [webpage](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/nearest_correlation_matrices).
2+
> This file has a lot of Latex and GitHub currently cannot render it on Markdown files. You can read all the math clearly as a [webpage](https://numericalalgorithmsgroup.github.io/NAGJavaExamples/nearest_correlation_matrices) or access this as a regular github [repository](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/tree/main/nearest_correlation_matrices).
3+
>
4+
> The source of this example can be found [here](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/blob/main/nearest_correlation_matrices/NcmNag.java).
35
>
46
> See the top directory of this repository for instructions to set up the [NAG Library for Java](https://github.com/numericalalgorithmsgroup/NAGJavaExamples).
57
68
# Nearest Correlation Matrices
79

810
This notebook looks at computing *nearest correlation matrices* using the NAG Library for *Java*.
911

10-
The source of this example can be found [here](https://github.com/numericalalgorithmsgroup/NAGJavaExamples/blob/main/nearest_correlation_matrices/NcmNag.java).
11-
1212
## Correlation Matrices
1313

1414
* An $$n$$ by $$n$$ matrix is a correlation matrix if:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import com.nag.exceptions.NAGBadIntegerException;
2+
import com.nag.routines.A00.A00AA;
3+
import com.nag.routines.Routine;
4+
5+
public class A00AAJE {
6+
7+
public static void main (String[] args) throws NAGBadIntegerException {
8+
Routine.init();
9+
A00AA a00aa = new A00AA();
10+
System.out.println(" A00AAJ Example Program Results\n");
11+
a00aa.eval();
12+
}
13+
14+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import com.nag.exceptions.NAGBadIntegerException;
2+
import com.nag.routines.A00.A00AD;
3+
import com.nag.routines.Routine;
4+
import java.util.Arrays;
5+
6+
public class A00ADJE {
7+
8+
/* Parameters */
9+
public static final int MSGLEN = 15;
10+
11+
/** A00ADJ Example Program */
12+
public static void main (String[] args) throws NAGBadIntegerException {
13+
/* Local Scalars */
14+
int i;
15+
long mkmaj, mkmin;
16+
boolean licval;
17+
String fcomp = getBlankString(80), hdware = getBlankString(80),
18+
impl = getBlankString(80), opsys = getBlankString(80),
19+
pcode = getBlankString(80), prec = getBlankString(80),
20+
vend = getBlankString(80);
21+
/* Local Arrays */
22+
int[] itime = new int[7];
23+
String[] msg = new String[MSGLEN];
24+
25+
// Instantiate arguments
26+
mkmaj = 0;
27+
mkmin = 0;
28+
licval = false;
29+
// Strings must be length expected by Fortran
30+
fcomp = getBlankString(80);
31+
hdware = getBlankString(80);
32+
impl = getBlankString(80);
33+
opsys = getBlankString(80);
34+
pcode = getBlankString(80);
35+
prec = getBlankString(80);
36+
vend = getBlankString(80);
37+
for (i = 0; i < MSGLEN; i++) msg[i] = getBlankString(102);
38+
39+
Routine.init();
40+
System.out.println(" A00ADJ Example Program Results\n");
41+
42+
A00AD a00ad = new A00AD();
43+
a00ad.eval(impl, prec, pcode, mkmaj, mkmin, hdware, opsys, fcomp, vend, licval);
44+
impl = a00ad.getIMPL();
45+
prec = a00ad.getPREC();
46+
pcode = a00ad.getPCODE();
47+
mkmaj = a00ad.getMKMAJ();
48+
mkmin = a00ad.getMKMIN();
49+
hdware = a00ad.getHDWARE();
50+
opsys = a00ad.getOPSYS();
51+
fcomp = a00ad.getFCOMP();
52+
vend = a00ad.getVEND();
53+
licval = a00ad.getLICVAL();
54+
55+
/* Print implementation details */
56+
57+
System.out.println("*** Start of NAG Library implementation details ***");
58+
System.out.println();
59+
System.out.println("Implementation title: " + impl.trim());
60+
System.out.println(" Precision: " + prec.trim());
61+
System.out.println(" Product code: " + pcode.trim());
62+
63+
if (mkmin < 10) {
64+
System.out.printf(" Mark: %2d.%1d\n", mkmaj, mkmin);
65+
}
66+
else {
67+
System.out.printf(" Mark: %2d.%2d\n", mkmaj, mkmin);
68+
}
69+
70+
if (vend.trim().equals("(self-contained)")) {
71+
System.out.println(" Vendor Library: None");
72+
}
73+
else {
74+
System.out.println(" Vendor Library: " + vend.trim());
75+
}
76+
77+
System.out.println("Applicable to:");
78+
System.out.println(" hardware: " + hdware.trim());
79+
System.out.println(" operating system: " + opsys.trim());
80+
System.out.println(" Fortran compiler: " + fcomp.trim());
81+
System.out.println("and compatible systems.");
82+
83+
if (!licval) {
84+
System.out.println(" Licence query: Unsuccessful");
85+
}
86+
else {
87+
System.out.println(" Licence query: Successful");
88+
}
89+
90+
System.out.println();
91+
System.out.println("*** End of NAG Library implementation details ***");
92+
93+
}
94+
95+
/**
96+
* Returns a new String, filled with spaces to the specified length
97+
*
98+
* @param len the required length of the String
99+
* @returns a String of spaces of the specified length
100+
*/
101+
public static String getBlankString(int len) {
102+
103+
if (len > 0) {
104+
char[] arr = new char[len];
105+
Arrays.fill(arr, ' ');
106+
return new String(arr);
107+
}
108+
109+
return "";
110+
111+
}
112+
113+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import com.nag.exceptions.NAGBadIntegerException;
2+
import com.nag.routines.C05.C05AY;
3+
import com.nag.routines.Routine;
4+
5+
6+
public class C05AYJE {
7+
8+
public static void main(String[] args) throws NAGBadIntegerException {
9+
10+
C05AY c05ay = new C05AY();
11+
OBJFUN f = new OBJFUN();
12+
double a = 0.0, b = 1.0, eps = 1.0e-5, eta = 0.0, x = 0.0;
13+
long ifail = 0;
14+
long[] iuser = new long[1];
15+
double[] ruser = new double[1];
16+
17+
System.out.println("C05AYJ Example Program Results");
18+
System.out.println();
19+
20+
Routine.init();
21+
c05ay.eval(a, b, eps, eta, f, x, iuser, ruser, ifail);
22+
x = c05ay.getX();
23+
24+
switch ((int)ifail) {
25+
case 0:
26+
System.out.printf("Zero at x = %12.5f\n", x);
27+
break;
28+
case 2: case 3:
29+
System.out.printf("Final point = %12.5f\n", x);
30+
break;
31+
default:
32+
System.out.println("Unexpected ifail = " + ifail);
33+
}
34+
35+
}
36+
37+
private static class OBJFUN extends C05AY.Abstract_C05AY_F {
38+
39+
public double eval() {
40+
return Math.exp(-X) - X;
41+
}
42+
43+
}
44+
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import com.nag.exceptions.NAGBadIntegerException;
2+
import com.nag.routines.C05.C05AZ;
3+
import com.nag.routines.Routine;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
public class C05AZJE {
8+
9+
public static void main (String[] args)throws NAGBadIntegerException{
10+
double tolx = 0.00001, x = 0.0, y = 1.0, fx;
11+
long ir = 0, ind = 1, ifail = -1;
12+
double[] c = new double[17];
13+
boolean keepOn = true;
14+
15+
Routine.init();
16+
17+
C05AZ c05az = new C05AZ();
18+
fx = fun(x);
19+
int ite = 0;
20+
21+
System.out.println(" C05AZJ Example Program Results\n");
22+
System.out.println(" Iterations\n");
23+
while (keepOn) {
24+
++ite;
25+
26+
c05az.eval(x,y,fx,tolx,ir,c,ind,ifail);
27+
28+
x = c05az.getX();
29+
y = c05az.getY();
30+
tolx = c05az.getTOLX();
31+
ir = c05az.getIR();
32+
ind = c05az.getIND();
33+
ifail = c05az.getIFAIL();
34+
35+
if(ind == 0){
36+
keepOn = false;
37+
}
38+
else{
39+
fx = fun(x);
40+
System.out.printf(" X = %7.5f FX = %11.4E IND = %1d\n", x, fx, ind);
41+
}
42+
}
43+
44+
45+
46+
switch ((int)ifail) {
47+
case 0:
48+
System.out.println("\n Solution\n");
49+
System.out.printf(" X = %8.5f Y = %8.5f\n",x,y);
50+
break;
51+
case 4:
52+
case 5:
53+
System.out.printf(" X = %8.5f Y = %8.5f\n",x,y);
54+
break;
55+
default:
56+
System.out.printf("Unexpected error ifail=%d\n",ifail);
57+
}
58+
}
59+
60+
private static double fun(double x) {
61+
double res = (Math.expm1(-x) + 1) - x;
62+
return res;
63+
}
64+
65+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import com.nag.exceptions.NAGBadIntegerException;
2+
import com.nag.routines.Routine;
3+
import com.nag.routines.C05.C05MB;
4+
import com.nag.routines.F06.DNRM2;
5+
import com.nag.routines.X02.X02AJ;
6+
7+
/**
8+
* C05MBJ Example Program Text
9+
* @author joed
10+
* @since 27.0
11+
*/
12+
/* ** There have been at least 200*(n+1) calls to fcn.
13+
** Consider restarting the calculation from the point held in x.
14+
** ABNORMAL EXIT from NAG Library routine
15+
** NAG soft failure - control returned
16+
*/
17+
public class C05MBJE {
18+
19+
private static final int n = 4;
20+
21+
private static class FCN extends C05MB.Abstract_C05MB_FCN {
22+
23+
public void eval() {
24+
25+
FVEC[0] = Math.cos(X[2]) - X[0];
26+
FVEC[1] = Math.sqrt(1.0 - Math.pow(X[3],2)) - X[1];
27+
FVEC[2] = Math.sin(X[0]) - X[2];
28+
FVEC[3] = Math.pow(X[1], 2) - X[3];
29+
30+
// Set iflag negative to terminate execution for any reason
31+
IFLAG = 0;
32+
33+
return;
34+
35+
}
36+
37+
}
38+
39+
/**
40+
* C05MBJ Example Main Program
41+
*/
42+
public static void main(String[] args) throws NAGBadIntegerException {
43+
44+
C05MB c05mb = new C05MB();
45+
DNRM2 dnrm2 = new DNRM2();
46+
X02AJ x02aj = new X02AJ();
47+
FCN fcn = new FCN();
48+
long cpuser;
49+
double atol, cndtol, fnorm, machpr, rtol;
50+
long astart, ifail, m;
51+
int i;
52+
double[] fvec, x;
53+
double[] ruser = new double[1];
54+
long[] iuser = new long[1];
55+
56+
System.out.println("C05MBJ Example Program Results\n");
57+
58+
// Initialise NAG for Java
59+
Routine.init();
60+
61+
// Get machine precision from X02AJ
62+
machpr = (new X02AJ()).eval();
63+
64+
fvec = new double[n];
65+
x = new double[n];
66+
67+
// The following starting values provide a rough solution
68+
x = new double[]{2.0, 0.5, 2.0, 0.5};
69+
70+
m = 2;
71+
atol = Math.sqrt(machpr);
72+
rtol = Math.sqrt(machpr);
73+
cndtol = 0.0;
74+
astart = 0;
75+
cpuser = 0;
76+
77+
ifail = -1;
78+
c05mb.eval(fcn, n, x, fvec, atol, rtol, m, cndtol, astart, iuser, ruser,
79+
cpuser, ifail);
80+
ifail = c05mb.getIFAIL();
81+
82+
if (ifail==0 || ifail==8 || ifail==9) {
83+
if (ifail==0) {
84+
// The NAG name equivalent of dnrm2 is f06ej
85+
fnorm = dnrm2.eval(n, fvec, 1);
86+
System.out.println();
87+
System.out.printf("Final 2-norm of the residuals = %12.4e\n", fnorm);
88+
System.out.println();
89+
System.out.println("Final approximate solution");
90+
}
91+
else {
92+
System.out.println();
93+
System.out.println("Approximate solution");
94+
}
95+
System.out.println();
96+
for (i = 1; i <= n; i++) {
97+
System.out.printf("%12.4f", x[i-1]);
98+
}
99+
System.out.printf("\n");
100+
}
101+
102+
}
103+
104+
}

0 commit comments

Comments
 (0)