Mathematics tests

refs #53
This commit is contained in:
Fabian Becker 2015-12-11 16:55:09 +01:00
parent 0cb1c875d9
commit 9d0b14becb
2 changed files with 61 additions and 10 deletions

View File

@ -811,7 +811,7 @@ public final class Mathematics {
}
/**
* Project the values in x to the range given. The range must be an vector
* Project the values in x to the range given. The range must be a vector
* of 2d-arrays each of which containing lower and upper bound in the i-th
* dimension. x must not be longer than the available ranges. Values
* exceeding the bounds are set on the bound. The number of bound violations
@ -841,9 +841,9 @@ public final class Mathematics {
/**
* Project the value to the range given.
*
* @param v
* @param min
* @param max
* @param v Value
* @param min Lower bound
* @param max Upper bound
* @return the closest value to v within [min,max]
*/
public static double projectValue(double v, double min, double max) {
@ -987,8 +987,8 @@ public final class Mathematics {
/**
* Rotate the vector by angle alpha around axis i/j
*
* @param vect
* @param alpha
* @param vect Vector
* @param alpha Rotation angle
* @param i
* @param j
*/
@ -1003,8 +1003,8 @@ public final class Mathematics {
* Rotate a given double vector using a rotation matrix. If the matrix is
* null, x will be returned unchanged. Matrix dimensions must fit.
*
* @param x
* @param rotMatrix
* @param x Vector
* @param rotMatrix Rotation matrix
* @return the rotated vector
*/
public static double[] rotate(double[] x, Matrix rotMatrix) {
@ -1077,6 +1077,7 @@ public final class Mathematics {
* Shift bounds by a constant value in every dimension.
*
* @param range
* @param dist
* @return
*/
public static void shiftRange(double[][] range, double dist) {
@ -1292,8 +1293,8 @@ public final class Mathematics {
/**
* Add vectors returning a new vector c = a + b;
*
* @param a
* @param b
* @param a Vector 1
* @param b Vector 2
* @return a new vector c = a + b
*/
public static double[] vvAdd(double[] a, double[] b) {

View File

@ -116,7 +116,29 @@ public class MathematicsTest {
@Test
public void testProjectToRange() throws Exception {
double[] v = {1.0, 11.0, 7.5, 9.0};
double[][] range = {
{5.0, 10.0},
{5.0, 10.0},
{5.0, 10.0},
{5.0, 10.0}
};
int violations = Mathematics.projectToRange(v, range);
// Has two violations
assertEquals(2, violations);
assertArrayEquals(new double[]{5.0, 10.0, 7.5, 9.0}, v, 0.0);
}
@Test
public void testProjectToValue() throws Exception {
// Lower range
assertEquals(5.0, Mathematics.projectValue(2.0, 5.0, 10.0), 0.0);
// Upper range
assertEquals(10.0, Mathematics.projectValue(12.0, 5.0, 10.0), 0.0);
// In range
assertEquals(7.5, Mathematics.projectValue(7.5, 5.0, 10.0), 0.0);
}
@Test
@ -226,4 +248,32 @@ public class MathematicsTest {
assertArrayEquals(new double[]{2.0, 2.0, 2.0}, Mathematics.vvSub(v1, v2), 0.0);
}
@Test
public void testVvAdd() throws Exception {
double[] v1 = {5.0, 6.0, 7.0}, v2 = {3.0, 4.0, 5.0};
assertArrayEquals(new double[]{8.0, 10.0, 12.0}, Mathematics.vvAdd(v1, v2), 0.0);
}
@Test
public void testSvMult() throws Exception {
double[] v1 = {3.0, 4.0, 5.0};
assertArrayEquals(new double[]{6.0, 8.0, 10.0}, Mathematics.svMult(2.0, v1), 0.0);
}
@Test
public void testSvDiv() throws Exception {
double[] v1 = {6.0, 8.0, 10.0};
assertArrayEquals(new double[]{3.0, 4.0, 5.0}, Mathematics.svDiv(2.0, v1), 0.0);
}
@Test
public void testSvAdd() throws Exception {
double[] v1 = {3.0, 4.0, 5.0};
assertArrayEquals(new double[]{5.0, 6.0, 7.0}, Mathematics.svAdd(2.0, v1), 0.0);
}
}