parent
a156eb0f23
commit
0cb1c875d9
@ -389,8 +389,8 @@ public final class Mathematics {
|
|||||||
/**
|
/**
|
||||||
* Intersect two ranges resulting in the maximum range contained in both.
|
* Intersect two ranges resulting in the maximum range contained in both.
|
||||||
*
|
*
|
||||||
* @param modRange
|
* @param r1
|
||||||
* @param makeRange
|
* @param r2
|
||||||
* @param destRange
|
* @param destRange
|
||||||
*/
|
*/
|
||||||
public static void intersectRange(double[][] r1, double[][] r2,
|
public static void intersectRange(double[][] r1, double[][] r2,
|
||||||
@ -538,6 +538,8 @@ public final class Mathematics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Linear interpolation between two points
|
||||||
|
*
|
||||||
* @param f0
|
* @param f0
|
||||||
* @param f1
|
* @param f1
|
||||||
* @param t
|
* @param t
|
||||||
@ -553,7 +555,7 @@ public final class Mathematics {
|
|||||||
*
|
*
|
||||||
* @param x The argument at the point with unknown function value
|
* @param x The argument at the point with unknown function value
|
||||||
* @param x0 The argument at the last position with a function value
|
* @param x0 The argument at the last position with a function value
|
||||||
* @param x1 The argument at the next known fuction value
|
* @param x1 The argument at the next known function value
|
||||||
* @param f0 The function value at the position x0
|
* @param f0 The function value at the position x0
|
||||||
* @param f1 The function value at the position x1
|
* @param f1 The function value at the position x1
|
||||||
* @return The function value at position x given by linear interpolation.
|
* @return The function value at position x given by linear interpolation.
|
||||||
@ -1214,9 +1216,7 @@ public final class Mathematics {
|
|||||||
*/
|
*/
|
||||||
public static double[] svDiv(double s, double[] v) {
|
public static double[] svDiv(double s, double[] v) {
|
||||||
double[] res = new double[v.length];
|
double[] res = new double[v.length];
|
||||||
for (int i = 0; i < v.length; i++) {
|
svDiv(s, v, res);
|
||||||
res[i] = v[i] / s;
|
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1243,9 +1243,7 @@ public final class Mathematics {
|
|||||||
*/
|
*/
|
||||||
public static double[] svMult(double s, double[] v) {
|
public static double[] svMult(double s, double[] v) {
|
||||||
double[] res = new double[v.length];
|
double[] res = new double[v.length];
|
||||||
for (int i = 0; i < v.length; i++) {
|
svMult(s, v, res);
|
||||||
res[i] = v[i] * s;
|
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1254,7 +1252,6 @@ public final class Mathematics {
|
|||||||
*
|
*
|
||||||
* @param s a scalar
|
* @param s a scalar
|
||||||
* @param v an array to be multiplied with s.
|
* @param v an array to be multiplied with s.
|
||||||
* @return a scaled array.
|
|
||||||
*/
|
*/
|
||||||
public static void svMult(double s, double[] v, double[] res) {
|
public static void svMult(double s, double[] v, double[] res) {
|
||||||
for (int i = 0; i < v.length; i++) {
|
for (int i = 0; i < v.length; i++) {
|
||||||
@ -1265,7 +1262,7 @@ public final class Mathematics {
|
|||||||
/**
|
/**
|
||||||
* Add vectors scaled: res[i] = s*v[i] + w[i]
|
* Add vectors scaled: res[i] = s*v[i] + w[i]
|
||||||
*
|
*
|
||||||
* @param s
|
* @param s Scaling factor
|
||||||
* @param v
|
* @param v
|
||||||
* @param w
|
* @param w
|
||||||
* @return
|
* @return
|
||||||
|
@ -4,9 +4,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
public class MathematicsTest {
|
public class MathematicsTest {
|
||||||
|
|
||||||
@ -45,6 +43,8 @@ public class MathematicsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testContains() throws Exception {
|
public void testContains() throws Exception {
|
||||||
assertTrue(Mathematics.contains(new int[]{1, 2, 3, 4, 5}, 4));
|
assertTrue(Mathematics.contains(new int[]{1, 2, 3, 4, 5}, 4));
|
||||||
|
|
||||||
|
assertFalse(Mathematics.contains(new int[]{1, 2, 3, 4, 5}, 9));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -145,7 +145,9 @@ public class MathematicsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScale() throws Exception {
|
public void testScale() throws Exception {
|
||||||
|
double[] values = {1.0, 2.0, 3.0};
|
||||||
|
Mathematics.scale(2, values);
|
||||||
|
assertArrayEquals(new double[]{2.0, 4.0, 6.0}, values, 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -175,5 +177,53 @@ public class MathematicsTest {
|
|||||||
assertFalse(Mathematics.isInRange(new double[]{9.9, 2.2}, ranges));
|
assertFalse(Mathematics.isInRange(new double[]{9.9, 2.2}, ranges));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInverse() throws Exception {
|
||||||
|
double[][] matrix = {{4.0, 3.0}, {3.0, 2.0}};
|
||||||
|
double[][] inverseMatrix = {{-2.0, 3.0}, {3.0, -4.0}};
|
||||||
|
|
||||||
|
double[][] result = Mathematics.inverse(matrix);
|
||||||
|
for (int i = 0; i < result.length; i++) {
|
||||||
|
assertArrayEquals(inverseMatrix[i], result[i], 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non square matrix
|
||||||
|
assertNull(Mathematics.inverse(new double[][]{{1.0, 2.0}}));
|
||||||
|
|
||||||
|
// Null matrix
|
||||||
|
assertNull(Mathematics.inverse(null));
|
||||||
|
|
||||||
|
// Matrix with determinant = 0
|
||||||
|
assertNull(Mathematics.inverse(new double[][]{
|
||||||
|
{1.0, 4.0, 5.0},
|
||||||
|
{2.0, 5.0, 7.0},
|
||||||
|
{3.0, 6.0, 9.0}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeterminant() throws Exception {
|
||||||
|
// 3x3 Matrix with determinant 0
|
||||||
|
assertEquals(0.0, Mathematics.determinant(new double[][]{
|
||||||
|
{1.0, 4.0, 5.0},
|
||||||
|
{2.0, 5.0, 7.0},
|
||||||
|
{3.0, 6.0, 9.0}
|
||||||
|
}), 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLinearInterpolation() throws Exception {
|
||||||
|
// Find x = 2.0 for x0(1,1) and x1(3,3)
|
||||||
|
assertEquals(2.0, Mathematics.linearInterpolation(2.0, 1.0, 3.0, 1.0, 3.0), 0.0);
|
||||||
|
|
||||||
|
// If x0 and x1 are the same we can't interpolate (returns f0)
|
||||||
|
assertEquals(3.0, Mathematics.linearInterpolation(3.0, 2.0, 2.0, 3.0, 3.0), 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testVvSub() throws Exception {
|
||||||
|
double[] v1 = {5.0, 6.0, 7.0}, v2 = {3.0, 4.0, 5.0};
|
||||||
|
|
||||||
|
assertArrayEquals(new double[]{2.0, 2.0, 2.0}, Mathematics.vvSub(v1, v2), 0.0);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user