1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.util;
18
19 import org.hipparchus.exception.MathIllegalArgumentException;
20 import org.hipparchus.linear.MatrixUtils;
21 import org.hipparchus.linear.RealMatrix;
22 import org.hipparchus.linear.RealVector;
23 import org.junit.jupiter.api.Test;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27
28 class MerweUnscentedTransformTest {
29
30
31 @Test
32 void testWrongStateDimension() {
33 assertThrows(MathIllegalArgumentException.class, () -> {
34 new MerweUnscentedTransform(0);
35 });
36 }
37
38
39 @Test
40 void testWeights() {
41
42
43 final int stateDim = 2;
44 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim);
45 final RealVector wc = merwe.getWc();
46 final RealVector wm = merwe.getWm();
47
48
49 assertEquals(5, wc.getDimension());
50 assertEquals(5, wm.getDimension());
51 assertEquals(-0.25, wc.getEntry(0), Double.MIN_VALUE);
52 assertEquals(-3.0, wm.getEntry(0), Double.MIN_VALUE);
53 assertEquals(1.0, wc.getEntry(1), Double.MIN_VALUE);
54 assertEquals(1.0, wm.getEntry(1), Double.MIN_VALUE);
55 assertEquals(1.0, wc.getEntry(2), Double.MIN_VALUE);
56 assertEquals(1.0, wm.getEntry(2), Double.MIN_VALUE);
57
58 }
59
60
61 @Test
62 void testUnscentedTransform() {
63
64
65 final int stateDim = 2;
66 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim, 0.5, 2.0, 0.0);
67 final RealVector state = MatrixUtils.createRealVector(new double[] {1.0, 1.0});
68 final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] {0.5, 0.5});
69
70
71 final RealVector[] sigma = merwe.unscentedTransform(state, covariance);
72
73
74 assertEquals(5, sigma.length);
75 checkSigmaPoint(sigma[0], 1.0, 1.0);
76 checkSigmaPoint(sigma[1], 1.5, 1.0);
77 checkSigmaPoint(sigma[2], 1.0, 1.5);
78 checkSigmaPoint(sigma[3], 0.5, 1.0);
79 checkSigmaPoint(sigma[4], 1.0, 0.5);
80 }
81
82
83 @Test
84 void testInverseUnscentedTransform() {
85
86
87 final int stateDim = 2;
88 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim, 0.5, 2.0, 0.0);
89 final RealVector[] sigmaPoints = new RealVector[] {MatrixUtils.createRealVector(new double[] {1.0, 1.0}),
90 MatrixUtils.createRealVector(new double[] {1.5, 1.0}),
91 MatrixUtils.createRealVector(new double[] {1.0, 1.5}),
92 MatrixUtils.createRealVector(new double[] {0.5, 1.0}),
93 MatrixUtils.createRealVector(new double[] {1.0, 0.5})};
94
95 final Pair<RealVector, RealMatrix> inverse = merwe.inverseUnscentedTransform(sigmaPoints);
96 final RealVector state = inverse.getFirst();
97 final RealMatrix covariance = inverse.getSecond();
98
99
100 assertEquals(2, state.getDimension());
101 assertEquals(1.0, state.getEntry(0), 0.);
102 assertEquals(1.0, state.getEntry(1), 0.);
103
104 assertEquals(2, covariance.getColumnDimension());
105 assertEquals(2, covariance.getRowDimension());
106 assertEquals(0.5, covariance.getEntry(0, 0), 0.);
107 assertEquals(0.0, covariance.getEntry(0, 1), 0.);
108 assertEquals(0.0, covariance.getEntry(1, 0), 0.);
109 assertEquals(0.5, covariance.getEntry(1, 1), 0.);
110 }
111
112 private static void checkSigmaPoint(final RealVector sigma, final double ref1, final double ref2) {
113 assertEquals(ref1, sigma.getEntry(0), Double.MIN_VALUE);
114 assertEquals(ref2, sigma.getEntry(1), Double.MIN_VALUE);
115 }
116
117 }