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, () -> new MerweUnscentedTransform(0));
34 }
35
36
37 @Test
38 void testWeights() {
39
40
41 final int stateDim = 2;
42 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim);
43 final RealVector wc = merwe.getWc();
44 final RealVector wm = merwe.getWm();
45
46
47 assertEquals(5, wc.getDimension());
48 assertEquals(5, wm.getDimension());
49 assertEquals(-0.25, wc.getEntry(0), Double.MIN_VALUE);
50 assertEquals(-3.0, wm.getEntry(0), Double.MIN_VALUE);
51 assertEquals(1.0, wc.getEntry(1), Double.MIN_VALUE);
52 assertEquals(1.0, wm.getEntry(1), Double.MIN_VALUE);
53 assertEquals(1.0, wc.getEntry(2), Double.MIN_VALUE);
54 assertEquals(1.0, wm.getEntry(2), Double.MIN_VALUE);
55
56 }
57
58
59 @Test
60 void testUnscentedTransform() {
61
62
63 final int stateDim = 2;
64 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim, 0.5, 2.0, 0.0);
65 final RealVector state = MatrixUtils.createRealVector(new double[] {1.0, 1.0});
66 final RealMatrix covariance = MatrixUtils.createRealDiagonalMatrix(new double[] {0.5, 0.5});
67
68
69 final RealVector[] sigma = merwe.unscentedTransform(state, covariance);
70
71
72 assertEquals(5, sigma.length);
73 checkSigmaPoint(sigma[0], 1.0, 1.0);
74 checkSigmaPoint(sigma[1], 1.5, 1.0);
75 checkSigmaPoint(sigma[2], 1.0, 1.5);
76 checkSigmaPoint(sigma[3], 0.5, 1.0);
77 checkSigmaPoint(sigma[4], 1.0, 0.5);
78 }
79
80
81 @Test
82 void testInverseUnscentedTransform() {
83
84
85 final int stateDim = 2;
86 final MerweUnscentedTransform merwe = new MerweUnscentedTransform(stateDim, 0.5, 2.0, 0.0);
87 final RealVector[] sigmaPoints = new RealVector[] {MatrixUtils.createRealVector(new double[] {1.0, 1.0}),
88 MatrixUtils.createRealVector(new double[] {1.5, 1.0}),
89 MatrixUtils.createRealVector(new double[] {1.0, 1.5}),
90 MatrixUtils.createRealVector(new double[] {0.5, 1.0}),
91 MatrixUtils.createRealVector(new double[] {1.0, 0.5})};
92
93 final Pair<RealVector, RealMatrix> inverse = merwe.inverseUnscentedTransform(sigmaPoints);
94 final RealVector state = inverse.getFirst();
95 final RealMatrix covariance = inverse.getSecond();
96
97
98 assertEquals(2, state.getDimension());
99 assertEquals(1.0, state.getEntry(0), 0.);
100 assertEquals(1.0, state.getEntry(1), 0.);
101
102 assertEquals(2, covariance.getColumnDimension());
103 assertEquals(2, covariance.getRowDimension());
104 assertEquals(0.5, covariance.getEntry(0, 0), 0.);
105 assertEquals(0.0, covariance.getEntry(0, 1), 0.);
106 assertEquals(0.0, covariance.getEntry(1, 0), 0.);
107 assertEquals(0.5, covariance.getEntry(1, 1), 0.);
108 }
109
110 private static void checkSigmaPoint(final RealVector sigma, final double ref1, final double ref2) {
111 assertEquals(ref1, sigma.getEntry(0), Double.MIN_VALUE);
112 assertEquals(ref2, sigma.getEntry(1), Double.MIN_VALUE);
113 }
114
115 }