Repository: Hipparchus-Math/hipparchus (java)

Issue:
Implement Comparable for UnivariateDerivative
In line with #274 and also [discussions on the forum](https://forum.orekit.org/t/error-when-evaluating-field-linear-splines/2545)

Pull request description:
Fixes issue #275 (UnivariateDerivative implementing Comparable)
Closes #275 

Patch:
diff --git a/hipparchus-core/src/changes/changes.xml b/hipparchus-core/src/changes/changes.xml
index d086d61bf..b30b041a9 100644
--- a/hipparchus-core/src/changes/changes.xml
+++ b/hipparchus-core/src/changes/changes.xml
@@ -35,7 +35,7 @@ The <action> type attribute can be add,update,fix,remove.
 -->
 
 <!-- NOTE:
-The description attribute entries below are specially formatted 
+The description attribute entries below are specially formatted
 so as to improve the layout of the generated text release notes.
 
 The parsing process removes all line feeds, replacing them with a single space.
@@ -50,6 +50,9 @@ If the output is not quite correct, check for invisible trailing spaces!
   </properties>
   <body>
     <release version="3.0" date="TBD" description="This is a major release.">
+      <action dev="serrof" type="fix" issue="issues/275">
+        UnivariateDerivative now implements Comparable.
+      </action>
       <action dev="luc" type="fix" issue="issues/274">
         Complex now implements Comparable, using real part as primary sort order
         and imaginary part as secondary sort order (similar to what ComplexComparator
@@ -453,7 +456,7 @@ If the output is not quite correct, check for invisible trailing spaces!
       </action>
       <action dev="luc" type="fix" issue="issues/35" >
         Added FieldUnivariateVectorFunction and FieldUnivariateMatrixFunction interfaces,
-        as well as RealFieldUnivariateVectorFunction and RealFieldUnivariateMatrixFunction 
+        as well as RealFieldUnivariateVectorFunction and RealFieldUnivariateMatrixFunction
         Github: fixes #35.
       </action>
       <action dev="luc" type="fix" issue="issues/34" >
@@ -560,7 +563,7 @@ If the output is not quite correct, check for invisible trailing spaces!
         the random package into stat.fitting and dropped ValueServer.
       </action>
       <action dev="psteitz" type="update">
-        Moved sampling (random deviate generation) from distribution implementations 
+        Moved sampling (random deviate generation) from distribution implementations
         to RandomDataGenerator in the random package.
       </action>
       <action dev="tn" type="fix">
@@ -651,7 +654,7 @@ If the output is not quite correct, check for invisible trailing spaces!
         "Frequency" class now uses generics. For similar functionality as previous
         class wrt integer numbers, use "LongFrequency" which supports adding
         different integer number types (e.g. int, long, short) to the same
-        "LongFrequency" instance. 
+        "LongFrequency" instance.
       </action>
       <action dev="luc" type="update">
         Simplified exception hierarchy.
diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative.java
index 3ae317a58..21e07b08a 100644
--- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative.java
+++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative.java
@@ -18,7 +18,6 @@ package org.hipparchus.analysis.differentiation;
 
 import java.io.Serializable;
 
-import org.hipparchus.CalculusFieldElement;
 import org.hipparchus.exception.LocalizedCoreFormats;
 import org.hipparchus.exception.MathIllegalArgumentException;
 
@@ -27,7 +26,7 @@ import org.hipparchus.exception.MathIllegalArgumentException;
  * @since 1.7
  */
 public abstract class UnivariateDerivative<T extends UnivariateDerivative<T>>
-    implements Derivative<T>, CalculusFieldElement<T>, Serializable {
+    implements Derivative<T>, Serializable, Comparable<T> {
 
     /** Serializable UID. */
     private static final long serialVersionUID = 20200519L;
diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java
index 9ceb9cb34..2d0aed915 100644
--- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java
+++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java
@@ -758,4 +758,21 @@ public class UnivariateDerivative1 extends UnivariateDerivative<UnivariateDeriva
         return 453 - 19 * Double.hashCode(f0) + 37 * Double.hashCode(f1);
     }
 
+    /** {@inheritDoc}
+     * <p>
+     * Comparison performed considering that derivatives are intrinsically linked to monomials in the corresponding
+     * Taylor expansion and that the higher the degree, the smaller the term.
+     * </p>
+     * @since 3.0
+     */
+    @Override
+    public int compareTo(final UnivariateDerivative1 o) {
+        final int cF0 = Double.compare(f0, o.getReal());
+        if (cF0 == 0) {
+            return Double.compare(f1, o.getFirstDerivative());
+        } else {
+            return cF0;
+        }
+    }
+
 }
diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java
index 626aa4e38..2cde3c981 100644
--- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java
+++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java
@@ -871,4 +871,26 @@ public class UnivariateDerivative2 extends UnivariateDerivative<UnivariateDeriva
         return 317 - 41 * Double.hashCode(f0) + 57 * Double.hashCode(f1) - 103 * Double.hashCode(f2);
     }
 
+    /** {@inheritDoc}
+     * <p>
+     * Comparison performed considering that derivatives are intrinsically linked to monomials in the corresponding
+     * Taylor expansion and that the higher the degree, the smaller the term.
+     * </p>
+     * @since 3.0
+     */
+    @Override
+    public int compareTo(final UnivariateDerivative2 o) {
+        final int cF0 = Double.compare(f0, o.getReal());
+        if (cF0 == 0) {
+            final int cF1 = Double.compare(f1, o.getFirstDerivative());
+            if (cF1 == 0) {
+                return Double.compare(f2, o.getSecondDerivative());
+            } else {
+                return cF1;
+            }
+        } else {
+            return cF0;
+        }
+    }
+
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 59563170f..57de43ac6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,9 @@ If the output is not quite correct, check for invisible trailing spaces!
   </properties>
   <body>
     <release version="3.0" date="TBD" description="This is a major release.">
+      <action dev="serrof" type="fix" issue="issues/275">
+        UnivariateDerivative now implements Comparable.
+      </action>
       <action dev="luc" type="fix" issue="issues/274">
         Complex now implements Comparable, using real part as primary sort order
         and imaginary part as secondary sort order (similar to what ComplexComparator


Test patch:
diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1Test.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1Test.java
index 030bfee66..f50f55238 100644
--- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1Test.java
+++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1Test.java
@@ -102,6 +102,31 @@ public class UnivariateDerivative1Test extends UnivariateDerivativeAbstractTest<
         Assert.assertNotEquals(ud1, new UnivariateDerivative1(21, -43));
     }
 
+
+    @Test
+    public void testComparableFirstTerm() {
+        // GIVEN
+        final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
+        final UnivariateDerivative1 ud1b = new UnivariateDerivative1(2, 0);
+        // WHEN
+        final int actualComparison = ud1a.compareTo(ud1b);
+        // THEN
+        final int expectedComparison = 1;
+        Assert.assertEquals(expectedComparison, actualComparison);
+    }
+
+    @Test
+    public void testComparableSecondTerm() {
+        // GIVEN
+        final UnivariateDerivative1 ud1a = new UnivariateDerivative1(12, -34);
+        final UnivariateDerivative1 ud1b = new UnivariateDerivative1(12, 0);
+        // WHEN
+        final int actualComparison = ud1a.compareTo(ud1b);
+        // THEN
+        final int expectedComparison = -1;
+        Assert.assertEquals(expectedComparison, actualComparison);
+    }
+
     @Test
     public void testRunTimeClass() {
         Field<UnivariateDerivative1> field = build(0.0).getField();
diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2Test.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2Test.java
index 7998b63fa..0bd8c823f 100644
--- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2Test.java
+++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2Test.java
@@ -106,6 +106,42 @@ public class UnivariateDerivative2Test extends UnivariateDerivativeAbstractTest<
         Assert.assertNotEquals(ud2, new UnivariateDerivative2(21, -43, 65));
     }
 
+    @Test
+    public void testComparableFirstTerm() {
+        // GIVEN
+        final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
+        final UnivariateDerivative2 ud2b = new UnivariateDerivative2(2, 0, 25);
+        // WHEN
+        final int actualComparison = ud2a.compareTo(ud2b);
+        // THEN
+        final int expectedComparison = 1;
+        Assert.assertEquals(expectedComparison, actualComparison);
+    }
+
+    @Test
+    public void testComparableSecondTerm() {
+        // GIVEN
+        final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
+        final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, 0, 25);
+        // WHEN
+        final int actualComparison = ud2a.compareTo(ud2b);
+        // THEN
+        final int expectedComparison = -1;
+        Assert.assertEquals(expectedComparison, actualComparison);
+    }
+
+    @Test
+    public void testComparableThirdTerm() {
+        // GIVEN
+        final UnivariateDerivative2 ud2a = new UnivariateDerivative2(12, -34, 25);
+        final UnivariateDerivative2 ud2b = new UnivariateDerivative2(12, -34, 25);
+        // WHEN
+        final int actualComparison = ud2a.compareTo(ud2b);
+        // THEN
+        final int expectedComparison = 0;
+        Assert.assertEquals(expectedComparison, actualComparison);
+    }
+
     @Test
     public void testRunTimeClass() {
         Field<UnivariateDerivative2> field = build(0.0).getField();