List of usage examples for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException
public DimensionMismatchException(int wrong, int expected)
From source file:cz.cuni.lf1.lge.ThunderSTORM.results.ModifiedLoess.java
/** * Compute a loess fit on the data at the original abscissae. * * @param xval the arguments for the interpolation points * @param yval the values for the interpolation points * @return values of the loess fit at corresponding original abscissae * @throws NonMonotonicSequenceException if {@code xval} not sorted in * strictly increasing order./* w w w . j a v a 2 s. c om*/ * @throws DimensionMismatchException if {@code xval} and {@code yval} have * different sizes. * @throws NoDataException if {@code xval} or {@code yval} has zero size. * @throws NotFiniteNumberException if any of the arguments and values are * not finite real numbers. * @throws NumberIsTooSmallException if the bandwidth is too small to * accomodate the size of the input data (i.e. the bandwidth must be * larger than 2/n). */ public final double[] smooth(final double[] xval, final double[] yval) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException { if (xval.length != yval.length) { throw new DimensionMismatchException(xval.length, yval.length); } final double[] unitWeights = new double[xval.length]; Arrays.fill(unitWeights, 1.0); return smooth(xval, yval, unitWeights); }
From source file:Armadillo.Analytics.Base.FastMathCalc.java
/** * Check two lengths are equal./* ww w. ja va2 s .com*/ * @param expectedLen expected length * @param actual actual length * @exception DimensionMismatchException if the two lengths are not equal */ private static void checkLen(int expectedLen, int actual) throws DimensionMismatchException { if (expectedLen != actual) { throw new DimensionMismatchException(actual, expectedLen); } }
From source file:com.clust4j.algo.HierarchicalAgglomerative.java
/** {@inheritDoc} */ @Override//ww w. j av a2 s. c o m public int[] predict(RealMatrix newData) { final int[] fit_labels = getLabels(); // throws the MNF exception if not fit final int numSamples = newData.getRowDimension(), n = newData.getColumnDimension(); // Make sure matches dimensionally if (n != this.data.getColumnDimension()) throw new DimensionMismatchException(n, data.getColumnDimension()); /* * There's no great way to predict on a hierarchical * algorithm, so we'll treat this like a CentroidLearner, * create centroids from the k clusters formed, then * predict via the CentroidUtils. This works because * Hierarchical is not a NoiseyClusterer */ // CORNER CASE: num_clusters == 1, return only label (0) if (1 == num_clusters) return VecUtils.repInt(fit_labels[0], numSamples); return new NearestCentroidParameters().setMetric(this.dist_metric) // if it fails, falls back to default Euclidean... .setVerbose(false) // just to be sure in case default ever changes... .fitNewModel(this.getData(), fit_labels).predict(newData); }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructure.java
/** * Compute composition of the instance by a univariate function. * /* w w w. j a v a2s.c o m*/ * @param f * array of value and derivatives of the function at * the current point (i.e. [f({@link #getValue()}), * f'({@link #getValue()}), f''({@link #getValue()})...]). * @return f(this) */ public DerivativeStructure compose(final double... f) { if (f.length != (getOrder() + 1)) { throw new DimensionMismatchException(f.length, getOrder() + 1); } final DerivativeStructure result = new DerivativeStructure(compiler); compiler.compose(data, f, result.data); return result; }
From source file:au.gov.ga.conn4d.utils.TricubicSplineInterpolatingFunction.java
/** * @param x Sample values of the x-coordinate, in increasing order. * @param y Sample values of the y-coordinate, in increasing order. * @param z Sample values of the y-coordinate, in increasing order. * @param f Values of the function on every grid point. * @param dFdX Values of the partial derivative of function with respect to x on every grid point. * @param dFdY Values of the partial derivative of function with respect to y on every grid point. * @param dFdZ Values of the partial derivative of function with respect to z on every grid point. * @param d2FdXdY Values of the cross partial derivative of function on every grid point. * @param d2FdXdZ Values of the cross partial derivative of function on every grid point. * @param d2FdYdZ Values of the cross partial derivative of function on every grid point. * @param d3FdXdYdZ Values of the cross partial derivative of function on every grid point. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the various arrays do not contain the expected number of elements. * @throws NonMonotonicSequenceException if {@code x}, {@code y} or {@code z} are not strictly increasing. *//*from w ww . j av a2 s . c om*/ public TricubicSplineInterpolatingFunction(double[] x, double[] y, double[] z, double[][][] f, double[][][] dFdX, double[][][] dFdY, double[][][] dFdZ, double[][][] d2FdXdY, double[][][] d2FdXdZ, double[][][] d2FdYdZ, double[][][] d3FdXdYdZ) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException { final int xLen = x.length; final int yLen = y.length; final int zLen = z.length; if (xLen == 0 || yLen == 0 || z.length == 0 || f.length == 0 || f[0].length == 0) { throw new NoDataException(); } if (xLen != f.length) { throw new DimensionMismatchException(xLen, f.length); } if (xLen != dFdX.length) { throw new DimensionMismatchException(xLen, dFdX.length); } if (xLen != dFdY.length) { throw new DimensionMismatchException(xLen, dFdY.length); } if (xLen != dFdZ.length) { throw new DimensionMismatchException(xLen, dFdZ.length); } if (xLen != d2FdXdY.length) { throw new DimensionMismatchException(xLen, d2FdXdY.length); } if (xLen != d2FdXdZ.length) { throw new DimensionMismatchException(xLen, d2FdXdZ.length); } if (xLen != d2FdYdZ.length) { throw new DimensionMismatchException(xLen, d2FdYdZ.length); } if (xLen != d3FdXdYdZ.length) { throw new DimensionMismatchException(xLen, d3FdXdYdZ.length); } MathArrays.checkOrder(x); MathArrays.checkOrder(y); MathArrays.checkOrder(z); xval = x.clone(); yval = y.clone(); zval = z.clone(); final int lastI = xLen - 1; final int lastJ = yLen - 1; final int lastK = zLen - 1; splines = new TricubicSplineFunction[lastI][lastJ][lastK]; for (int i = 0; i < lastI; i++) { if (f[i].length != yLen) { throw new DimensionMismatchException(f[i].length, yLen); } if (dFdX[i].length != yLen) { throw new DimensionMismatchException(dFdX[i].length, yLen); } if (dFdY[i].length != yLen) { throw new DimensionMismatchException(dFdY[i].length, yLen); } if (dFdZ[i].length != yLen) { throw new DimensionMismatchException(dFdZ[i].length, yLen); } if (d2FdXdY[i].length != yLen) { throw new DimensionMismatchException(d2FdXdY[i].length, yLen); } if (d2FdXdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdXdZ[i].length, yLen); } if (d2FdYdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdYdZ[i].length, yLen); } if (d3FdXdYdZ[i].length != yLen) { throw new DimensionMismatchException(d3FdXdYdZ[i].length, yLen); } final int ip1 = i + 1; for (int j = 0; j < lastJ; j++) { if (f[i][j].length != zLen) { throw new DimensionMismatchException(f[i][j].length, zLen); } if (dFdX[i][j].length != zLen) { throw new DimensionMismatchException(dFdX[i][j].length, zLen); } if (dFdY[i][j].length != zLen) { throw new DimensionMismatchException(dFdY[i][j].length, zLen); } if (dFdZ[i][j].length != zLen) { throw new DimensionMismatchException(dFdZ[i][j].length, zLen); } if (d2FdXdY[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdY[i][j].length, zLen); } if (d2FdXdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdZ[i][j].length, zLen); } if (d2FdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdYdZ[i][j].length, zLen); } if (d3FdXdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d3FdXdYdZ[i][j].length, zLen); } final int jp1 = j + 1; for (int k = 0; k < lastK; k++) { final int kp1 = k + 1; final double[] beta = new double[] { f[i][j][k], f[ip1][j][k], f[i][jp1][k], f[ip1][jp1][k], f[i][j][kp1], f[ip1][j][kp1], f[i][jp1][kp1], f[ip1][jp1][kp1], dFdX[i][j][k], dFdX[ip1][j][k], dFdX[i][jp1][k], dFdX[ip1][jp1][k], dFdX[i][j][kp1], dFdX[ip1][j][kp1], dFdX[i][jp1][kp1], dFdX[ip1][jp1][kp1], dFdY[i][j][k], dFdY[ip1][j][k], dFdY[i][jp1][k], dFdY[ip1][jp1][k], dFdY[i][j][kp1], dFdY[ip1][j][kp1], dFdY[i][jp1][kp1], dFdY[ip1][jp1][kp1], dFdZ[i][j][k], dFdZ[ip1][j][k], dFdZ[i][jp1][k], dFdZ[ip1][jp1][k], dFdZ[i][j][kp1], dFdZ[ip1][j][kp1], dFdZ[i][jp1][kp1], dFdZ[ip1][jp1][kp1], d2FdXdY[i][j][k], d2FdXdY[ip1][j][k], d2FdXdY[i][jp1][k], d2FdXdY[ip1][jp1][k], d2FdXdY[i][j][kp1], d2FdXdY[ip1][j][kp1], d2FdXdY[i][jp1][kp1], d2FdXdY[ip1][jp1][kp1], d2FdXdZ[i][j][k], d2FdXdZ[ip1][j][k], d2FdXdZ[i][jp1][k], d2FdXdZ[ip1][jp1][k], d2FdXdZ[i][j][kp1], d2FdXdZ[ip1][j][kp1], d2FdXdZ[i][jp1][kp1], d2FdXdZ[ip1][jp1][kp1], d2FdYdZ[i][j][k], d2FdYdZ[ip1][j][k], d2FdYdZ[i][jp1][k], d2FdYdZ[ip1][jp1][k], d2FdYdZ[i][j][kp1], d2FdYdZ[ip1][j][kp1], d2FdYdZ[i][jp1][kp1], d2FdYdZ[ip1][jp1][kp1], d3FdXdYdZ[i][j][k], d3FdXdYdZ[ip1][j][k], d3FdXdYdZ[i][jp1][k], d3FdXdYdZ[ip1][jp1][k], d3FdXdYdZ[i][j][kp1], d3FdXdYdZ[ip1][j][kp1], d3FdXdYdZ[i][jp1][kp1], d3FdXdYdZ[ip1][jp1][kp1], }; splines[i][j][k] = new TricubicSplineFunction(computeSplineCoefficients(beta)); } } } }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructureOnlyCompose.java
/** * Compute composition of the instance by a univariate function. * /* ww w .j a v a2 s.com*/ * @param f * array of value and derivatives of the function at * the current point (i.e. [f({@link #getValue()}), * f'({@link #getValue()}), f''({@link #getValue()})...]). * @return f(this) */ public DerivativeStructureOnlyCompose compose(final double... f) { if (f.length != (getOrder() + 1)) { throw new DimensionMismatchException(f.length, getOrder() + 1); } final DerivativeStructureOnlyCompose result = new DerivativeStructureOnlyCompose(compiler); compiler.compose(data, f, result.data); return result; }
From source file:com.clust4j.utils.MatUtils.java
/** * Extract the diagonal vector from a square matrix * @param data/*from ww w . jav a 2 s.c o m*/ * @throws NonUniformMatrixException if the matrix is not uniform * @throws DimensionMismatchException if the row dims do not match the col dims * @return the diagonal vector of a square matrix */ public static double[] diagFromSquare(final double[][] data) { checkDimsForUniformity(data); final int m = data.length, n = data[0].length; if (m != n) throw new DimensionMismatchException(m, n); final double[] out = new double[n]; for (int i = 0; i < m; i++) out[i] = data[i][i]; return out; }
From source file:com.clust4j.utils.MatUtils.java
/** * Flattens an upper triangular matrix into a vector of M choose 2 length * @param mat - the square upper triangular matrix * @throws DimensionMismatchException if the matrix is not square * @throws IllegalArgumentException if the matrix has no rows * @throws NonUniformMatrixException if the matrix is jagged * @return the upper triangular vector//from ww w. j a va 2 s . c o m */ public static double[] flattenUpperTriangularMatrix(final double[][] mat) { checkDimsForUniformity(mat); final int m = mat.length, n = mat[0].length; if (m != n) throw new DimensionMismatchException(m, n); final int s = m * (m - 1) / 2; // The shape of the flattened upper triangular matrix (m choose 2) final double[] vec = new double[s]; for (int i = 0, r = 0; i < m - 1; i++) for (int j = i + 1; j < m; j++, r++) vec[r] = mat[i][j]; return vec; }
From source file:au.gov.ga.conn4d.utils.TricubicSplineInterpolatingFunction.java
/** * @param x Sample values of the x-coordinate, in increasing order. * @param y Sample values of the y-coordinate, in increasing order. * @param z Sample values of the y-coordinate, in increasing order. * @param f Values of the function on every grid point. * @param dFdX Values of the partial derivative of function with respect to x on every grid point. * @param dFdY Values of the partial derivative of function with respect to y on every grid point. * @param dFdZ Values of the partial derivative of function with respect to z on every grid point. * @param d2FdXdY Values of the cross partial derivative of function on every grid point. * @param d2FdXdZ Values of the cross partial derivative of function on every grid point. * @param d2FdYdZ Values of the cross partial derivative of function on every grid point. * @param d3FdXdYdZ Values of the cross partial derivative of function on every grid point. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the various arrays do not contain the expected number of elements. * @throws NonMonotonicSequenceException if {@code x}, {@code y} or {@code z} are not strictly increasing. */// w w w. ja v a 2 s . co m public TricubicSplineInterpolatingFunction(double[] x, double[] y, double[] z, float[][][] f, double[][][] dFdX, double[][][] dFdY, double[][][] dFdZ, double[][][] d2FdXdY, double[][][] d2FdXdZ, double[][][] d2FdYdZ, double[][][] d3FdXdYdZ) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException { final int xLen = x.length; final int yLen = y.length; final int zLen = z.length; if (xLen == 0 || yLen == 0 || z.length == 0 || f.length == 0 || f[0].length == 0) { throw new NoDataException(); } if (xLen != f.length) { throw new DimensionMismatchException(xLen, f.length); } if (xLen != dFdX.length) { throw new DimensionMismatchException(xLen, dFdX.length); } if (xLen != dFdY.length) { throw new DimensionMismatchException(xLen, dFdY.length); } if (xLen != dFdZ.length) { throw new DimensionMismatchException(xLen, dFdZ.length); } if (xLen != d2FdXdY.length) { throw new DimensionMismatchException(xLen, d2FdXdY.length); } if (xLen != d2FdXdZ.length) { throw new DimensionMismatchException(xLen, d2FdXdZ.length); } if (xLen != d2FdYdZ.length) { throw new DimensionMismatchException(xLen, d2FdYdZ.length); } if (xLen != d3FdXdYdZ.length) { throw new DimensionMismatchException(xLen, d3FdXdYdZ.length); } MathArrays.checkOrder(x); MathArrays.checkOrder(y); MathArrays.checkOrder(z); xval = x.clone(); yval = y.clone(); zval = z.clone(); final int lastI = xLen - 1; final int lastJ = yLen - 1; final int lastK = zLen - 1; splines = new TricubicSplineFunction[lastI][lastJ][lastK]; for (int i = 0; i < lastI; i++) { if (f[i].length != yLen) { throw new DimensionMismatchException(f[i].length, yLen); } if (dFdX[i].length != yLen) { throw new DimensionMismatchException(dFdX[i].length, yLen); } if (dFdY[i].length != yLen) { throw new DimensionMismatchException(dFdY[i].length, yLen); } if (dFdZ[i].length != yLen) { throw new DimensionMismatchException(dFdZ[i].length, yLen); } if (d2FdXdY[i].length != yLen) { throw new DimensionMismatchException(d2FdXdY[i].length, yLen); } if (d2FdXdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdXdZ[i].length, yLen); } if (d2FdYdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdYdZ[i].length, yLen); } if (d3FdXdYdZ[i].length != yLen) { throw new DimensionMismatchException(d3FdXdYdZ[i].length, yLen); } final int ip1 = i + 1; for (int j = 0; j < lastJ; j++) { if (f[i][j].length != zLen) { throw new DimensionMismatchException(f[i][j].length, zLen); } if (dFdX[i][j].length != zLen) { throw new DimensionMismatchException(dFdX[i][j].length, zLen); } if (dFdY[i][j].length != zLen) { throw new DimensionMismatchException(dFdY[i][j].length, zLen); } if (dFdZ[i][j].length != zLen) { throw new DimensionMismatchException(dFdZ[i][j].length, zLen); } if (d2FdXdY[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdY[i][j].length, zLen); } if (d2FdXdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdZ[i][j].length, zLen); } if (d2FdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdYdZ[i][j].length, zLen); } if (d3FdXdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d3FdXdYdZ[i][j].length, zLen); } final int jp1 = j + 1; for (int k = 0; k < lastK; k++) { final int kp1 = k + 1; final double[] beta = new double[] { f[i][j][k], f[ip1][j][k], f[i][jp1][k], f[ip1][jp1][k], f[i][j][kp1], f[ip1][j][kp1], f[i][jp1][kp1], f[ip1][jp1][kp1], dFdX[i][j][k], dFdX[ip1][j][k], dFdX[i][jp1][k], dFdX[ip1][jp1][k], dFdX[i][j][kp1], dFdX[ip1][j][kp1], dFdX[i][jp1][kp1], dFdX[ip1][jp1][kp1], dFdY[i][j][k], dFdY[ip1][j][k], dFdY[i][jp1][k], dFdY[ip1][jp1][k], dFdY[i][j][kp1], dFdY[ip1][j][kp1], dFdY[i][jp1][kp1], dFdY[ip1][jp1][kp1], dFdZ[i][j][k], dFdZ[ip1][j][k], dFdZ[i][jp1][k], dFdZ[ip1][jp1][k], dFdZ[i][j][kp1], dFdZ[ip1][j][kp1], dFdZ[i][jp1][kp1], dFdZ[ip1][jp1][kp1], d2FdXdY[i][j][k], d2FdXdY[ip1][j][k], d2FdXdY[i][jp1][k], d2FdXdY[ip1][jp1][k], d2FdXdY[i][j][kp1], d2FdXdY[ip1][j][kp1], d2FdXdY[i][jp1][kp1], d2FdXdY[ip1][jp1][kp1], d2FdXdZ[i][j][k], d2FdXdZ[ip1][j][k], d2FdXdZ[i][jp1][k], d2FdXdZ[ip1][jp1][k], d2FdXdZ[i][j][kp1], d2FdXdZ[ip1][j][kp1], d2FdXdZ[i][jp1][kp1], d2FdXdZ[ip1][jp1][kp1], d2FdYdZ[i][j][k], d2FdYdZ[ip1][j][k], d2FdYdZ[i][jp1][k], d2FdYdZ[ip1][jp1][k], d2FdYdZ[i][j][kp1], d2FdYdZ[ip1][j][kp1], d2FdYdZ[i][jp1][kp1], d2FdYdZ[ip1][jp1][kp1], d3FdXdYdZ[i][j][k], d3FdXdYdZ[ip1][j][k], d3FdXdYdZ[i][jp1][k], d3FdXdYdZ[ip1][jp1][k], d3FdXdYdZ[i][j][kp1], d3FdXdYdZ[ip1][j][kp1], d3FdXdYdZ[i][jp1][kp1], d3FdXdYdZ[ip1][jp1][kp1], }; splines[i][j][k] = new TricubicSplineFunction(computeSplineCoefficients(beta)); } } } }
From source file:com.clust4j.algo.NearestNeighborHeapSearch.java
public double[] kernelDensity(double[][] X, double bandwidth, PartialKernelDensity kern, double absTol, double relTol, boolean returnLog) { double b_c = bandwidth, logAbsTol = FastMath.log(absTol), logRelTol = FastMath.log(relTol); MutableDouble logMinBound = new MutableDouble(), logMaxBound = new MutableDouble(), logBoundSpread = new MutableDouble(); MutableDouble dist_LB = new MutableDouble(), dist_UB = new MutableDouble(); int m = data_arr.length, n = data_arr[0].length, i; // Ensure X col dim matches training data col dim MatUtils.checkDims(X);/* w w w. ja va 2 s . c om*/ if (X[0].length != n) throw new DimensionMismatchException(n, X[0].length); final double logKNorm = logKernelNorm(b_c, n, kern), logM = FastMath.log(m), log2 = FastMath.log(2); double[][] Xarr = MatUtils.copy(X); double[] logDensity = new double[Xarr.length], pt; for (i = 0; i < Xarr.length; i++) { pt = Xarr[i]; minMaxDist(this, 0, pt, dist_LB, dist_UB); logMinBound.value = logM + kern.getDensity(dist_UB.value, b_c); logMaxBound.value = logM + kern.getDensity(dist_LB.value, b_c); logBoundSpread.value = logSubExp(logMaxBound.value, logMinBound.value); estimateKernelDensitySingleDepthFirst(0, pt, kern, b_c, logKNorm, logAbsTol, logRelTol, logMinBound.value, logBoundSpread.value, logMinBound, logBoundSpread); logDensity[i] = logAddExp(logMinBound.value, logBoundSpread.value - log2); } // Norm results for (i = 0; i < logDensity.length; i++) logDensity[i] += logKNorm; return returnLog ? logDensity : VecUtils.exp(logDensity); }