55
66/**
77 * In numerical analysis, Neville's algorithm is an algorithm used for
8- * polynomial interpolation. Given n+1 points, there is a unique polynomial
9- * of degree at most n that passes through all the points. Neville's
10- * algorithm computes the value of this polynomial at a given point.
8+ * polynomial interpolation. Given n+1 points, there is a unique polynomial of
9+ * degree at most n that passes through all the points. Neville's algorithm
10+ * computes the value of this polynomial at a given point.
1111 *
1212 * <p>
1313 * Wikipedia: https://en.wikipedia.org/wiki/Neville%27s_algorithm
1414 *
1515 * @author Mitrajit Ghorui(KeyKyrios)
1616 */
1717public final class Neville {
18+
1819 private Neville () {
1920 }
2021
2122 /**
22- * Evaluates the polynomial that passes through the given points at a specific x-coordinate.
23+ * Evaluates the polynomial that passes through the given points at a
24+ * specific x-coordinate.
2325 *
2426 * @param x The x-coordinates of the points. Must be the same length as y.
2527 * @param y The y-coordinates of the points. Must be the same length as x.
2628 * @param target The x-coordinate at which to evaluate the polynomial.
2729 * @return The interpolated y-value at the target x-coordinate.
28- * @throws IllegalArgumentException if the lengths of x and y arrays are different,
29- * if the arrays are empty, or if x-coordinates are not unique.
30+ * @throws IllegalArgumentException if the lengths of x and y arrays are
31+ * different, if the arrays are empty, or if x-coordinates are not unique.
3032 */
3133 public static double interpolate (double [] x , double [] y , double target ) {
3234 if (x .length != y .length ) {
@@ -36,6 +38,7 @@ public static double interpolate(double[] x, double[] y, double target) {
3638 throw new IllegalArgumentException ("Input arrays cannot be empty." );
3739 }
3840
41+ // Check for duplicate x-coordinates to prevent division by zero
3942 Set <Double > seenX = new HashSet <>();
4043 for (double val : x ) {
4144 if (!seenX .add (val )) {
0 commit comments