View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 JulierUnscentedTransformTest {
29  
30      /** test state dimension equal to 0 */
31      @Test
32      void testWrongStateDimension() {
33          assertThrows(MathIllegalArgumentException.class, () -> {
34              new JulierUnscentedTransform(0);
35          });
36      }
37  
38      /** test weight computation */
39      @Test
40      void testWeights() {
41  
42          // Initialize
43          final int stateDim = 2;
44          final JulierUnscentedTransform julier = new JulierUnscentedTransform(stateDim, 0.0);
45          final RealVector wc = julier.getWc();
46          final RealVector wm = julier.getWm();
47  
48          // Verify
49          assertEquals(5,    wc.getDimension());
50          assertEquals(5,    wm.getDimension());
51          assertEquals(0.0,  wc.getEntry(0), Double.MIN_VALUE);
52          assertEquals(0.0,  wm.getEntry(0), Double.MIN_VALUE);
53          assertEquals(0.25, wc.getEntry(1), Double.MIN_VALUE);
54          assertEquals(0.25, wm.getEntry(1), Double.MIN_VALUE);
55          assertEquals(0.25, wc.getEntry(2), Double.MIN_VALUE);
56          assertEquals(0.25, wm.getEntry(2), Double.MIN_VALUE);
57  
58      }
59  
60      /** Test unscented transform */
61      @Test
62      void testUnscentedTransform() {
63  
64          // Initialize
65          final int stateDim = 2;
66          final JulierUnscentedTransform julier = new JulierUnscentedTransform(stateDim);
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          // Action
71          final RealVector[] sigma = julier.unscentedTransform(state, covariance);
72  
73          // Verify
74          assertEquals(5, sigma.length);
75          checkSigmaPoint(sigma[0], 1.0, 1.0);
76          checkSigmaPoint(sigma[1], 2.0, 1.0);
77          checkSigmaPoint(sigma[2], 1.0, 2.0);
78          checkSigmaPoint(sigma[3], 0.0, 1.0);
79          checkSigmaPoint(sigma[4], 1.0, 0.0);
80  
81      }
82  
83      /** Test inverse unscented transform */
84      @Test
85      void testInverseUnscentedTransform() {
86          
87          // Initialize
88          final int stateDim = 2;
89          final JulierUnscentedTransform julier = new JulierUnscentedTransform(stateDim);
90          final RealVector[] sigmaPoints = new RealVector[] {MatrixUtils.createRealVector(new double[] {1.0, 1.0}),
91                                                             MatrixUtils.createRealVector(new double[] {2.0, 1.0}),
92                                                             MatrixUtils.createRealVector(new double[] {1.0, 2.0}),
93                                                             MatrixUtils.createRealVector(new double[] {0.0, 1.0}),
94                                                             MatrixUtils.createRealVector(new double[] {1.0, 0.0})};
95          // Action
96          final Pair<RealVector, RealMatrix> inverse = julier.inverseUnscentedTransform(sigmaPoints);
97          final RealVector state = inverse.getFirst();
98          final RealMatrix covariance = inverse.getSecond();
99          
100         // Verify
101         assertEquals(2, state.getDimension());
102         assertEquals(1.0, state.getEntry(0), 0.);
103         assertEquals(1.0, state.getEntry(1), 0.);
104         
105         assertEquals(2, covariance.getColumnDimension());
106         assertEquals(2, covariance.getRowDimension());
107         assertEquals(0.5, covariance.getEntry(0, 0), 0.);
108         assertEquals(0.0, covariance.getEntry(0, 1), 0.);
109         assertEquals(0.0, covariance.getEntry(1, 0), 0.);
110         assertEquals(0.5, covariance.getEntry(1, 1), 0.);
111     }
112 
113     private static void checkSigmaPoint(final RealVector sigma, final double ref1, final double ref2) {
114         assertEquals(ref1,  sigma.getEntry(0), Double.MIN_VALUE);
115         assertEquals(ref2,  sigma.getEntry(1), Double.MIN_VALUE);
116     }
117 
118 }