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.SinCos;
22  import org.hipparchus.util.SinhCosh;
23  import org.hipparchus.util.FieldSinCos;
24  import org.hipparchus.util.FieldSinhCosh;
25  
26  /** Interface representing an object holding partial derivatives up to first order.
27   * @param <T> the type of the field elements
28   * @see Derivative
29   * @see UnivariateDerivative1
30   * @see Gradient
31   * @see SparseGradient
32   * @since 3.1
33   */
34  public interface Derivative1<T extends CalculusFieldElement<T>> extends Derivative<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(double f0, double f1);
48  
49      /** {@inheritDoc} */
50      @Override
51      default T square() {
52          final double f0 = getValue();
53          return compose(f0 * f0, 2 * f0);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      default T reciprocal () {
59          final double inv1 = 1.0 / getValue();
60          final double inv2 = -inv1 * inv1;
61          return compose(inv1, inv2);
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      default T sqrt() {
67          final double s = FastMath.sqrt(getValue());
68          return compose(s, 1 / (2 * s));
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      default T cbrt() {
74          final double c = FastMath.cbrt(getValue());
75          return compose(c, 1 / (3 * c * c));
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      default T rootN(int n) {
81          if (n == 2) {
82              return sqrt();
83          } else if (n == 3) {
84              return cbrt();
85          } else {
86              final double r = FastMath.pow(getValue(), 1.0 / n);
87              return compose(r, 1 / (n * FastMath.pow(r, n - 1)));
88          }
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      default T exp() {
94          final double exp = FastMath.exp(getValue());
95          return compose(exp, exp);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     default T expm1() {
101         final double exp   = FastMath.exp(getValue());
102         final double expM1 = FastMath.expm1(getValue());
103         return compose(expM1, exp);
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     default T log() {
109         return compose(FastMath.log(getValue()), 1 / getValue());
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     default T log1p() {
115         return compose(FastMath.log1p(getValue()), 1 / (1 + getValue()));
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     default T log10() {
121         return compose(FastMath.log10(getValue()), 1 / (getValue() * FastMath.log(10.0)));
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     default T cos() {
127         final SinCos sinCos = FastMath.sinCos(getValue());
128         return compose(sinCos.cos(), -sinCos.sin());
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     default T sin() {
134         final SinCos sinCos = FastMath.sinCos(getValue());
135         return compose(sinCos.sin(), sinCos.cos());
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     default FieldSinCos<T> sinCos() {
141         final SinCos sinCos = FastMath.sinCos(getValue());
142         return new FieldSinCos<>(compose(sinCos.sin(), sinCos.cos()),
143                 compose(sinCos.cos(), -sinCos.sin()));
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     default T tan() {
149         final double tan = FastMath.tan(getValue());
150         return compose(tan, 1 + tan * tan);
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     default T acos() {
156         final double f0 = getValue();
157         return compose(FastMath.acos(f0), -1 / FastMath.sqrt(1 - f0 * f0));
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     default T asin() {
163         final double f0 = getValue();
164         return compose(FastMath.asin(f0), 1 / FastMath.sqrt(1 - f0 * f0));
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     default T atan() {
170         final double f0 = getValue();
171         return compose(FastMath.atan(f0), 1 / (1 + f0 * f0));
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     default T cosh() {
177         return compose(FastMath.cosh(getValue()), FastMath.sinh(getValue()));
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     default T sinh() {
183         return compose(FastMath.sinh(getValue()), FastMath.cosh(getValue()));
184     }
185 
186     /** {@inheritDoc} */
187     @Override
188     default FieldSinhCosh<T> sinhCosh() {
189         final SinhCosh sinhCosh = FastMath.sinhCosh(getValue());
190         return new FieldSinhCosh<>(compose(sinhCosh.sinh(), sinhCosh.cosh()),
191                 compose(sinhCosh.cosh(), sinhCosh.sinh()));
192     }
193 
194     /** {@inheritDoc} */
195     @Override
196     default T tanh() {
197         final double tanh = FastMath.tanh(getValue());
198         return compose(tanh, 1 - tanh * tanh);
199     }
200 
201     /** {@inheritDoc} */
202     @Override
203     default T acosh() {
204         final double f0 = getValue();
205         return compose(FastMath.acosh(f0), 1 / FastMath.sqrt(f0 * f0 - 1));
206     }
207 
208     /** {@inheritDoc} */
209     @Override
210     default T asinh() {
211         final double f0 = getValue();
212         return compose(FastMath.asinh(f0), 1 / FastMath.sqrt(f0 * f0 + 1));
213     }
214 
215     /** {@inheritDoc} */
216     @Override
217     default T atanh() {
218         final double f0 = getValue();
219         return compose(FastMath.atanh(f0), 1 / (1 - f0 * f0));
220     }
221 
222 }