You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/07/25 14:28:16 UTC

[GitHub] [commons-math] aherbert commented on a diff in pull request #210: Derivative computations for BicubicInterpolator

aherbert commented on code in PR #210:
URL: https://github.com/apache/commons-math/pull/210#discussion_r928911104


##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunction.java:
##########
@@ -317,49 +325,6 @@ private int searchIndex(double c, double[] val) {
         return r;
     }
 
-    /**
-     * Compute the spline coefficients from the list of function values and
-     * function partial derivatives values at the four corners of a grid
-     * element. They must be specified in the following order:
-     * <ul>
-     *  <li>f(0,0)</li>
-     *  <li>f(1,0)</li>
-     *  <li>f(0,1)</li>
-     *  <li>f(1,1)</li>
-     *  <li>f<sub>x</sub>(0,0)</li>
-     *  <li>f<sub>x</sub>(1,0)</li>
-     *  <li>f<sub>x</sub>(0,1)</li>
-     *  <li>f<sub>x</sub>(1,1)</li>
-     *  <li>f<sub>y</sub>(0,0)</li>
-     *  <li>f<sub>y</sub>(1,0)</li>
-     *  <li>f<sub>y</sub>(0,1)</li>
-     *  <li>f<sub>y</sub>(1,1)</li>
-     *  <li>f<sub>xy</sub>(0,0)</li>
-     *  <li>f<sub>xy</sub>(1,0)</li>
-     *  <li>f<sub>xy</sub>(0,1)</li>
-     *  <li>f<sub>xy</sub>(1,1)</li>
-     * </ul>
-     * where the subscripts indicate the partial derivative with respect to
-     * the corresponding variable(s).
-     *
-     * @param beta List of function values and function partial derivatives
-     * values.
-     * @return the spline coefficients.
-     */
-    private double[] computeSplineCoefficients(double[] beta) {
-        final double[] a = new double[NUM_COEFF];
-
-        for (int i = 0; i < NUM_COEFF; i++) {
-            double result = 0;
-            final double[] row = AINV[i];

Review Comment:
   Q. Why has this been replaced with the matrix multiply?
   
   Using the Matrix objects is far less efficient.



##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunction.java:
##########
@@ -322,4 +485,40 @@ private double apply(double[] pX, double[] pY, double[][] coeff) {
 
         return result;
     }
+
+    /**
+     * @return the partial derivative wrt {@code x}.
+     */
+    public BivariateFunction partialDerivativeX() {

Review Comment:
   These can all drop the public keyword.



##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunction.java:
##########
@@ -181,6 +226,75 @@ public boolean isValidPoint(double x, double y) {
             y > yval[yval.length - 1]);
     }
 
+    /**
+     * @return the first partial derivative respect to x.
+     * @throws NullPointerException if the internal data were not initialized
+     * (cf. {@link #BicubicSplineInterpolatingFunction(double[],double[],double[][],

Review Comment:
   `BicubicSplineInterpolatingFunction` does not exist. Please update all 5 references in the javadoc of this class to `BicubicInterpolatingFunction`



##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolator.java:
##########
@@ -41,6 +41,30 @@
  */
 public class BicubicInterpolator
     implements BivariateGridInterpolator {
+    /** Whether to initialize internal data used to compute the analytical
+    derivatives of the splines. */
+    private final boolean initializeDerivatives;
+
+    /**
+     * Default constructor.
+     * The argument {@link #BicubicSplineInterpolator(boolean) initializeDerivatives}

Review Comment:
   `BicubicSplineInterpolator` does not exist



##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolator.java:
##########
@@ -41,6 +41,30 @@
  */
 public class BicubicInterpolator
     implements BivariateGridInterpolator {
+    /** Whether to initialize internal data used to compute the analytical
+    derivatives of the splines. */

Review Comment:
   Add ` * ` at the start of the line for correct javadoc formatting



##########
commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunctionTest.java:
##########
@@ -304,7 +304,7 @@ public void testSplinePartialDerivatives() {
             }
         }
 
-        final BicubicFunction f = new BicubicFunction(coeff, true);
+        final BicubicFunction f = new BicubicFunction(coeff, 1, 1, true);

Review Comment:
   Have you tested this when `xR` and `yR` are not 1? I would suggest simply repeating the test with `xR` and `yR` as  0.5 and 2 then testing the derivative is correctly scaled.
   



##########
commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunctionTest.java:
##########
@@ -286,6 +287,231 @@ public double value(double x, double y) {
                           maxTolerance,
                           false);
     }
+    
+    /**
+     * Test for partial derivatives of {@link BicubicSplineFunction}.

Review Comment:
   `BicubicSplineFunction` does not exist; it should be `BicubicFunction`



##########
commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolatingFunctionTest.java:
##########
@@ -286,6 +287,231 @@ public double value(double x, double y) {
                           maxTolerance,
                           false);
     }
+    
+    /**
+     * Test for partial derivatives of {@link BicubicSplineFunction}.
+     * <p>
+     * f(x, y) = &Sigma;<sub>i</sub>&Sigma;<sub>j</sub> (i+1) (j+2) x<sup>i</sup> y<sup>j</sup>
+     */
+    @Test
+    public void testSplinePartialDerivatives() {
+        final int N = 4;
+        final double[] coeff = new double[16];
+
+        for (int i = 0; i < N; i++) {
+            for (int j = 0; j < N; j++) {
+                coeff[i + N * j] = (i + 1) * (j + 2);
+            }
+        }
+
+        final BicubicFunction f = new BicubicFunction(coeff, 1, 1, true);
+        BivariateFunction derivative;
+        final double x = 0.435;
+        final double y = 0.776;
+        final double tol = 1e-13;
+
+        derivative = new BivariateFunction() {
+                public double value(double x, double y) {
+                    final double x2 = x * x;
+                    final double y2 = y * y;
+                    final double y3 = y2 * y;
+                    final double yFactor = 2 + 3 * y + 4 * y2 + 5 * y3;
+                    return yFactor * (2 + 6 * x + 12 * x2);
+                }
+            };
+        Assert.assertEquals("dFdX", derivative.value(x, y),
+                            f.partialDerivativeX().value(x, y), tol);
+
+        derivative = new BivariateFunction() {
+                public double value(double x, double y) {
+                    final double x2 = x * x;
+                    final double x3 = x2 * x;
+                    final double y2 = y * y;
+                    final double xFactor = 1 + 2 * x + 3 * x2 + 4 * x3;
+                    return xFactor * (3 + 8 * y + 15 * y2);
+                }
+            };
+        Assert.assertEquals("dFdY", derivative.value(x, y),
+                            f.partialDerivativeY().value(x, y), tol);
+
+        derivative = new BivariateFunction() {
+                public double value(double x, double y) {
+                    final double y2 = y * y;
+                    final double y3 = y2 * y;
+                    final double yFactor = 2 + 3 * y + 4 * y2 + 5 * y3;
+                    return yFactor * (6 + 24 * x);
+                }
+            };
+        Assert.assertEquals("d2FdX2", derivative.value(x, y),
+                            f.partialDerivativeXX().value(x, y), tol);
+
+        derivative = new BivariateFunction() {
+                public double value(double x, double y) {
+                    final double x2 = x * x;
+                    final double x3 = x2 * x;
+                    final double xFactor = 1 + 2 * x + 3 * x2 + 4 * x3;
+                    return xFactor * (8 + 30 * y);
+                }
+            };
+        Assert.assertEquals("d2FdY2", derivative.value(x, y),
+                            f.partialDerivativeYY().value(x, y), tol);
+
+        derivative = new BivariateFunction() {
+                public double value(double x, double y) {
+                    final double x2 = x * x;
+                    final double y2 = y * y;
+                    final double yFactor = 3 + 8 * y + 15 * y2;
+                    return yFactor * (2 + 6 * x + 12 * x2);
+                }
+            };
+        Assert.assertEquals("d2FdXdY", derivative.value(x, y),
+                            f.partialDerivativeXY().value(x, y), tol);
+    }
+
+    /**
+     * Test that the partial derivatives computed from a
+     * {@link BicubicSplineInterpolatingFunction} match the input data.

Review Comment:
   `BicubicSplineInterpolatingFunction` does not exist



##########
commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/analysis/interpolation/BicubicInterpolator.java:
##########
@@ -41,6 +41,30 @@
  */
 public class BicubicInterpolator
     implements BivariateGridInterpolator {
+    /** Whether to initialize internal data used to compute the analytical
+    derivatives of the splines. */
+    private final boolean initializeDerivatives;
+
+    /**
+     * Default constructor.
+     * The argument {@link #BicubicSplineInterpolator(boolean) initializeDerivatives}
+     * is set to {@code false}.
+     */
+    public BicubicInterpolator() {
+        this(false);
+    }
+
+    /**
+     * Creates an interpolator.
+     *
+     * @param initializeDerivatives Whether to initialize the internal data
+     * needed for calling any of the methods that compute the partial derivatives
+     * of the {@link BicubicSplineInterpolatingFunction function} returned from

Review Comment:
   `BicubicSplineInterpolatingFunction` does not exist



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org