Adding test coverage for math4.analysis.funtiona and euclidean.twod
This commit is contained in:
parent
f16d5b1722
commit
37c4939a8a
3
pom.xml
3
pom.xml
|
@ -327,6 +327,9 @@
|
|||
<contributor>
|
||||
<name>Mauro Talevi</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Rob Tompkins</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Radoslav Tsvetkov</name>
|
||||
</contributor>
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.analysis.function;
|
||||
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for all uncovered classes in org.apache.commons.math4.analysis.function that implement BivariateFunction explicitly.
|
||||
*/
|
||||
public class BivariateFunctionTest {
|
||||
|
||||
private static final double EPS = Math.ulp(1d);
|
||||
|
||||
@Test
|
||||
public void testAtan2() {
|
||||
Atan2 atan2 = new Atan2();
|
||||
Assert.assertEquals(FastMath.PI/4,atan2.value(1,1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubtract() {
|
||||
Subtract subtract = new Subtract();
|
||||
Assert.assertEquals(5, subtract.value(10,5), EPS);
|
||||
Assert.assertEquals(-5, subtract.value(5,10), EPS);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.analysis.function;
|
||||
|
||||
import org.apache.commons.math4.analysis.differentiation.DerivativeStructure;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for all classes in org.apache.commons.math4.analysis.function that implement UnivariateDifferentiableFunction explicitly.
|
||||
*/
|
||||
public class UnivariateDifferentiableFunctionTest {
|
||||
|
||||
private static final double EPS = Math.ulp(1d);
|
||||
|
||||
@Test
|
||||
public void testAcos() {
|
||||
Acos acos = new Acos();
|
||||
Assert.assertEquals(FastMath.PI/3, acos.value(0.5), EPS);
|
||||
Assert.assertEquals(FastMath.PI/4, acos.value(Double.valueOf(1/FastMath.sqrt(2))), EPS);
|
||||
double a = 0.5;
|
||||
Assert.assertEquals(-1/FastMath.sqrt(1-FastMath.pow(a,2)), acos.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcosh() {
|
||||
Acosh acosh = new Acosh();
|
||||
Assert.assertEquals(0,acosh.value(1), EPS);
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a,acosh.value(FastMath.cosh(a)), EPS);
|
||||
Assert.assertEquals(1/(FastMath.sqrt(a-1)*FastMath.sqrt(a+1)),acosh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsin() {
|
||||
Asin asin = new Asin();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, asin.value(FastMath.sin(a)), EPS);
|
||||
Assert.assertEquals(1/FastMath.sqrt(1 - FastMath.pow(a,2)), asin.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsinh() {
|
||||
Asinh asinh = new Asinh();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, asinh.value(FastMath.sinh(a)), EPS);
|
||||
Assert.assertEquals(1/FastMath.sqrt(FastMath.pow(a,2.0) + 1), asinh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtan() {
|
||||
Atan atan = new Atan();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, atan.value(FastMath.tan(a)), EPS);
|
||||
Assert.assertEquals(1/(FastMath.pow(a,2.0) + 1), atan.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAtanh() {
|
||||
Atanh atanh = new Atanh();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, atanh.value(FastMath.tanh(a)), EPS);
|
||||
Assert.assertEquals(1/(1 - FastMath.pow(a,2.0)), atanh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCbrt() {
|
||||
Cbrt cbrt = new Cbrt();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, cbrt.value(FastMath.pow(a,3)), EPS);
|
||||
Assert.assertEquals(1.0/(3.0*FastMath.pow(a, 2.0/3.0)), cbrt.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstant() {
|
||||
double a = 123.456;
|
||||
Constant constantNeg1 = new Constant(-1);
|
||||
Constant constant0 = new Constant(0);
|
||||
Constant constant5 = new Constant(5);
|
||||
Assert.assertEquals(-1, constantNeg1.value(a), EPS);
|
||||
Assert.assertEquals(0, constant0.value(a), EPS);
|
||||
Assert.assertEquals(5, constant5.value(a), EPS);
|
||||
Assert.assertEquals(0, constantNeg1.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
Assert.assertEquals(0, constant0.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
Assert.assertEquals(0, constant5.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCos() {
|
||||
Cos cos = new Cos();
|
||||
double a = 0.987;
|
||||
Assert.assertEquals(a, cos.value(FastMath.acos(a)), EPS);
|
||||
Assert.assertEquals(-FastMath.sin(a), cos.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCosh() {
|
||||
Cosh cosh = new Cosh();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, cosh.value(FastMath.acosh(a)), EPS);
|
||||
Assert.assertEquals(FastMath.sinh(a), cosh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExp() {
|
||||
Exp exp= new Exp();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, exp.value(FastMath.log(a)), EPS);
|
||||
Assert.assertEquals(exp.value(a), exp.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpm1() {
|
||||
Expm1 expm1 = new Expm1();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a-1, expm1.value(FastMath.log(a)), EPS);
|
||||
Assert.assertEquals(FastMath.exp(a), expm1.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdentity() {
|
||||
Identity identity = new Identity();
|
||||
double a = 123.456;
|
||||
Assert.assertEquals(a, identity.value(a), EPS);
|
||||
Assert.assertEquals(1, identity.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInverse() {
|
||||
Inverse inverse = new Inverse();
|
||||
double a = 123.456;
|
||||
Assert.assertEquals(1/a, inverse.value(a), EPS);
|
||||
Assert.assertEquals(-1/FastMath.pow(a,2), inverse.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLog() {
|
||||
double a = 123.456;
|
||||
Log log = new Log();
|
||||
Assert.assertEquals(Math.log(a), log.value(a), EPS);
|
||||
Assert.assertEquals(1/a,log.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLog10() {
|
||||
Log10 log10 = new Log10();
|
||||
double a =1.2345;
|
||||
Assert.assertEquals(a, log10.value(FastMath.pow(10, a)), EPS);
|
||||
Assert.assertEquals(1/(a*FastMath.log(10)), log10.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
//TODO: pick up here
|
||||
|
||||
@Test
|
||||
public void testLog1p() {
|
||||
Log1p log1p = new Log1p();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a+1,FastMath.exp(log1p.value(a)), EPS);
|
||||
Assert.assertEquals(1/(1+a), log1p.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinus() {
|
||||
Minus minus = new Minus();
|
||||
double a = 123.456;
|
||||
Assert.assertEquals(-a, minus.value(a), EPS);
|
||||
Assert.assertEquals(-1, minus.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPower() {
|
||||
Power squared = new Power(2);
|
||||
Power power2_5 = new Power(2.5);
|
||||
double a = 123.456;
|
||||
Assert.assertEquals(FastMath.pow(a,2), squared.value(a), EPS);
|
||||
Assert.assertEquals(FastMath.pow(a, 2.5), power2_5.value(a), EPS);
|
||||
Assert.assertEquals(2*a, squared.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
Assert.assertEquals(2.5*FastMath.pow(a,1.5), power2_5.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSin() {
|
||||
Sin sin = new Sin();
|
||||
double a = 0.987;
|
||||
Assert.assertEquals(a, sin.value(FastMath.asin(a)), EPS);
|
||||
Assert.assertEquals(FastMath.cos(a), sin.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSinh() {
|
||||
Sinh sinh = new Sinh();
|
||||
double a = 1.2345;
|
||||
Assert.assertEquals(a, sinh.value(FastMath.asinh(a)), EPS);
|
||||
Assert.assertEquals(FastMath.cosh(a), sinh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTan() {
|
||||
Tan tan = new Tan();
|
||||
double a = 0.987;
|
||||
Assert.assertEquals(a, tan.value(FastMath.atan(a)), EPS);
|
||||
Assert.assertEquals(1/(FastMath.pow(FastMath.cos(a),2)), tan.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTanh() {
|
||||
Tanh tanh = new Tanh();
|
||||
double a = 0.987;
|
||||
Assert.assertEquals(a, tanh.value(FastMath.atanh(a)), EPS);
|
||||
Assert.assertEquals(1/FastMath.pow(FastMath.cosh(a),2), tanh.value(new DerivativeStructure(1,1,0,a)).getPartialDerivative(1), EPS);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.analysis.function;
|
||||
|
||||
import org.apache.commons.math4.analysis.UnivariateFunction;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for all classes in org.apache.commons.math4.analysis.function that implement UnivariateFunction explicitly.
|
||||
*/
|
||||
public class UnivariateFunctionTest {
|
||||
|
||||
|
||||
private final double EPS = Math.ulp(1d);
|
||||
|
||||
@Test
|
||||
public void testAbs() {
|
||||
Abs abs = new Abs();
|
||||
Assert.assertEquals(5, abs.value(-5), EPS);
|
||||
Assert.assertEquals(5, abs.value(5), EPS);
|
||||
Assert.assertEquals(5.123, abs.value(-5.123), EPS);
|
||||
Assert.assertEquals(5.123, abs.value(5.123), EPS);
|
||||
Assert.assertEquals(0, abs.value(0), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCeil() {
|
||||
Ceil ceil = new Ceil();
|
||||
Assert.assertEquals(-5, ceil.value(-5), EPS);
|
||||
Assert.assertEquals(-4, ceil.value(-4.999), EPS);
|
||||
Assert.assertEquals(0, ceil.value(0), EPS);
|
||||
Assert.assertEquals(1, ceil.value(1), EPS);
|
||||
Assert.assertEquals(2, ceil.value(1.000000001), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloor() {
|
||||
Floor floor = new Floor();
|
||||
Assert.assertEquals(-5, floor.value(-5), EPS);
|
||||
Assert.assertEquals(-5, floor.value(-4.999), EPS);
|
||||
Assert.assertEquals(0, floor.value(0), EPS);
|
||||
Assert.assertEquals(1, floor.value(1), EPS);
|
||||
Assert.assertEquals(1, floor.value(1.000000001), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRint() {
|
||||
//Rint function is round half even.
|
||||
Rint rint = new Rint();
|
||||
Assert.assertEquals(-5, rint.value(-5), EPS);
|
||||
Assert.assertEquals(-4, rint.value(-4.5), EPS);
|
||||
Assert.assertEquals(0, rint.value(0), EPS);
|
||||
Assert.assertEquals(1, rint.value(1), EPS);
|
||||
Assert.assertEquals(2, rint.value(1.5), EPS);
|
||||
Assert.assertEquals(2, rint.value(2.5), EPS);
|
||||
Assert.assertEquals(-1, rint.value(-0.99999999), EPS);
|
||||
Assert.assertEquals(11, rint.value(10.99999999), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignum() {
|
||||
Signum signum = new Signum();
|
||||
Assert.assertEquals(-1, signum.value(-5), EPS);
|
||||
Assert.assertEquals(-1, signum.value(-4.5), EPS);
|
||||
Assert.assertEquals(0, signum.value(0), EPS);
|
||||
Assert.assertEquals(-0, signum.value(-0), EPS);
|
||||
Assert.assertEquals(1, signum.value(1), EPS);
|
||||
Assert.assertEquals(1, signum.value(1.5), EPS);
|
||||
Assert.assertEquals(1, signum.value(2.5), EPS);
|
||||
Assert.assertEquals(-1, signum.value(-0.99999999), EPS);
|
||||
Assert.assertEquals(1, signum.value(10.99999999), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStepFunction() {
|
||||
final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
|
||||
final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };
|
||||
|
||||
final UnivariateFunction f = new StepFunction(x, y);
|
||||
|
||||
Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
|
||||
Assert.assertEquals(4, f.value(-10), EPS);
|
||||
Assert.assertEquals(-1, f.value(-0.4), EPS);
|
||||
Assert.assertEquals(-5.5, f.value(0), EPS);
|
||||
Assert.assertEquals(0.4, f.value(2), EPS);
|
||||
Assert.assertEquals(5.8, f.value(10), EPS);
|
||||
Assert.assertEquals(51.2, f.value(30), EPS);
|
||||
Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUlp() {
|
||||
Ulp ulp = new Ulp();
|
||||
Assert.assertEquals(expectedUlp(1),ulp.value(1), EPS);
|
||||
Assert.assertEquals(expectedUlp(1.123456789),ulp.value(1.123456789), EPS);
|
||||
Assert.assertEquals(expectedUlp(-1),ulp.value(-1), EPS);
|
||||
Assert.assertEquals(expectedUlp(-1.123456789),ulp.value(-1.123456789), EPS);
|
||||
Assert.assertEquals(expectedUlp(0),ulp.value(0), EPS);
|
||||
Assert.assertEquals(expectedUlp(500000000),ulp.value(500000000), EPS);
|
||||
Assert.assertEquals(expectedUlp(-500000000),ulp.value(-500000000), EPS);
|
||||
}
|
||||
|
||||
private double expectedUlp(double x) {
|
||||
return FastMath.abs(x - Double.longBitsToDouble(Double.doubleToRawLongBits(x) ^ 1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.geometry.euclidean.twod;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class NestedLoopsTest {
|
||||
|
||||
private static final double EPS = Math.ulp(1d);
|
||||
|
||||
@Test
|
||||
public void testNestedLoops() throws Exception {
|
||||
Vector2D oneOne = new Vector2D(1.0, 1.0);
|
||||
Vector2D oneNegativeOne = new Vector2D(1.0, -1.0);
|
||||
Vector2D negativeOneNegativeOne = new Vector2D(-1.0, -1.0);
|
||||
Vector2D negativeOneOne = new Vector2D(-1.0, 1.0);
|
||||
Vector2D origin = new Vector2D(0, 0);
|
||||
|
||||
Vector2D [] vertices = new Vector2D[]{
|
||||
oneOne,
|
||||
oneNegativeOne,
|
||||
negativeOneNegativeOne,
|
||||
negativeOneOne,
|
||||
origin
|
||||
};
|
||||
|
||||
NestedLoops nestedLoops = new NestedLoops(0.00000001);
|
||||
nestedLoops.add(vertices);
|
||||
nestedLoops.correctOrientation();
|
||||
|
||||
Field surroundedField = nestedLoops.getClass().getDeclaredField("surrounded");
|
||||
Field loopField = nestedLoops.getClass().getDeclaredField("loop");
|
||||
surroundedField.setAccessible(Boolean.TRUE);
|
||||
loopField.setAccessible(Boolean.TRUE);
|
||||
List<NestedLoops> surrounded = (List<NestedLoops>) surroundedField.get(nestedLoops);
|
||||
Vector2D[] loop = (Vector2D []) loopField.get(surrounded.get(0));
|
||||
Set<Vector2D> vertexSet = new HashSet<>(Arrays.asList(loop));
|
||||
Assert.assertTrue(vertexSet.contains(oneOne));
|
||||
Assert.assertTrue(vertexSet.contains(oneNegativeOne));
|
||||
Assert.assertTrue(vertexSet.contains(negativeOneNegativeOne));
|
||||
Assert.assertTrue(vertexSet.contains(negativeOneOne));
|
||||
Assert.assertTrue(vertexSet.contains(origin));
|
||||
}
|
||||
|
||||
}
|
|
@ -16,26 +16,224 @@
|
|||
*/
|
||||
package org.apache.commons.math4.geometry.euclidean.twod;
|
||||
|
||||
import org.apache.commons.math4.geometry.euclidean.twod.Vector2D;
|
||||
import org.apache.commons.math4.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math4.exception.MathArithmeticException;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.FieldPosition;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParsePosition;
|
||||
|
||||
public class Vector2DTest {
|
||||
|
||||
private static final double EPS = Math.ulp(1d);
|
||||
|
||||
@Test
|
||||
public void testScaledVectorTripleConstructor() {
|
||||
Vector2D oneOne = new Vector2D(1.0,1.0);
|
||||
Vector2D oneTwo = new Vector2D(1.0,2.0);
|
||||
Vector2D oneThree = new Vector2D(1.0,3.0);
|
||||
|
||||
Vector2D tripleCombo = new Vector2D(3.0, oneOne, 1.0, oneTwo, 2.5, oneThree);
|
||||
|
||||
Assert.assertEquals(3.0 * 1 + 1.0 * 1 + 2.5 * 1,tripleCombo.getX(), EPS);
|
||||
Assert.assertEquals(3.0 * 1 + 1.0 * 2 + 2.5 * 3,tripleCombo.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScaledVectorQuadrupleConstructor() {
|
||||
Vector2D oneOne = new Vector2D(1.0, 1.0);
|
||||
Vector2D oneTwo = new Vector2D(1.0, 2.0);
|
||||
Vector2D oneThree = new Vector2D(1.0, 3.0);
|
||||
Vector2D oneFour = new Vector2D(1.0, 4.0);
|
||||
|
||||
Vector2D tripleCombo = new Vector2D(3.0, oneOne, 1.0, oneTwo, 2.5, oneThree, 2.0, oneFour);
|
||||
|
||||
Assert.assertEquals(3.0 * 1.0 + 1.0 * 1.0 + 2.5 * 1.0 + 2.0 * 1.0,tripleCombo.getX(), EPS);
|
||||
Assert.assertEquals(3.0 * 1.0 + 1.0 * 2.0 + 2.5 * 3.0 + 2.0 * 4.0,tripleCombo.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorExceptions() {
|
||||
double[] v = new double[] {0.0, 1.0, 2.0};
|
||||
try {
|
||||
new Vector2D(v);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof DimensionMismatchException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToArray() {
|
||||
Vector2D oneTwo = new Vector2D(1.0, 2.0);
|
||||
double[] array = oneTwo.toArray();
|
||||
Assert.assertEquals(1.0, array[0], EPS);
|
||||
Assert.assertEquals(2.0, array[1], EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetZero() {
|
||||
Vector2D zero = (new Vector2D(1.0, 1.0)).getZero();
|
||||
Assert.assertEquals(0.0, zero.getX(), EPS);
|
||||
Assert.assertEquals(0.0, zero.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNorm1() {
|
||||
Vector2D oneTwo = new Vector2D(-1.0, 2.0);
|
||||
Assert.assertEquals(3.0, oneTwo.getNorm1(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormSq() {
|
||||
Vector2D oneTwo = new Vector2D(-1.0, 2.0);
|
||||
Assert.assertEquals(5.0, oneTwo.getNormSq(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormInf() {
|
||||
Vector2D oneTwo = new Vector2D(-1.0, 2.0);
|
||||
Assert.assertEquals(2.0, oneTwo.getNormInf(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVectorAddition() {
|
||||
Vector2D minusOneTwo = new Vector2D(-1.0,2.0);
|
||||
Vector2D threeFive = new Vector2D(3.0,5.0);
|
||||
Vector2D addition = minusOneTwo.add(threeFive);
|
||||
Assert.assertEquals(2.0, addition.getX(), EPS);
|
||||
Assert.assertEquals(7.0, addition.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScaledVectorAddition() {
|
||||
Vector2D minusOneTwo = new Vector2D(-1.0,2.0);
|
||||
Vector2D threeFive = new Vector2D(3.0,5.0);
|
||||
Vector2D addition = minusOneTwo.add(2.0, threeFive);
|
||||
Assert.assertEquals(5.0, addition.getX(), EPS);
|
||||
Assert.assertEquals(12.0, addition.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVectorSubtraction() {
|
||||
Vector2D minusOneTwo = new Vector2D(-1.0,2.0);
|
||||
Vector2D threeFive = new Vector2D(3.0,5.0);
|
||||
Vector2D addition = minusOneTwo.subtract(threeFive);
|
||||
Assert.assertEquals(-4.0, addition.getX(), EPS);
|
||||
Assert.assertEquals(-3.0, addition.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScaledVectorSubtraction() {
|
||||
Vector2D minusOneTwo = new Vector2D(-1.0,2.0);
|
||||
Vector2D threeFive = new Vector2D(3.0,5.0);
|
||||
Vector2D addition = minusOneTwo.subtract(2.0, threeFive);
|
||||
Assert.assertEquals(-7.0, addition.getX(), EPS);
|
||||
Assert.assertEquals(-8.0, addition.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalize() {
|
||||
Vector2D minusOneTwo = new Vector2D(-1.0,2.0);
|
||||
Vector2D normalizedMinusOneTwo = minusOneTwo.normalize();
|
||||
Assert.assertEquals(-1.0/FastMath.sqrt(5), normalizedMinusOneTwo.getX(), EPS);
|
||||
Assert.assertEquals(2.0/FastMath.sqrt(5), normalizedMinusOneTwo.getY(), EPS);
|
||||
Vector2D zero = minusOneTwo.getZero();
|
||||
try {
|
||||
zero.normalize();
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof MathArithmeticException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAngle() {
|
||||
Vector2D oneOne = new Vector2D(1.0, 1.0);
|
||||
try {
|
||||
Vector2D.angle(oneOne.getZero(), oneOne.getZero());
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof MathArithmeticException);
|
||||
}
|
||||
Vector2D oneZero = new Vector2D(1.0,0.0);
|
||||
double angle = Vector2D.angle(oneOne, oneZero);
|
||||
Assert.assertEquals(FastMath.PI/4, angle, EPS);
|
||||
Assert.assertEquals(0.004999958333958323, Vector2D.angle(new Vector2D(20.0,0.0), new Vector2D(20.0,0.1)), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegate() {
|
||||
Vector2D oneOne = new Vector2D(1.0,1.0);
|
||||
Vector2D negated = oneOne.negate();
|
||||
Assert.assertEquals(-1.0, negated.getX(), EPS);
|
||||
Assert.assertEquals(-1.0, negated.getY(), EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInfinite() {
|
||||
Vector2D oneOne = new Vector2D(1.0, 1.0);
|
||||
Vector2D infiniteVector = new Vector2D(Double.POSITIVE_INFINITY, 0.0);
|
||||
Assert.assertFalse(oneOne.isInfinite());
|
||||
Assert.assertTrue(infiniteVector.isInfinite());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistance1() {
|
||||
Vector2D oneOne = new Vector2D(1.0,1.0);
|
||||
Vector2D fiveEleven = new Vector2D(5.0,11.0);
|
||||
double distance1 = oneOne.distance1(fiveEleven);
|
||||
Assert.assertEquals(14.0, distance1, EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistanceInf() {
|
||||
Vector2D oneOne = new Vector2D(1.0,1.0);
|
||||
Vector2D fiveEleven = new Vector2D(5.0,11.0);
|
||||
double distanceInf = oneOne.distanceInf(fiveEleven);
|
||||
double staticDistanceInf = Vector2D.distanceInf(oneOne, fiveEleven);
|
||||
Assert.assertEquals(10.0, distanceInf, EPS);
|
||||
Assert.assertEquals(distanceInf, staticDistanceInf, EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistanceSq() {
|
||||
Vector2D oneFive = new Vector2D(1.0, 5.0);
|
||||
Vector2D fourOne = new Vector2D(4.0, 1.0);
|
||||
double distanceSq = oneFive.distanceSq(fourOne);
|
||||
double staticDistanceSq = Vector2D.distanceSq(oneFive, fourOne);
|
||||
Assert.assertEquals(25.0, distanceSq, EPS);
|
||||
Assert.assertEquals(distanceSq, staticDistanceSq, EPS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
int hashCode = (new Vector2D(1.0,1.0)).hashCode();
|
||||
Assert.assertEquals(887095296, hashCode);
|
||||
Assert.assertEquals(542, (new Vector2D(Double.NaN, Double.NaN)).hashCode());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
Assert.assertEquals("{1; 2}", (new Vector2D(1.0,2.0)).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCrossProduct() {
|
||||
final double epsilon = 1e-10;
|
||||
|
||||
Vector2D p1 = new Vector2D(1, 1);
|
||||
Vector2D p2 = new Vector2D(2, 2);
|
||||
|
||||
Vector2D p3 = new Vector2D(3, 3);
|
||||
Assert.assertEquals(0.0, p3.crossProduct(p1, p2), epsilon);
|
||||
Assert.assertEquals(0.0, p3.crossProduct(p1, p2), EPS);
|
||||
|
||||
Vector2D p4 = new Vector2D(1, 2);
|
||||
Assert.assertEquals(1.0, p4.crossProduct(p1, p2), epsilon);
|
||||
Assert.assertEquals(1.0, p4.crossProduct(p1, p2), EPS);
|
||||
|
||||
Vector2D p5 = new Vector2D(2, 1);
|
||||
Assert.assertEquals(-1.0, p5.crossProduct(p1, p2), epsilon);
|
||||
Assert.assertEquals(-1.0, p5.crossProduct(p1, p2), EPS);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue