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  
18  package org.hipparchus.analysis.differentiation;
19  
20  import org.hipparchus.Field;
21  import org.hipparchus.UnitTestUtils;
22  import org.hipparchus.complex.Complex;
23  import org.hipparchus.complex.ComplexField;
24  import org.hipparchus.util.Binary64;
25  import org.hipparchus.util.FastMath;
26  import org.hipparchus.util.MathUtils;
27  import org.hipparchus.util.Precision;
28  import org.junit.jupiter.api.Test;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertFalse;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   * Test for class {@link FieldDerivativeStructure} on {@link Complex}.
39   */
40  class FieldDerivativeStructureComplexTest extends FieldDerivativeStructureAbstractTest<Complex> {
41  
42      @Override
43      protected Field<Complex> getField() {
44          return ComplexField.getInstance();
45      }
46  
47      @Override
48      @Test
49      public void testComposeField() {
50          doTestComposeField(new double[] { 1.0e-100, 5.0e-14, 2.0e-13, 3.0e-13, 2.0e-13, 1.0e-100 });
51      }
52  
53      @Override
54      @Test
55      public void testComposePrimitive() {
56          doTestComposePrimitive(new double[] { 1.0e-100, 5.0e-14, 2.0e-13, 3.0e-13, 2.0e-13, 1.0e-100 });
57      }
58  
59      @Override
60      @Test
61      public void testHypotNoOverflow() {
62          doTestHypotNoOverflow(180);
63      }
64  
65      @Override
66      @Test
67      public void testLinearCombinationReference() {
68          doTestLinearCombinationReference(this::build, 4.8e-16, 1.0);
69      }
70  
71      @Override
72      @Test
73      public void testAtan2() {
74          double[] epsilon = new double[] { 9.0e-16, 3.0e-15, 2.9e-14, 1.0e-12, 8.0e-11 };
75          for (int maxOrder = 0; maxOrder < 5; ++maxOrder) {
76              final FDSFactory<Complex> factory = buildFactory(2, maxOrder);
77              for (double x = -1.7; x < 2; x += 0.2) {
78                  FieldDerivativeStructure<Complex> dsX = factory.variable(0, x);
79                  for (double y = -1.7; y < 2; y += 0.2) {
80                      FieldDerivativeStructure<Complex> dsY = factory.variable(1, y);
81                      FieldDerivativeStructure<Complex> atan2 = FieldDerivativeStructure.atan2(dsY, dsX);
82                      FieldDerivativeStructure<Complex> ref = dsY.divide(dsX).atan();
83                      if (x < 0) {
84                          ref = (y < 0) ? ref.subtract(FastMath.PI) : ref.add(FastMath.PI);
85                      }
86                      double fullTurns = MathUtils.normalizeAngle(atan2.getValue().getReal(), ref.getValue().getReal()) - atan2.getValue().getReal();
87                      atan2 = atan2.add(fullTurns);
88                      FieldDerivativeStructure<Complex> zero = atan2.subtract(ref);
89                      for (int n = 0; n <= maxOrder; ++n) {
90                          for (int m = 0; m <= maxOrder; ++m) {
91                              if (n + m <= maxOrder) {
92                                  assertEquals(0, zero.getPartialDerivative(n, m).getReal(), epsilon[n + m]);
93                              }
94                          }
95                      }
96                  }
97              }
98          }
99      }
100 
101     @Override
102     @Test
103     public void testAtan2SpecialCases() {
104         assertTrue(build(+0.0).atan2(build(+0.0)).isNaN());
105         assertTrue(build(-0.0).atan2(build(+0.0)).isNaN());
106         assertTrue(build(+0.0).atan2(build(-0.0)).isNaN());
107         assertTrue(build(-0.0).atan2(build(-0.0)).isNaN());
108     }
109 
110     @Test
111     public void testAtan2SpecialCasesDerivatives() {
112 
113         final FDSFactory<Complex> factory = buildFactory(2, 2);
114         FieldDerivativeStructure<Complex> pp =
115                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(+0.0)), factory.variable(1, buildScalar(+0.0)));
116         assertTrue(pp.getValue().isNaN());
117 
118         FieldDerivativeStructure<Complex> pn =
119                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(+0.0)), factory.variable(1, buildScalar(-0.0)));
120         assertTrue(pn.getValue().isNaN());
121 
122         FieldDerivativeStructure<Complex> np =
123                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(-0.0)), factory.variable(1, buildScalar(+0.0)));
124         assertTrue(np.getValue().isNaN());
125 
126         FieldDerivativeStructure<Complex> nn =
127                 FieldDerivativeStructure.atan2(factory.variable(1, buildScalar(-0.0)), factory.variable(1, buildScalar(-0.0)));
128         assertTrue(nn.getValue().isNaN());
129 
130     }
131 
132     @Override
133     @Test
134     public void testPowDoubleDS() {
135         for (int maxOrder = 1; maxOrder < 5; ++maxOrder) {
136 
137             final FDSFactory<Complex> factory = buildFactory(3, maxOrder);
138             FieldDerivativeStructure<Complex> x = factory.variable(0, 0.1);
139             FieldDerivativeStructure<Complex> y = factory.variable(1, 0.2);
140             FieldDerivativeStructure<Complex> z = factory.variable(2, 0.3);
141             List<FieldDerivativeStructure<Complex>> list = Arrays.asList(x, y, z,
142                                                                            x.add(y).add(z),
143                                                                            x.multiply(y).multiply(z));
144 
145             for (FieldDerivativeStructure<Complex> ds : list) {
146                 // the special case a = 0 is included here
147                 for (double a : new double[] { 0.0, 0.1, 1.0, 2.0, 5.0 }) {
148                     FieldDerivativeStructure<Complex> reference = (a == 0) ?
149                                                     x.getField().getZero() :
150                                                     factory.constant(a).pow(ds);
151                     FieldDerivativeStructure<Complex> result = FieldDerivativeStructure.pow(a, ds);
152                     checkEquals(reference, result, 2.0e-14 * FastMath.abs(reference.getReal()));
153                 }
154 
155             }
156 
157             // negative base: -1^x can be evaluated for integers only, so value is sometimes OK, derivatives are always NaN
158             FieldDerivativeStructure<Complex> negEvenInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 2.0));
159             assertEquals(4.0, negEvenInteger.getReal(), 1.0e-15);
160             assertTrue(Double.isNaN(negEvenInteger.getPartialDerivative(1, 0, 0).getReal()));
161             FieldDerivativeStructure<Complex> negOddInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 3.0));
162             assertEquals(-8.0, negOddInteger.getReal(), 1.0e-15);
163             assertTrue(Double.isNaN(negOddInteger.getPartialDerivative(1, 0, 0).getReal()));
164             FieldDerivativeStructure<Complex> negNonInteger = FieldDerivativeStructure.pow(-2.0, factory.variable(0, 2.001));
165             assertEquals(4.0027537969708465469, negNonInteger.getValue().getReal(), 2.0e-15);
166             assertEquals(0.012575063293019489803, negNonInteger.getValue().getImaginary(), 2.0e-15);
167             assertTrue(Double.isNaN(negNonInteger.getPartialDerivative(1, 0, 0).getReal()));
168 
169             FieldDerivativeStructure<Complex> zeroNeg = FieldDerivativeStructure.pow(0.0, factory.variable(0, -1.0));
170             assertTrue(Double.isNaN(zeroNeg.getReal()));
171             assertTrue(Double.isNaN(zeroNeg.getPartialDerivative(1, 0, 0).getReal()));
172             FieldDerivativeStructure<Complex> posNeg = FieldDerivativeStructure.pow(2.0, factory.variable(0, -2.0));
173             assertEquals(1.0 / 4.0, posNeg.getReal(), 1.0e-15);
174             assertEquals(FastMath.log(2.0) / 4.0, posNeg.getPartialDerivative(1, 0, 0).getReal(), 1.0e-15);
175 
176             // very special case: 0^0 where the power is a primitive
177             FieldDerivativeStructure<Complex> zeroDsZeroDouble = factory.variable(0, 0.0).pow(0.0);
178             boolean first = true;
179             for (final Complex d : zeroDsZeroDouble.getAllDerivatives()) {
180                 if (first) {
181                     assertEquals(1.0, d.getReal(), Precision.EPSILON);
182                     first = false;
183                 } else {
184                     assertEquals(0.0, d.getReal(), Precision.SAFE_MIN);
185                 }
186             }
187             FieldDerivativeStructure<Complex> zeroDsZeroInt = factory.variable(0, 0.0).pow(0);
188             first = true;
189             for (final Complex d : zeroDsZeroInt.getAllDerivatives()) {
190                 if (first) {
191                     assertEquals(1.0, d.getReal(), Precision.EPSILON);
192                     first = false;
193                 } else {
194                     assertEquals(0.0, d.getReal(), Precision.SAFE_MIN);
195                 }
196             }
197 
198             // 0^p with p smaller than 1.0
199             FieldDerivativeStructure<Complex> u = factory.variable(1, -0.0).pow(0.25);
200             for (int i0 = 0; i0 <= maxOrder; ++i0) {
201                 for (int i1 = 0; i1 <= maxOrder; ++i1) {
202                     for (int i2 = 0; i2 <= maxOrder; ++i2) {
203                         if (i0 + i1 + i2 <= maxOrder) {
204                             assertEquals(0.0, u.getPartialDerivative(i0, i1, i2).getReal(), 1.0e-10);
205                         }
206                     }
207                 }
208             }
209         }
210 
211     }
212 
213     @Override
214     @Test
215     public void testLog10() {
216         for (double x = -0.9; x < 0.9; x += 0.05) {
217             if (x <= 0) {
218                 assertTrue(Double.isNaN(FastMath.log10(x)));
219                 assertFalse(build(x).log10().getValue().isNaN());
220             } else {
221                 checkRelative(FastMath.log10(x), build(x).log10());
222             }
223         }
224     }
225 
226     @Override
227     @Test
228     public void testRootN() {
229         for (double x = -0.9; x < 0.9; x += 0.05) {
230             for (int n = 1; n < 5; ++n) {
231                 if (x < 0) {
232                     // special case for Complex
233                     final double doubleRoot = new Binary64(x).rootN(n).getReal();
234                     if (n % 2 == 0) {
235                         assertTrue(Double.isNaN(doubleRoot));
236                     } else {
237                         assertTrue(doubleRoot < 0);
238                     }
239                     assertEquals(FastMath.PI / n, build(x).rootN(n).getValue().getArgument(), 1.0e-15);
240                 } else {
241                     checkRelative(FastMath.pow(x, 1.0 / n), build(x).rootN(n));
242                 }
243             }
244         }
245     }
246 
247     @Override
248     @Test
249     public void testRootNSingularity() {
250         doTestRootNSingularity(false);
251     }
252 
253     @Override
254     @Test
255     public void testCbrt() {
256         for (double x = -0.9; x < 0.9; x += 0.05) {
257             if ( x < 0) {
258                 // special case for Complex
259                 assertTrue(FastMath.cbrt(x) < 0);
260                 assertEquals(FastMath.PI / 3, build(x).cbrt().getValue().getArgument(), 1.0e-15);
261             } else {
262                 checkRelative(FastMath.cbrt(x), build(x).cbrt());
263             }
264         }
265     }
266 
267     @Test
268     void testCbrtComplex() {
269         Complex z = new Complex(15, 2);
270         UnitTestUtils.customAssertEquals(z, z.square().multiply(z).cbrt(), 1.0e-14);
271         Complex branchCutPlus = new Complex(-8.0, +0.0);
272         Complex cbrtPlus = branchCutPlus.cbrt();
273         UnitTestUtils.customAssertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14);
274         assertEquals(1.0, cbrtPlus.getReal(), 1.0e-15);
275         assertEquals(FastMath.sqrt(3.0), cbrtPlus.getImaginary(), 1.0e-15);
276         Complex branchCutMinus = new Complex(-8.0, -0.0);
277         Complex cbrtMinus = branchCutMinus.cbrt();
278         UnitTestUtils.customAssertEquals(branchCutMinus, cbrtMinus.multiply(cbrtMinus).multiply(cbrtMinus), 1.0e-14);
279         assertEquals(1.0, cbrtMinus.getReal(), 1.0e-15);
280         assertEquals(-FastMath.sqrt(3.0), cbrtMinus.getImaginary(), 1.0e-15);
281     }
282 
283     @Override
284     @Test
285     public void testPowField() {
286         for (double x = -0.9; x < 0.9; x += 0.05) {
287             for (double y = 0.1; y < 4; y += 0.2) {
288                 if ( x < 0) {
289                     // special case for Complex
290                     assertTrue(Double.isNaN(FastMath.pow(x, y)));
291                     assertFalse(build(x).pow(build(y)).isNaN());
292                 } else {
293                     checkRelative(FastMath.pow(x, y), build(x).pow(build(y)));
294                 }
295             }
296         }
297     }
298 
299     @Override
300     @Test
301     public void testPowDouble() {
302         for (double x = -0.9; x < 0.9; x += 0.05) {
303             for (double y = 0.1; y < 4; y += 0.2) {
304                 if ( x < 0) {
305                     // special case for Complex
306                     assertTrue(Double.isNaN(FastMath.pow(x, y)));
307                     assertFalse(build(x).pow(y).isNaN());
308                 } else {
309                     checkRelative(FastMath.pow(x, y), build(x).pow(y));
310                 }
311             }
312         }
313     }
314 
315 }