Example usage for org.apache.commons.math.linear LUDecomposition getSolver

List of usage examples for org.apache.commons.math.linear LUDecomposition getSolver

Introduction

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

Prototype

DecompositionSolver getSolver();

Source Link

Document

Get a solver for finding the A × X = B solution in exact linear sense.

Usage

From source file:adams.data.utils.SavitzkyGolay.java

/**
 * Determines the coefficients for the smoothing, with optional debugging
 * output.//from  ww  w .  ja  v a  2s. c  o m
 *
 * @param numLeft   the number of points to the left
 * @param numRight   the number of points to the right
 * @param polyOrder   the polynomial order
 * @param derOrder   the derivative order
 * @param debug   whether to output debugging information
 * @return      the coefficients
 */
public static double[] determineCoefficients(int numLeft, int numRight, int polyOrder, int derOrder,
        boolean debug) {
    double[] result;
    RealMatrix A;
    int i;
    int j;
    int k;
    float sum;
    RealMatrix b;
    LUDecomposition lu;
    RealMatrix solution;

    result = new double[numLeft + numRight + 1];

    // no window?
    if (result.length == 1) {
        result[0] = 1.0;
        return result;
    }

    // Note: "^" = superscript, "." = subscript

    // {A^T*A}.ij = Sum[k:-nl..nr](k^(i+j))
    A = new Array2DRowRealMatrix(polyOrder + 1, polyOrder + 1);
    for (i = 0; i < A.getRowDimension(); i++) {
        for (j = 0; j < A.getColumnDimension(); j++) {
            sum = 0;
            for (k = -numLeft; k <= numRight; k++)
                sum += Math.pow(k, i + j);
            A.setEntry(i, j, sum);
        }
    }
    if (debug)
        System.out.println("A:\n" + A);

    // LU decomp for inverse matrix
    b = new Array2DRowRealMatrix(polyOrder + 1, 1);
    b.setEntry(derOrder, 0, 1.0);
    if (debug)
        System.out.println("b:\n" + b);

    lu = new LUDecompositionImpl(A);
    solution = lu.getSolver().solve(b);
    if (debug)
        System.out.println("LU decomp. - solution:\n" + solution);

    // coefficients: c.n = Sum[m:0..M]((A^T*A)^-1).0m * n^m with n=-nl..nr
    for (i = -numLeft; i <= numRight; i++) {
        sum = 0;
        for (j = 0; j <= polyOrder; j++)
            sum += solution.getEntry(j, 0) * Math.pow(i, j);
        result[i + numLeft] = sum;
    }
    if (debug)
        System.out.println("Coefficients:\n" + Utils.arrayToString(result));

    return result;
}

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

/**
 * @param lu The result of the LU decomposition, not null. $\mathbf{L}$ cannot be singular.
 *//*from w  w  w. j  a v a  2s. c o  m*/
public LUDecompositionCommonsResult(final LUDecomposition lu) {
    Validate.notNull(lu, "LU decomposition");
    Validate.notNull(lu.getL(), "Matrix is singular; could not perform LU decomposition");
    _determinant = lu.getDeterminant();
    _l = CommonsMathWrapper.unwrap(lu.getL());
    _p = CommonsMathWrapper.unwrap(lu.getP());
    _pivot = lu.getPivot();
    _solver = lu.getSolver();
    _u = CommonsMathWrapper.unwrap(lu.getU());
}

From source file:org.interpss.core.sparse.UCTE2000SparseMatrixCasesTest.java

@Test
public void testCase1() throws Exception {
    AclfNetwork net = CorePluginObjFactory.getFileAdapter(IpssFileAdapter.FileFormat.IEEECDF)
            .load("testData/ieee_format/UCTE_2000_WinterOffPeak.ieee").getAclfNet();

    SparseEqnMatrix2x2 eqn = net.formJMatrix();

    RealMatrix m = sparseMatrix2Ary(eqn);

    double[] b = new double[m.getRowDimension()];
    for (int i = 0; i < m.getRowDimension(); i++)
        b[i] = 1.0;/*from w w  w.  ja v a  2s.co  m*/

    System.out.println("Common Math ... ");
    long starttime = System.currentTimeMillis();
    LUDecomposition lu = new LUDecompositionImpl(m);
    double[] result = lu.getSolver().solve(b);
    System.out.println("time for full matrix : " + (System.currentTimeMillis() - starttime) * 0.001);

    //      int cnt = 0;
    //      for (double d : result)
    //         System.out.println(cnt++ + ", " + d);

    int n = eqn.getDimension() / 2;
    for (int i = 0; i < n; i++)
        eqn.setB(new Vector_xy(1.0, 1.0), i + 1);

    System.out.println("Interpss ... ");
    starttime = System.currentTimeMillis();
    eqn.luMatrixAndSolveEqn(1.0e-20);
    System.out.println("time for spase matrix : " + (System.currentTimeMillis() - starttime) * 0.001);

    int cnt = 0;
    for (int i = 0; i < n; i++) {
        Vector_xy xy = eqn.getX(i + 1);
        //System.out.println(cnt++ + ", " + xy.x);
        //System.out.println(cnt++ + ", " + xy.y);
        assertTrue(Math.abs(result[cnt++] - xy.x) < 0.0001);
        assertTrue(Math.abs(result[cnt++] - xy.y) < 0.0001);
    }

    /*
    J-matrix Dimension:2508
    time for full matrix : 54.321
    time for sparse matrix : 0.197
     */
}

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

@Override
public double[] solve(double[][] m, double[] b) {
    if (m == null || b == null) {
        throw new NullPointerException();
    }//from w  w  w  .ja v a 2s  .  co m
    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();
    }
}