FieldDerivative1.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.FieldSinCos;
  21. import org.hipparchus.util.FieldSinhCosh;

  22. /** Interface representing a Field object holding partial derivatives up to first order.
  23.  * @param <S> the type of the field elements
  24.  * @param <T> the type of the function derivative
  25.  * @see FieldDerivative
  26.  * @see FieldUnivariateDerivative1
  27.  * @see FieldGradient
  28.  * @see Derivative1
  29.  * @since 3.1
  30.  */
  31. public interface FieldDerivative1<S extends CalculusFieldElement<S>, T extends FieldDerivative<S, T>>
  32.         extends FieldDerivative<S, 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(S f0, S f1);

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

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

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     default T exp() {
  60.         final S exp = getValue().exp();
  61.         return compose(exp, exp);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     default T sqrt() {
  66.         final S s = getValue().sqrt();
  67.         return compose(s, s.add(s).reciprocal());
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     default T cbrt() {
  72.         final S c = getValue().cbrt();
  73.         return compose(c, c.square().multiply(3).reciprocal());
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     default T expm1() {
  78.         final S exp   = FastMath.exp(getValue());
  79.         final S expM1 = FastMath.expm1(getValue());
  80.         return compose(expM1, exp);
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     default T log() {
  85.         return compose(getValue().log(), getValue().reciprocal());
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     default T log1p() {
  90.         return compose(getValue().log1p(), getValue().add(1).reciprocal());
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     default T log10() {
  95.         return compose(getValue().log10(), getValue().multiply(FastMath.log(10.0)).reciprocal());
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     default T cos() {
  100.         final FieldSinCos<S> sinCos = getValue().sinCos();
  101.         return compose(sinCos.cos(), sinCos.sin().negate());
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     default T sin() {
  106.         final FieldSinCos<S> sinCos = getValue().sinCos();
  107.         return compose(sinCos.sin(), sinCos.cos());
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     default FieldSinCos<T> sinCos() {
  112.         final FieldSinCos<S> sinCos = getValue().sinCos();
  113.         return new FieldSinCos<>(compose(sinCos.sin(), sinCos.cos()),
  114.                 compose(sinCos.cos(), sinCos.sin().negate()));
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     default T tan() {
  119.         final S tan = getValue().tan();
  120.         return compose(tan, tan.multiply(tan).add(1));
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     default T acos() {
  125.         return compose(getValue().acos(), getValue().square().negate().add(1).sqrt().reciprocal().negate());
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     default T asin() {
  130.         return compose(getValue().asin(), getValue().square().negate().add(1).sqrt().reciprocal());
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     default T atan() {
  135.         return compose(getValue().atan(), getValue().square().add(1).reciprocal());
  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     default T cosh() {
  140.         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
  141.         return compose(sinhCosh.cosh(), sinhCosh.sinh());
  142.     }

  143.     /** {@inheritDoc} */
  144.     @Override
  145.     default T sinh() {
  146.         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
  147.         return compose(sinhCosh.sinh(), sinhCosh.cosh());
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     default FieldSinhCosh<T> sinhCosh() {
  152.         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
  153.         return new FieldSinhCosh<>(compose(sinhCosh.sinh(), sinhCosh.cosh()),
  154.                 compose(sinhCosh.cosh(), sinhCosh.sinh()));
  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     default T tanh() {
  159.         final S tanh = getValue().tanh();
  160.         return compose(tanh, tanh.multiply(tanh).negate().add(1));
  161.     }

  162.     /** {@inheritDoc} */
  163.     @Override
  164.     default T acosh() {
  165.         return compose(getValue().acosh(), getValue().square().subtract(1).sqrt().reciprocal());
  166.     }

  167.     /** {@inheritDoc} */
  168.     @Override
  169.     default T asinh() {
  170.         return compose(getValue().asinh(), getValue().square().add(1).sqrt().reciprocal());
  171.     }

  172.     /** {@inheritDoc} */
  173.     @Override
  174.     default T atanh() {
  175.         return compose(getValue().atanh(), getValue().square().negate().add(1).reciprocal());
  176.     }

  177. }