Example usage for org.apache.commons.math.linear QRDecompositionImpl QRDecompositionImpl

List of usage examples for org.apache.commons.math.linear QRDecompositionImpl QRDecompositionImpl

Introduction

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

Prototype

public QRDecompositionImpl(RealMatrix matrix) 

Source Link

Document

Calculates the QR-decomposition of the given matrix.

Usage

From source file:com.opengamma.analytics.math.linearalgebra.QRDecompositionCommons.java

/**
 * {@inheritDoc}//w  ww. j  a v  a2s . c  o  m
 */
@Override
public QRDecompositionResult evaluate(final DoubleMatrix2D x) {
    Validate.notNull(x);
    final RealMatrix temp = CommonsMathWrapper.wrap(x);
    final QRDecomposition qr = new QRDecompositionImpl(temp);
    return new QRDecompositionCommonsResult(qr);
}

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  va 2 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:geogebra.kernel.statistics.AlgoFit.java

protected final void compute() {
    GeoElement geo1 = null;/*from   www. j a  va  2s . 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.AlgoFit.java

@Override
public final void compute() {
    GeoElement geo1 = null;//  www  .j  a v a 2s.  com
    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.common.kernel.statistics.RegressionMath.java

/** Does the Polynom regression for degree > 4 */
public final boolean doPolyN(GeoList gl, int degree) {
    error = false;//from  w  w w  .  j  a va2s . c o  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 w  w.ja va2  s  .  c o 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;
}

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)
*//*ww w.ja  v  a2  s  . co  m*/
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

/*** Rigid body transform (rotation and translation only)
        /*  ww  w  .j a  va2  s .  co 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:playground.mmoyo.taste_variations.MyLeastSquareSolutionCalculator.java

private double[] getQRDsolution(final RealMatrix matrixA, final double[] arrayB) {
    QRDecomposition qrd = new QRDecompositionImpl(matrixA);
    DecompositionSolver qrSolver = qrd.getSolver();
    double[] arrayX = qrSolver.solve(arrayB);
    return arrayX;
}