FieldDerivative.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 org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.exception.MathIllegalArgumentException;
  20. import org.hipparchus.util.FastMath;

  21. /** Interface representing both the value and the differentials of a function.
  22.  * @param <S> the type of the field elements
  23.  * @param <T> the type of the function derivative
  24.  * @see Derivative
  25.  * @since 1.7
  26.  */
  27. public interface FieldDerivative<S extends CalculusFieldElement<S>, T extends FieldDerivative<S, T>>
  28.         extends CalculusFieldElement<T>, DifferentialAlgebra {

  29.     /** Get the value part of the function.
  30.      * @return value part of the value of the function
  31.      */
  32.     S getValue();

  33.     /** Get a partial derivative.
  34.      * @param orders derivation orders with respect to each variable (if all orders are 0,
  35.      * the value is returned)
  36.      * @return partial derivative
  37.      * @see #getValue()
  38.      * @exception MathIllegalArgumentException if the numbers of variables does not
  39.      * match the instance
  40.      * @exception MathIllegalArgumentException if sum of derivation orders is larger
  41.      * than the instance limits
  42.      */
  43.     S getPartialDerivative(int ... orders)
  44.         throws MathIllegalArgumentException;

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     default double getReal() {
  48.         return getValue().getReal();
  49.     }

  50.     /** Create an instance corresponding to a constant Field value.
  51.      * <p>
  52.      * This default implementation is there so that no API gets broken
  53.      * by the next release, which is not a major one. Custom inheritors
  54.      * should probably overwrite it.
  55.      * </p>
  56.      * @param value constant value
  57.      * @return instance corresponding to a constant Field value
  58.      * @since 3.1
  59.      */
  60.     default T newInstance(S value) {
  61.         return newInstance(value.getReal());
  62.     }

  63.     /** Create a new object with new value (zeroth-order derivative, as passed as input)
  64.      * and same derivatives of order one and above.
  65.      * <p>
  66.      * This default implementation is there so that no API gets broken
  67.      * by the next release, which is not a major one. Custom inheritors
  68.      * should probably overwrite it.
  69.      * </p>
  70.      * @param value zeroth-order derivative of new represented function
  71.      * @return new object with changed value
  72.      * @since 3.1
  73.      */
  74.     default T withValue(S value) {
  75.         return add(newInstance(value.subtract(getValue())));
  76.     }

  77.     /** '+' operator.
  78.      * @param a right hand side parameter of the operator
  79.      * @return this+a
  80.      * @since 3.1
  81.      */
  82.     default T add(S a) {
  83.         return withValue(getValue().add(a));
  84.     }

  85.     /** '-' operator.
  86.      * @param a right hand side parameter of the operator
  87.      * @return this-a
  88.      * @since 3.1
  89.      */
  90.     default T subtract(S a) {
  91.         return withValue(getValue().subtract(a));
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     default T log10() {
  96.         return log().divide(FastMath.log(10.));
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     default T pow(T e) {
  101.         return log().multiply(e).exp();
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     default T cosh() {
  106.         return (exp().add(negate().exp())).divide(2);
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     default T sinh() {
  111.         return (exp().subtract(negate().exp())).divide(2);
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     default T acos() {
  116.         return asin().negate().add(getPi().divide(2));
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     default T ceil() {
  121.         return newInstance(getValue().ceil());
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     default T floor() {
  126.         return newInstance(getValue().floor());
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     default T rint() {
  131.         return newInstance(getValue().rint());
  132.     }

  133.     /** {@inheritDoc} */
  134.     @Override
  135.     default T ulp() {
  136.         return newInstance(getValue().ulp());
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     default T sign() {
  141.         return newInstance(getValue().sign());
  142.     }

  143.     /** {@inheritDoc} */
  144.     @Override
  145.     default int getExponent() {
  146.         return getValue().getExponent();
  147.     }

  148. }