1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
30
31
32
33
34 public interface Derivative1<T extends CalculusFieldElement<T>> extends Derivative<T> {
35
36
37 @Override
38 default int getOrder() {
39 return 1;
40 }
41
42
43
44
45
46
47 T compose(double f0, double f1);
48
49
50 @Override
51 default T square() {
52 final double f0 = getValue();
53 return compose(f0 * f0, 2 * f0);
54 }
55
56
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
65 @Override
66 default T sqrt() {
67 final double s = FastMath.sqrt(getValue());
68 return compose(s, 1 / (2 * s));
69 }
70
71
72 @Override
73 default T cbrt() {
74 final double c = FastMath.cbrt(getValue());
75 return compose(c, 1 / (3 * c * c));
76 }
77
78
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
92 @Override
93 default T exp() {
94 final double exp = FastMath.exp(getValue());
95 return compose(exp, exp);
96 }
97
98
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
107 @Override
108 default T log() {
109 return compose(FastMath.log(getValue()), 1 / getValue());
110 }
111
112
113 @Override
114 default T log1p() {
115 return compose(FastMath.log1p(getValue()), 1 / (1 + getValue()));
116 }
117
118
119 @Override
120 default T log10() {
121 return compose(FastMath.log10(getValue()), 1 / (getValue() * FastMath.log(10.0)));
122 }
123
124
125 @Override
126 default T cos() {
127 final SinCos sinCos = FastMath.sinCos(getValue());
128 return compose(sinCos.cos(), -sinCos.sin());
129 }
130
131
132 @Override
133 default T sin() {
134 final SinCos sinCos = FastMath.sinCos(getValue());
135 return compose(sinCos.sin(), sinCos.cos());
136 }
137
138
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
147 @Override
148 default T tan() {
149 final double tan = FastMath.tan(getValue());
150 return compose(tan, 1 + tan * tan);
151 }
152
153
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
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
168 @Override
169 default T atan() {
170 final double f0 = getValue();
171 return compose(FastMath.atan(f0), 1 / (1 + f0 * f0));
172 }
173
174
175 @Override
176 default T cosh() {
177 return compose(FastMath.cosh(getValue()), FastMath.sinh(getValue()));
178 }
179
180
181 @Override
182 default T sinh() {
183 return compose(FastMath.sinh(getValue()), FastMath.cosh(getValue()));
184 }
185
186
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
195 @Override
196 default T tanh() {
197 final double tanh = FastMath.tanh(getValue());
198 return compose(tanh, 1 - tanh * tanh);
199 }
200
201
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
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
216 @Override
217 default T atanh() {
218 final double f0 = getValue();
219 return compose(FastMath.atanh(f0), 1 / (1 - f0 * f0));
220 }
221
222 }