/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestNumberCluster(final Collection<Cluster<T>> clusters) throws ConvergenceException { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
/** * Construct the exception with a specific context and arguments. * * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public ConvergenceException(Localizable pattern, Object ... args) { getContext().addMessage(pattern, args); } }
/** {@inheritDoc} */ public ConvexHull2D generate(final Collection<Vector2D> points) throws NullArgumentException, ConvergenceException { // check for null points MathUtils.checkNotNull(points); Collection<Vector2D> hullVertices = null; if (points.size() < 2) { hullVertices = points; } else { hullVertices = findHullVertices(points); } try { return new ConvexHull2D(hullVertices.toArray(new Vector2D[hullVertices.size()]), tolerance); } catch (MathIllegalArgumentException e) { // the hull vertices may not form a convex hull if the tolerance value is to large throw new ConvergenceException(); } }
/** * Construct the exception with a specific context and arguments. * * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public ConvergenceException(Localizable pattern, Object ... args) { getContext().addMessage(pattern, args); } }
@Override protected RealVector solve(final RealMatrix jacobian, final RealVector residuals) { try { return new QRDecomposition(jacobian, SINGULARITY_THRESHOLD) .getSolver() .solve(residuals); } catch (SingularMatrixException e) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM, e); } } },
/** * Construct the exception with a specific context and arguments. * * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public ConvergenceException(Localizable pattern, Object ... args) { getContext().addMessage(pattern, args); } }
/** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestNumberCluster(final Collection<? extends Cluster<T>> clusters) throws ConvergenceException { int maxNumber = 0; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }
/** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center * @throws ConvergenceException if clusters are all empty */ private T getFarthestPoint(final Collection<Cluster<T>> clusters) throws ConvergenceException { double maxDistance = Double.NEGATIVE_INFINITY; Cluster<T> selectedCluster = null; int selectedPoint = -1; for (final Cluster<T> cluster : clusters) { // get the farthest point final T center = cluster.getCenter(); final List<T> points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = points.get(i).distanceFrom(center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); }
/** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center * @throws ConvergenceException if clusters are all empty */ private T getFarthestPoint(final Collection<CentroidCluster<T>> clusters) throws ConvergenceException { double maxDistance = Double.NEGATIVE_INFINITY; Cluster<T> selectedCluster = null; int selectedPoint = -1; for (final CentroidCluster<T> cluster : clusters) { // get the farthest point final Clusterable center = cluster.getCenter(); final List<T> points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = distance(points.get(i), center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); }
/** * Returns the first Bessel function, \(J_{order}(x)\). * * @param order Order of the Bessel function * @param x Argument * @return Value of the Bessel function of the first kind, \(J_{order}(x)\) * @throws MathIllegalArgumentException if {@code x} is too large relative to {@code order} * @throws ConvergenceException if the algorithm fails to converge */ public static double value(double order, double x) throws MathIllegalArgumentException, ConvergenceException { final int n = (int) order; final double alpha = order - n; final int nb = n + 1; final BesselJResult res = rjBesl(x, alpha, nb); if (res.nVals >= nb) { return res.vals[n]; } else if (res.nVals < 0) { throw new MathIllegalArgumentException(LocalizedFormats.BESSEL_FUNCTION_BAD_ARGUMENT,order, x); } else if (FastMath.abs(res.vals[res.nVals - 1]) < 1e-100) { return res.vals[n]; // underflow; return value (will be zero) } throw new ConvergenceException(LocalizedFormats.BESSEL_FUNCTION_FAILED_CONVERGENCE, order, x); }
throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
@Override protected RealVector solve(final RealMatrix jacobian, final RealVector residuals) { try { final Pair<RealMatrix, RealVector> normalEquation = computeNormalMatrix(jacobian, residuals); final RealMatrix normal = normalEquation.getFirst(); final RealVector jTr = normalEquation.getSecond(); return new LUDecomposition(normal, SINGULARITY_THRESHOLD) .getSolver() .solve(jTr); } catch (SingularMatrixException e) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM, e); } } },
@Override protected RealVector solve(final RealMatrix jacobian, final RealVector residuals) { try { final Pair<RealMatrix, RealVector> normalEquation = computeNormalMatrix(jacobian, residuals); final RealMatrix normal = normalEquation.getFirst(); final RealVector jTr = normalEquation.getSecond(); return new CholeskyDecomposition( normal, SINGULARITY_THRESHOLD, SINGULARITY_THRESHOLD) .getSolver() .solve(jTr); } catch (NonPositiveDefiniteMatrixException e) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM, e); } } },
throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE, x); throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE, x);
throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC);
throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC);
break; default : throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
break; default : throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS);
throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC);