Derivative1.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.util.FastMath;
  20. import org.hipparchus.util.SinCos;
  21. import org.hipparchus.util.SinhCosh;
  22. import org.hipparchus.util.FieldSinCos;
  23. import org.hipparchus.util.FieldSinhCosh;

  24. /** Interface representing an object holding partial derivatives up to first order.
  25.  * @param <T> the type of the field elements
  26.  * @see Derivative
  27.  * @see UnivariateDerivative1
  28.  * @see Gradient
  29.  * @see SparseGradient
  30.  * @since 3.1
  31.  */
  32. public interface Derivative1<T extends CalculusFieldElement<T>> extends Derivative<T> {

  33.     /** {@inheritDoc} */
  34.     @Override
  35.     default int getOrder() {
  36.         return 1;
  37.     }

  38.     /** Compute composition of the instance by a univariate function differentiable at order 1.
  39.      * @param f0 value of function
  40.      * @param f1 first-order derivative
  41.      * @return f(this)
  42.      */
  43.     T compose(double f0, double f1);

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     default T square() {
  47.         final double f0 = getValue();
  48.         return compose(f0 * f0, 2 * f0);
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     default T reciprocal () {
  53.         final double inv1 = 1.0 / getValue();
  54.         final double inv2 = -inv1 * inv1;
  55.         return compose(inv1, inv2);
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     default T sqrt() {
  60.         final double s = FastMath.sqrt(getValue());
  61.         return compose(s, 1 / (2 * s));
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     default T cbrt() {
  66.         final double c = FastMath.cbrt(getValue());
  67.         return compose(c, 1 / (3 * c * c));
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     default T rootN(int n) {
  72.         if (n == 2) {
  73.             return sqrt();
  74.         } else if (n == 3) {
  75.             return cbrt();
  76.         } else {
  77.             final double r = FastMath.pow(getValue(), 1.0 / n);
  78.             return compose(r, 1 / (n * FastMath.pow(r, n - 1)));
  79.         }
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     default T exp() {
  84.         final double exp = FastMath.exp(getValue());
  85.         return compose(exp, exp);
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     default T expm1() {
  90.         final double exp   = FastMath.exp(getValue());
  91.         final double expM1 = FastMath.expm1(getValue());
  92.         return compose(expM1, exp);
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     default T log() {
  97.         return compose(FastMath.log(getValue()), 1 / getValue());
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     default T log1p() {
  102.         return compose(FastMath.log1p(getValue()), 1 / (1 + getValue()));
  103.     }

  104.     /** {@inheritDoc} */
  105.     @Override
  106.     default T log10() {
  107.         return compose(FastMath.log10(getValue()), 1 / (getValue() * FastMath.log(10.0)));
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     default T cos() {
  112.         final SinCos sinCos = FastMath.sinCos(getValue());
  113.         return compose(sinCos.cos(), -sinCos.sin());
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     default T sin() {
  118.         final SinCos sinCos = FastMath.sinCos(getValue());
  119.         return compose(sinCos.sin(), sinCos.cos());
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     default FieldSinCos<T> sinCos() {
  124.         final SinCos sinCos = FastMath.sinCos(getValue());
  125.         return new FieldSinCos<>(compose(sinCos.sin(), sinCos.cos()),
  126.                 compose(sinCos.cos(), -sinCos.sin()));
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     default T tan() {
  131.         final double tan = FastMath.tan(getValue());
  132.         return compose(tan, 1 + tan * tan);
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     default T acos() {
  137.         final double f0 = getValue();
  138.         return compose(FastMath.acos(f0), -1 / FastMath.sqrt(1 - f0 * f0));
  139.     }

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     default T asin() {
  143.         final double f0 = getValue();
  144.         return compose(FastMath.asin(f0), 1 / FastMath.sqrt(1 - f0 * f0));
  145.     }

  146.     /** {@inheritDoc} */
  147.     @Override
  148.     default T atan() {
  149.         final double f0 = getValue();
  150.         return compose(FastMath.atan(f0), 1 / (1 + f0 * f0));
  151.     }

  152.     /** {@inheritDoc} */
  153.     @Override
  154.     default T cosh() {
  155.         return compose(FastMath.cosh(getValue()), FastMath.sinh(getValue()));
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     default T sinh() {
  160.         return compose(FastMath.sinh(getValue()), FastMath.cosh(getValue()));
  161.     }

  162.     /** {@inheritDoc} */
  163.     @Override
  164.     default FieldSinhCosh<T> sinhCosh() {
  165.         final SinhCosh sinhCosh = FastMath.sinhCosh(getValue());
  166.         return new FieldSinhCosh<>(compose(sinhCosh.sinh(), sinhCosh.cosh()),
  167.                 compose(sinhCosh.cosh(), sinhCosh.sinh()));
  168.     }

  169.     /** {@inheritDoc} */
  170.     @Override
  171.     default T tanh() {
  172.         final double tanh = FastMath.tanh(getValue());
  173.         return compose(tanh, 1 - tanh * tanh);
  174.     }

  175.     /** {@inheritDoc} */
  176.     @Override
  177.     default T acosh() {
  178.         final double f0 = getValue();
  179.         return compose(FastMath.acosh(f0), 1 / FastMath.sqrt(f0 * f0 - 1));
  180.     }

  181.     /** {@inheritDoc} */
  182.     @Override
  183.     default T asinh() {
  184.         final double f0 = getValue();
  185.         return compose(FastMath.asinh(f0), 1 / FastMath.sqrt(f0 * f0 + 1));
  186.     }

  187.     /** {@inheritDoc} */
  188.     @Override
  189.     default T atanh() {
  190.         final double f0 = getValue();
  191.         return compose(FastMath.atanh(f0), 1 / (1 - f0 * f0));
  192.     }

  193. }