Example usage for org.apache.commons.math3.linear RealVector getEntry

List of usage examples for org.apache.commons.math3.linear RealVector getEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealVector getEntry.

Prototype

public abstract double getEntry(int index) throws OutOfRangeException;

Source Link

Document

Return the entry at the specified index.

Usage

From source file:whitebox.stats.PolynomialLeastSquares2DFitting.java

public void calculateEquations() {
    try {/*from  ww w.j  a  va2s  .c  om*/
        int m, i, j, k;

        int n = xCoords2.length;

        // How many coefficients are there?
        numCoefficients = 0;

        for (j = 0; j <= polyOrder; j++) {
            for (k = 0; k <= (polyOrder - j); k++) {
                numCoefficients++;
            }
        }

        //            for (i = 0; i < n; i++) {
        //                xCoords1[i] -= xMin1;
        //                yCoords1[i] -= yMin1;
        //                xCoords2[i] -= xMin2;
        //                yCoords2[i] -= yMin2;
        //            }

        // Solve the forward transformation equations
        double[][] forwardCoefficientMatrix = new double[n][numCoefficients];
        for (i = 0; i < n; i++) {
            m = 0;
            for (j = 0; j <= polyOrder; j++) {
                for (k = 0; k <= (polyOrder - j); k++) {
                    forwardCoefficientMatrix[i][m] = Math.pow(xCoords1[i], j) * Math.pow(yCoords1[i], k);
                    m++;
                }
            }
        }

        RealMatrix coefficients = new Array2DRowRealMatrix(forwardCoefficientMatrix, false);
        DecompositionSolver solver = new SingularValueDecomposition(coefficients).getSolver();
        //DecompositionSolver solver = new QRDecomposition(coefficients).getSolver();

        // do the x-coordinate first
        RealVector constants = new ArrayRealVector(xCoords2, false);
        RealVector solution = solver.solve(constants);
        forwardRegressCoeffX = new double[n];
        for (int a = 0; a < numCoefficients; a++) {
            forwardRegressCoeffX[a] = solution.getEntry(a);
        }

        double[] residualsX = new double[n];
        double SSresidX = 0;
        for (i = 0; i < n; i++) {
            double yHat = 0.0;
            for (j = 0; j < numCoefficients; j++) {
                yHat += forwardCoefficientMatrix[i][j] * forwardRegressCoeffX[j];
            }
            residualsX[i] = xCoords2[i] - yHat;
            SSresidX += residualsX[i] * residualsX[i];
        }

        double sumX = 0;
        double SSx = 0;
        for (i = 0; i < n; i++) {
            SSx += xCoords2[i] * xCoords2[i];
            sumX += xCoords2[i];
        }
        double varianceX = (SSx - (sumX * sumX) / n) / n;
        double SStotalX = (n - 1) * varianceX;
        double rsqX = 1 - SSresidX / SStotalX;

        // now the y-coordinate 
        constants = new ArrayRealVector(yCoords2, false);
        solution = solver.solve(constants);
        forwardRegressCoeffY = new double[numCoefficients];
        for (int a = 0; a < numCoefficients; a++) {
            forwardRegressCoeffY[a] = solution.getEntry(a);
        }

        double[] residualsY = new double[n];
        residualsXY = new double[n];
        residualsOrientation = new double[n];
        double SSresidY = 0;
        for (i = 0; i < n; i++) {
            double yHat = 0.0;
            for (j = 0; j < numCoefficients; j++) {
                yHat += forwardCoefficientMatrix[i][j] * forwardRegressCoeffY[j];
            }
            residualsY[i] = yCoords2[i] - yHat;
            SSresidY += residualsY[i] * residualsY[i];
            residualsXY[i] = Math.sqrt(residualsX[i] * residualsX[i] + residualsY[i] * residualsY[i]);
            residualsOrientation[i] = Math.atan2(residualsY[i], residualsX[i]);
        }

        double sumY = 0;
        double sumR = 0;
        double SSy = 0;
        double SSr = 0;
        for (i = 0; i < n; i++) {
            SSy += yCoords2[i] * yCoords2[i];
            SSr += residualsXY[i] * residualsXY[i];
            sumY += yCoords2[i];
            sumR += residualsXY[i];
        }
        double varianceY = (SSy - (sumY * sumY) / n) / n;
        double varianceResiduals = (SSr - (sumR * sumR) / n) / n;
        double SStotalY = (n - 1) * varianceY;
        double rsqY = 1 - SSresidY / SStotalY;
        overallRMSE = Math.sqrt(varianceResiduals);

        //System.out.println("y-coordinate r-square: " + rsqY);

        //            // Print the residuals.
        //            System.out.println("\nResiduals:");
        //            for (i = 0; i < n; i++) {
        //                System.out.println("Point " + (i + 1) + "\t" + residualsX[i]
        //                        + "\t" + residualsY[i] + "\t" + residualsXY[i]);
        //            }

        // Solve the backward transformation equations
        double[][] backCoefficientMatrix = new double[n][numCoefficients];
        for (i = 0; i < n; i++) {
            m = 0;
            for (j = 0; j <= polyOrder; j++) {
                for (k = 0; k <= (polyOrder - j); k++) {
                    backCoefficientMatrix[i][m] = Math.pow(xCoords2[i], j) * Math.pow(yCoords2[i], k);
                    m++;
                }
            }
        }

        coefficients = new Array2DRowRealMatrix(backCoefficientMatrix, false);
        //DecompositionSolver solver = new SingularValueDecomposition(coefficients).getSolver();
        solver = new QRDecomposition(coefficients).getSolver();

        // do the x-coordinate first
        constants = new ArrayRealVector(xCoords1, false);
        solution = solver.solve(constants);
        backRegressCoeffX = new double[numCoefficients];
        for (int a = 0; a < numCoefficients; a++) {
            backRegressCoeffX[a] = solution.getEntry(a);
        }

        // now the y-coordinate 
        constants = new ArrayRealVector(yCoords1, false);
        solution = solver.solve(constants);
        backRegressCoeffY = new double[n];
        for (int a = 0; a < numCoefficients; a++) {
            backRegressCoeffY[a] = solution.getEntry(a);
        }
    } catch (Exception e) {
        e.printStackTrace();
        //            showFeedback("Error in ImageRectificationDialog.calculateEquations: "
        //                    + e.getMessage());
    }
}