View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) 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 ASF 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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.geometry.euclidean.oned;
24  
25  import java.text.DecimalFormat;
26  import java.text.DecimalFormatSymbols;
27  import java.text.NumberFormat;
28  import java.util.Locale;
29  
30  import org.hipparchus.exception.MathIllegalArgumentException;
31  import org.hipparchus.exception.MathRuntimeException;
32  import org.hipparchus.geometry.Space;
33  import org.hipparchus.geometry.euclidean.twod.FieldVector2D;
34  import org.hipparchus.util.Binary64Field;
35  import org.hipparchus.util.FastMath;
36  import org.hipparchus.util.Precision;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  public class Vector1DTest {
41      @Test
42      public void testConstructors() throws MathIllegalArgumentException {
43          checkVector(new Vector1D(3, new Vector1D(FastMath.PI / 3)),
44                      FastMath.PI);
45          checkVector(new Vector1D(2, Vector1D.ONE, -3, new Vector1D(2)),
46                      -4);
47          checkVector(new Vector1D(2, Vector1D.ONE,
48                                   5, new Vector1D(2),
49                                   -3, new Vector1D(3)),
50                      3);
51          checkVector(new Vector1D(2, Vector1D.ONE,
52                                   5, new Vector1D(2),
53                                   5, new Vector1D(-2),
54                                   -3, new Vector1D(-3)),
55                      11);
56      }
57  
58      @Test
59      public void testSpace() {
60          Space space = new Vector1D(1).getSpace();
61          Assert.assertEquals(1, space.getDimension());
62      }
63  
64      @Test
65      public void testZero() {
66          Assert.assertEquals(0, new Vector1D(1).getZero().getNorm(), 1.0e-15);
67      }
68  
69      @SuppressWarnings("unlikely-arg-type")
70      @Test
71      public void testEquals() {
72          Vector1D u1 = new Vector1D(1);
73          Vector1D u2 = new Vector1D(1);
74          Assert.assertTrue(u1.equals(u1));
75          Assert.assertTrue(u1.equals(u2));
76          Assert.assertFalse(u1.equals(FieldVector2D.getPlusI(Binary64Field.getInstance())));
77          Assert.assertFalse(u1.equals(new Vector1D(1 + 10 * Precision.EPSILON)));
78          Assert.assertTrue(new Vector1D(Double.NaN).equals(new Vector1D(Double.NaN)));
79          Assert.assertTrue(Vector1D.NaN.equals(Vector1D.NaN));
80      }
81  
82      @Test
83      public void testEqualsIeee754() {
84          Vector1D u1 = new Vector1D(1);
85          Vector1D u2 = new Vector1D(1);
86          Assert.assertTrue(u1.equalsIeee754(u1));
87          Assert.assertTrue(u1.equalsIeee754(u2));
88          Assert.assertFalse(u1.equalsIeee754(FieldVector2D.getPlusI(Binary64Field.getInstance())));
89          Assert.assertFalse(u1.equalsIeee754(new Vector1D(1 + 10 * Precision.EPSILON)));
90          Assert.assertFalse(new Vector1D(Double.NaN).equalsIeee754(new Vector1D(Double.NaN)));
91          Assert.assertFalse(Vector1D.NaN.equalsIeee754(Vector1D.NaN));
92          Assert.assertFalse(Vector1D.NaN.equalsIeee754(Vector1D.NaN));
93      }
94  
95      @Test
96      public void testHash() {
97          Assert.assertEquals(new Vector1D(Double.NaN).hashCode(), new Vector1D(Double.NaN).hashCode());
98          Vector1D u = new Vector1D(1);
99          Vector1D v = new Vector1D(1 + 10 * Precision.EPSILON);
100         Assert.assertTrue(u.hashCode() != v.hashCode());
101     }
102 
103     @Test
104     public void testInfinite() {
105         Assert.assertTrue(new Vector1D(Double.NEGATIVE_INFINITY).isInfinite());
106         Assert.assertTrue(new Vector1D(Double.POSITIVE_INFINITY).isInfinite());
107         Assert.assertFalse(new Vector1D(1).isInfinite());
108         Assert.assertFalse(new Vector1D(Double.NaN).isInfinite());
109     }
110 
111     @Test
112     public void testNaN() {
113         Assert.assertTrue(new Vector1D(Double.NaN).isNaN());
114         Assert.assertFalse(new Vector1D(1).isNaN());
115         Assert.assertFalse(new Vector1D(Double.NEGATIVE_INFINITY).isNaN());
116     }
117 
118     @Test
119     public void testToString() {
120         Assert.assertEquals("{3}", new Vector1D(3).toString());
121         NumberFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US));
122         Assert.assertEquals("{3.000}", new Vector1D(3).toString(format));
123     }
124 
125     @Test
126     public void testCoordinates() {
127         Vector1D v = new Vector1D(1);
128         Assert.assertTrue(FastMath.abs(v.getX() - 1) < 1.0e-12);
129     }
130 
131     @Test
132     public void testNorm1() {
133         Assert.assertEquals(0.0, Vector1D.ZERO.getNorm1(), 0);
134         Assert.assertEquals(6.0, new Vector1D(6).getNorm1(), 0);
135     }
136 
137     @Test
138     public void testNorm() {
139         Assert.assertEquals(0.0, Vector1D.ZERO.getNorm(), 0);
140         Assert.assertEquals(3.0, new Vector1D(-3).getNorm(), 1.0e-12);
141     }
142 
143     @Test
144     public void testNormSq() {
145         Assert.assertEquals(0.0, new Vector1D(0).getNormSq(), 0);
146         Assert.assertEquals(9.0, new Vector1D(-3).getNormSq(), 1.0e-12);
147     }
148 
149     @Test
150     public void testNormInf() {
151         Assert.assertEquals(0.0, Vector1D.ZERO.getNormInf(), 0);
152         Assert.assertEquals(3.0, new Vector1D(-3).getNormInf(), 0);
153     }
154 
155     @Test
156     public void testDistance1() {
157         Vector1D v1 = new Vector1D(1);
158         Vector1D v2 = new Vector1D(-4);
159         Assert.assertEquals(0.0, new Vector1D(-1).distance1(new Vector1D(-1)), 0);
160         Assert.assertEquals(5.0, v1.distance1(v2), 1.0e-12);
161         Assert.assertEquals(v1.subtract(v2).getNorm1(), v1.distance1(v2), 1.0e-12);
162     }
163 
164     @Test
165     public void testDistance() {
166         Vector1D v1 = new Vector1D(1);
167         Vector1D v2 = new Vector1D(-4);
168         Assert.assertEquals(0.0, Vector1D.distance(new Vector1D(-1), new Vector1D(-1)), 0);
169         Assert.assertEquals(5.0, Vector1D.distance(v1, v2), 1.0e-12);
170         Assert.assertEquals(v1.subtract(v2).getNorm(), Vector1D.distance(v1, v2), 1.0e-12);
171     }
172 
173     @Test
174     public void testDistanceSq() {
175         Vector1D v1 = new Vector1D(1);
176         Vector1D v2 = new Vector1D(-4);
177         Assert.assertEquals(0.0, Vector1D.distanceSq(new Vector1D(-1), new Vector1D(-1)), 0);
178         Assert.assertEquals(25.0, Vector1D.distanceSq(v1, v2), 1.0e-12);
179         Assert.assertEquals(Vector1D.distance(v1, v2) * Vector1D.distance(v1, v2),
180                             Vector1D.distanceSq(v1, v2), 1.0e-12);
181   }
182 
183     @Test
184     public void testDistanceInf() {
185         Vector1D v1 = new Vector1D(1);
186         Vector1D v2 = new Vector1D(-4);
187         Assert.assertEquals(0.0, Vector1D.distanceInf(new Vector1D(-1), new Vector1D(-1)), 0);
188         Assert.assertEquals(5.0, Vector1D.distanceInf(v1, v2), 1.0e-12);
189         Assert.assertEquals(v1.subtract(v2).getNormInf(), Vector1D.distanceInf(v1, v2), 1.0e-12);
190     }
191 
192     @Test
193     public void testSubtract() {
194         Vector1D v1 = new Vector1D(1);
195         Vector1D v2 = new Vector1D(-3);
196         v1 = v1.subtract(v2);
197         checkVector(v1, 4);
198 
199         checkVector(v2.subtract(v1), -7);
200         checkVector(v2.subtract(3, v1), -15);
201     }
202 
203     @Test
204     public void testAdd() {
205         Vector1D v1 = new Vector1D(1);
206         Vector1D v2 = new Vector1D(-3);
207         v1 = v1.add(v2);
208         checkVector(v1, -2);
209 
210         checkVector(v2.add(v1), -5);
211         checkVector(v2.add(3, v1), -9);
212     }
213 
214     @Test
215     public void testScalarProduct() {
216         Vector1D v = new Vector1D(1);
217         v = v.scalarMultiply(3);
218         checkVector(v, 3);
219 
220         checkVector(v.scalarMultiply(0.5), 1.5);
221     }
222 
223     @Test
224     public void testNormalize() throws MathRuntimeException {
225         Assert.assertEquals(1.0, new Vector1D(5).normalize().getNorm(), 1.0e-12);
226         try {
227             Vector1D.ZERO.normalize();
228             Assert.fail("an exception should have been thrown");
229         } catch (MathRuntimeException ae) {
230             // expected behavior
231         }
232     }
233 
234     @Test
235     public void testNegate() {
236         checkVector(new Vector1D(0.1).negate(), -0.1);
237     }
238 
239     @Test
240     public void testArithmeticBlending() {
241 
242         // Given
243         final Vector1D v1 = new Vector1D(1);
244         final Vector1D v2 = new Vector1D(2);
245 
246         final double blendingValue = 0.7;
247 
248         // When
249         final Vector1D blendedVector = v1.blendArithmeticallyWith(v2, blendingValue);
250 
251         // Then
252         checkVector(blendedVector, 1.7);
253     }
254 
255     private void checkVector(Vector1D v, double x) {
256         Assert.assertEquals(x, v.getX(), 1.0e-12);
257     }
258 }