PenaltyMatrixGenerator { public static DoubleMatrix2D getDifferenceMatrix(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 = new DoubleMatrix2D(m, m); double[][] data = d.getData(); int[] coeff = new int[k + 1]; int sign = 1; for (int i = k; i >= 0; i--) { coeff[i] = (int) (sign * binomialCoefficient(k, i)); sign = -sign; } for (int i = k; i < m; i++) { for (int j = 0; j < k + 1; j++) { data[i][j + i - k] = coeff[j]; } } return 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 differenceMatrix1DTest() { final int n = 7; DoubleMatrix2D d0 = getDifferenceMatrix(n, 0); AssertMatrix.assertEqualsMatrix(new IdentityMatrix(n), d0, 1e-15); DoubleMatrix1D zeroVector = new DoubleMatrix1D(n); DoubleMatrix2D d1 = getDifferenceMatrix(n, 1); assertEquals(n, d1.getNumberOfRows()); assertEquals(n, d1.getNumberOfColumns()); AssertMatrix.assertEqualsVectors(zeroVector, d1.getRowVector(0), 1e-15); final DoubleMatrix1D x = new DoubleMatrix1D(n, 1.0); DoubleMatrix1D d1x = (DoubleMatrix1D) MA.multiply(d1, x); AssertMatrix.assertEqualsVectors(zeroVector, d1x, 1e-14); DoubleMatrix2D d2 = getDifferenceMatrix(n, 2); assertEquals(n, d2.getNumberOfRows()); assertEquals(n, d2.getNumberOfColumns()); AssertMatrix.assertEqualsVectors(zeroVector, d2.getRowVector(0), 1e-15); AssertMatrix.assertEqualsVectors(zeroVector, d2.getRowVector(1), 1e-15); for (int i = 0; i < n; i++) { x.getData()[i] = i; } d1x = (DoubleMatrix1D) MA.multiply(d1, x); DoubleMatrix1D ones = new DoubleMatrix1D(n, 1.0); ones.getData()[0] = 0.0; AssertMatrix.assertEqualsVectors(ones, d1x, 1e-14); DoubleMatrix1D d2x = (DoubleMatrix1D) MA.multiply(d2, x); AssertMatrix.assertEqualsVectors(zeroVector, d2x, 1e-14); DoubleMatrix2D d3 = getDifferenceMatrix(n, 3); assertEquals(n, d3.getNumberOfRows()); assertEquals(n, d3.getNumberOfColumns()); AssertMatrix.assertEqualsVectors(zeroVector, d3.getRowVector(0), 1e-15); AssertMatrix.assertEqualsVectors(zeroVector, d3.getRowVector(1), 1e-15); AssertMatrix.assertEqualsVectors(zeroVector, d3.getRowVector(2), 1e-15); for (int i = 0; i < n; i++) { x.getData()[i] = 0.5 + i + 0.1 * i * i; } d1x = (DoubleMatrix1D) MA.multiply(d1, x); DoubleMatrix1D exp = new DoubleMatrix1D(n); for (int i = 1; i < n; i++) { exp.getData()[i] = 0.9 + 0.2 * i; } AssertMatrix.assertEqualsVectors(exp, d1x, 1e-14); exp = new DoubleMatrix1D(n, 0.2); exp.getData()[0] = 0.0; exp.getData()[1] = 0.0; d2x = (DoubleMatrix1D) MA.multiply(d2, x); AssertMatrix.assertEqualsVectors(exp, d2x, 1e-14); DoubleMatrix1D d3x = (DoubleMatrix1D) MA.multiply(d3, x); AssertMatrix.assertEqualsVectors(zeroVector, d3x, 1e-14); }
@Test(expectedExceptions = IllegalArgumentException.class) public void diffOrderTooHighTest() { getDifferenceMatrix(6, 6); }