Search in sources :

Example 1 with MathIllegalArgumentException

use of org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException in project ignite by apache.

the class SplitDataGenerator method split.

/**
 * Split region by continuous coordinate using given threshold.
 *
 * @param regIdx Region index.
 * @param coordIdx Coordinate index.
 * @param threshold Threshold.
 * @return {@code this}.
 */
SplitDataGenerator<V> split(int regIdx, int coordIdx, double threshold) {
    Region regToSplit = regs.get(regIdx);
    ContCoordInfo cci = regToSplit.contCoords.get(coordIdx);
    double left = cci.left;
    double right = cci.right;
    if (threshold < left || threshold > right)
        throw new MathIllegalArgumentException("Threshold is out of region bounds.");
    regToSplit.incTwoPow();
    Region newReg = Utils.copy(regToSplit);
    newReg.contCoords.get(coordIdx).left = threshold;
    regs.add(regIdx + 1, newReg);
    cci.right = threshold;
    IgniteBiTuple<Double, Double> bounds = boundsData.get(coordIdx);
    double min = bounds.get1();
    double max = bounds.get2();
    boundsData.put(coordIdx, new IgniteBiTuple<>(Math.min(threshold, min), Math.max(max, threshold)));
    return this;
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) LabeledVectorDouble(org.apache.ignite.ml.structures.LabeledVectorDouble)

Example 2 with MathIllegalArgumentException

use of org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException in project ignite by apache.

the class Precision method roundUnscaled.

/**
 * Rounds the given non-negative value to the "nearest" integer. Nearest is
 * determined by the rounding method specified. Rounding methods are defined
 * in {@link BigDecimal}.
 *
 * @param unscaled Value to round.
 * @param sign Sign of the original, scaled value.
 * @param roundingMtd Rounding method, as defined in {@link BigDecimal}.
 * @return the rounded value.
 * @throws MathArithmeticException if an exact operation is required but result is not exact
 * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method.
 * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0)
 */
private static double roundUnscaled(double unscaled, double sign, int roundingMtd) throws MathArithmeticException, MathIllegalArgumentException {
    switch(roundingMtd) {
        case BigDecimal.ROUND_CEILING:
            if (sign == -1)
                unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            else
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            break;
        case BigDecimal.ROUND_DOWN:
            unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            break;
        case BigDecimal.ROUND_FLOOR:
            if (sign == -1)
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            else
                unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            break;
        case BigDecimal.ROUND_HALF_DOWN:
            {
                unscaled = Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY);
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction > 0.5)
                    unscaled = Math.ceil(unscaled);
                else
                    unscaled = Math.floor(unscaled);
                break;
            }
        case BigDecimal.ROUND_HALF_EVEN:
            {
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction > 0.5)
                    unscaled = Math.ceil(unscaled);
                else if (fraction < 0.5)
                    unscaled = Math.floor(unscaled);
                else {
                    // The following equality test is intentional and needed for rounding purposes
                    if (Math.floor(unscaled) / 2.0 == Math.floor(Math.floor(unscaled) / 2.0)) {
                        // even
                        unscaled = Math.floor(unscaled);
                    } else {
                        // odd
                        unscaled = Math.ceil(unscaled);
                    }
                }
                break;
            }
        case BigDecimal.ROUND_HALF_UP:
            {
                unscaled = Math.nextAfter(unscaled, Double.POSITIVE_INFINITY);
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction >= 0.5)
                    unscaled = Math.ceil(unscaled);
                else
                    unscaled = Math.floor(unscaled);
                break;
            }
        case BigDecimal.ROUND_UNNECESSARY:
            if (unscaled != Math.floor(unscaled))
                throw new MathArithmeticException();
            break;
        case BigDecimal.ROUND_UP:
            // do not round if the discarded fraction is equal to zero
            if (unscaled != Math.floor(unscaled))
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            break;
        default:
            throw new MathIllegalArgumentException(INVALID_ROUNDING_METHOD, roundingMtd, "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, "ROUND_UP", BigDecimal.ROUND_UP);
    }
    return unscaled;
}
Also used : MathArithmeticException(org.apache.ignite.ml.math.exceptions.MathArithmeticException) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)

Example 3 with MathIllegalArgumentException

use of org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException in project ignite by apache.

the class SplitDataGenerator method split.

/**
 * Split region by categorical coordinate.
 *
 * @param regIdx Region index.
 * @param coordIdx Coordinate index.
 * @param cats Categories allowed for the left sub region.
 * @return {@code this}.
 */
SplitDataGenerator<V> split(int regIdx, int coordIdx, int[] cats) {
    BitSet subset = new BitSet();
    Arrays.stream(cats).forEach(subset::set);
    Region regToSplit = regs.get(regIdx);
    CatCoordInfo cci = regToSplit.catCoords.get(coordIdx);
    BitSet ssc = (BitSet) subset.clone();
    BitSet set = cci.bs;
    ssc.and(set);
    if (ssc.length() != subset.length())
        throw new MathIllegalArgumentException("Splitter set is not a subset of a parent subset.");
    ssc.xor(set);
    set.and(subset);
    regToSplit.incTwoPow();
    Region newReg = Utils.copy(regToSplit);
    newReg.catCoords.put(coordIdx, new CatCoordInfo(ssc));
    regs.add(regIdx + 1, newReg);
    return this;
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) BitSet(java.util.BitSet)

Example 4 with MathIllegalArgumentException

use of org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException in project ignite by apache.

the class FuzzyCMeansDistributedClusterer method cluster.

/**
 * {@inheritDoc}
 */
@Override
public FuzzyCMeansModel cluster(SparseDistributedMatrix points, int k) throws MathIllegalArgumentException, ConvergenceException {
    GridArgumentCheck.notNull(points, "points");
    if (k < 2)
        throw new MathIllegalArgumentException("The number of clusters is less than 2");
    Vector[] centers = initializeCenters(points, k);
    MembershipsAndSums membershipsAndSums = null;
    int iteration = 0;
    boolean finished = false;
    while (!finished && iteration < cMeansMaxIterations) {
        MembershipsAndSums newMembershipsAndSums = calculateMembership(points, centers);
        Vector[] newCenters = calculateNewCenters(points, newMembershipsAndSums, k);
        if (stopCond == StopCondition.STABLE_CENTERS)
            finished = isFinished(centers, newCenters);
        else
            finished = isFinished(membershipsAndSums, newMembershipsAndSums);
        centers = newCenters;
        membershipsAndSums = newMembershipsAndSums;
        iteration++;
    }
    if (iteration == cMeansMaxIterations)
        throw new ConvergenceException("Fuzzy C-Means algorithm has not converged after " + Integer.toString(iteration) + " iterations");
    return new FuzzyCMeansModel(centers, measure);
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) ConvergenceException(org.apache.ignite.ml.math.exceptions.ConvergenceException) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 5 with MathIllegalArgumentException

use of org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException in project ignite by apache.

the class SplitDataGenerator method split.

/**
 * Split region by continuous coordinate.using given threshold.
 *
 * @param regIdx Region index.
 * @param coordIdx Coordinate index.
 * @param threshold Threshold.
 * @return {@code this}.
 */
public SplitDataGenerator<V> split(int regIdx, int coordIdx, double threshold) {
    Region regToSplit = regs.get(regIdx);
    ContCoordInfo cci = regToSplit.contCoords.get(coordIdx);
    double left = cci.left;
    double right = cci.right;
    if (threshold < left || threshold > right)
        throw new MathIllegalArgumentException("Threshold is out of region bounds.");
    regToSplit.incTwoPow();
    Region newReg = Utils.copy(regToSplit);
    newReg.contCoords.get(coordIdx).left = threshold;
    regs.add(regIdx + 1, newReg);
    cci.right = threshold;
    IgniteBiTuple<Double, Double> bounds = boundsData.get(coordIdx);
    double min = bounds.get1();
    double max = bounds.get2();
    boundsData.put(coordIdx, new IgniteBiTuple<>(Math.min(threshold, min), Math.max(max, threshold)));
    return this;
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)

Aggregations

MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 BitSet (java.util.BitSet)2 Vector (org.apache.ignite.ml.math.Vector)2 ConvergenceException (org.apache.ignite.ml.math.exceptions.ConvergenceException)2 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)2 Matrix (org.apache.ignite.ml.math.Matrix)1 MathArithmeticException (org.apache.ignite.ml.math.exceptions.MathArithmeticException)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1 LabeledVectorDouble (org.apache.ignite.ml.structures.LabeledVectorDouble)1