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.differentiation;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.hipparchus.CalculusFieldElement;
23  import org.hipparchus.Field;
24  
25  /** Field for {@link FieldUnivariateDerivative2} instances.
26   * @param <T> the type of the function parameters and value
27   * @since 1.7
28   */
29  public class FieldUnivariateDerivative2Field<T extends CalculusFieldElement<T>> implements Field<FieldUnivariateDerivative2<T>> {
30  
31      /** Cached fields. */
32      private static final Map<Field<?>, FieldUnivariateDerivative2Field<?>> CACHE = new HashMap<>();
33  
34      /** Zero constant. */
35      private final FieldUnivariateDerivative2<T> zero;
36  
37      /** One constant. */
38      private final FieldUnivariateDerivative2<T> one;
39  
40      /** Associated factory for conversions to {@link FieldDerivativeStructure}. */
41      private final FDSFactory<T> factory;
42  
43      /** Private constructor for populating the cache.
44       * @param valueField field for the function parameters and value
45       */
46      private FieldUnivariateDerivative2Field(final Field<T> valueField) {
47          zero    = new FieldUnivariateDerivative2<>(valueField.getZero(), valueField.getZero(), valueField.getZero());
48          one     = new FieldUnivariateDerivative2<>(valueField.getOne(), valueField.getZero(), valueField.getZero());
49          factory = new FDSFactory<>(valueField, 1, 2);
50      }
51  
52      /** Get the univariate derivative field corresponding to a value field.
53       * @param valueField field for the function parameters and value
54       * @param <T> the type of the function parameters and value
55       * @return univariate derivative field
56       */
57      public static <T extends CalculusFieldElement<T>> FieldUnivariateDerivative2Field<T> getUnivariateDerivative2Field(final Field<T> valueField) {
58          synchronized (CACHE) {
59              FieldUnivariateDerivative2Field<?> cached = CACHE.get(valueField);
60              if (cached == null) {
61                  cached = new FieldUnivariateDerivative2Field<>(valueField);
62                  CACHE.put(valueField, cached);
63              }
64              @SuppressWarnings("unchecked")
65              final FieldUnivariateDerivative2Field<T> tCached = (FieldUnivariateDerivative2Field<T>) cached;
66              return tCached;
67          }
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public FieldUnivariateDerivative2<T> getOne() {
73          return one;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public FieldUnivariateDerivative2<T> getZero() {
79          return zero;
80      }
81  
82      /** Get the factory for converting to {@link DerivativeStructure}.
83       * <p>
84       * This factory is used only for conversions. {@code UnivariateDerivative2} by
85       * itself does not rely at all on {@link DSFactory}, {@link DSCompiler}
86       * or {@link DerivativeStructure} for its computation. For this reason,
87       * the factory here is hidden and this method is package private, so
88       * only {@link UnivariateDerivative2#toDerivativeStructure()} can call it on an
89       * existing {@link UnivariateDerivative2} instance
90       * </p>
91       * @return factory for conversions
92       */
93      FDSFactory<T> getConversionFactory() {
94          return factory;
95      }
96  
97      /** {@inheritDoc} */
98      @SuppressWarnings("unchecked")
99      @Override
100     public Class<FieldUnivariateDerivative2<T>> getRuntimeClass() {
101         return (Class<FieldUnivariateDerivative2<T>>) zero.getClass();
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public boolean equals(final Object other) {
107         return this == other;
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public int hashCode() {
113         return 0x3f4b793e;
114     }
115 
116 }