View Javadoc
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  
19  import org.hipparchus.CalculusFieldElement;
20  import org.hipparchus.util.FastMath;
21  import org.hipparchus.util.FieldSinCos;
22  import org.hipparchus.util.FieldSinhCosh;
23  
24  /** Interface representing a Field object holding partial derivatives up to first order.
25   * @param <S> the type of the field elements
26   * @param <T> the type of the function derivative
27   * @see FieldDerivative
28   * @see FieldUnivariateDerivative1
29   * @see FieldGradient
30   * @see Derivative1
31   * @since 3.1
32   */
33  public interface FieldDerivative1<S extends CalculusFieldElement<S>, T extends FieldDerivative<S, T>>
34          extends FieldDerivative<S, T> {
35  
36      /** {@inheritDoc} */
37      @Override
38      default int getOrder() {
39          return 1;
40      }
41  
42      /** Compute composition of the instance by a univariate function differentiable at order 1.
43       * @param f0 value of function
44       * @param f1 first-order derivative
45       * @return f(this)
46       */
47      T compose(S f0, S f1);
48  
49      /** {@inheritDoc} */
50      @Override
51      default T square() {
52          final S f0 = getValue();
53          return compose(f0.square(), f0.multiply(2));
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      default T reciprocal() {
59          final S inv1 = getValue().reciprocal();
60          final S inv2 = inv1.square().negate();
61          return compose(inv1, inv2);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      default T exp() {
67          final S exp = getValue().exp();
68          return compose(exp, exp);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      default T sqrt() {
74          final S s = getValue().sqrt();
75          return compose(s, s.add(s).reciprocal());
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      default T cbrt() {
81          final S c = getValue().cbrt();
82          return compose(c, c.square().multiply(3).reciprocal());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      default T expm1() {
88          final S exp   = FastMath.exp(getValue());
89          final S expM1 = FastMath.expm1(getValue());
90          return compose(expM1, exp);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      default T log() {
96          return compose(getValue().log(), getValue().reciprocal());
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     default T log1p() {
102         return compose(getValue().log1p(), getValue().add(1).reciprocal());
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     default T log10() {
108         return compose(getValue().log10(), getValue().multiply(FastMath.log(10.0)).reciprocal());
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     default T cos() {
114         final FieldSinCos<S> sinCos = getValue().sinCos();
115         return compose(sinCos.cos(), sinCos.sin().negate());
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     default T sin() {
121         final FieldSinCos<S> sinCos = getValue().sinCos();
122         return compose(sinCos.sin(), sinCos.cos());
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     default FieldSinCos<T> sinCos() {
128         final FieldSinCos<S> sinCos = getValue().sinCos();
129         return new FieldSinCos<>(compose(sinCos.sin(), sinCos.cos()),
130                 compose(sinCos.cos(), sinCos.sin().negate()));
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     default T tan() {
136         final S tan = getValue().tan();
137         return compose(tan, tan.multiply(tan).add(1));
138     }
139 
140     /** {@inheritDoc} */
141     @Override
142     default T acos() {
143         return compose(getValue().acos(), getValue().square().negate().add(1).sqrt().reciprocal().negate());
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     default T asin() {
149         return compose(getValue().asin(), getValue().square().negate().add(1).sqrt().reciprocal());
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     default T atan() {
155         return compose(getValue().atan(), getValue().square().add(1).reciprocal());
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     default T cosh() {
161         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
162         return compose(sinhCosh.cosh(), sinhCosh.sinh());
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     default T sinh() {
168         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
169         return compose(sinhCosh.sinh(), sinhCosh.cosh());
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     default FieldSinhCosh<T> sinhCosh() {
175         final FieldSinhCosh<S> sinhCosh = getValue().sinhCosh();
176         return new FieldSinhCosh<>(compose(sinhCosh.sinh(), sinhCosh.cosh()),
177                 compose(sinhCosh.cosh(), sinhCosh.sinh()));
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     default T tanh() {
183         final S tanh = getValue().tanh();
184         return compose(tanh, tanh.multiply(tanh).negate().add(1));
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     default T acosh() {
190         return compose(getValue().acosh(), getValue().square().subtract(1).sqrt().reciprocal());
191     }
192 
193     /** {@inheritDoc} */
194     @Override
195     default T asinh() {
196         return compose(getValue().asinh(), getValue().square().add(1).sqrt().reciprocal());
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     default T atanh() {
202         return compose(getValue().atanh(), getValue().square().negate().add(1).reciprocal());
203     }
204 
205 }