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

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

Introduction

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

Prototype

RealMatrix solve(final RealMatrix b) throws IllegalArgumentException, InvalidMatrixException;

Source Link

Document

Solve the linear equation A × X = B for matrices A.

Usage

From source file:com.zinnia.nectar.util.math.MatrixSolver.java

public static double[] solveMatrix(double[][] coefficientMatrix, double[] rhsVector) {
    RealVector x = null;/*www .  j a v a 2  s.  c  o m*/
    try {
        RealMatrix a = new Array2DRowRealMatrix(coefficientMatrix);
        RealVector b = new ArrayRealVector(rhsVector);
        DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();
        x = solver.solve(b);
    } catch (Exception e) {
        e.printStackTrace();
    }

    double[] result;
    result = x.toArray();

    return result;
}

From source file:MathFunctions.java

public static AffineTransform generateAffineTransformFromPointPairs(
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    RealMatrix u = new Array2DRowRealMatrix(pointPairs.size(), 3);
    RealMatrix v = new Array2DRowRealMatrix(pointPairs.size(), 3);

    // Create u (source) and v (dest) matrices whose row vectors
    // are [x,y,1] for each Point2D.Double:

    int i = 0;//from   w  w w . ja va2 s .  co m
    for (Map.Entry pair : pointPairs.entrySet()) {
        Point2D.Double uPt = (Point2D.Double) pair.getKey();
        Point2D.Double vPt = (Point2D.Double) pair.getValue();

        insertPoint2DInMatrix(u, uPt, i);
        insertPoint2DInMatrix(v, vPt, i);

        i++;
    }
    // Find the 3x3 linear least squares solution to u*m'=v
    // (the last row should be [0,0,1]):
    DecompositionSolver solver = (new QRDecompositionImpl(u)).getSolver();
    double[][] m = solver.solve(v).transpose().getData();

    // Create an AffineTransform object from the elements of m
    // (the last row is omitted as specified in AffineTransform class):
    return new AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][2], m[1][2]);
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

/*** Rigid body transform (rotation and translation only)
        //  ww w  .j  ava 2 s  . c  o  m
        
 /**
 * Creates an AffineTransform object that uses only rotation and translation
 * 
 * @pointPairs is a Map of points measured in the two coordinates systems (srcPt->destPt)
 */
public static AffineTransform generateRigidBodyTransform(Map<Point2D.Double, Point2D.Double> pointPairs) {
    int number = pointPairs.size();

    RealMatrix X = new Array2DRowRealMatrix(2 * number, 4);
    RealMatrix U = new Array2DRowRealMatrix(2 * number, 1);

    int i = 0;
    for (Map.Entry<Point2D.Double, Point2D.Double> pair : pointPairs.entrySet()) {
        double[] thisRow = { pair.getKey().x, pair.getKey().y, 1.0, 0.0 };
        X.setRow(i, thisRow);
        double[] otherRow = { pair.getKey().y, -pair.getKey().x, 0.0, 1.0 };
        X.setRow(i + number, otherRow);

        U.setEntry(i, 0, pair.getValue().x);
        U.setEntry(i + number, 0, pair.getValue().y);
        i++;
    }

    DecompositionSolver solver = (new QRDecompositionImpl(X)).getSolver();
    double[][] m = solver.solve(U).getData();

    return new AffineTransform(m[0][0], m[1][0], -m[1][0], m[0][0], m[2][0], m[3][0]);
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

/**
* Creates an AffineTransform object that maps a source planar coordinate system to
* a destination planar coordinate system. At least three point pairs are needed.
* 
* @pointPairs is a Map of points measured in the two coordinates systems (srcPt->destPt)
*//*w w w.j  av a 2  s.com*/
public static AffineTransform generateAffineTransformFromPointPairs(
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    RealMatrix u = new Array2DRowRealMatrix(pointPairs.size(), 3);
    RealMatrix v = new Array2DRowRealMatrix(pointPairs.size(), 3);

    // Create u (source) and v (dest) matrices whose row vectors
    // are [x,y,1] for each Point2D.Double:

    int i = 0;
    for (Map.Entry pair : pointPairs.entrySet()) {
        Point2D.Double uPt = (Point2D.Double) pair.getKey();
        Point2D.Double vPt = (Point2D.Double) pair.getValue();

        insertPoint2DInMatrix(u, uPt, i);
        insertPoint2DInMatrix(v, vPt, i);

        i++;
    }
    // Find the 3x3 linear least squares solution to u*m'=v
    // (the last row should be [0,0,1]):
    DecompositionSolver solver = (new QRDecompositionImpl(u)).getSolver();
    double[][] m = solver.solve(v).transpose().getData();

    // Create an AffineTransform object from the elements of m
    // (the last row is omitted as specified in AffineTransform class):
    return new AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][2], m[1][2]);
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

public static PolynomialCoefficients fitPolynomial(ExponentPairs exponentPairs,
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    final List<Point2D.Double> srcPoints = new ArrayList(pointPairs.keySet());
    final RealMatrix matrix = new Array2DRowRealMatrix(srcPoints.size(), exponentPairs.size());
    for (int i = 0; i < srcPoints.size(); ++i) {
        matrix.setRow(i, powerTerms(srcPoints.get(i).x, srcPoints.get(i).y, exponentPairs));
    }//  w ww  .  j  a v a  2s.co m
    final DecompositionSolver solver = new LUDecompositionImpl(matrix).getSolver();
    final double[] destX = new double[srcPoints.size()];
    final double[] destY = new double[srcPoints.size()];
    for (int i = 0; i < srcPoints.size(); ++i) {
        final Point2D.Double destPoint = pointPairs.get(srcPoints.get(i));
        destX[i] = destPoint.x;
        destY[i] = destPoint.y;
    }
    final PolynomialCoefficients polys = new PolynomialCoefficients();
    polys.polyX = solver.solve(destX);
    polys.polyY = solver.solve(destY);
    return polys;
}

From source file:edu.valelab.gaussianfit.datasettransformations.CoordinateMapper.java

public static PolynomialCoefficients fitPolynomial(ExponentPairs exponentPairs,
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    final List<Point2D.Double> srcPoints = new ArrayList<Point2D.Double>(pointPairs.keySet());
    final RealMatrix matrix = new Array2DRowRealMatrix(srcPoints.size(), exponentPairs.size());
    for (int i = 0; i < srcPoints.size(); ++i) {
        matrix.setRow(i, powerTerms(srcPoints.get(i).x, srcPoints.get(i).y, exponentPairs));
    }//from  w w  w  . j  a v  a 2  s  . com
    final DecompositionSolver solver = new LUDecompositionImpl(matrix).getSolver();
    final double[] destX = new double[srcPoints.size()];
    final double[] destY = new double[srcPoints.size()];
    for (int i = 0; i < srcPoints.size(); ++i) {
        final Point2D.Double destPoint = pointPairs.get(srcPoints.get(i));
        destX[i] = destPoint.x;
        destY[i] = destPoint.y;
    }
    final PolynomialCoefficients polys = new PolynomialCoefficients();
    polys.polyX = solver.solve(destX);
    polys.polyY = solver.solve(destY);
    return polys;
}

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

@Override
public final void compute() {
    GeoElement geo1 = null;//w ww .j a va2 s . c o  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.statistics.AlgoFit.java

protected final void compute() {
    GeoElement geo1 = null;// w  w  w.j  a v a  2 s . c  o  m
    GeoElement geo2 = null;
    datasize = pointlist.size(); //rows in M and Y
    functionsize = functionlist.size(); //cols in M
    functionarray = new GeoFunction[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;
    } else { //We are in business...
        //Best to also check:
        geo1 = functionlist.get(0);
        geo2 = pointlist.get(0);
        if (!geo1.isGeoFunction() || !geo2.isGeoPoint()) {
            fitfunction.setUndefined();
            return;
        } //if wrong contents in lists
        try {
            makeMatrixes(); //Get functions, x and y from lists

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

            //mprint("P:",P);               //debug, erase later

            fitfunction = makeFunction();
            //walk(fitfunction.getFunctionExpression());  //debug, erase later
            //System.out.println(fitfunction.getFunctionExpression().toString());

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

        } catch (Throwable t) {
            fitfunction.setUndefined();
            errorMsg(t.getMessage());
            if (DEBUG) {
                t.printStackTrace();
            }
        } //try-catch
    } //if   

}

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

/** Does the Polynom regression for degree > 4 */
public final boolean doPolyN(GeoList gl, int degree) {
    error = false;/*  ww w.  j  a v  a  2 s.co m*/
    geolist = gl;
    size = geolist.size();
    getPoints(); // getPoints from geolist
    if (error) {
        return false;
    }
    try {
        /*
         * Old Jama version: long time=System.currentTimeMillis();
         * makeMatrixArrays(degree); //make marray and yarray Matrix M=new
         * Matrix(marray); Matrix Y=new Matrix(yarray); Matrix
         * Par=M.solve(Y); //Par.print(3,3);
         * pararray=Par.getRowPackedCopy();
         * System.out.println(System.currentTimeMillis()-time);
         */
        makeMatrixArrays(degree); // make marray and yarray
        RealMatrix M = new Array2DRowRealMatrix(marray, false);
        DecompositionSolver solver = new QRDecompositionImpl(M).getSolver();
        // time=System.currentTimeMillis();
        RealMatrix Y = new Array2DRowRealMatrix(yarray, false);
        RealMatrix P = solver.solve(Y);

        pararray = P.getColumn(0);

        // System.out.println(System.currentTimeMillis()-time);
        // diff(pararray,par);
    } catch (Throwable t) {
        App.debug(t.toString());
        error = true;
    } // try-catch. ToDo: A bit more fine-grained error-handling...
    return !error;
}

From source file:geogebra.kernel.statistics.RegressionMath.java

/** Does the Polynom regression for degree > 4*/
public final boolean doPolyN(GeoList gl, int degree) {
    error = false;//from  w ww . ja  va  2s  .co m
    geolist = gl;
    size = geolist.size();
    getPoints(); //getPoints from geolist
    if (error) {
        return false;
    }
    try {
        /* Old Jama version:
        long time=System.currentTimeMillis();
        makeMatrixArrays(degree);         //make marray and yarray
        Matrix M=new Matrix(marray);
        Matrix Y=new Matrix(yarray);
        Matrix Par=M.solve(Y);            //Par.print(3,3);
        pararray=Par.getRowPackedCopy();
        System.out.println(System.currentTimeMillis()-time);
        */
        makeMatrixArrays(degree); //make marray and yarray
        RealMatrix M = new Array2DRowRealMatrix(marray, false);
        DecompositionSolver solver = new QRDecompositionImpl(M).getSolver();
        //time=System.currentTimeMillis();
        RealMatrix Y = new Array2DRowRealMatrix(yarray, false);
        RealMatrix P = solver.solve(Y);

        pararray = P.getColumn(0);

        //System.out.println(System.currentTimeMillis()-time);
        //diff(pararray,par);
    } catch (Throwable t) {
        Application.debug(t.toString());
        error = true;
    } //try-catch.  ToDo: A bit more fine-grained error-handling...
    return !error;
}