PenaltyMatrixGenerator { public static DoubleMatrix2D getPenaltyMatrix(int m, int k) { ArgumentChecker.notNegativeOrZero(m, "m"); ArgumentChecker.notNegative(k, "k"); ArgumentChecker.isTrue(k < m, "Difference order too high, require m > k, but have: m = {} and k = {}", m, k); if (k == 0) { return new IdentityMatrix(m); } DoubleMatrix2D d = getDifferenceMatrix(m, k); DoubleMatrix2D dt = MA.getTranspose(d); return (DoubleMatrix2D) MA.multiply(dt, d); }  static DoubleMatrix2D getDifferenceMatrix(int m, int k); static DoubleMatrix2D getPenaltyMatrix(int m, int k); static DoubleMatrix2D getPenaltyMatrix(int[] numElements, int k, int index); static DoubleMatrix2D getPenaltyMatrix(int[] numElements, int[] k, double[] lambda); static DoubleMatrix2D getDerivativeMatrix(double[] x, int k, boolean includeEnds); static DoubleMatrix2D getPenaltyMatrix(double[] x, int k); static DoubleMatrix2D getPenaltyMatrix(double[][] x, int k, int index); static DoubleMatrix2D getPenaltyMatrix(double[][] x, int[] k, double[] lambda); static DoubleMatrix1D flattenMatrix(DoubleMatrix2D aMatrix); static DoubleMatrix2D getMatrixForFlattened(int[] numElements, DoubleMatrix2D m, int index);  }
@Test public void penaltyMatrix1DTest() { final int n = 10; DoubleMatrix2D p0 = getPenaltyMatrix(n, 0); AssertMatrix.assertEqualsMatrix(new IdentityMatrix(n), p0, 1e-15); final DoubleMatrix1D x = new DoubleMatrix1D(n, 2.0); DoubleMatrix2D p = PenaltyMatrixGenerator.getPenaltyMatrix(n, 2); double r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r); final double[] data = x.getData(); for (int i = 0; i < n; i++) { data[i] = i; } r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r); for (int i = 0; i < n; i++) { data[i] = 0.4 + 0.4 * i + i * i; } r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(32.0, r, 1e-11); p = PenaltyMatrixGenerator.getPenaltyMatrix(n, 3); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r, 1e-13); }
@Test(expectedExceptions = IllegalArgumentException.class) public void penaltyMatrixDiffOrderTooHighTest() { getPenaltyMatrix(6, 10); }
@Test public void penaltyMatrix3DTest() { final int n1 = 5; final int n2 = 13; final int n3 = 4; final DoubleMatrix1D x = new DoubleMatrix1D(n1 * n2 * n3, 2.0); DoubleMatrix2D p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 1, 0); double r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r); p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 1, 1); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r); p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 1, 2); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r); final double[] data = x.getData(); for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { for (int k = 0; k < n3; k++) { data[i * n2 * n3 + j * n3 + k] = 0.4 + i - k + j * j - 3.0 * i * k + 4 * i * j; } } } p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 2, 0); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r, 1e-11); p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 3, 1); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r, 3e-10); p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 2, 2); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(0.0, r, 5e-11); p = PenaltyMatrixGenerator.getPenaltyMatrix(new int[] {n1, n2, n3 }, 2, 1); r = MA.getInnerProduct(x, MA.multiply(p, x)); assertEquals(880, r, 1e-9); }
@Test(dataProvider = "data") public void penaltyMatrix1DTest(double[] x, DoubleMatrix1D y, DoubleMatrix1D dydx, DoubleMatrix1D d2ydx2) { int n = x.length; double expected = 0.0; for (int i = 0; i < n; i++) { if (i > 0 && i < (n - 1)) { expected += FunctionUtils.square(d2ydx2.getEntry(i)); } } double scale = Math.pow(2.0, 4); expected *= scale; DoubleMatrix2D p2 = getPenaltyMatrix(x, 2); double r = MA.getInnerProduct(y, MA.multiply(p2, y)); assertEquals(expected, r, 1e-11); }
@Test public void penaltyMatrixScaleTest() { double[] x = new double[] {0.0, 0.3, 0.7, 0.8, 1.2, 2.0 }; double scale = 5.0; int n = x.length; double[] xScaled = new double[n]; DoubleMatrix1D y = new DoubleMatrix1D(n); for (int i = 0; i < n; i++) { double xi = x[i]; y.getData()[i] = 0.3 + xi + Math.sin(xi); xScaled[i] = xi * scale; } DoubleMatrix2D p1 = getPenaltyMatrix(x, 1); DoubleMatrix2D p1s = getPenaltyMatrix(xScaled, 1); double r = MA.getInnerProduct(y, MA.multiply(p1, y)); double rs = MA.getInnerProduct(y, MA.multiply(p1s, y)); assertEquals(r, rs, 1e-10); DoubleMatrix2D p2 = getPenaltyMatrix(x, 2); DoubleMatrix2D p2s = getPenaltyMatrix(xScaled, 2); r = MA.getInnerProduct(y, MA.multiply(p2, y)); rs = MA.getInnerProduct(y, MA.multiply(p2s, y)); assertEquals(r, rs, 1e-10); }
@Test(expectedExceptions = IllegalArgumentException.class) public void xNotUniqueTest() { double[] x = new double[] {0.0, 0.3, 0.8, 0.8, 1.2, 2.0 }; @SuppressWarnings("unused") DoubleMatrix2D p1 = getPenaltyMatrix(x, 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void xNotAscendingTest() { double[] x = new double[] {0.0, 0.3, 0.8, 0.7, 1.2, 2.0 }; @SuppressWarnings("unused") DoubleMatrix2D p1 = getPenaltyMatrix(x, 2); }
@Test(expectedExceptions = IllegalArgumentException.class) public void emptyXTest() { @SuppressWarnings("unused") DoubleMatrix2D p1 = getPenaltyMatrix(new double[0], 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void negRangeTest() { double[] x = new double[] {0.0, -2.0 }; @SuppressWarnings("unused") DoubleMatrix2D p1 = getPenaltyMatrix(x, 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void singlePointTest() { double[] x = new double[] {0.2 }; @SuppressWarnings("unused") DoubleMatrix2D p1 = getPenaltyMatrix(x, 1); }
@Test public void zeroOrderSinglePointTest() { double[] x = new double[] {0.2 }; DoubleMatrix2D p1 = getPenaltyMatrix(x, 0); AssertMatrix.assertEqualsMatrix(new IdentityMatrix(1), p1, 1e-15); }