Example usage for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException

List of usage examples for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException.

Prototype

public DimensionMismatchException(int wrong, int expected) 

Source Link

Document

Construct an exception from the mismatched dimensions.

Usage

From source file:edu.byu.nlp.math.RealVectors.java

/**
 * Adds vector a to b and stores the results in a.
 * /*from   w w w.  jav a 2 s .c  o  m*/
 * @throws NullPointerException if a or b are null 
 */
public static void addToSelf(RealVector a, RealVector b) {
    Preconditions.checkNotNull(a);
    Preconditions.checkNotNull(b);
    if (a.getDimension() != b.getDimension()) {
        throw new DimensionMismatchException(b.getDimension(), a.getDimension());
    }

    a.combineToSelf(1.0, 1.0, b);
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java

/**
 * Elementwise multiplication of elements in two arrays. This is equivalent to
 * using A*B in R when both A and B are matrices.
 *
 * @param A a matrix/* ww  w .j  av a  2  s.  c o m*/
 * @param B a matrix of the same dimension as A
 * @return a matrix with elements that are the produce of elements in A and B.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 */
public static RealMatrix multiplyElements(RealMatrix A, RealMatrix B) throws DimensionMismatchException {
    int nrow = A.getRowDimension();
    int ncol = A.getColumnDimension();
    if (nrow != B.getRowDimension()) {
        throw new DimensionMismatchException(nrow, B.getRowDimension());
    }
    if (ncol != B.getColumnDimension()) {
        throw new DimensionMismatchException(ncol, B.getColumnDimension());
    }

    RealMatrix M = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            M.setEntry(i, j, A.getEntry(i, j) * B.getEntry(i, j));
        }
    }
    return M;
}

From source file:com.insightml.models.regression.OLS.java

public static double[][] addIntercept(final double[][] x) {
    final int nVars = x[0].length;
    final double[][] xAug = new double[x.length][nVars + 1];
    for (int i = 0; i < x.length; ++i) {
        if (x[i].length != nVars) {
            throw new DimensionMismatchException(x[i].length, nVars);
        }//www.  j av a2  s  . c  om
        xAug[i][0] = 1.0d;
        System.arraycopy(x[i], 0, xAug[i], 1, nVars);
    }
    return xAug;
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MatrixUtils.java

/**
 * Elementwise multiplication of two matrices.
 *
 * @param A a matrix that is multiplied by the elements of B
 * @param B another matrix/*from www  .  ja  v  a2  s . c  o m*/
 * @throws DimensionMismatchException
 */
public static void multiplyElementsBy(RealMatrix A, RealMatrix B) throws DimensionMismatchException {
    int nrow = A.getRowDimension();
    int ncol = A.getColumnDimension();
    if (nrow != B.getRowDimension()) {
        throw new DimensionMismatchException(nrow, B.getRowDimension());
    }
    if (ncol != B.getColumnDimension()) {
        throw new DimensionMismatchException(ncol, B.getColumnDimension());
    }

    RealMatrix M = new Array2DRowRealMatrix(nrow, ncol);
    for (int i = 0; i < nrow; i++) {
        for (int j = 0; j < ncol; j++) {
            A.multiplyEntry(i, j, B.getEntry(i, j));
        }
    }
}

From source file:com.itemanalysis.psychometrics.kernel.LocalLinearRegression.java

public double[] evaluate(double[] x, double[] y) throws DimensionMismatchException {
    if (x.length != y.length)
        throw new DimensionMismatchException(x.length, y.length);
    n = x.length;/*w ww  .  ja v a2  s .  c  o m*/
    double[] yprime = new double[n];
    double d = 0;
    double kernD = 0;
    double s0 = 0, s1 = 0, s2 = 0;

    for (int j = 0; j < numPoints; j++) {
        for (int i = 0; i < n; i++) {
            d = x[i] - points[j];
            kernD = kernel.value(d / bandwidth);
            s0 += kernD;
            s1 += d * kernD;
            s2 += d * d * kernD;
        }
        s0 /= n;
        s1 /= n;
        s2 /= n;

        for (int i = 0; i < n; i++) {
            yprime[j] += ((s2 - s1 * d) * kernD * y[i]) / (s2 * s0 - Math.pow(s1, 2));
        }
        yprime[j] /= n;
    }
    return yprime;
}

From source file:edu.ucsf.valelab.saim.calculations.SaimParameterValidator.java

public SaimParameterValidator(double[] lowerBounds, double[] upperBounds) {
    if (lowerBounds.length != 3)
        throw new DimensionMismatchException(lowerBounds.length, 3);
    if (upperBounds.length != 3)
        throw new DimensionMismatchException(upperBounds.length, 3);
    lowerBounds_ = lowerBounds;/*from ww  w .  ja va  2s  . c o  m*/
    upperBounds_ = upperBounds;
}

From source file:es.uvigo.ei.sing.laimages.core.operations.BilinearInterpolator.java

@Override
public BilinearInterpolatingFunction interpolate(double[] xval, double[] yval, double[][] fval)
        throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException {
    if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
        throw new NoDataException();
    }//from  w  w w . j ava 2  s . co  m
    if (xval.length != fval.length) {
        throw new DimensionMismatchException(xval.length, fval.length);
    }
    if (yval.length != fval[0].length) {
        throw new DimensionMismatchException(yval.length, fval[0].length);
    }

    MathArrays.checkOrder(xval);
    MathArrays.checkOrder(yval);

    return new BilinearInterpolatingFunction(xval, yval, fval);
}

From source file:de.bund.bfr.math.LinearFunction.java

public LinearFunction(double[] x, double[] y) throws NullArgumentException, NoDataException,
        DimensionMismatchException, NonMonotonicSequenceException {
    if (x == null || y == null) {
        throw new NullArgumentException();
    }/*from w w w  . j av a  2 s.  c  o  m*/
    if (x.length == 0 || y.length == 0) {
        throw new NoDataException();
    }
    if (y.length != x.length) {
        throw new DimensionMismatchException(y.length, x.length);
    }
    MathArrays.checkOrder(x);

    abscissa = MathArrays.copyOf(x);
    ordinate = MathArrays.copyOf(y);
}

From source file:com.itemanalysis.psychometrics.polycor.PolyserialLogLikelihoodTwoStep.java

public void summarize() throws DimensionMismatchException {
    if (dataX.length != dataY.length)
        throw new DimensionMismatchException(dataX.length, dataY.length);
    Frequency table = new Frequency();
    meanX = new Mean();
    sdX = new StandardDeviation();
    rxy = new PearsonCorrelation();
    for (int i = 0; i < nrow; i++) {
        meanX.increment(dataX[i]);//from  w  w  w .  j  av  a2s.co m
        sdX.increment(dataX[i]);
        rxy.increment(dataX[i], (double) dataY[i]);
        table.addValue(dataY[i]);
    }

    //compute thresholds
    nrow = table.getUniqueCount();
    freqDataY = new double[nrow];
    double ntotal = table.getSumFreq();
    for (int i = 0; i < (nrow - 1); i++) {
        freqDataY[i] = table.getCumFreq(i + 1);
        alpha[i] = normal.inverseCumulativeProbability(freqDataY[i] / ntotal);
    }
    alpha[nrow - 1] = 10;//set last threshold to a large number less than infinity
}

From source file:com.clust4j.algo.preprocess.MedianCenterer.java

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();//from  w  w  w  . ja  va  2 s  .co m

    // This effectively copies, so no need to do a copy later
    double[][] data = X.getData();
    final int m = data.length;
    final int n = data[0].length;

    if (n != medians.length)
        throw new DimensionMismatchException(n, medians.length);

    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            data[i][j] += medians[j];
        }
    }

    return new Array2DRowRealMatrix(data, false);
}