public void paintComponent(Graphics g0) { super.paintComponent(g0); Graphics2D g = (Graphics2D) g0; g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setFont(font); fontMetrics = g.getFontMetrics(); g.setColor(this.model.getGridColor()); g.setFont(new Font("Impact", Font.PLAIN, 11)); xGrid = this.model.getXGrid(); yGrid = this.model.getYGrid(); xUnit = this.model.getXUnit(); yUnit = this.model.getYUnit(); graphPrecision = this.model.getGraphPrecision(); // Draws grid g.drawLine(xGrid, 0, xGrid, height); g.drawLine(0, yGrid, width, yGrid); // Draws a white line on the x and y grids every 10 pixels for (int x = xGrid; x <= width; x += 10) g.drawLine(x, yGrid + 3, x, yGrid - 3); for (int x = xGrid; x >= 0; x -= 10) g.drawLine(x, yGrid + 3, x, yGrid - 3); for (int y = yGrid; y <= height; y += 10) g.drawLine(xGrid + 3, y, xGrid - 3, y); for (int y = yGrid; y >= 0; y -= 10) g.drawLine(xGrid + 3, y, xGrid - 3, y); // Draws an orange line on the grid for every (default = 5) white lines g.setColor(this.model.getGraphColor()); int piUnit = (int) Math.round((double) xUnit * Math.PI); if (model.isIntegerScale()) { for (int x = xGrid + xUnit; x <= width; x += xUnit) { g.drawLine(x, yGrid + 5, x, yGrid - 5); xCoordinate = x - xGrid; xCoordinate /= xUnit; g.drawString("" + xCoordinate, x - 2, yGrid + 20); } for (int x = xGrid - xUnit; x >= 0; x -= xUnit) { g.drawLine(x, yGrid + 5, x, yGrid - 5); xCoordinate = x - xGrid; xCoordinate /= xUnit; g.drawString("" + xCoordinate, x - 5, yGrid + 20); } } else { /* int piScale = (int)Math.round(xUnit * Math.PI); for(int x = xGrid + piScale; x <= width; x += piScale) { g.drawLine(x, yGrid + 5, x, yGrid - 5); xCoordinate = x - (xGrid + piScale); xCoordinate = (xCoordinate/piScale) + 1; g.drawString(""+(xCoordinate == 1 ? "": xCoordinate) + "π", x-2, yGrid + 20); } for(int x = xGrid - piScale; x >= 0; x -= piScale) { g.drawLine(x, yGrid + 5, x, yGrid - 5); xCoordinate = x - (xGrid - piScale); xCoordinate = (xCoordinate/piScale) - 1; g.drawString(""+(xCoordinate == -1 ? "-": xCoordinate) + "π", x - 5, yGrid + 20); } */ int piStandardDeno = 1; for (int x = piDenominators.length - 1; x >= 0; x--) { if (piUnit / piDenominators[x] < 40) continue; piStandardDeno = piDenominators[x]; break; } if (piStandardDeno != 1) { int count = 1; for (int x = xGrid + (int) Math.round(((double) xUnit * Math.PI) / (double) piStandardDeno); x <= width; x = xGrid + (int) Math.round( ((double) (count * xUnit) * Math.PI) / (double) piStandardDeno)) { if (count % piStandardDeno != 0) { g.drawLine(x, yGrid + 3, x, yGrid - 3); int fraction[] = Algebra.simplifyFraction(count, piStandardDeno); String label = (new StringBuilder()) .append(fraction[0] != 1 ? ((Integer.valueOf(fraction[0]))) : "") .append("\u03C0/") .append(fraction[1]) .toString(); g.drawString(label, x - fontMetrics.stringWidth(label) / 2, yGrid + 20); } count++; } count = 1; for (int x = xGrid - (int) Math.round(((double) xUnit * Math.PI) / (double) piStandardDeno); x >= 0; x = xGrid - (int) Math.round( ((double) (count * xUnit) * 3.1415926535897931D) / (double) piStandardDeno)) { if (count % piStandardDeno != 0) { g.drawLine(x, yGrid + 3, x, yGrid - 3); int fraction[] = Algebra.simplifyFraction(count, piStandardDeno); String label = (new StringBuilder()) .append(fraction[0] != 1 ? ((Integer.valueOf(-fraction[0]))) : "-") .append("\u03C0/") .append(fraction[1]) .toString(); g.drawString(label, x - fontMetrics.stringWidth(label) / 2, yGrid + 20); } count++; } } } for (int y = yGrid + yUnit; y <= height; y += yUnit) { g.drawLine(xGrid + 5, y, xGrid - 5, y); yCoordinate = yGrid - y; yCoordinate /= yUnit; g.drawString("" + yCoordinate, xGrid + 20, y + 5); } for (int y = yGrid - yUnit; y >= 0; y -= yUnit) { g.drawLine(xGrid + 5, y, xGrid - 5, y); g.drawLine(xGrid + 5, y, xGrid - 5, y); yCoordinate = yGrid - y; yCoordinate /= yUnit; g.drawString("" + yCoordinate, xGrid + 20, y + 5); } // if polynomials } // end paintComponent
/** Symmetric tridiagonal QL algorithm. */ private void tql2() { // This is derived from the Algol procedures tql2, by // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding // Fortran subroutine in EISPACK. for (int i = 1; i < n; i++) { e[i - 1] = e[i]; } e[n - 1] = 0.0; double f = 0.0; double tst1 = 0.0; double eps = Math.pow(2.0, -52.0); for (int l = 0; l < n; l++) { // Find small subdiagonal element tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l])); int m = l; while (m < n) { if (Math.abs(e[m]) <= eps * tst1) { break; } m++; } // If m == l, d[l] is an eigenvalue, // otherwise, iterate. if (m > l) { int iter = 0; do { iter = iter + 1; // (Could check iteration count here.) // Compute implicit shift double g = d[l]; double p = (d[l + 1] - g) / (2.0 * e[l]); double r = Algebra.hypot(p, 1.0); if (p < 0) { r = -r; } d[l] = e[l] / (p + r); d[l + 1] = e[l] * (p + r); double dl1 = d[l + 1]; double h = g - d[l]; for (int i = l + 2; i < n; i++) { d[i] -= h; } f = f + h; // Implicit QL transformation. p = d[m]; double c = 1.0; double c2 = c; double c3 = c; double el1 = e[l + 1]; double s = 0.0; double s2 = 0.0; for (int i = m - 1; i >= l; i--) { c3 = c2; c2 = c; s2 = s; g = c * e[i]; h = c * p; r = Algebra.hypot(p, e[i]); e[i + 1] = s * r; s = e[i] / r; c = p / r; p = c * d[i] - s * g; d[i + 1] = h + s * (c * g + s * d[i]); // Accumulate transformation. for (int k = 0; k < n; k++) { h = V[k][i + 1]; V[k][i + 1] = s * V[k][i] + c * h; V[k][i] = c * V[k][i] - s * h; } } p = -s * s2 * c3 * el1 * e[l] / dl1; e[l] = s * p; d[l] = c * p; // Check for convergence. } while (Math.abs(e[l]) > eps * tst1); } d[l] = d[l] + f; e[l] = 0.0; } // Sort eigenvalues and corresponding vectors. for (int i = 0; i < n - 1; i++) { int k = i; double p = d[i]; for (int j = i + 1; j < n; j++) { if (d[j] < p) { k = j; p = d[j]; } } if (k != i) { d[k] = d[i]; d[i] = p; for (int j = 0; j < n; j++) { p = V[j][i]; V[j][i] = V[j][k]; V[j][k] = p; } } } }
/** Creates a new instance of testmatrix */ public GLSsolver(double[][] p_MatrixgleichNull) throws IllegalArgumentException { // -------------------------------------- // Kontrolle, ob Eingabematrix rechteckig // -------------------------------------- int nplus1 = p_MatrixgleichNull[0].length; for (int i = 1; i < p_MatrixgleichNull.length; i++) { // Zeilen i if (p_MatrixgleichNull[i].length != nplus1) { System.err.println( "Programmfehler: Matrix des GLS ist nicht rechteckig! (im solver entdeckt)"); throw new IllegalArgumentException(); } } if (nplus1 <= 1) throw new IllegalArgumentException("keine Unbekannte"); // keine Unbekannte!!! // Umgeht einen Fehler in der colt-Bibliothek // TODO wenn behoben, Workaround entfernen // ------ int anzGl = p_MatrixgleichNull.length; if (anzGl < nplus1 - 1) { // anzGleichungen < anz Unbekannte if (debug) System.out.println("WorkAround fuer Fehler in colt: 0 = 0 Gleichungen anhaengen"); anzGl = nplus1 - 1; // = Anzahl Unbek, 0 0 0 ... 0 = 0 Zeile angehängt } // ------------------------- // Daten in A und b einlesen // ------------------------- // so dass A*x = b A = new DenseDoubleMatrix2D(anzGl, (nplus1 - 1)); DenseDoubleMatrix2D b = new DenseDoubleMatrix2D(anzGl, 1); for (int i = 0; i < p_MatrixgleichNull.length; i++) { // Zeilen i for (int j = 0; j < nplus1 - 1; j++) { // Spalten A.set(i, j, p_MatrixgleichNull[i][j]); } b.set(i, 0, -p_MatrixgleichNull[i][nplus1 - 1]); } if (debug) { System.out.println(" A = " + A.toString()); System.out.println(" b = " + b.toString()); System.out.println(""); } // -------------- // LR - Zerlegung // -------------- LUDecomposition ALU = new LUDecomposition(A); if (debug) System.out.println(ALU.toString()); DoubleMatrix2D L = ALU.getL(); R = ALU.getU(); int[] piv = ALU.getPivot(); Algebra alg = new Algebra(); // if (debug) System.out.println("L = " + L.toString()); // if (debug) System.out.println("Kontrolle L*R = " + alg.mult(L,R).toString()); // if (debug) System.out.println("Kontrolle P*b = " + alg.permute(b, piv, null) ); // // if (debug) System.out.println("Rx = c: R = " + R.toString()); // if (debug) System.out.println("alg.permute(b, piv, null) = " + alg.permute(b, piv, // null).toString()); c = alg.solve(L, alg.permute(b, piv, null)); // TODO: kann zu Problemen führen, // wenn weniger Gleichungen als Unbek --> s.Workaround oben if (debug) System.out.println("Lc = Pb: c = " + c.toString()); if (debug) { System.out.println("Rang A: " + alg.rank(A)); System.out.println("Rang R: " + alg.rank(R)); } assert (alg.rank(A) == alg.rank(R)) : "Rang von A ungleich Rang von R --> Programmfehler"; anzUnbestParam = A.columns() - alg.rank(A); if (debug) System.out.println("Anz unbest Parameter: " + anzUnbestParam); }