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.analysis.interpolation;
18  
19  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  
24  import java.util.Arrays;
25  import java.util.List;
26  
27  public class AxisCheckerTest {
28  
29      @Test
30      public void testMonoDimensional() {
31  
32          final List<D1> data = Arrays.asList(new D1(2.0), new D1(1.0), new D1(4.0), new D1(3.0));
33  
34          final RegularIndexer indexer = new AxisChecker<>(D1::getX, 1.0e-15).checkGridData(data);
35  
36          Assertions.assertEquals(1.0, indexer.getMin(),  1.0e-15);
37          Assertions.assertEquals(4.0, indexer.getMax(),  1.0e-15);
38          Assertions.assertEquals(  4, indexer.getN());
39          Assertions.assertEquals(1.0, indexer.getStep(), 1.0e-15);
40  
41          Assertions.assertEquals(0, indexer.index(1.0));
42          Assertions.assertEquals(1, indexer.index(2.0));
43          Assertions.assertEquals(2, indexer.index(3.0));
44          Assertions.assertEquals(3, indexer.index(4.0));
45  
46          Assertions.assertEquals(1.0, indexer.coordinate(0), 1.0e-15);
47          Assertions.assertEquals(2.0, indexer.coordinate(1), 1.0e-15);
48          Assertions.assertEquals(3.0, indexer.coordinate(2), 1.0e-15);
49          Assertions.assertEquals(4.0, indexer.coordinate(3), 1.0e-15);
50  
51      }
52  
53      @Test
54      public void testBiDimensional() {
55  
56          final List<D2> data = Arrays.asList(new D2(0.0, 2.0), new D2(4.0, 1.0), new D2(2.0, 4.0), new D2(2.0, 2.0),
57                                              new D2(2.0, 3.0), new D2(2.0, 1.0), new D2(0.0, 4.0), new D2(0.0, 3.0),
58                                              new D2(4.0, 2.0), new D2(0.0, 1.0), new D2(4.0, 4.0), new D2(4.0, 3.0));
59  
60          final RegularIndexer xIndexer = new AxisChecker<>(D2::getX, 1.0e-15).checkGridData(data);
61          final RegularIndexer yIndexer = new AxisChecker<>(D2::getY, 1.0e-15).checkGridData(data);
62  
63          Assertions.assertEquals(0.0, xIndexer.getMin(),  1.0e-15);
64          Assertions.assertEquals(4.0, xIndexer.getMax(),  1.0e-15);
65          Assertions.assertEquals(  3, xIndexer.getN());
66          Assertions.assertEquals(2.0, xIndexer.getStep(), 1.0e-15);
67  
68          Assertions.assertEquals(0, xIndexer.index(0.0));
69          Assertions.assertEquals(1, xIndexer.index(2.0));
70          Assertions.assertEquals(2, xIndexer.index(4.0));
71  
72          Assertions.assertEquals(0.0, xIndexer.coordinate(0), 1.0e-15);
73          Assertions.assertEquals(2.0, xIndexer.coordinate(1), 1.0e-15);
74          Assertions.assertEquals(4.0, xIndexer.coordinate(2), 1.0e-15);
75  
76          Assertions.assertEquals(1.0, yIndexer.getMin(),  1.0e-15);
77          Assertions.assertEquals(4.0, yIndexer.getMax(),  1.0e-15);
78          Assertions.assertEquals(  4, yIndexer.getN());
79          Assertions.assertEquals(1.0, yIndexer.getStep(), 1.0e-15);
80  
81          Assertions.assertEquals(0, yIndexer.index(1.0));
82          Assertions.assertEquals(1, yIndexer.index(2.0));
83          Assertions.assertEquals(2, yIndexer.index(3.0));
84          Assertions.assertEquals(3, yIndexer.index(4.0));
85  
86          Assertions.assertEquals(1.0, yIndexer.coordinate(0), 1.0e-15);
87          Assertions.assertEquals(2.0, yIndexer.coordinate(1), 1.0e-15);
88          Assertions.assertEquals(3.0, yIndexer.coordinate(2), 1.0e-15);
89          Assertions.assertEquals(4.0, yIndexer.coordinate(3), 1.0e-15);
90  
91      }
92  
93      @Test
94      public void testNaN() {
95          final List<D1> data = Arrays.asList(new D1(2.0), new D1(1.0), new D1(Double.NaN), new D1(3.0));
96          try {
97              new AxisChecker<>(D1::getX, 1.0e-15).checkGridData(data);
98              Assertions.fail("an exception should have been thrown");
99          } catch (MathIllegalArgumentException miae) {
100             Assertions.assertEquals(LocalizedCoreFormats.NAN_ELEMENT_AT_INDEX, miae.getSpecifier());
101             Assertions.assertEquals(2, miae.getParts()[0]);
102         }
103     }
104 
105     @Test
106     public void testDuplicatedNode() {
107 
108         final List<D2> data = Arrays.asList(new D2(0.0, 2.0), new D2(4.0, 1.0), new D2(2.0, 4.0), new D2(2.0, 2.0),
109                                             new D2(2.0, 3.0), new D2(2.0, 1.0), new D2(0.0, 3.0), new D2(0.0, 3.0),
110                                             new D2(4.0, 2.0), new D2(0.0, 1.0), new D2(4.0, 4.0), new D2(4.0, 3.0));
111 
112         final RegularIndexer xIndexer = new AxisChecker<>(D2::getX, 1.0e-15).checkGridData(data);
113         Assertions.assertEquals(0.0, xIndexer.getMin(),  1.0e-15);
114         Assertions.assertEquals(4.0, xIndexer.getMax(),  1.0e-15);
115         Assertions.assertEquals(  3, xIndexer.getN());
116         Assertions.assertEquals(2.0, xIndexer.getStep(), 1.0e-15);
117 
118         Assertions.assertEquals(0, xIndexer.index(0.0));
119         Assertions.assertEquals(1, xIndexer.index(2.0));
120         Assertions.assertEquals(2, xIndexer.index(4.0));
121 
122         try {
123             new AxisChecker<>(D2::getY, 1.0e-15).checkGridData(data);
124             Assertions.fail("an exception should have been thrown");
125         } catch (MathIllegalArgumentException miae) {
126             Assertions.assertEquals(LocalizedCoreFormats.IRREGULAR_GRID, miae.getSpecifier());
127             Assertions.assertEquals(3, miae.getParts()[0]);
128             Assertions.assertEquals(2, miae.getParts()[1]);
129             Assertions.assertEquals(4, miae.getParts()[2]);
130         }
131     }
132 
133     @Test
134     public void testOffsetPoint() {
135 
136         final List<D2> data = Arrays.asList(new D2(0.0,   2.0), new D2(4.0, 1.0), new D2(2.0, 4.0), new D2(2.0, 2.0),
137                                             new D2(2.001, 3.0), new D2(2.0, 1.0), new D2(0.0, 3.0), new D2(0.0, 4.0),
138                                             new D2(4.0,   2.0), new D2(0.0, 1.0), new D2(4.0, 4.0), new D2(4.0, 3.0));
139         try {
140             new AxisChecker<>(D2::getX, 1.0e-15).checkGridData(data);
141             Assertions.fail("an exception should have been thrown");
142         } catch (MathIllegalArgumentException miae) {
143             Assertions.assertEquals(LocalizedCoreFormats.MISALIGNED_GRID_POINT, miae.getSpecifier());
144             Assertions.assertEquals(1,      miae.getParts()[0]);
145             Assertions.assertEquals(2.0,    (Double) miae.getParts()[1], 1.0e-15);
146             Assertions.assertEquals(1.0e-3, (Double) miae.getParts()[2], 1.0e-15);
147         }
148     }
149 
150     private static class D1 {
151 
152         private final double x;
153 
154         public D1(final double x) {
155             this.x = x;
156         }
157 
158         public double getX() {
159             return x;
160         }
161 
162     }
163 
164     private static class D2 extends D1 {
165 
166         private final double y;
167 
168         public D2(final double x, final double y) {
169             super(x);
170             this.y = y;
171         }
172 
173         public double getY() {
174             return y;
175         }
176 
177     }
178 
179 }