Derivative.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 <T> the type of the field elements
  23.  * @since 1.7
  24.  */
  25. public interface Derivative<T extends CalculusFieldElement<T>> extends CalculusFieldElement<T>, DifferentialAlgebra {

  26.     /** {@inheritDoc} */
  27.     @Override
  28.     default double getReal() {
  29.         return getValue();
  30.     }

  31.     /** Get the value part of the function.
  32.      * @return value part of the value of the function
  33.      */
  34.     double getValue();

  35.     /** Create a new object with new value (zeroth-order derivative, as passed as input)
  36.      * and same derivatives of order one and above.
  37.      * <p>
  38.      * This default implementation is there so that no API gets broken
  39.      * by the next release, which is not a major one. Custom inheritors
  40.      * should probably overwrite it.
  41.      * </p>
  42.      * @param value zeroth-order derivative of new represented function
  43.      * @return new object with changed value
  44.      * @since 3.1
  45.      */
  46.     default T withValue(double value) {
  47.         return add(newInstance(value - getValue()));
  48.     }

  49.     /** Get a partial derivative.
  50.      * @param orders derivation orders with respect to each variable (if all orders are 0,
  51.      * the value is returned)
  52.      * @return partial derivative
  53.      * @see #getValue()
  54.      * @exception MathIllegalArgumentException if the numbers of variables does not
  55.      * match the instance
  56.      * @exception MathIllegalArgumentException if sum of derivation orders is larger
  57.      * than the instance limits
  58.      */
  59.     double getPartialDerivative(int ... orders)
  60.         throws MathIllegalArgumentException;

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     default T add(double a) {
  64.         return withValue(getValue() + a);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     default T subtract(double a) {
  69.         return withValue(getValue() - a);
  70.     }

  71.     /** Compute composition of the instance by a univariate function.
  72.      * @param f array of value and derivatives of the function at
  73.      * the current point (i.e. [f({@link #getValue()}),
  74.      * f'({@link #getValue()}), f''({@link #getValue()})...]).
  75.      * @return f(this)
  76.      * @exception MathIllegalArgumentException if the number of derivatives
  77.      * in the array is not equal to {@link #getOrder() order} + 1
  78.      */
  79.     T compose(double... f)
  80.         throws MathIllegalArgumentException;

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     default T log10() {
  84.         return log().divide(FastMath.log(10.));
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     default T pow(T e) {
  89.         return log().multiply(e).exp();
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     default T cosh() {
  94.         return (exp().add(negate().exp())).divide(2);
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     default T sinh() {
  99.         return (exp().subtract(negate().exp())).divide(2);
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     default T acos() {
  104.         return asin().negate().add(getPi().divide(2));
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     default int getExponent() {
  109.         return FastMath.getExponent(getValue());
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     default T remainder(double a) {
  114.         return withValue(FastMath.IEEEremainder(getValue(), a));
  115.     }
  116. }