Example usage for org.apache.commons.math.linear DecompositionSolver isNonSingular

List of usage examples for org.apache.commons.math.linear DecompositionSolver isNonSingular

Introduction

In this page you can find the example usage for org.apache.commons.math.linear DecompositionSolver isNonSingular.

Prototype

boolean isNonSingular();

Source Link

Document

Check if the decomposed matrix is non-singular.

Usage

From source file:geogebra.common.kernel.statistics.AlgoFit.java

@Override
public final void compute() {
    GeoElement geo1 = null;//from  w ww.ja  v  a 2s.co m
    GeoElement geo2 = null;
    datasize = pointlist.size(); // rows in M and Y
    functionsize = functionlist.size(); // cols in M
    functionarray = new GeoFunctionable[functionsize];
    M = new Array2DRowRealMatrix(datasize, functionsize);
    Y = new Array2DRowRealMatrix(datasize, 1);
    P = new Array2DRowRealMatrix(functionsize, 1); // Solution parameters

    if (!pointlist.isDefined() || // Lot of things can go wrong...
            !functionlist.isDefined() || (functionsize > datasize) || (functionsize < 1) || (datasize < 1) // Perhaps a max
    // restriction of
    // functions and data?
    ) // Even if noone would try 500 datapoints and 100 functions...
    {
        fitfunction.setUndefined();
        return;
    }
    // We are in business...
    // Best to also check:
    geo1 = functionlist.get(0);
    geo2 = pointlist.get(0);
    if (!(geo1 instanceof GeoFunctionable) || !geo2.isGeoPoint()) {
        fitfunction.setUndefined();
        return;
    } // if wrong contents in lists
    try {

        // Get functions, x and y from lists
        if (!makeMatrixes()) {
            fitfunction.setUndefined();
            return;
        }

        // Solve for parametermatrix P:
        DecompositionSolver solver = new QRDecompositionImpl(M).getSolver();
        if (solver.isNonSingular()) {
            P = solver.solve(Y);

            fitfunction = makeFunction();

        } else {
            fitfunction.setUndefined();
        }
        // walk(fitfunction.getFunctionExpression()); //debug, erase
        // later

        // First solution (Borcherds):
        // fitfunction.set(kernel.getAlgebraProcessor().evaluateToFunction(buildFunction()));
        // fitfunction.setDefined(true);

    } catch (Throwable t) {
        fitfunction.setUndefined();
        // t.printStackTrace();
    }

}

From source file:geogebra.kernel.implicit.GeoImplicitPoly.java

/**
 * make curve through given points//from  w ww.j av  a 2 s. co m
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1);
    RealMatrix matrix = new RealMatrixImpl(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results;

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new RealMatrixImpl(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints + 1; i++)
            if (i == solutionColumn)
                continue;
            else
                matrix.setColumn(j++, extendMatrix.getColumn(i));
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    double[] solution = new double[partialSolution.length + 1];

    for (int i = 0, j = 0; i < solution.length; i++)
        if (i == solutionColumn - 1)
            solution[i] = 1;
        else {
            solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j];
            j++;
        }

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; i + j < realDegree + 1; j++)
            coeffMatrix[i][j] = solution[k++];

    this.setCoeff(coeffMatrix, true);

    this.defined = true;
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i), 1)) {
            this.setUndefined();
            return;
        }

}

From source file:geogebra.kernel.GeoImplicitPoly.java

/**
 * make curve through given points//w w w .j  av a  2 s .c o m
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    if (pointsOnCurve == null)
        pointsOnCurve = new Coords[points.size()];

    for (int i = 0; i < points.size(); i++) {
        if (pointsOnCurve[i] == null)
            pointsOnCurve[i] = new Coords(points.get(i).x, points.get(i).y, points.get(i).z);
        else {
            pointsOnCurve[i].setX(points.get(i).x);
            pointsOnCurve[i].setY(points.get(i).y);
            pointsOnCurve[i].setZ(points.get(i).z);
        }
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1);
    RealMatrix matrix = new RealMatrixImpl(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results = new double[points.size()];

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new RealMatrixImpl(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints; i++)
            if (i == solutionColumn)
                continue;
            else
                matrix.setColumn(j++, extendMatrix.getColumn(i));
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    for (int i = 0; i < partialSolution.length; i++)
        if (Kernel.isZero(partialSolution[i]))
            partialSolution[i] = 0;

    for (int i = 0; i < partialSolution.length; i++)
        if (Kernel.isZero(partialSolution[i]))
            partialSolution[i] = 0;

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; j + i != realDegree + 1; j++)
            if (k == solutionColumn - 1)
                coeffMatrix[i][j] = 1;
            else
                coeffMatrix[i][j] = partialSolution[k++];

    this.setCoeff(coeffMatrix);
    this.update();
    this.defined = true;
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i))) {
            this.setUndefined();
            return;
        }

    this.type = IMPLICIT_POLY_THROUGH_POINTS;
}

From source file:geogebra.common.kernel.implicit.GeoImplicitPoly.java

/**
 * make curve through given points/*from   ww w.java  2s.c o  m*/
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new Array2DRowRealMatrix(points.size(), points.size() + 1);
    RealMatrix matrix = new Array2DRowRealMatrix(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results;

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new Array2DRowRealMatrix(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new Array2DRowRealMatrix(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints + 1; i++) {
            if (i == solutionColumn)
                continue;
            matrix.setColumn(j++, extendMatrix.getColumn(i));
        }
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    double[] solution = new double[partialSolution.length + 1];

    for (int i = 0, j = 0; i < solution.length; i++)
        if (i == solutionColumn - 1)
            solution[i] = 1;
        else {
            solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j];
            j++;
        }

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; i + j < realDegree + 1; j++)
            coeffMatrix[i][j] = solution[k++];

    this.setCoeff(coeffMatrix, true);

    setDefined();
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i), 1)) {
            this.setUndefined();
            return;
        }

}

From source file:org.smurn.jsift.LUSolver3.java

@Override
public double[] solve(double[][] m, double[] b) {
    if (m == null || b == null) {
        throw new NullPointerException();
    }/*  ww  w .j a va2  s. com*/
    if (b.length != 3) {
        throw new IllegalArgumentException();
    }
    if (m.length != 3) {
        throw new IllegalArgumentException();
    }
    try {
        RealMatrix matrix = new Array2DRowRealMatrix(m);
        LUDecomposition lu = new LUDecompositionImpl(matrix);
        DecompositionSolver solver = lu.getSolver();
        if (!solver.isNonSingular()) {
            return null;
        }
        return solver.solve(b);
    } catch (NonSquareMatrixException e) {
        throw new IllegalArgumentException();
    }
}