FieldUnivariateDerivative2Field.java

  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. import java.util.HashMap;
  19. import java.util.Map;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.Field;

  22. /** Field for {@link FieldUnivariateDerivative2} instances.
  23.  * @param <T> the type of the function parameters and value
  24.  * @since 1.7
  25.  */
  26. public class FieldUnivariateDerivative2Field<T extends CalculusFieldElement<T>> implements Field<FieldUnivariateDerivative2<T>> {

  27.     /** Cached fields. */
  28.     private static final Map<Field<?>, FieldUnivariateDerivative2Field<?>> CACHE = new HashMap<>();

  29.     /** Zero constant. */
  30.     private final FieldUnivariateDerivative2<T> zero;

  31.     /** One constant. */
  32.     private final FieldUnivariateDerivative2<T> one;

  33.     /** Associated factory for conversions to {@link FieldDerivativeStructure}. */
  34.     private final FDSFactory<T> factory;

  35.     /** Private constructor for populating the cache.
  36.      * @param valueField field for the function parameters and value
  37.      */
  38.     private FieldUnivariateDerivative2Field(final Field<T> valueField) {
  39.         zero    = new FieldUnivariateDerivative2<>(valueField.getZero(), valueField.getZero(), valueField.getZero());
  40.         one     = new FieldUnivariateDerivative2<>(valueField.getOne(), valueField.getZero(), valueField.getZero());
  41.         factory = new FDSFactory<>(valueField, 1, 2);
  42.     }

  43.     /** Get the univariate derivative field corresponding to a value field.
  44.      * @param valueField field for the function parameters and value
  45.      * @param <T> the type of the function parameters and value
  46.      * @return univariate derivative field
  47.      */
  48.     public static <T extends CalculusFieldElement<T>> FieldUnivariateDerivative2Field<T> getUnivariateDerivative2Field(final Field<T> valueField) {
  49.         synchronized (CACHE) {
  50.             FieldUnivariateDerivative2Field<?> cached = CACHE.get(valueField);
  51.             if (cached == null) {
  52.                 cached = new FieldUnivariateDerivative2Field<>(valueField);
  53.                 CACHE.put(valueField, cached);
  54.             }
  55.             @SuppressWarnings("unchecked")
  56.             final FieldUnivariateDerivative2Field<T> tCached = (FieldUnivariateDerivative2Field<T>) cached;
  57.             return tCached;
  58.         }
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public FieldUnivariateDerivative2<T> getOne() {
  63.         return one;
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public FieldUnivariateDerivative2<T> getZero() {
  68.         return zero;
  69.     }

  70.     /** Get the factory for converting to {@link DerivativeStructure}.
  71.      * <p>
  72.      * This factory is used only for conversions. {@code UnivariateDerivative2} by
  73.      * itself does not rely at all on {@link DSFactory}, {@link DSCompiler}
  74.      * or {@link DerivativeStructure} for its computation. For this reason,
  75.      * the factory here is hidden and this method is package private, so
  76.      * only {@link UnivariateDerivative2#toDerivativeStructure()} can call it on an
  77.      * existing {@link UnivariateDerivative2} instance
  78.      * </p>
  79.      * @return factory for conversions
  80.      */
  81.     FDSFactory<T> getConversionFactory() {
  82.         return factory;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @SuppressWarnings("unchecked")
  86.     @Override
  87.     public Class<FieldUnivariateDerivative2<T>> getRuntimeClass() {
  88.         return (Class<FieldUnivariateDerivative2<T>>) zero.getClass();
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public boolean equals(final Object other) {
  93.         return this == other;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public int hashCode() {
  98.         return 0x3f4b793e;
  99.     }

  100. }