List of usage examples for org.apache.commons.math3.exception DimensionMismatchException DimensionMismatchException
public DimensionMismatchException(int wrong, int expected)
From source file:com.clust4j.data.DataSet.java
public void addColumn(String s, double[] col) { VecUtils.checkDims(col);//from w ww. ja v a2 s .c o m final int m = col.length; if (m != data.getRowDimension()) throw new DimensionMismatchException(m, data.getRowDimension()); final int n = data.getColumnDimension(); s = null == s ? (COL_PREFIX + n) : s; String[] newHeaders = new String[n + 1]; double[][] newData = new double[m][n + 1]; double[][] oldData = data.getDataRef(); for (int i = 0; i < m; i++) { for (int j = 0; j < n + 1; j++) { if (i == 0) newHeaders[j] = j != n ? headers[j] : s; newData[i][j] = j != n ? oldData[i][j] : col[i]; } } this.headers = newHeaders; this.data = new Array2DRowRealMatrix(newData, false); }
From source file:eu.tsp.sal.NPointCrossover.java
/** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome// ww w . ja v a 2 s. c o m * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different * @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes */ private ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second) throws DimensionMismatchException, NumberIsTooLargeException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } if (crossoverPoints >= length) { throw new NumberIsTooLargeException(crossoverPoints, length, false); } // array representations of the parents final List<T> parent1Rep = first.getRepresentation(); final List<T> parent2Rep = second.getRepresentation(); // and of the children final List<T> child1Rep = new ArrayList<T>(length); final List<T> child2Rep = new ArrayList<T>(length); final RandomGenerator random = SensorGeneticAlgorithm.getRandomGenerator(); List<T> c1 = child1Rep; List<T> c2 = child2Rep; int remainingPoints = crossoverPoints; int lastIndex = 0; for (int i = 0; i < crossoverPoints; i++, remainingPoints--) { // select the next crossover point at random final int crossoverIndex = 1 + lastIndex + random.nextInt(length - lastIndex - remainingPoints); // copy the current segment for (int j = lastIndex; j < crossoverIndex; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } // swap the children for the next segment List<T> tmp = c1; c1 = c2; c2 = tmp; lastIndex = crossoverIndex; } // copy the last segment for (int j = lastIndex; j < length; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); }
From source file:com.clust4j.utils.MatUtils.java
/** * Determine whether the col dims of A are equal to the row dims of B * @param a/*from w ww.j a v a 2 s.com*/ * @param b */ final static public void checkMultipliability(final double[][] a, final double[][] b) { checkDims(a); checkDims(b); if (a[0].length != b.length) throw new DimensionMismatchException(a[0].length, b.length); }
From source file:com.itemanalysis.psychometrics.irt.model.IrmGRM.java
public void setScoreWeights(double[] scoreWeight) throws DimensionMismatchException { if (scoreWeight.length != step.length) throw new DimensionMismatchException(scoreWeight.length, step.length); this.scoreWeight = scoreWeight; }
From source file:edu.ucsf.valelab.saim.calculations.SaimFunction.java
/** * Calculates the derivative of the SAIM function with respect to A, B, and h * at a given angle/* w w w .ja va 2s. com*/ * * @param x - angle in radians * @param parameters - array of 3 values: * A - scaling parameter * B - offset parameter, accounting for background * h - height in nm * @return - array of 3 values with the partial derivatives for A, B, and h */ @Override public double[] gradient(double x, double... parameters) { if (parameters.length != 3) throw new DimensionMismatchException(parameters.length, 3); angle_ = x; double A = parameters[0]; double h = parameters[2]; // partial derivative for A is the square of |1+rTE*eiphi(h)| Complex rTE = getFresnelTE(angle_); double f = 4.0 * Math.PI * sd_.nSample_ * Math.cos(angle_) / sd_.wavelength_; double phaseDiff = f * h; double c = rTE.getReal(); double d = rTE.getImaginary(); double val = 1 + 2 * c * Math.cos(phaseDiff) - 2 * d * Math.sin(phaseDiff) + c * c + d * d; // partial derivative for B is 1 or angle double bDerivative = 1.0; if (sd_.useBAngle_) bDerivative = angle_; // partial derivate for h is // - 2*A*c*f*sin(fh) - 2*A*d*f*cos(fh) // where f = phaseDiffFactor // c = rTE.Real(), and d = rTE.Imaginary() double pdh = -2 * A * f * (c * Math.sin(phaseDiff) + d * Math.cos(phaseDiff)); double result[] = { val, bDerivative, pdh }; return result; }
From source file:com.clust4j.algo.MeanShift.java
/** * Constructor with custom MeanShiftPlanner * @param data/* w w w . j a v a 2 s . co m*/ * @param planner */ protected MeanShift(RealMatrix data, MeanShiftParameters planner) { super(data, planner); // Check bandwidth... if (planner.getBandwidth() <= 0.0) error(new IllegalArgumentException("bandwidth " + "must be greater than 0.0")); // Check seeds dimension if (null != planner.getSeeds()) { if (planner.getSeeds().length == 0) error(new IllegalArgumentException("seeds " + "length must be greater than 0")); // Throws NonUniformMatrixException if non uniform... MatUtils.checkDimsForUniformity(planner.getSeeds()); if (planner.getSeeds()[0].length != (n = this.data.getColumnDimension())) error(new DimensionMismatchException(planner.getSeeds()[0].length, n)); if (planner.getSeeds().length > this.data.getRowDimension()) error(new IllegalArgumentException("seeds " + "length cannot exceed number of datapoints")); info("initializing kernels from given seeds"); // Handle the copying in the planner seeds = planner.getSeeds(); } else { // Default = all*/ info("no seeds provided; defaulting to all datapoints"); seeds = this.data.getData(); // use THIS as it's already scaled... n = this.data.getColumnDimension(); } /* * 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.maxIter = planner.getMaxIter(); this.tolerance = planner.getConvergenceTolerance(); this.autoEstimate = planner.getAutoEstimate(); final LogTimer aeTimer = new LogTimer(); /* * Assign bandwidth */ this.bandwidth = /* if all singular, just pick a number... */ this.singular_value ? 0.5 : /* Otherwise if we're auto-estimating, estimate it */ autoEstimate ? autoEstimateBW(this, planner.getAutoEstimationQuantile()) : planner.getBandwidth(); /* * Give auto-estimation timer update */ if (autoEstimate && !this.singular_value) info("bandwidth auto-estimated in " + (parallel ? "parallel in " : "") + aeTimer.toString()); logModelSummary(); }
From source file:com.wwidesigner.optimization.BaseObjectiveFunction.java
/** * Calculate errors at each fingering target. * /*w w w . j a v a 2 s . com*/ * @param point * - geometry values to test. point.length == nrDimensions. * @return array of error values, one for each fingering target. * @throws OperationCancelledException * @throws DimensionMismatchException. */ public double[] getErrorVector(double[] point) { if (cancel) { cancel = false; throw new OperationCancelledException("Operation cancelled."); } if (point.length != nrDimensions) { throw new DimensionMismatchException(point.length, nrDimensions); } setGeometryPoint(point); double[] errorVector = evaluator.calculateErrorVector(fingeringTargets); return errorVector; }
From source file:com.clust4j.data.DataSet.java
public void addColumns(String[] s, double[][] cols) { MatUtils.checkDims(cols);/* w w w. j a v a2s .com*/ final int m = cols.length; if (m != data.getRowDimension()) throw new DimensionMismatchException(m, data.getRowDimension()); int i, j; final int n = data.getColumnDimension(), newN = n + cols[0].length; // build headers if (null == s) { s = new String[cols[0].length]; for (i = 0, j = n; i < cols[0].length; i++, j++) s[i] = COL_PREFIX + j; } else { // Ensure no nulls for (i = 0, j = n; i < cols[0].length; i++, j++) s[i] = null == s[i] ? (COL_PREFIX + j) : s[i]; } String[] newHeaders = new String[newN]; double[][] newData = new double[m][newN]; double[][] oldData = data.getDataRef(); for (i = 0; i < m; i++) { for (j = 0; j < newN; j++) { if (i == 0) { newHeaders[j] = j < n ? headers[j] : s[j - n]; } newData[i][j] = j < n ? oldData[i][j] : cols[i][j - n]; } } this.headers = newHeaders; this.data = new Array2DRowRealMatrix(newData, false); }
From source file:MultivariateNormalDistribution.java
/** {@inheritDoc} */ public double density(final double[] vals) throws DimensionMismatchException { final int dim = getDimension(); if (vals.length != dim) { throw new DimensionMismatchException(vals.length, dim); }/*from w w w. j a v a 2 s . co m*/ return FastMath.pow(2 * FastMath.PI, -dim / 2) * FastMath.pow(covarianceMatrixDeterminant, -0.5) * getExponentTerm(vals); }
From source file:gamlss.utilities.WLSMultipleLinearRegression.java
/** * {@inheritDoc}/*from w ww.j a v a2 s . c om*/ * <p>This implementation computes and caches the QR decomposition of the X matrix * once it is successfully loaded.</p> */ protected void newXSampleData(double[][] x, ArrayRealVector w) { if (x == null) { throw new NullArgumentException(); } if (x.length == 0) { throw new NoDataException(); } if (this.isNoIntercept()) { this.xMatrix = new Array2DRowRealMatrix(x, true); } else { // Augment design matrix with initial unitary column 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); } xAug[i][0] = w.getEntry(i); System.arraycopy(x[i], 0, xAug[i], 1, nVars); } this.xMatrix = new Array2DRowRealMatrix(xAug, false); } this.qr = new QRDecomposition(getX()); }