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 java.io.Serializable;
20
21 import org.hipparchus.exception.LocalizedCoreFormats;
22 import org.hipparchus.exception.MathIllegalArgumentException;
23 import org.hipparchus.util.FastMath;
24
25 /** Abstract class representing both the value and the differentials of a function.
26 * @param <T> the type of the function derivative
27 * @since 1.7
28 */
29 public abstract class UnivariateDerivative<T extends UnivariateDerivative<T>>
30 implements Derivative<T>, Serializable, Comparable<T> {
31
32 /** Serializable UID. */
33 private static final long serialVersionUID = 20200519L;
34
35 /** Empty constructor.
36 * <p>
37 * This constructor is not strictly necessary, but it prevents spurious
38 * javadoc warnings with JDK 18 and later.
39 * </p>
40 * @since 3.0
41 */
42 protected UnivariateDerivative() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
43 // nothing to do
44 }
45
46 /** {@inheritDoc} */
47 @Override
48 public int getFreeParameters() {
49 return 1;
50 }
51
52 /** {@inheritDoc} */
53 @Override
54 public double getPartialDerivative(final int ... orders) throws MathIllegalArgumentException {
55 if (orders.length != 1) {
56 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
57 orders.length, 1);
58 }
59 return getDerivative(orders[0]);
60 }
61
62 /** Get a derivative from the univariate derivative.
63 * @param n derivation order (must be between 0 and {@link #getOrder()}, both inclusive)
64 * @return n<sup>th</sup> derivative
65 * @exception MathIllegalArgumentException if n is
66 * either negative or strictly larger than {@link #getOrder()}
67 */
68 public abstract double getDerivative(int n) throws MathIllegalArgumentException;
69
70 /** Convert the instance to a {@link DerivativeStructure}.
71 * @return derivative structure with same value and derivative as the instance
72 */
73 public abstract DerivativeStructure toDerivativeStructure();
74
75 /** {@inheritDoc} */
76 @Override
77 public double norm() {
78 double sum = 0.;
79 for (int i = 0; i <= getOrder(); i++) {
80 sum += FastMath.abs(getDerivative(i));
81 }
82 return sum;
83 }
84 }