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