import java.awt.*; import java.awt.event.*; import java.util.*; // javac -target 1.1 CorrDimApplet.java public class CorrDimApplet extends java.applet.Applet implements ActionListener { TextArea ta1, ta2; Button calculateButton, clearButton, testButton; static final double log10 = Math.log(10); static final int EMBED = 15; double[][] CorrDim(int mmax, int lag, int n, double[] x) { double[][] result = new double[mmax][101]; for (int i = 0; i <= 100; i++) result[0][i] = 0.02 * i; normalize(n, x); double[] t1 = new double[mmax]; double[] t2 = new double[mmax]; for (int m = 2; m <= mmax; m++) { for (int i = 0; i < n - (m - 1) * lag - 1; i++) { for (int k = 0; k < m; k++) t1[k] = x[i + k * lag]; for (int j = i + 1; j < n - (m - 1) * lag; j++) { for (int k = 0; k < m; k++) t2[k] = x[j + k * lag]; double d = 100 * distance(m, t1, t2); if (d < 0.1) d = 0.1; int c = (int)(50 * Math.log(d) / log10 + 1); if (c < 0) c = 0; if (c <= 100) result[m-1][c]++; } } for (int c = 1; c <= 100; c++) result[m-1][c] += result[m-1][c-1]; for (int c = 0; c <= 100; c++) if (result[m-1][c] == 0) result[m-1][c] = -2; else result[m-1][c] = Math.log(result[m-1][c]) / log10; } return result; } final double distance(int m, double[] p, double[] q) { double d = 0; for (int k = 0; k < m; k++) d += (p[k] - q[k]) * (p[k] - q[k]); return Math.sqrt(d / m); } void normalize(int n, double[] x) { double max = x[0]; double min = x[0]; for (int i = 1; i < n; i++) if (x[i] > max) max = x[i]; else if (x[i] < min) min = x[i]; for (int i = 0; i < n; i++) x[i] = (x[i] - min) / (max - min); } public void init() { setLayout(new BorderLayout()); ta1 = new TextArea(10, 50); ta2 = new TextArea(10, 50); ta1.setEditable(true); ta2.setEditable(false); add("North", ta1); add("South", ta2); Panel pa = new Panel(); add("Center", pa); calculateButton = new Button("Calculate"); pa.add(calculateButton); calculateButton.addActionListener(this); clearButton = new Button("Clear"); pa.add(clearButton); clearButton.addActionListener(this); testButton = new Button("Test (Henon)"); pa.add(testButton); testButton.addActionListener(this); } static void henon(int n, double[] x) { double u = 0.3, v = 0.3; for (int i = 0; i < 100; i++) { double t = 1 - 1.4 * u * u + 0.3 * v; v = u; u = t; } for (int i = 0; i < n; i++) { double t = 1 - 1.4 * u * u + 0.3 * v; v = u; x[i] = u = t; } } void doit(int n, double[] x) { double[][] result = CorrDim(EMBED, 1, n, x); for (int i = 0; i <= 100; i++) { for (int m = 0; m < EMBED-1; m++) if (result[m][i] > -1) ta2.append((float)result[m][i] + "\t"); else ta2.append("NA\t"); if (result[EMBED-1][i] > -1) ta2.append((float)result[EMBED-1][i] + "\n"); else ta2.append("NA\n"); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == clearButton) { ta2.setText(""); } else if (e.getSource() == calculateButton) { String s = ta1.getText(); StringTokenizer t = new StringTokenizer(s); double[] x = new double[t.countTokens()]; int n = 0; while (t.hasMoreTokens()) { try { x[n++] = Double.valueOf(t.nextToken()).doubleValue(); } catch (NumberFormatException ex) { } } ta2.append("n = " + n + "\n----------\n"); doit(n, x); } else if (e.getSource() == testButton) { double[] x = new double[1000]; henon(1000, x); for (int i = 0; i < 1000; i++) ta1.append(x[i] + "\n"); ta2.append("n = " + 1000 + "\n----------\n"); doit(1000, x); } } }