org.apache.commons.math3.exception.MathUnsupportedOperationException Java Examples

The following examples show how to use org.apache.commons.math3.exception.MathUnsupportedOperationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the square-root of the matrix.
 * This implementation assumes that the matrix is symmetric and positive
 * definite.
 *
 * @return the square-root of the matrix.
 * @throws MathUnsupportedOperationException if the matrix is not
 * symmetric or not positive definite.
 * @since 3.1
 */
public RealMatrix getSquareRoot() {
    if (!isSymmetric) {
        throw new MathUnsupportedOperationException();
    }

    final double[] sqrtEigenValues = new double[realEigenvalues.length];
    for (int i = 0; i < realEigenvalues.length; i++) {
        final double eigen = realEigenvalues[i];
        if (eigen <= 0) {
            throw new MathUnsupportedOperationException();
        }
        sqrtEigenValues[i] = FastMath.sqrt(eigen);
    }
    final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues);
    final RealMatrix v = getV();
    final RealMatrix vT = getVT();

    return v.multiply(sqrtEigen).multiply(vT);
}
 
Example #2
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSparseIterator() {
    RealVector u = createVector();
    Iterator<Entry> i = u.sparseIterator();
    RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Iterator<Entry> j = v.sparseIterator();
    boolean flag;
    while (i.hasNext()) {
        Assert.assertTrue(j.hasNext());
        Entry exp = i.next();
        Entry act = j.next();
        Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
        Assert.assertTrue(equals(exp.getValue(), act.getValue()));
        exp.setIndex(RANDOM.nextInt(DIM));
        act.setIndex(RANDOM.nextInt(DIM));
        flag = false;
        try {
            act.setValue(RANDOM.nextDouble());
        } catch (MathUnsupportedOperationException e) {
            flag = true;
        }
        Assert.assertTrue("exception should have been thrown", flag);
    }
    Assert.assertFalse(j.hasNext());
}
 
Example #3
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIterator() {
    RealVector u = createVector();
    Iterator<Entry> i = u.iterator();
    RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Iterator<Entry> j = v.iterator();
    boolean flag;
    while (i.hasNext()) {
        Assert.assertTrue(j.hasNext());
        Entry exp = i.next();
        Entry act = j.next();
        Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
        Assert.assertTrue(equals(exp.getValue(), act.getValue()));
        exp.setIndex(RANDOM.nextInt(DIM));
        act.setIndex(RANDOM.nextInt(DIM));
        flag = false;
        try {
            act.setValue(RANDOM.nextDouble());
        } catch (MathUnsupportedOperationException e) {
            flag = true;
        }
        Assert.assertTrue("exception should have been thrown", flag);
    }
    Assert.assertFalse(j.hasNext());
}
 
Example #4
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIterator() {
    RealVector u = createVector();
    Iterator<Entry> i = u.iterator();
    RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Iterator<Entry> j = v.iterator();
    boolean flag;
    while (i.hasNext()) {
        Assert.assertTrue(j.hasNext());
        Entry exp = i.next();
        Entry act = j.next();
        Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
        Assert.assertTrue(equals(exp.getValue(), act.getValue()));
        exp.setIndex(RANDOM.nextInt(DIM));
        act.setIndex(RANDOM.nextInt(DIM));
        flag = false;
        try {
            act.setValue(RANDOM.nextDouble());
        } catch (MathUnsupportedOperationException e) {
            flag = true;
        }
        Assert.assertTrue("exception should have been thrown", flag);
    }
    Assert.assertFalse(j.hasNext());
}
 
Example #5
Source File: EigenDecomposition.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the square-root of the matrix.
 * This implementation assumes that the matrix is symmetric and positive
 * definite.
 *
 * @return the square-root of the matrix.
 * @throws MathUnsupportedOperationException if the matrix is not
 * symmetric or not positive definite.
 * @since 3.1
 */
public RealMatrix getSquareRoot() {
    if (!isSymmetric) {
        throw new MathUnsupportedOperationException();
    }

    final double[] sqrtEigenValues = new double[realEigenvalues.length];
    for (int i = 0; i < realEigenvalues.length; i++) {
        final double eigen = realEigenvalues[i];
        if (eigen <= 0) {
            throw new MathUnsupportedOperationException();
        }
        sqrtEigenValues[i] = FastMath.sqrt(eigen);
    }
    final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues);
    final RealMatrix v = getV();
    final RealMatrix vT = getVT();

    return v.multiply(sqrtEigen).multiply(vT);
}
 
Example #6
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSparseIterator() {
    RealVector u = createVector();
    Iterator<Entry> i = u.sparseIterator();
    RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Iterator<Entry> j = v.sparseIterator();
    boolean flag;
    while (i.hasNext()) {
        Assert.assertTrue(j.hasNext());
        Entry exp = i.next();
        Entry act = j.next();
        Assert.assertTrue(equals(exp.getIndex(), act.getIndex()));
        Assert.assertTrue(equals(exp.getValue(), act.getValue()));
        exp.setIndex(RANDOM.nextInt(DIM));
        act.setIndex(RANDOM.nextInt(DIM));
        flag = false;
        try {
            act.setValue(RANDOM.nextDouble());
        } catch (MathUnsupportedOperationException e) {
            flag = true;
        }
        Assert.assertTrue("exception should have been thrown", flag);
    }
    Assert.assertFalse(j.hasNext());
}
 
Example #7
Source File: NonLinearConjugateGradientOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #8
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 * @throws NullArgumentException if no initial simplex was passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (simplex == null) {
        throw new NullArgumentException();
    }
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #9
Source File: LevenbergMarquardtOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #10
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    optimizer.optimize(new MaxEval(100),
                       new ObjectiveFunction(fourExtrema),
                       GoalType.MINIMIZE,
                       new InitialGuess(new double[] { -3, 0 }),
                       new NelderMeadSimplex(new double[] { 0.2, 0.2 }),
                       new SimpleBounds(new double[] { -5, -1 },
                                        new double[] { 5, 1 }));
}
 
Example #11
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testConstraintsUnsupported() {
    createOptimizer().optimize(new MaxEval(100),
                               new Target(new double[] { 2 }),
                               new Weight(new double[] { 1 }),
                               new InitialGuess(new double[] { 1, 2 }),
                               new SimpleBounds(new double[] { -10, 0 },
                                                new double[] { 20, 30 }));
}
 
Example #12
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    optimizer.optimize(new MaxEval(100),
                       new ObjectiveFunction(fourExtrema),
                       GoalType.MINIMIZE,
                       new InitialGuess(new double[] { -3, 0 }),
                       new NelderMeadSimplex(new double[] { 0.2, 0.2 }),
                       new SimpleBounds(new double[] { -5, -1 },
                                        new double[] { 5, 1 }));
}
 
Example #13
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = MathUnsupportedOperationException.class)
public void testSetEntry() {
    RealVector u = createVector();
    RealVector v = RealVector.unmodifiableRealVector(u);
    for (int i = 0; i < DIM; i++) {
        v.setEntry(i, 0d);
    }
}
 
Example #14
Source File: NonLinearConjugateGradientOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #15
Source File: NonLinearConjugateGradientOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #16
Source File: EigenDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testSquareRootNonSymmetric() {
    final double[][] data = {
        { 1,  2, 4 },
        { 2,  3, 5 },
        { 11, 5, 9 }
    };

    final EigenDecomposition dec = new EigenDecomposition(MatrixUtils.createRealMatrix(data));
    @SuppressWarnings("unused")
    final RealMatrix sqrtM = dec.getSquareRoot();
}
 
Example #17
Source File: ChineseRingsClassifier.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an iterator that will present a series of points coordinates in
 * a random order.
 *
 * @param numSamples Number of samples.
 * @return the iterator.
 */
private Iterator<double[]> createRandomIterator(final long numSamples) {
    return new Iterator<double[]>() {
        /** Data. */
        final Vector3D[] points = rings.getPoints();
        /** RNG. */
        final RandomGenerator rng = new Well19937c();
        /** Number of samples. */
        private long n = 0;

        /** {@inheritDoc} */
        public boolean hasNext() {
            return n < numSamples;
        }

        /** {@inheritDoc} */
        public double[] next() {
            ++n;
            return points[rng.nextInt(points.length)].toArray();
        }

        /** {@inheritDoc} */
        public void remove() {
            throw new MathUnsupportedOperationException();
        }
    };
}
 
Example #18
Source File: Math_6_SimplexOptimizer_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 * @throws NullArgumentException if no initial simplex was passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (simplex == null) {
        throw new NullArgumentException();
    }
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #19
Source File: Math_6_SimplexOptimizer_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 * @throws NullArgumentException if no initial simplex was passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (simplex == null) {
        throw new NullArgumentException();
    }
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #20
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is the general test of most methods in the
 * {@link RealVector#unmodifiableRealVector(RealVector) unmodifiable vector}.
 * It works as follows.
 * First, an unmodifiable view of a copy of the specified random vector
 * {@code u} is created: this defines {@code v}. Then the <em>same</em>
 * method {@code m} is invoked on {@code u} and {@code v}, with randomly
 * generated parameters {@code args}.
 * If it turns out that {@code u} has changed after the call of method
 * {@code m}, then this test checks that the call of this method on
 * {@code v} resulted in a {@link MathUnsupportedOperationException}. If
 * {@code u} was not modified, then this test checks that the results
 * returned by the call of method {@code m} on {@code u} and {@code v}
 * returned the same result.
 *
 * @param m Method to be tested.
 * @param u Random vector from which the unmodifiable view is to be
 *constructed.
 * @param args Arguments to be passed to method {@code m}.
 */
private void callMethod(final Method m,
                        final RealVector u,
                        final Object... args)
    throws IllegalAccessException,
           IllegalArgumentException,
           InvocationTargetException {
    final RealVector uu = u.copy();
    final RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Object exp = m.invoke(u, args);
    if (equals(uu, u)) {
        Object act = m.invoke(v, args);
        Assert.assertTrue(m.toGenericString() + ", unmodifiable vector has changed",
                          equals(uu, v));
        Assert.assertTrue(m.toGenericString() + ", wrong result",
                          equals(exp, act));

    } else {
        boolean flag = false;
        try {
            m.invoke(v, args);
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof MathUnsupportedOperationException) {
                flag = true;
            }
        }
        Assert.assertTrue(m.toGenericString()+", exception should have been thrown", flag);
    }
}
 
Example #21
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected = MathUnsupportedOperationException.class)
public void testAddToEntry() {
    RealVector u = createVector();
    RealVector v = RealVector.unmodifiableRealVector(u);
    for (int i = 0; i < DIM; i++) {
        v.addToEntry(i, 0d);
    }
}
 
Example #22
Source File: UnmodifiableRealVectorAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This is the general test of most methods in the
 * {@link RealVector#unmodifiableRealVector(RealVector) unmodifiable vector}.
 * It works as follows.
 * First, an unmodifiable view of a copy of the specified random vector
 * {@code u} is created: this defines {@code v}. Then the <em>same</em>
 * method {@code m} is invoked on {@code u} and {@code v}, with randomly
 * generated parameters {@code args}.
 * If it turns out that {@code u} has changed after the call of method
 * {@code m}, then this test checks that the call of this method on
 * {@code v} resulted in a {@link MathUnsupportedOperationException}. If
 * {@code u} was not modified, then this test checks that the results
 * returned by the call of method {@code m} on {@code u} and {@code v}
 * returned the same result.
 *
 * @param m Method to be tested.
 * @param u Random vector from which the unmodifiable view is to be
 *constructed.
 * @param args Arguments to be passed to method {@code m}.
 */
private void callMethod(final Method m,
                        final RealVector u,
                        final Object... args)
    throws IllegalAccessException,
           IllegalArgumentException,
           InvocationTargetException {
    final RealVector uu = u.copy();
    final RealVector v = RealVector.unmodifiableRealVector(u.copy());
    Object exp = m.invoke(u, args);
    if (equals(uu, u)) {
        Object act = m.invoke(v, args);
        Assert.assertTrue(m.toGenericString() + ", unmodifiable vector has changed",
                          equals(uu, v));
        Assert.assertTrue(m.toGenericString() + ", wrong result",
                          equals(exp, act));

    } else {
        boolean flag = false;
        try {
            m.invoke(v, args);
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof MathUnsupportedOperationException) {
                flag = true;
            }
        }
        Assert.assertTrue(m.toGenericString()+", exception should have been thrown", flag);
    }
}
 
Example #23
Source File: NonLinearConjugateGradientOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #24
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    final MultivariateFunction func = new SumSincFunction(-1);
    final PowellOptimizer optim = new PowellOptimizer(1e-8, 1e-5,
                                                      1e-4, 1e-4);

    optim.optimize(new MaxEval(100),
                   new ObjectiveFunction(func),
                   GoalType.MINIMIZE,
                   new InitialGuess(new double[] { -3, 0 }),
                   new SimpleBounds(new double[] { -5, -1 },
                                    new double[] { 5, 1 }));
}
 
Example #25
Source File: SimplexOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 * @throws NullArgumentException if no initial simplex was passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (simplex == null) {
        throw new NullArgumentException();
    }
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}
 
Example #26
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testConstraintsUnsupported() {
    createOptimizer().optimize(new MaxEval(100),
                               new Target(new double[] { 2 }),
                               new Weight(new double[] { 1 }),
                               new InitialGuess(new double[] { 1, 2 }),
                               new SimpleBounds(new double[] { -10, 0 },
                                                new double[] { 20, 30 }));
}
 
Example #27
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6));
    optimizer.optimize(new MaxEval(100),
                       problem.getObjectiveFunction(),
                       problem.getObjectiveFunctionGradient(),
                       GoalType.MINIMIZE,
                       new InitialGuess(new double[] { 0 }),
                       new SimpleBounds(new double[] { -1 },
                                        new double[] { 1 }));
}
 
Example #28
Source File: RealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generic dense iterator. Iteration is in increasing order
 * of the vector index.
 *
 * <p>Note: derived classes are required to return an {@link Iterator} that
 * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
 * returns {@code true}.</p>
 *
 * @return a dense iterator.
 */
public Iterator<Entry> iterator() {
    final int dim = getDimension();
    return new Iterator<Entry>() {

        /** Current index. */
        private int i = 0;

        /** Current entry. */
        private Entry e = new Entry();

        /** {@inheritDoc} */
        public boolean hasNext() {
            return i < dim;
        }

        /** {@inheritDoc} */
        public Entry next() {
            if (i < dim) {
                e.setIndex(i++);
                return e;
            } else {
                throw new NoSuchElementException();
            }
        }

        /**
         * {@inheritDoc}
         *
         * @throws MathUnsupportedOperationException in all circumstances.
         */
        public void remove() throws MathUnsupportedOperationException {
            throw new MathUnsupportedOperationException();
        }
    };
}
 
Example #29
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=MathUnsupportedOperationException.class)
public void testBoundsUnsupported() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6));
    optimizer.optimize(new MaxEval(100),
                       problem.getObjectiveFunction(),
                       problem.getObjectiveFunctionGradient(),
                       GoalType.MINIMIZE,
                       new InitialGuess(new double[] { 0 }),
                       new SimpleBounds(new double[] { -1 },
                                        new double[] { 1 }));
}
 
Example #30
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws MathUnsupportedOperationException if bounds were passed to the
 * {@link #optimize(OptimizationData[]) optimize} method.
 */
private void checkParameters() {
    if (getLowerBound() != null ||
        getUpperBound() != null) {
        throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT);
    }
}