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.CalculusFieldElementAbstractTest;
20  import org.hipparchus.exception.LocalizedCoreFormats;
21  import org.hipparchus.exception.MathIllegalArgumentException;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertNotEquals;
28  
29  public class FieldTupleTest extends CalculusFieldElementAbstractTest<FieldTuple<Binary64>> {
30      public static final Binary64 X = new Binary64(1.2345);
31  
32      public static final FieldTuple<Binary64> PLUS_X = new FieldTuple<>(X, X);
33  
34      public static final FieldTuple<Binary64> MINUS_X = new FieldTuple<>(X.negate(), X.negate());
35  
36      public static final Binary64 Y = new Binary64(6.789);
37  
38      public static final FieldTuple<Binary64> PLUS_Y = new FieldTuple<>(Y, Y);
39  
40      public static final FieldTuple<Binary64> MINUS_Y = new FieldTuple<>(Y.negate(), Y.negate());
41  
42      public static final FieldTuple<Binary64> PLUS_ZERO = new FieldTuple<>(new Binary64(0.0), new Binary64(0.0));
43  
44      public static final FieldTuple<Binary64> MINUS_ZERO = new FieldTuple<>(new Binary64(-0.0), new Binary64(-0.0));
45  
46      @Override
47      protected FieldTuple<Binary64> build(final double x) {
48          return new FieldTuple<>(new Binary64(x), new Binary64(x));
49      }
50  
51      @Test
52      void testNorm() {
53          final FieldTuple<Binary64> oneToFive = new FieldTuple<>(Binary64.ONE, new Binary64(2));
54          assertEquals(3, oneToFive.norm());
55      }
56  
57      @Test
58      void testComponents() {
59          assertEquals(2, PLUS_ZERO.getDimension());
60          final FieldTuple<Binary64> oneToFive = new FieldTuple<>(new Binary64(1),
61                                                                            new Binary64(2),
62                                                                            new Binary64(3),
63                                                                            new Binary64(4),
64                                                                            new Binary64(5));
65          assertEquals(5, oneToFive.getDimension());
66          assertArrayEquals(new Binary64[] {
67              new Binary64(1), new Binary64(2), new Binary64(3), new Binary64(4), new Binary64(5) },
68                                   oneToFive.getComponents());
69          assertEquals(1, oneToFive.getComponent(0).getReal(), 1.0e-10);
70          assertEquals(2, oneToFive.getComponent(1).getReal(), 1.0e-10);
71          assertEquals(3, oneToFive.getComponent(2).getReal(), 1.0e-10);
72          assertEquals(4, oneToFive.getComponent(3).getReal(), 1.0e-10);
73          assertEquals(5, oneToFive.getComponent(4).getReal(), 1.0e-10);
74      }
75  
76      @Test
77      void testEquals() {
78          assertNotEquals(PLUS_ZERO, null);
79          assertEquals(PLUS_ZERO, PLUS_ZERO);
80          assertEquals(PLUS_X,    PLUS_X);
81          assertEquals(PLUS_Y,    PLUS_Y);
82          assertEquals(MINUS_X,   MINUS_X);
83          assertEquals(MINUS_Y,   MINUS_Y);
84          assertNotEquals(PLUS_X,  new FieldTuple<>(new Binary64(1),
85                                                           new Binary64(2),
86                                                           new Binary64(3),
87                                                           new Binary64(4),
88                                                           new Binary64(5)));
89          assertNotEquals(PLUS_X,  new FieldTuple<>(PLUS_X.getComponent(0), new Binary64(999.999)));
90          assertNotEquals(null, PLUS_ZERO.getField());
91          assertNotEquals(PLUS_X.getField(),  new FieldTuple<>(new Binary64(1),
92                          new Binary64(2),
93                          new Binary64(3),
94                          new Binary64(4),
95                          new Binary64(5)).getField());
96          assertEquals(PLUS_ZERO.getField(), MINUS_Y.getField());
97      }
98  
99      @Test
100     void testHashcode() {
101         assertEquals(-1264241693, PLUS_ZERO.getField().hashCode());
102         assertEquals(1492525478, PLUS_ZERO.hashCode());
103         assertEquals(1492525478, MINUS_ZERO.hashCode());
104         assertEquals(211878758, PLUS_X.hashCode());
105         assertEquals(211878758, MINUS_X.hashCode());
106         assertEquals(1460019622, new FieldTuple<>(new Binary64(1), new Binary64(2)).hashCode());
107         assertEquals(1491476902, new FieldTuple<>(new Binary64(2), new Binary64(1)).hashCode());
108     }
109 
110     @Test
111     void testAdd() {
112         FieldTuple<Binary64> expected, actual;
113 
114         expected = new FieldTuple<>(X.add(Y), X.add(Y));
115         actual = PLUS_X.add(PLUS_Y);
116         assertEquals(expected, actual);
117         actual = PLUS_Y.add(PLUS_X);
118         assertEquals(expected, actual);
119 
120         expected = new FieldTuple<>(X.add(Y.negate()), X.add(Y.negate()));
121         actual = PLUS_X.add(MINUS_Y);
122         assertEquals(expected, actual);
123         actual = MINUS_Y.add(PLUS_X);
124         assertEquals(expected, actual);
125 
126         expected = new FieldTuple<>(X.negate().add(Y.negate()), X.negate().add(Y.negate()));
127         actual = MINUS_X.add(MINUS_Y);
128         assertEquals(expected, actual);
129         actual = MINUS_Y.add(MINUS_X);
130         assertEquals(expected, actual);
131 
132     }
133 
134     @Test
135     void testSubtract() {
136         FieldTuple<Binary64> expected, actual;
137 
138         expected = new FieldTuple<>(X.subtract(Y), X.subtract(Y));
139         actual = PLUS_X.subtract(PLUS_Y);
140         assertEquals(expected, actual);
141 
142         expected = new FieldTuple<>(X.subtract(Y.negate()), X.subtract(Y.negate()));
143         actual = PLUS_X.subtract(MINUS_Y);
144         assertEquals(expected, actual);
145 
146         expected = new FieldTuple<>(X.negate().subtract(Y), X.negate().subtract(Y));
147         actual = MINUS_X.subtract(PLUS_Y);
148         assertEquals(expected, actual);
149 
150         expected = new FieldTuple<>(X.negate().subtract(Y.negate()), X.negate().subtract(Y.negate()));
151         actual = MINUS_X.subtract(MINUS_Y);
152         assertEquals(expected, actual);
153 
154     }
155 
156     @Test
157     void testNegate() {
158         FieldTuple<Binary64> expected, actual;
159 
160         expected = MINUS_X;
161         actual = PLUS_X.negate();
162         assertEquals(expected, actual);
163 
164         expected = PLUS_X;
165         actual = MINUS_X.negate();
166         assertEquals(expected, actual);
167 
168         expected = MINUS_ZERO;
169         actual = PLUS_ZERO.negate();
170         assertEquals(expected, actual);
171 
172         expected = PLUS_ZERO;
173         actual = MINUS_ZERO.negate();
174         assertEquals(expected, actual);
175 
176     }
177 
178     @Test
179     void testMultiply() {
180         FieldTuple<Binary64> expected, actual;
181 
182         expected = new FieldTuple<>(X.multiply(Y), X.multiply(Y));
183         actual = PLUS_X.multiply(PLUS_Y);
184         assertEquals(expected, actual);
185         actual = PLUS_Y.multiply(PLUS_X);
186         assertEquals(expected, actual);
187 
188         expected = new FieldTuple<>(X.multiply(Y.negate()), X.multiply(Y.negate()));
189         actual = PLUS_X.multiply(MINUS_Y);
190         assertEquals(expected, actual);
191         actual = MINUS_Y.multiply(PLUS_X);
192         assertEquals(expected, actual);
193 
194         expected = new FieldTuple<>(X.negate().multiply(Y.negate()), X.negate().multiply(Y.negate()));
195         actual = MINUS_X.multiply(MINUS_Y);
196         assertEquals(expected, actual);
197         actual = MINUS_Y.multiply(MINUS_X);
198         assertEquals(expected, actual);
199 
200     }
201 
202     @Test
203     void testDivide() {
204         FieldTuple<Binary64> expected, actual;
205 
206         expected = new FieldTuple<>(X.divide(Y), X.divide(Y));
207         actual = PLUS_X.divide(PLUS_Y);
208         assertEquals(expected, actual);
209 
210         expected = new FieldTuple<>(X.divide(Y.negate()), X.divide(Y.negate()));
211         actual = PLUS_X.divide(MINUS_Y);
212         assertEquals(expected, actual);
213 
214         expected = new FieldTuple<>(X.negate().divide(Y), X.negate().divide(Y));
215         actual = MINUS_X.divide(PLUS_Y);
216         assertEquals(expected, actual);
217 
218         expected = new FieldTuple<>(X.negate().divide(Y.negate()), X.negate().divide(Y.negate()));
219         actual = MINUS_X.divide(MINUS_Y);
220         assertEquals(expected, actual);
221 
222     }
223 
224     @Test
225     void testReciprocal() {
226         FieldTuple<Binary64> expected, actual;
227 
228         expected = new FieldTuple<>(X.reciprocal(), X.reciprocal());
229         actual = PLUS_X.reciprocal();
230         assertEquals(expected, actual);
231 
232         expected = new FieldTuple<>(X.negate().reciprocal(), X.negate().reciprocal());
233         actual = MINUS_X.reciprocal();
234         assertEquals(expected, actual);
235 
236         expected = PLUS_ZERO;
237         actual = new FieldTuple<>(new Binary64(Double.POSITIVE_INFINITY), new Binary64(Double.POSITIVE_INFINITY)).reciprocal();
238         assertEquals(expected, actual);
239 
240         expected = MINUS_ZERO;
241         actual = new FieldTuple<>(new Binary64(Double.NEGATIVE_INFINITY), new Binary64(Double.NEGATIVE_INFINITY)).reciprocal();
242         assertEquals(expected, actual);
243     }
244 
245     @Test
246     void testToDegreesDefinition() {
247         double epsilon = 3.0e-16;
248         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
249             for (double x = 0.1; x < 1.2; x += 0.001) {
250                 final Binary64 dec64 = new Binary64(x);
251                 FieldTuple<Binary64> value = new FieldTuple<>(dec64, dec64);
252                 assertEquals(FastMath.toDegrees(x), value.toDegrees().getReal(), epsilon);
253             }
254         }
255     }
256 
257     @Test
258     void testToRadiansDefinition() {
259         double epsilon = 3.0e-16;
260         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
261             for (double x = 0.1; x < 1.2; x += 0.001) {
262                 final Binary64 dec64 = new Binary64(x);
263                 FieldTuple<Binary64> value = new FieldTuple<>(dec64, dec64);
264                 assertEquals(FastMath.toRadians(x), value.toRadians().getReal(), epsilon);
265             }
266         }
267     }
268 
269     @Test
270     void testDegRad() {
271         for (double x = 0.1; x < 1.2; x += 0.001) {
272             final Binary64 dec64 = new Binary64(x);
273             FieldTuple<Binary64> value = new FieldTuple<>(dec64, dec64);
274             FieldTuple<Binary64> rebuilt = value.toDegrees().toRadians();
275             FieldTuple<Binary64> zero = rebuilt.subtract(value);
276             assertEquals(0, zero.getReal(), 3.0e-16);
277         }
278     }
279 
280     @Test
281     public void testIsSmallDimensionMismatch() {
282         try {
283             new FieldTuple<>(new Binary64(0.0)).isSmall(new FieldTuple<>(new Binary64(0.0), new Binary64(1.0)), 1.0e-15);
284             Assertions.fail("an exception should have been thrown");
285         } catch (MathIllegalArgumentException e) {
286             Assertions.assertEquals(LocalizedCoreFormats.DIMENSIONS_MISMATCH, e.getSpecifier());
287         }
288     }
289 
290     @Test
291     public void testIsSmall() {
292         Assertions.assertTrue(new FieldTuple<>(new Binary64(1.0e-13), new Binary64(2.5e-13)).
293                               isSmall(new FieldTuple<>(new Binary64(1.0), new Binary64(1.0)), 1.0e-12));
294         Assertions.assertFalse(new FieldTuple<>(new Binary64(1.0e-11), new Binary64(2.5e-13)).
295                                isSmall(new FieldTuple<>(new Binary64(1.0), new Binary64(1.0)), 1.0e-12));
296         Assertions.assertFalse(new FieldTuple<>(new Binary64(1.0e-13), new Binary64(2.5e-11)).
297                                isSmall(new FieldTuple<>(new Binary64(1.0), new Binary64(1.0)), 1.0e-12));
298     }
299 
300 }