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:com.clust4j.algo.preprocess.MeanCenterer.java

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();/*w w w  . j av a 2 s. com*/

    // 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 != means.length)
        throw new DimensionMismatchException(n, means.length);

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

    return new Array2DRowRealMatrix(data, false);
}

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

@Override
public double[][] transform(double[][] data) {
    checkFit();//  ww  w  . ja  v  a2  s  .c  om
    MatUtils.checkDimsForUniformity(data);

    final int m = data.length;
    final int n = data[0].length;

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

    double[][] X = new double[m][n];
    // second pass, subtract to center:
    for (int j = 0; j < n; j++) {
        double mn = mins[j];
        double rng = maxes[j] - mn;

        for (int i = 0; i < m; i++) {
            X[i][j] = ((data[i][j] - mn) / rng) * (max - min) + min;
        }
    }

    // assign
    return X;
}

From source file:com.itemanalysis.psychometrics.irt.estimation.IrtExaminee.java

public IrtExaminee(String groupID, ItemResponseModel[] irm, ItemResponseVector responseVector)
        throws DimensionMismatchException {
    //        super(groupID, irm.length);
    this.groupID = groupID;
    this.irm = irm;
    this.responseVector = responseVector;
    this.nItems = irm.length;
    if (irm.length != responseVector.getNumberOfItems())
        throw new DimensionMismatchException(irm.length, responseVector.getNumberOfItems());
    initializeScores();// w  w  w.jav  a  2s  .com
}

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

@Override
public double[][] transform(double[][] data) {
    checkFit();/*  w  ww.  ja  v a  2s  .co m*/
    MatUtils.checkDimsForUniformity(data);

    final int m = data.length;
    if (data[0].length != n)
        throw new DimensionMismatchException(n, data[0].length);

    double[][] X = new double[m][n];
    // mult to weight:
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            X[i][j] = data[i][j] * weights[j];
        }
    }

    // assign
    return X;
}

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

@Override
public double[][] transform(double[][] data) {
    checkFit();/*from ww w .j a va2 s  . c  o m*/
    MatUtils.checkDimsForUniformity(data);

    final int m = data.length;
    final int n = data[0].length;

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

    double[][] X = new double[m][n];
    // second pass, subtract to center:
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            X[i][j] = (data[i][j] - means[j]) / stdevs[j];
        }
    }

    // assign
    return X;
}

From source file:com.itemanalysis.psychometrics.irt.estimation.IrtExaminee.java

public IrtExaminee(ItemResponseModel[] irm, ItemResponseVector responseVector)
        throws DimensionMismatchException {
    //        super("", irm.length);
    this.groupID = "";
    this.irm = irm;
    this.responseVector = responseVector;
    this.nItems = irm.length;
    if (irm.length != this.responseVector.getNumberOfItems())
        throw new DimensionMismatchException(irm.length, this.responseVector.getNumberOfItems());
    initializeScores();/*from  w  w w .  java  2s .  com*/
}

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

@Override
public RealMatrix inverseTransform(RealMatrix X) {
    checkFit();/*from   www . j a  v a2 s.  c  o  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 != mins.length)
        throw new DimensionMismatchException(n, mins.length);

    double rng, mn;
    for (int j = 0; j < n; j++) {
        mn = mins[j];
        rng = maxes[j] - mn;

        for (int i = 0; i < m; i++) {
            data[i][j] -= min; // First subtract the min
            data[i][j] /= (max - min); // then divide over max - min
            data[i][j] *= rng; // multiply back by the range
            data[i][j] += mn; // finally add the mn back
        }
    }

    return new Array2DRowRealMatrix(data, false);
}

From source file:au.gov.ga.conn4d.utils.BicubicSplineInterpolator.java

/**
 * {@inheritDoc}/*w ww  . j av a 2s. c  o m*/
 */
public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval,
        final float[][] fval) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException,
        NumberIsTooSmallException {
    if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
        throw new NoDataException();
    }
    if (xval.length != fval.length) {
        throw new DimensionMismatchException(xval.length, fval.length);
    }

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

    final int xLen = xval.length;
    final int yLen = yval.length;

    // Samples (first index is y-coordinate, i.e. subarray variable is x)
    // 0 <= i < xval.length
    // 0 <= j < yval.length
    // fX[j][i] = f(xval[i], yval[j])
    final double[][] fX = new double[yLen][xLen];
    for (int i = 0; i < xLen; i++) {
        if (fval[i].length != yLen) {
            throw new DimensionMismatchException(fval[i].length, yLen);
        }

        for (int j = 0; j < yLen; j++) {
            fX[j][i] = fval[i][j];
        }
    }

    final SplineInterpolator spInterpolator = new SplineInterpolator();

    // For each line y[j] (0 <= j < yLen), construct a 1D spline with
    // respect to variable x
    final PolynomialSplineFunction[] ySplineX = new PolynomialSplineFunction[yLen];
    for (int j = 0; j < yLen; j++) {
        ySplineX[j] = spInterpolator.interpolate(xval, fX[j]);
    }

    // For each line x[i] (0 <= i < xLen), construct a 1D spline with
    // respect to variable y generated by array fY_1[i]
    final PolynomialSplineFunction[] xSplineY = new PolynomialSplineFunction[xLen];
    for (int i = 0; i < xLen; i++) {
        xSplineY[i] = spInterpolator.interpolate(yval, fval[i]);
    }

    // Partial derivatives with respect to x at the grid knots
    final double[][] dFdX = new double[xLen][yLen];
    for (int j = 0; j < yLen; j++) {
        final UnivariateFunction f = ySplineX[j].derivative();
        for (int i = 0; i < xLen; i++) {
            dFdX[i][j] = f.value(xval[i]);
        }
    }

    // Partial derivatives with respect to y at the grid knots
    final double[][] dFdY = new double[xLen][yLen];
    for (int i = 0; i < xLen; i++) {
        final UnivariateFunction f = xSplineY[i].derivative();
        for (int j = 0; j < yLen; j++) {
            dFdY[i][j] = f.value(yval[j]);
        }
    }

    // Cross partial derivatives
    final double[][] d2FdXdY = new double[xLen][yLen];
    for (int i = 0; i < xLen; i++) {
        final int nI = nextIndex(i, xLen);
        final int pI = previousIndex(i);
        for (int j = 0; j < yLen; j++) {
            final int nJ = nextIndex(j, yLen);
            final int pJ = previousIndex(j);
            d2FdXdY[i][j] = (fval[nI][nJ] - fval[nI][pJ] - fval[pI][nJ] + fval[pI][pJ])
                    / ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ]));
        }
    }

    // Create the interpolating splines
    return new BicubicSplineInterpolatingFunction(xval, yval, fval, dFdX, dFdY, d2FdXdY);
}

From source file:com.itemanalysis.psychometrics.irt.equating.HaebaraMethod.java

/**
 * For a common item linking design, both test form must have a set of items that are the same.
 * The parameter estimates will differ, but the common item must be paired. This method checks
 * that the common items are found in both item sets (Form X and Form Y). If not an exception occurs.
 *
 * This method checks that HashMaps have the same number of elements and that the keys in each map
 * are the same. The KeySet from itemFormY (sY) will be used for the keys hereafter.
 *
 *
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 *//*from   w  w  w.ja  v a  2 s.com*/
private void checkDimensions() throws DimensionMismatchException {
    Set<String> sX = itemFormX.keySet();
    sY = itemFormY.keySet();
    if (sX.size() != sY.size())
        throw new DimensionMismatchException(itemFormX.size(), itemFormY.size());
    int mismatch = 0;
    for (String s : sX) {
        if (!sY.contains(s))
            mismatch++;
    }
    for (String s : sY) {
        if (!sX.contains(s))
            mismatch++;
    }
    if (mismatch > 0)
        throw new DimensionMismatchException(mismatch, 0);
}

From source file:com.clust4j.algo.NearestCentroid.java

/**
 * Builds an instance of {@link NearestCentroid}
 * with an existing instance of {@link NearestCentroidParameters}
 * @param data/* w  w  w  . ja v a 2 s.  co m*/
 * @param y
 * @param planner
 * @throws DimensionMismatchException if the dims of y do not match the dims of data
 * @throws IllegalArgumentException if there is only one unique class in y
 */
protected NearestCentroid(RealMatrix data, int[] y, NearestCentroidParameters planner) {
    super(data, planner);

    VecUtils.checkDims(y);
    if ((m = data.getRowDimension()) != y.length)
        error(new DimensionMismatchException(y.length, m));

    // Build the label encoder
    /*
    try {
       this.encoder = new LabelEncoder(y).fit();
    } catch(IllegalArgumentException e) {
       error(e.getMessage());
       throw new IllegalArgumentException("Error in NearestCentroid: " + e.getMessage(), e);
    }
    */

    // Opting for SafeLabelEncoder in favor of allowing single class systems...
    this.encoder = new SafeLabelEncoder(y).fit();

    this.numClasses = encoder.numClasses;
    this.y_truth = VecUtils.copy(y);
    this.y_encodings = encoder.getEncodedLabels();

    /*
     * Check metric for validity
     */
    if (!isValidMetric(this.dist_metric)) {
        warn(this.dist_metric.getName() + " is not valid for " + getName() + ". "
                + "Falling back to default Euclidean dist");
        setSeparabilityMetric(DEF_DIST);
    }

    this.shrinkage = planner.getShrinkage();
    logModelSummary();
}