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

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

Introduction

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

Prototype

public RealMatrixImpl(final double[] v) 

Source Link

Document

Create a new (column) RealMatrix using v as the data for the unique column of the v.length x 1 matrix created.

Usage

From source file:com.discursive.jccook.math.SolveEquations.java

public static void main(String[] args) {

    double[][] coefficients = { { 3.0, 20.0, 89.0 }, { 4.0, 40.0, 298.0 }, { 7.0, 21.0, 0.42 } };

    double[] values = { 1324, 2999, 2039 };

    RealMatrix matrix = new RealMatrixImpl(coefficients);

    double[] answers = matrix.solve(values);

    System.out.println("Answers: " + ArrayUtils.toString(answers));

    double[][] badCoefficients = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };

    double[] badValues = { 0, 1, 3 };

    matrix = new RealMatrixImpl(badCoefficients);

    answers = matrix.solve(badValues);//from   w ww .j  a v  a 2 s  . c  om

    System.out.println("Answers: " + ArrayUtils.toString(answers));

}

From source file:lib.regressions.MultipleRegression.java

/**
 * Perform the regression computations//from   w  w w.  jav  a  2s  .c  o m
 */
private void compute() {
    // Set everything to 0.
    for (int i = 0; i < (myNumVar + 1); i++) {
        myCoef[i] = 0.0;
        myStdErr[i] = 0.0;
        myTStat[i] = 0.0;
    }
    myChiSq = 0.0;
    myRSq = 0.0;
    myAdjustedRSq = 0.0;

    // Set coefficients, t-stat, etc. if there has been enough data added.
    if (myCount >= (myNumVar + 1)) {
        RealMatrix dataMatrix = new RealMatrixImpl(mySums.getSumXX());
        RealMatrix xxMatrix = dataMatrix.getSubMatrix(1, myNumVar + 1, 1, myNumVar + 1);
        RealMatrix xyMatrix = dataMatrix.getSubMatrix(1, myNumVar + 1, 0, 0);

        computeOkX(); // Determine which X components to use.
        int[] listX = getListX();
        int[] listY = { 0 };
        int numX = listX.length;
        RealMatrix xxSubMatrix = xxMatrix.getSubMatrix(listX, listX);
        RealMatrix xySubMatrix = xyMatrix.getSubMatrix(listX, listY);

        double sumY = mySums.getSumXX()[0][1];
        double sumYY = mySums.getSumXX()[0][0];

        if (!xxSubMatrix.isSingular()) {
            RealMatrix xxInverse = xxSubMatrix.inverse();
            RealMatrix coefMatrix = xxInverse.multiply(xySubMatrix);
            double[] coef = coefMatrix.getColumn(0);

            // Compute chi-squared
            myChiSq = sumYY - 2 * coefMatrix.transpose().multiply(xySubMatrix).getEntry(0, 0)
                    + +coefMatrix.transpose().multiply(xxSubMatrix).multiply(coefMatrix).getEntry(0, 0);

            // Compute R^2 and adjusted R^2
            int offset = getUseIntercept() ? 1 : 0;
            myRSq = 1.0 - myChiSq / (sumYY - sumY * sumY / myCount);
            myAdjustedRSq = 1 - ((1 - myRSq) * (myCount - 1) + 1 - offset) / (myCount - numX);

            // Compute standard errors and t-stats
            int j = 0;
            for (int i = 0; i < (myNumVar + 1); i++) {
                if (myOkX[i]) {
                    j++;
                    myCoef[i] = coef[j - 1];
                    myStdErr[i] = Math.sqrt(myChiSq * xxInverse.getEntry(j - 1, j - 1) / (myCount - numX));
                    myTStat[i] = myCoef[i] / myStdErr[i];
                }
            }
        }

    }
    myIsComputed = true;
}

From source file:com.facebook.infrastructure.service.StorageService.java

/**
 * Get the estimated disk space of the target endpoint in its primary range.
 * //  w  ww.j  av a 2  s. c om
 * @param target
 *          whose primary range we are interested in.
 * @return disk space of the target in the primary range.
 */
private double getDiskSpaceForPrimaryRange(EndPoint target) {
    double primaryDiskSpace = 0d;
    Map<BigInteger, EndPoint> tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap();
    Set<BigInteger> tokens = tokenToEndPointMap.keySet();
    Range[] allRanges = getAllRanges(tokens);
    Arrays.sort(allRanges);
    /* Mapping from Range to its ordered position on the ring */
    Map<Range, Integer> rangeIndex = new HashMap<Range, Integer>();
    for (int i = 0; i < allRanges.length; ++i) {
        rangeIndex.put(allRanges[i], i);
    }
    /* Get the coefficients for the equations */
    List<double[]> equations = new ArrayList<double[]>();
    /* Get the endpoint to range map */
    Map<EndPoint, List<Range>> endPointToRangesMap = constructEndPointToRangesMap();
    Set<EndPoint> eps = endPointToRangesMap.keySet();

    for (EndPoint ep : eps) {
        List<Range> ranges = endPointToRangesMap.get(ep);
        double[] equation = new double[allRanges.length];
        for (Range range : ranges) {
            int index = rangeIndex.get(range);
            equation[index] = 1;
        }
        equations.add(equation);
    }
    double[][] coefficients = equations.toArray(new double[0][0]);

    /* Get the constants which are the aggregate disk space for each endpoint */
    double[] constants = new double[allRanges.length];
    int index = 0;
    for (EndPoint ep : eps) {
        /* reset the port back to control port */
        ep.setPort(DatabaseDescriptor.getControlPort());
        String lInfo = null;
        if (ep.equals(StorageService.udpAddr_))
            lInfo = getLoadInfo();
        else
            lInfo = getLoadInfo(ep);
        LoadInfo li = new LoadInfo(lInfo);
        constants[index++] = FileUtils.stringToFileSize(li.diskSpace());
    }

    RealMatrix matrix = new RealMatrixImpl(coefficients);
    double[] solutions = matrix.solve(constants);
    Range primaryRange = getPrimaryRangeForEndPoint(target);
    primaryDiskSpace = solutions[rangeIndex.get(primaryRange)];
    return primaryDiskSpace;
}