Repository: Hipparchus-Math/hipparchus (java)

Issue:
Unusable Splines with Field not heriting from interface Comparable
Any call to method `value` of `FieldSplinePolynomialFunction` (after actual interpolation) fails if the `Field` does not extent `Comparable`, as the code tries to see which polynomial to evaluate by comparing inputs to knots. 

See discussion on [forum](https://forum.orekit.org/t/error-when-evaluating-field-linear-splines/2545).

Pull request:
Solved issue #257
Closes #257

Patch:
diff --git a/hipparchus-core/src/changes/changes.xml b/hipparchus-core/src/changes/changes.xml
index 09b1bb9da..42f011e5d 100644
--- a/hipparchus-core/src/changes/changes.xml
+++ b/hipparchus-core/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="luc" type="fix" due-to="Romain Serra" issue="issues/257">
+        Made FieldPolynomialSplineFunction operational with Field not inheriting from Comparable.
+      </action>
       <action dev="luc" type="fix" issue="issues/262">
         Fixed determinant computation in eigen decomposition with complex eigenvalues.
       </action>
diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunction.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunction.java
index 044bf6a22..dd85e4471 100644
--- a/hipparchus-core/src/main/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunction.java
+++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunction.java
@@ -157,7 +157,7 @@ public class FieldPolynomialSplineFunction<T extends CalculusFieldElement<T>> im
     @Override
     public T value(final T v) {
         MathUtils.checkRangeInclusive(v.getReal(), knots[0].getReal(), knots[n].getReal());
-        int i = Arrays.binarySearch(knots, v);
+        int i = Arrays.binarySearch(Arrays.stream(knots).map(T::getReal).toArray(), v.getReal());
         if (i < 0) {
             i = -i - 2;
         }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7918aa4e4..ce401224d 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="luc" type="fix" due-to="Romain Serra" issue="issues/257">
+        Made FieldPolynomialSplineFunction operational with Field not inheriting from Comparable.
+      </action>
       <action dev="maxime" type="add" issue="issues/263">
         Created a rule to forbid Python keywords in method, class, interface and enum names.
       </action>


Test patch:
diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunctionTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunctionTest.java
index d903b331d..b20b58224 100644
--- a/hipparchus-core/src/test/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunctionTest.java
+++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/polynomials/FieldPolynomialSplineFunctionTest.java
@@ -24,6 +24,9 @@ package org.hipparchus.analysis.polynomials;
 import java.lang.reflect.Array;
 import java.util.Arrays;
 
+import org.hipparchus.analysis.differentiation.Gradient;
+import org.hipparchus.analysis.differentiation.GradientField;
+import org.hipparchus.analysis.interpolation.LinearInterpolator;
 import org.hipparchus.exception.LocalizedCoreFormats;
 import org.hipparchus.exception.MathIllegalArgumentException;
 import org.hipparchus.exception.MathIllegalStateException;
@@ -222,5 +225,28 @@ public class FieldPolynomialSplineFunctionTest {
          };
      }
 
+     @Test
+     public void testValueGradient() {
+         // issue #257
+         // Given
+         final int freeParameters = 1;
+         final GradientField field = GradientField.getField(freeParameters);
+         final Gradient zero = field.getZero();
+         final Gradient time1 = zero.add(1.0);
+         final Gradient time2 = zero.add(2.0);
+         final Gradient time3 = zero.add(4.0);
+         final Gradient x1 = zero.add(4.0);
+         final Gradient x2 = Gradient.variable(freeParameters, 0, -2.0);
+         final Gradient x3 = zero.add(0.0);
+         final Gradient[] times = new Gradient[] {time1, time2, time3};
+         final Gradient[] xs = new Gradient[] {x1, x2, x3};
+         // When
+         final FieldPolynomialSplineFunction<Gradient> spline = new LinearInterpolator().interpolate(times, xs);
+         final Gradient actualEvaluation = spline.value(zero.add(3.));
+         // Then
+         final Gradient expectedEvaluation = Gradient.variable(freeParameters, 0, 0).multiply(0.5).add(-1.0);
+         Assert.assertEquals(expectedEvaluation, actualEvaluation);
+     }
+
 }