Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hipparchus-core/src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,9 @@ If the output is not quite correct, check for invisible trailing spaces!
<action dev="psteitz" type="update">
Forked from the Apache Commons Math library.
</action>
<action dev="sean-g-rae" type="update">
Added some basic edge case unit tests.
</action>
</release>
</body>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TaylorMap implements DifferentialAlgebra {

/** Simple constructor.
* <p>
* The number of number of parameters and derivation orders of all
* The number of parameters and derivation orders of all
* functions must match.
* </p>
* @param point point at which map is evaluated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ void testAdd() {
assertEquals(4 - 2, FunctionUtils.add(c, FunctionUtils.compose(m, id)).value(2), EPS);
}

@Test
void testAddEmpty() {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
FunctionUtils.add(new UnivariateFunction[0]).value(1.0);
});
}

@Test
void testAddDifferentiable() {
UnivariateDifferentiableFunction sin = new Sin();
Expand All @@ -133,6 +140,13 @@ void testAddDifferentiable() {
assertEquals(4 + FastMath.sin(1.2), FunctionUtils.add(sin, c).value(1.2), EPS);
}

@Test
void testAddDifferentiableEmpty() {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
FunctionUtils.add(new UnivariateDifferentiableFunction[0]).value(1.0);
});
}

@Test
void testMultiply() {
UnivariateFunction c = new Constant(4);
Expand All @@ -143,6 +157,13 @@ void testMultiply() {
assertEquals(1, FunctionUtils.multiply(FunctionUtils.compose(inv, pow), pow).value(3.5), EPS);
}

@Test
void testMultiplyEmpty() {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
FunctionUtils.multiply(new UnivariateFunction[0]).value(1.0);
});
}

@Test
void testMultiplyDifferentiable() {
UnivariateDifferentiableFunction c = new Constant(4);
Expand Down Expand Up @@ -196,6 +217,14 @@ void testCollector() {
assertEquals(-24, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0);
}

@Test
void testCollectorEmpty() {
MultivariateFunction coll = FunctionUtils.collector(new Add(), 0);
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
coll.value(new double[0]);
});
}

@Test
void testSinc() {
BivariateFunction div = new Divide();
Expand Down Expand Up @@ -278,6 +307,13 @@ void testToDifferentiableUnivariate() {
assertEquals(2, ((Integer) e.getParts()[1]).intValue());
assertEquals(3, ((Integer) e.getParts()[0]).intValue());
}

try {
FunctionUtils.compose((UnivariateFunction) null).value(1.0);
fail("an exception should have been thrown");
} catch (NullPointerException e) {
// expected
}
}

@Test
Expand Down Expand Up @@ -340,6 +376,14 @@ void testToDifferentiableMultivariateInconsistentGradient() {
assertEquals(3, ((Integer) e.getParts()[0]).intValue());
assertEquals(2, ((Integer) e.getParts()[1]).intValue());
}

try {
MultivariateFunction df = FunctionUtils.derivative(mdf, new int[] { 1 });
df.value(new double[] { 1.0, 2.0 });
fail("an exception should have been thrown");
} catch (MathIllegalArgumentException e) {
// expected dimension mismatch
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import org.hipparchus.util.CombinatoricsUtils;
import org.hipparchus.util.FastMath;
import org.hipparchus.util.Precision;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests the PolynomialsUtils class.
Expand Down Expand Up @@ -265,6 +267,16 @@ void testHighDegreeLegendre() {
}
}

@Test
void testNegativeDegree() {
try {
PolynomialsUtils.createChebyshevPolynomial(-1);
fail("Expected MathIllegalArgumentException");
} catch (org.hipparchus.exception.MathIllegalArgumentException e) {
// expected
}
}

@Test
void testJacobiLegendre() {
for (int i = 0; i < 10; ++i) {
Expand Down Expand Up @@ -348,6 +360,26 @@ void testShift() {
PolynomialFunction f2x3
= new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 3));
checkPolynomial(f2x3, "29648 + 49239 x + 32745 x^2 + 10898 x^3 + 1815 x^4 + 121 x^5");

// Zero shift
double[] coeffs = { 1.0, 2.0, 3.0 };
Assertions.assertArrayEquals(coeffs, PolynomialsUtils.shift(coeffs, 0), 1e-15);

// Empty coeffs
double[] empty = new double[0];
Assertions.assertArrayEquals(empty, PolynomialsUtils.shift(empty, 1.0), 1e-15);

// Single coeff
double[] single = { 5.0 };
Assertions.assertArrayEquals(single, PolynomialsUtils.shift(single, 10.0), 1e-15);

// Null coeffs
try {
PolynomialsUtils.shift(null, 1.0);
fail("Expected NullPointerException");
} catch (NullPointerException e) {
// expected
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,64 @@ final void testToString() {
final Quaternion q = new Quaternion(1, 2, 3, 4);
assertEquals("[1.0 2.0 3.0 4.0]", q.toString());
}

@Test
final void testGetPositivePolarForm() {
final Quaternion q = new Quaternion(-3, 1, -2, 1);
final Quaternion p = q.getPositivePolarForm();
assertTrue(p.getQ0() > 0);
assertEquals(1.0, p.getNorm(), EPS);

final Quaternion q2 = new Quaternion(3, 1, -2, 1);
final Quaternion p2 = q2.getPositivePolarForm();
assertTrue(p2.getQ0() > 0);
assertEquals(1.0, p2.getNorm(), EPS);

final Quaternion q3 = new Quaternion(0, 1, -2, 1);
final Quaternion p3 = q3.getPositivePolarForm();
assertEquals(0, p3.getQ0(), EPS);
assertEquals(1.0, p3.getNorm(), EPS);
}

@Test
final void testHashCode() {
final Quaternion q1 = new Quaternion(1, 2, 3, 4);
final Quaternion q2 = new Quaternion(1, 2, 3, 4);
assertEquals(q1.hashCode(), q2.hashCode());

final Quaternion q3 = new Quaternion(1, 2, 3, 5);
assertNotEquals(q1.hashCode(), q3.hashCode());
}

@Test
final void testObjectEqualsMore() {
final Quaternion q1 = new Quaternion(1, 2, 3, 4);
assertEquals(q1, q1);
assertNotEquals(null, q1);
assertNotEquals(q1, "not a quaternion");
final Quaternion q2 = new Quaternion(1, 2, 3, 4);
assertEquals(q1, q2);
}

@Test
final void testInstanceMethods() {
final Quaternion q1 = new Quaternion(1, 2, 3, 4);
final Quaternion q2 = new Quaternion(5, 6, 7, 8);

// multiply(Quaternion)
final Quaternion qM = q1.multiply(q2);
assertEquals(Quaternion.multiply(q1, q2), qM);

// add(Quaternion)
final Quaternion qA = q1.add(q2);
assertEquals(Quaternion.add(q1, q2), qA);

// subtract(Quaternion)
final Quaternion qS = q1.subtract(q2);
assertEquals(Quaternion.subtract(q1, q2), qS);

// dotProduct(Quaternion)
final double d = q1.dotProduct(q2);
assertEquals(Quaternion.dotProduct(q1, q2), d, EPS);
}
}